Repository: apple/swift-nio
File: Sources/NIOPosix/NIOThreadPool.swift (Swift)

Code before:
    /// Submit a `WorkItem` to process.
    ///
    /// - Note: This is a low-level method, in most cases the `runIfActive` method should be used.
    ///
    /// - Parameters:
    ///   - body: The `WorkItem` to process by the `NIOThreadPool`.
    @preconcurrency
    public func submit(_ body: @escaping WorkItem) {
        self._submit(id: nil, body)
    }

    private func _submit(id: Int?, _ body: @escaping WorkItem) {
        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)
                workState = items.isEmpty ? .hasNoWork : .hasWork
                submitted = true

            case .shuttingDown, .stopped:
                workState = .hasWork
                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
        if !submitted {
            body(.cancelled)
        }
    }

    /// Initialize a `NIOThreadPool` thread pool with `numberOfThreads` threads.
    ///
    /// - Parameters:
    ///   - numberOfThreads: The number of threads to use for the thread pool.
    public convenience init(numberOfThreads: Int) {
        self.init(numberOfThreads: numberOfThreads, canBeStopped: true)
    }

    /// Create a ``NIOThreadPool`` that is already started, cannot be shut down and must not be `deinit`ed.
    ///

Review comment (suggestion):
Same question here. It might be useful to write out the strategy for the lock value in a comment above it, which will make it easier to audit.

Code after:
            }
            queue.async {
                callback(nil)
            }
        }
    }

    /// Submit a `WorkItem` to process.
    ///
    /// - Note: This is a low-level method, in most cases the `runIfActive` method should be used.
    ///
    /// - Parameters:
    ///   - body: The `WorkItem` to process by the `NIOThreadPool`.
    @preconcurrency
    public func submit(_ body: @escaping WorkItem) {
        self._submit(id: nil, body)
    }

    private func _submit(id: Int?, _ body: @escaping WorkItem) {
        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)
                workState = items.isEmpty ? .hasNoWork : .hasWork
                submitted = true

            case .shuttingDown, .stopped:
                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
        if !submitted {
            body(.cancelled)
        }
    }

    /// Initialize a `NIOThreadPool` thread pool with `numberOfThreads` threads.
    ///
    /// - Parameters: