Skip to content

Commit

Permalink
Avoid use Task.Delay().Wait()
Browse files Browse the repository at this point in the history
use method with no HANDLE alloc, fix cuteant#37
  • Loading branch information
yyjdelete committed May 17, 2021
1 parent f79d65b commit d24906d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
11 changes: 10 additions & 1 deletion src/DotNetty.Common/Concurrency/XThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,16 @@ public void Start(object parameter)

public static void Sleep(int millisecondsTimeout)
{
Task.Delay(millisecondsTimeout).Wait();
Thread.Sleep(millisecondsTimeout);
}

/// <exception cref="T:System.OperationCanceledException"><paramref name="cancellationToken" /> was canceled.</exception>
public static void Sleep(int millisecondsTimeout, CancellationToken cancellationToken)
{
using (var ev = new ManualResetEventSlim())
{
ev.Wait(millisecondsTimeout, cancellationToken);
}
}

public int Id => _threadId;
Expand Down
6 changes: 2 additions & 4 deletions src/DotNetty.Common/Utilities/HashedWheelTimer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,13 @@ TimeSpan WaitForNextTick()
: currentTime;
}

Task delay = null;
try
{
long sleepTimeMs = sleepTime.Ticks / TimeSpan.TicksPerMillisecond; // we've already rounded so no worries about the remainder > 0 here
Debug.Assert(sleepTimeMs <= int.MaxValue);
delay = Task.Delay((int)sleepTimeMs, _owner.CancellationToken);
delay.Wait();
XThread.Sleep((int)sleepTimeMs, _owner.CancellationToken);
}
catch (AggregateException) when (delay is object && delay.IsCanceled)
catch (OperationCanceledException)
{
if (Volatile.Read(ref _owner.v_workerState) == WorkerStateShutdown)
{
Expand Down

0 comments on commit d24906d

Please sign in to comment.