Commit 6a304322 authored by Jeremy Meng's avatar Jeremy Meng

Add package for Timer. Use SpinWait struct to simulate the Thread.Yield. ...

Add package for Timer.  Use SpinWait struct to simulate the Thread.Yield.  According to MSDN it will always yield after the initial several spins.  Simple test shows it yields after 11 spins.
parent 36e08e89
......@@ -145,7 +145,7 @@ private void ProcessAsyncCompletionQueueImpl()
// give it a moment and try again, noting that we might lose the battle
// when we pause
Interlocked.CompareExchange(ref activeAsyncWorkerThread, 0, currentThread);
if (Thread.Yield() && Interlocked.CompareExchange(ref activeAsyncWorkerThread, currentThread, 0) == 0)
if (SpinWait() && Interlocked.CompareExchange(ref activeAsyncWorkerThread, currentThread, 0) == 0)
{
// we paused, and we got the lock back; anything else?
lock (asyncCompletionQueue)
......@@ -176,5 +176,20 @@ private void ProcessAsyncCompletionQueueImpl()
Interlocked.CompareExchange(ref activeAsyncWorkerThread, 0, currentThread);
}
}
private bool SpinWait()
{
var sw = new SpinWait();
byte maxSpins = 128;
do
{
if (sw.NextSpinWillYield)
return true;
maxSpins--;
}
while (maxSpins > 0);
return false;
}
}
}
{
"supports": {
"net46.app": {},
"dnxcore50.app": {}
},
"dependencies": {
"Microsoft.NETCore": "5.0.0",
"Microsoft.NETCore.Portable.Compatibility": "1.0.0",
"System.Collections": "4.0.10",
"System.Reflection.Emit": "4.0.0"
},
"frameworks": {
"dotnet": {
"imports": "portable-net452"
}
}
}
\ No newline at end of file
......@@ -43,6 +43,7 @@
"System.Threading": "4.0.0",
"System.Threading.Thread": "4.0.0-*",
"System.Threading.ThreadPool": "4.0.10-beta-*",
"System.Threading.Timer": "4.0.1-beta-*",
"System.Threading.Tasks": "4.0.0"
}
}
......
{
"sdk": {
"version": "1.0.0-beta5",
"runtime": "coreclr",
"architecture": "x86"
}
{
"sdk": {
"version": "1.0.0-beta7",
"runtime": "coreclr",
"architecture": "x86"
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment