/// <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) { _waitThreadLock.Acquire(); try { if (_waitThreadsHead == null) // Lazily create the first wait thread. { _waitThreadsTail = _waitThreadsHead = new WaitThreadNode { Thread = new WaitThread() }; } // Register the wait handle on the first wait thread that is not at capacity. WaitThreadNode prev; WaitThreadNode current = _waitThreadsHead; do { if (current.Thread.RegisterWaitHandle(handle)) { return; } prev = current; current = current.Next; } while (current != null); // If all wait threads are full, create a new one. prev.Next = _waitThreadsTail = new WaitThreadNode { Thread = new WaitThread() }; prev.Next.Thread.RegisterWaitHandle(handle); return; } finally { _waitThreadLock.Release(); } }
/// <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 (PortableThreadPoolEventSource.Log.IsEnabled()) { PortableThreadPoolEventSource.Log.ThreadPoolIOEnqueue(handle); } _waitThreadLock.Acquire(); try { WaitThreadNode?current = _waitThreadsHead; if (current == null) // Lazily create the first wait thread. { _waitThreadsHead = current = new WaitThreadNode(new WaitThread()); } // Register the wait handle on the first wait thread that is not at capacity. WaitThreadNode prev; do { if (current.Thread.RegisterWaitHandle(handle)) { return; } prev = current; current = current.Next; } while (current != null); // If all wait threads are full, create a new one. prev.Next = new WaitThreadNode(new WaitThread()); prev.Next.Thread.RegisterWaitHandle(handle); return; } finally { _waitThreadLock.Release(); } }
/// <summary> /// Removes the wait thread from the list. /// </summary> /// <param name="thread">The wait thread to remove from the list.</param> private void RemoveWaitThread(WaitThread thread) { if (_waitThreadsHead.Thread == thread) { _waitThreadsHead = _waitThreadsHead.Next; return; } WaitThreadNode prev; WaitThreadNode current = _waitThreadsHead; do { prev = current; current = current.Next; } while (current != null && current.Thread != thread); Debug.Assert(current != null, "The wait thread to remove was not found in the list of thread pool wait threads."); if (current != null) { prev.Next = current.Next; } }