diff --git a/Sources/NIOPosix/NIOThreadPool.swift b/Sources/NIOPosix/NIOThreadPool.swift index 34af904eb..78bcba1e3 100644 --- a/Sources/NIOPosix/NIOThreadPool.swift +++ b/Sources/NIOPosix/NIOThreadPool.swift @@ -115,6 +115,7 @@ public final class NIOThreadPool { public let numberOfThreads: Int private let canBeStopped: Bool + private let pinnedCPUIDs: [Int]? /// Gracefully shutdown this `NIOThreadPool`. All tasks will be run before shutdown will take place. /// @@ -213,20 +214,42 @@ public final class NIOThreadPool { /// - Parameters: /// - numberOfThreads: The number of threads to use for the thread pool. public convenience init(numberOfThreads: Int) { - self.init(numberOfThreads: numberOfThreads, canBeStopped: true) + self.init( + numberOfThreads: numberOfThreads, + pinnedCPUIDs: nil, + canBeStopped: true + ) } + #if !os(WASI) + #if os(Linux) || os(Android) + /// Initialize a `NIOThreadPool` thread pool with threads pinned to specific CPUs. + /// + /// - Parameters: + /// - pinnedCPUIDs: The IDs of the CPUs to use for the thread pool. One thread will be + /// created per entry in this array. + public convenience init(pinnedCPUIDs: [Int]) { + self.init( + numberOfThreads: pinnedCPUIDs.count, + pinnedCPUIDs: pinnedCPUIDs, + canBeStopped: true + ) + } + + #endif + #endif /// Create a ``NIOThreadPool`` that is already started, cannot be shut down and must not be `deinit`ed. /// /// This is only useful for global singletons. public static func _makePerpetualStartedPool(numberOfThreads: Int, threadNamePrefix: String) -> NIOThreadPool { - let pool = self.init(numberOfThreads: numberOfThreads, canBeStopped: false) + let pool = self.init(numberOfThreads: numberOfThreads, pinnedCPUIDs: nil, canBeStopped: false) pool._start(threadNamePrefix: threadNamePrefix) return pool } - private init(numberOfThreads: Int, canBeStopped: Bool) { + private init(numberOfThreads: Int, pinnedCPUIDs: [Int]?, canBeStopped: Bool) { self.numberOfThreads = numberOfThreads + self.pinnedCPUIDs = pinnedCPUIDs self.canBeStopped = canBeStopped self._workAvailable = NIOThreadPoolWorkAvailable() } @@ -331,8 +354,18 @@ public final class NIOThreadPool { // that would otherwise be emitted. let readyThreads = ConditionLock(value: 0) for id in 0..([]) + let threadNameCollectionSem = DispatchSemaphore(value: 0) + let threadBlockingSem = DispatchSemaphore(value: 0) + + // let's use up all the threads + for i in (0..