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

Make the loop handle a simple piece of memory #49

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/Microsoft.AspNet.Server.Kestrel/Networking/UvLoopHandle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

namespace Microsoft.AspNet.Server.Kestrel.Networking
{
public class UvLoopHandle : UvHandle
public class UvLoopHandle : SafeHandle
{
public void Init(Libuv uv)
private Libuv _uv;
private int _threadId;

public UvLoopHandle() : base(IntPtr.Zero, true) { }

internal IntPtr InternalGetHandle() => handle;
public override bool IsInvalid => handle == IntPtr.Zero;
public Libuv Libuv => _uv;
public int ThreadId => _threadId;

public void Validate(bool closed = false)
{
CreateMemory(
uv,
Thread.CurrentThread.ManagedThreadId,
uv.loop_size());
Trace.Assert(closed || !IsClosed, "Handle is closed");
Trace.Assert(!IsInvalid, "Handle is invalid");
Trace.Assert(_threadId == Thread.CurrentThread.ManagedThreadId, "ThreadId is incorrect");
}

public void Init(Libuv uv)
{
_uv = uv;
_threadId = Thread.CurrentThread.ManagedThreadId;
handle = Marshal.AllocCoTaskMem(_uv.loop_size());
_uv.loop_init(this);
}

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

protected override bool ReleaseHandle()
{
var memory = this.handle;
if (memory != IntPtr.Zero)
{
_uv.loop_close(this);
handle = IntPtr.Zero;
DestroyMemory(memory);
}
_uv.loop_close(this);
Marshal.FreeCoTaskMem(handle);
return true;
}

}
}