Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit f408ee7

Browse files
author
Kai Ruhnau
committed
Make the loop handle a simple piece of memory
1 parent e421b3f commit f408ee7

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/Microsoft.AspNet.Server.Kestrel/Networking/UvLoopHandle.cs

Lines changed: 25 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,36 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Diagnostics;
6+
using System.Runtime.InteropServices;
57
using System.Threading;
68

79
namespace Microsoft.AspNet.Server.Kestrel.Networking
810
{
9-
public class UvLoopHandle : UvHandle
11+
public class UvLoopHandle : SafeHandle
1012
{
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)
1224
{
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+
}
1729

30+
public void Init(Libuv uv)
31+
{
32+
_uv = uv;
33+
_threadId = Thread.CurrentThread.ManagedThreadId;
34+
handle = Marshal.AllocCoTaskMem(_uv.loop_size());
1835
_uv.loop_init(this);
1936
}
2037

@@ -30,15 +47,9 @@ public void Stop()
3047

3148
protected override bool ReleaseHandle()
3249
{
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);
4052
return true;
4153
}
42-
4354
}
4455
}

0 commit comments

Comments
 (0)