2
2
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3
3
4
4
using System ;
5
+ using System . Diagnostics ;
6
+ using System . Runtime . InteropServices ;
5
7
using System . Threading ;
6
8
7
9
namespace Microsoft . AspNet . Server . Kestrel . Networking
8
10
{
9
- public class UvLoopHandle : UvHandle
11
+ public class UvLoopHandle : SafeHandle
10
12
{
11
- public void Init ( Libuv uv )
13
+ private Libuv _uv ;
14
+ private int _threadId ;
15
+
16
+ public UvLoopHandle ( ) : base ( IntPtr . Zero , true ) { }
17
+
18
+ internal IntPtr InternalGetHandle ( ) => handle ;
19
+ public override bool IsInvalid => handle == IntPtr . Zero ;
20
+ public Libuv Libuv => _uv ;
21
+ public int ThreadId => _threadId ;
22
+
23
+ public void Validate ( bool closed = false )
12
24
{
13
- CreateMemory (
14
- uv ,
15
- Thread . CurrentThread . ManagedThreadId ,
16
- uv . loop_size ( ) ) ;
25
+ Trace . Assert ( closed || ! IsClosed , "Handle is closed" ) ;
26
+ Trace . Assert ( ! IsInvalid , "Handle is invalid" ) ;
27
+ Trace . Assert ( _threadId == Thread . CurrentThread . ManagedThreadId , "ThreadId is incorrect" ) ;
28
+ }
17
29
30
+ public void Init ( Libuv uv )
31
+ {
32
+ _uv = uv ;
33
+ _threadId = Thread . CurrentThread . ManagedThreadId ;
34
+ handle = Marshal . AllocCoTaskMem ( _uv . loop_size ( ) ) ;
18
35
_uv . loop_init ( this ) ;
19
36
}
20
37
@@ -30,15 +47,9 @@ public void Stop()
30
47
31
48
protected override bool ReleaseHandle ( )
32
49
{
33
- var memory = this . handle ;
34
- if ( memory != IntPtr . Zero )
35
- {
36
- _uv . loop_close ( this ) ;
37
- handle = IntPtr . Zero ;
38
- DestroyMemory ( memory ) ;
39
- }
50
+ _uv . loop_close ( this ) ;
51
+ Marshal . FreeCoTaskMem ( handle ) ;
40
52
return true ;
41
53
}
42
-
43
54
}
44
55
}
0 commit comments