From 4015e33e19782c86cf1bee55e6de8b01f8080fe0 Mon Sep 17 00:00:00 2001 From: George Barnett Date: Tue, 19 Nov 2024 11:16:40 +0000 Subject: [PATCH] Use condition variable in NIOThreadPool (#2964) Motivation: The thread performance checker warns that a threads with QoS are waiting on threads in the NIOThreadPool. We can avoid these warnings by using a condition variable instead. Modifications: - Replace the usage of a semaphore and lock in NIOThreadPool with a condition lock. - The condition value indicates whether the threads have work to do (where work is processing work items or exiting their run loop). Result: - Fewer warnings - Resolves #2960 (cherry picked from commit af0e612e8f23c14ccf5243b6b738e50245333e0a) --- Sources/NIOPosix/NIOThreadPool.swift | 187 ++++++++++++++++++++------- 1 file changed, 141 insertions(+), 46 deletions(-) diff --git a/Sources/NIOPosix/NIOThreadPool.swift b/Sources/NIOPosix/NIOThreadPool.swift index e60a73812..7854cbff7 100644 --- a/Sources/NIOPosix/NIOThreadPool.swift +++ b/Sources/NIOPosix/NIOThreadPool.swift @@ -79,9 +79,22 @@ public final class NIOThreadPool { /// It should never be "leaked" outside of the lock block. case modifying } - private let semaphore = DispatchSemaphore(value: 0) - private let lock = NIOLock() - private var threads: [NIOThread]? = nil // protected by `lock` + + /// Whether threads in the pool have work. + private enum WorkState: Hashable { + case hasWork + case hasNoWork + } + + // The condition lock is used in place of a lock and a semaphore to avoid warnings from the + // thread performance checker. + // + // Only the worker threads wait for the condition lock to take a value, no other threads need + // to wait for a given value. The value indicates whether the thread has some work to do. Work + // in this case can be either processing a work item or exiting the threads processing + // loop (i.e. shutting down). + private let conditionLock: ConditionLock + private var threads: [NIOThread]? = nil // protected by `conditionLock` private var state: State = .stopped // WorkItems don't have a handle so they can't be cancelled directly. Instead an ID is assigned @@ -124,7 +137,7 @@ public final class NIOThreadPool { return } - let threadsToJoin = self.lock.withLock { () -> [NIOThread] in + let threadsToJoin = self.conditionLock.withLock { switch self.state { case .running(let items): self.state = .modifying @@ -133,17 +146,17 @@ public final class NIOThreadPool { item.workItem(.cancelled) } } - self.state = .shuttingDown(Array(repeating: true, count: numberOfThreads)) - for _ in (0.. WorkItem? in + let submitted = self.conditionLock.withLock { + let workState: WorkState + let submitted: Bool + switch self.state { case .running(var items): self.state = .modifying items.append(.init(workItem: body, id: id)) self.state = .running(items) - self.semaphore.signal() - return nil + workState = items.isEmpty ? .hasNoWork : .hasWork + submitted = true + case .shuttingDown, .stopped: - return body + workState = .hasNoWork + submitted = false + case .modifying: fatalError(".modifying state misuse") } + + return (unlockWith: workState, result: submitted) } + // if item couldn't be added run it immediately indicating that it couldn't be run - item.map { $0(.cancelled) } + if !submitted { + body(.cancelled) + } } /// Initialize a `NIOThreadPool` thread pool with `numberOfThreads` threads. @@ -209,17 +233,18 @@ public final class NIOThreadPool { private init(numberOfThreads: Int, canBeStopped: Bool) { self.numberOfThreads = numberOfThreads self.canBeStopped = canBeStopped + self.conditionLock = ConditionLock(value: .hasNoWork) } private func process(identifier: Int) { var itemAndState: (item: WorkItem, state: WorkItemState)? = nil repeat { - // wait until work has become available - itemAndState = nil // ensure previous work item is not retained for duration of semaphore wait - self.semaphore.wait() + itemAndState = nil // ensure previous work item is not retained while waiting for the condition + itemAndState = self.conditionLock.withLock(when: .hasWork) { + let workState: WorkState + let result: (WorkItem, WorkItemState)? - itemAndState = self.lock.withLock { () -> (WorkItem, WorkItemState)? in switch self.state { case .running(var items): self.state = .modifying @@ -233,18 +258,32 @@ public final class NIOThreadPool { } self.state = .running(items) - return (itemAndID.workItem, state) + + workState = items.isEmpty ? .hasNoWork : .hasWork + result = (itemAndID.workItem, state) + case .shuttingDown(var aliveStates): + self.state = .modifying assert(aliveStates[identifier]) aliveStates[identifier] = false self.state = .shuttingDown(aliveStates) - return nil + + // Unlock with '.hasWork' to resume any other threads waiting to shutdown. + workState = .hasWork + result = nil + case .stopped: - return nil + // Unreachable: 'stopped' is the initial state which is left when starting the + // thread pool, and before any thread calls this function. + fatalError("Invalid state") + case .modifying: fatalError(".modifying state misuse") } + + return (unlockWith: workState, result: result) } + // if there was a work item popped, run it itemAndState.map { item, state in item(state) } } while itemAndState != nil @@ -256,16 +295,24 @@ public final class NIOThreadPool { } public func _start(threadNamePrefix: String) { - let alreadyRunning: Bool = self.lock.withLock { + let alreadyRunning = self.conditionLock.withLock { switch self.state { - case .running(_): - return true - case .shuttingDown(_): + case .running: + // Already running, this has no effect on whether there is more work for the + // threads to run. + return (unlockWith: nil, result: true) + + case .shuttingDown: // This should never happen fatalError("start() called while in shuttingDown") + case .stopped: self.state = .running(Deque(minimumCapacity: 16)) - return false + assert(self.threads == nil) + self.threads = [] + self.threads!.reserveCapacity(self.numberOfThreads) + return (unlockWith: .hasNoWork, result: false) + case .modifying: fatalError(".modifying state misuse") } @@ -278,21 +325,34 @@ public final class NIOThreadPool { // We use this condition lock as a tricky kind of semaphore. // This is done to sidestep the thread performance checker warning // that would otherwise be emitted. - let cond = ConditionLock(value: 0) - - self.lock.withLock { - assert(self.threads == nil) - self.threads = [] - self.threads?.reserveCapacity(self.numberOfThreads) - } - + let readyThreads = ConditionLock(value: 0) for id in 0.. Int { + self.conditionLock.withLock { + (unlockWith: nil, result: self.threads?.count ?? -1) + } + } + assert(threadCount() == self.numberOfThreads) } deinit { @@ -374,8 +440,9 @@ extension NIOThreadPool { } } } onCancel: { - self.lock.withLockVoid { + self.conditionLock.withLock { self.cancelledWorkIDs.insert(workID) + return (unlockWith: nil, result: ()) } } } @@ -427,3 +494,31 @@ extension NIOThreadPool { } } } + +extension ConditionLock { + @inlinable + func _lock(when value: T?) { + if let value = value { + self.lock(whenValue: value) + } else { + self.lock() + } + } + + @inlinable + func _unlock(with value: T?) { + if let value = value { + self.unlock(withValue: value) + } else { + self.unlock() + } + } + + @inlinable + func withLock(when value: T? = nil, _ body: () -> (unlockWith: T?, result: Result)) -> Result { + self._lock(when: value) + let (unlockValue, result) = body() + self._unlock(with: unlockValue) + return result + } +}