Skip to content
This repository has been archived by the owner on Nov 1, 2020. It is now read-only.

Commit

Permalink
Remove CoreRT specific locks from portable thread pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
filipnavara committed Jan 30, 2019
1 parent 1a66e33 commit 3b33465
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,15 @@ internal partial class ClrThreadPool
private WaitThreadNode _waitThreadsHead;
private WaitThreadNode _waitThreadsTail;

#if CORERT
private readonly Lock _waitThreadLock = new Lock();
#else
private object _waitThreadLock = new object();
#endif

/// <summary>
/// Register a wait handle on a <see cref="WaitThread"/>.
/// </summary>
/// <param name="handle">A description of the requested registration.</param>
internal void RegisterWaitHandle(RegisteredWaitHandle handle)
{
#if CORERT
using (LockHolder.Hold(_waitThreadLock))
#else
lock (_waitThreadLock)
#endif
{
if (_waitThreadsHead == null) // Lazily create the first wait thread.
{
Expand Down Expand Up @@ -72,11 +64,7 @@ internal void RegisterWaitHandle(RegisteredWaitHandle handle)
/// <returns><c>true</c> if the thread was successfully removed; otherwise, <c>false</c></returns>
private bool TryRemoveWaitThread(WaitThread thread)
{
#if CORERT
using (LockHolder.Hold(_waitThreadLock))
#else
lock (_waitThreadLock)
#endif
{
if (thread.AnyUserWaits)
{
Expand Down Expand Up @@ -269,11 +257,7 @@ private void WaitThreadStart()
/// </summary>
private void ProcessRemovals()
{
#if CORERT
using (LockHolder.Hold(ThreadPoolInstance._waitThreadLock))
#else
lock (ThreadPoolInstance._waitThreadLock)
#endif
{
Debug.Assert(_numPendingRemoves >= 0);
Debug.Assert(_numPendingRemoves <= _pendingRemoves.Length);
Expand Down Expand Up @@ -389,11 +373,7 @@ private void UnregisterWait(RegisteredWaitHandle handle, bool blocking)
{
bool pendingRemoval = false;
// TODO: Optimization: Try to unregister wait directly if it isn't being waited on.
#if CORERT
using (LockHolder.Hold(ThreadPoolInstance._waitThreadLock))
#else
lock (ThreadPoolInstance._waitThreadLock)
#endif
{
// If this handle is not already pending removal and hasn't already been removed
if (Array.IndexOf(_registeredWaits, handle) != -1 && Array.IndexOf(_pendingRemoves, handle) == -1)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ internal sealed partial class ClrThreadPool
#elif BIT32
private const short MaxPossibleThreadCount = 1023;
#else
#error Unknown platform
#error Unknown platform
#endif

private const int CpuUtilizationHigh = 95;
Expand All @@ -34,11 +34,7 @@ internal sealed partial class ClrThreadPool

private short _minThreads;
private short _maxThreads;
#if CORERT
private readonly Lock _maxMinThreadLock = new Lock();
#else
private readonly object _maxMinThreadLock = new object();
#endif

[StructLayout(LayoutKind.Explicit, Size = CacheLineSize * 5)]
private struct CacheLineSeparated
Expand All @@ -65,11 +61,7 @@ private struct CacheLineSeparated
private int _completionCount = 0;
private int _threadAdjustmentIntervalMs;

#if CORERT
private readonly Lock _hillClimbingThreadAdjustmentLock = new Lock();
#else
private readonly object _hillClimbingThreadAdjustmentLock = new object();
#endif

private volatile int _numRequestedWorkers = 0;

Expand Down Expand Up @@ -98,11 +90,7 @@ private ClrThreadPool()

public bool SetMinThreads(int minThreads)
{
#if CORERT
using (LockHolder.Hold(_maxMinThreadLock))
#else
lock (_maxMinThreadLock)
#endif
{
if (minThreads < 0 || minThreads > _maxThreads)
{
Expand Down Expand Up @@ -146,11 +134,7 @@ public bool SetMinThreads(int minThreads)

public bool SetMaxThreads(int maxThreads)
{
#if CORERT
using (LockHolder.Hold(_maxMinThreadLock))
#else
lock (_maxMinThreadLock)
#endif
{
if (maxThreads < _minThreads || maxThreads == 0)
{
Expand Down Expand Up @@ -204,33 +188,16 @@ internal bool NotifyWorkItemComplete()
Interlocked.Increment(ref _completionCount);
Volatile.Write(ref _separated.lastDequeueTime, Environment.TickCount);

if (ShouldAdjustMaxWorkersActive())
if (ShouldAdjustMaxWorkersActive() && Monitor.TryEnter(_hillClimbingThreadAdjustmentLock))
{
#if CORERT
if (_hillClimbingThreadAdjustmentLock.TryAcquire(0))
try
{
try
{
AdjustMaxWorkersActive();
}
finally
{
_hillClimbingThreadAdjustmentLock.Release();
}
AdjustMaxWorkersActive();
}
#else
if (Monitor.TryEnter(_hillClimbingThreadAdjustmentLock))
finally
{
try
{
AdjustMaxWorkersActive();
}
finally
{
Monitor.Exit(_hillClimbingThreadAdjustmentLock);
}
Monitor.Exit(_hillClimbingThreadAdjustmentLock);
}
#endif
}

return !WorkerThread.ShouldStopProcessingWorkNow();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,7 @@ internal void RestartTimeout(int currentTimeMs)
/// </summary>
private int _numRequestedCallbacks;

#if CORERT
private readonly Lock _callbackLock = new Lock();
#else
private readonly object _callbackLock = new object();
#endif

/// <summary>
/// Notes if we need to signal the user's unregister event after all callbacks complete.
Expand All @@ -133,11 +129,7 @@ internal void RestartTimeout(int currentTimeMs)
public bool Unregister(WaitHandle waitObject)
{
GC.SuppressFinalize(this);
#if CORERT
using (LockHolder.Hold(_callbackLock))
#else
lock (_callbackLock)
#endif
{
try
{
Expand Down

0 comments on commit 3b33465

Please sign in to comment.