From: Stephen Toub Date: Wed, 1 Feb 2017 15:49:27 +0000 (-0500) Subject: Simplify Dequeue method X-Git-Tag: submit/tizen/20210909.063632~11030^2~8246^2~2 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=2a0ebaad7dda77624f535014b9cfad394b7e1307;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Simplify Dequeue method - Consolidate branches - Pass resulting work item as a return value rather than an out - Avoid an unnecessary write to the bool passed by-ref Commit migrated from https://github.com/dotnet/coreclr/commit/ff1caae983fd65b825c44a331e39a37ffb5b1710 --- diff --git a/src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs b/src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs index fb47d82..0645ce5 100644 --- a/src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs +++ b/src/coreclr/src/mscorlib/src/System/Threading/ThreadPool.cs @@ -476,31 +476,18 @@ namespace System.Threading internal bool LocalFindAndPop(IThreadPoolWorkItem callback) { ThreadPoolWorkQueueThreadLocals tl = ThreadPoolWorkQueueThreadLocals.threadLocals; - if (null == tl) - return false; - - return tl.workStealingQueue.LocalFindAndPop(callback); + return tl != null && tl.workStealingQueue.LocalFindAndPop(callback); } - public void Dequeue(ThreadPoolWorkQueueThreadLocals tl, out IThreadPoolWorkItem callback, out bool missedSteal) + public IThreadPoolWorkItem Dequeue(ThreadPoolWorkQueueThreadLocals tl, ref bool missedSteal) { - callback = null; - missedSteal = false; WorkStealingQueue wsq = tl.workStealingQueue; + IThreadPoolWorkItem callback; - if (wsq.LocalPop(out callback)) - Debug.Assert(null != callback); - - if (null == callback) - { - if (workItems.TryDequeue(out callback)) - { - Debug.Assert(null != callback); - } - } - - if (null == callback) + if (!wsq.LocalPop(out callback) && // first try the local queue + !workItems.TryDequeue(out callback)) // then try the global queue { + // finally try to steal from another thread's local queue WorkStealingQueue[] otherQueues = allThreadQueues.Current; int c = otherQueues.Length; int maxIndex = c - 1; @@ -520,6 +507,8 @@ namespace System.Threading c--; } } + + return callback; } static internal bool Dispatch() @@ -570,7 +559,7 @@ namespace System.Threading finally { bool missedSteal = false; - workQueue.Dequeue(tl, out workItem, out missedSteal); + workItem = workQueue.Dequeue(tl, ref missedSteal); if (workItem == null) {