Repository: apple/swift-nio
File: Sources/NIOConcurrencyHelpers/NIOThreadPoolWorkAvailable.swift (Swift)

Code before:
        }
    }

    @usableFromInline
    let _storage: _Storage

    @usableFromInline
    package enum Signal: Sendable {
        /// No signal after unlock.
        case none
        /// Wake one waiting thread (``pthread_cond_signal``).
        case signalOne
        /// Wake all waiting threads (``pthread_cond_broadcast``).
        case broadcastAll
    }

    @inlinable
    package init() {
        self._storage = _Storage()
    }

    /// Lock, run `body` with access to the `workAvailable` counter,
    /// unlock, then signal as indicated by the return value.
    @inlinable
    package func withLock<Result>(
        _ body: (inout Int) -> (signal: Signal, result: Result)
    ) -> Result {
        self._storage.lock.lock()
        let (signal, result) = body(&self._storage.workAvailable)
        self._storage.lock.unlock()
        self._signal(signal)
        return result
    }

    /// Lock, wait while `workAvailable <= 0`, run `body` with access to the
    /// `workAvailable` counter, unlock, then signal as indicated.
    @inlinable
    package func withLockWaitingForWork<Result>(
        _ body: (inout Int) -> (signal: Signal, result: Result)
    ) -> Result {
        self._storage.lock.lock()
        while self._storage.workAvailable <= 0 {
            self._storage.lock.withLockPrimitive { mutex in
                #if os(Windows)
                let ok = SleepConditionVariableSRW(self._storage.cond, mutex, INFINITE, 0)
                precondition(
                    ok,
                    "\(#function) failed in SleepConditionVariableSRW with error \(GetLastError())"
                )
                #elseif (compiler(<6.1) && !os(WASI)) || (compiler(>=6.1) && _runtime(_multithreaded))
                let err = pthread_cond_wait(self._storage.cond, mutex)

Review comment (suggestion):
I think the `(inout Int)` closure is leading to the increase in heap allocations. I'll investigate an approach using delta tracking or fixed values.

Code after:
        }
    }

    @usableFromInline
    let _storage: _Storage

    @usableFromInline
    package enum Signal: Sendable {
        /// No signal after unlock.
        case none
        /// Wake one waiting thread (``pthread_cond_signal``).
        case signalOne
        /// Wake all waiting threads (``pthread_cond_broadcast``).
        case broadcastAll
    }

    @inlinable
    package init() {
        self._storage = _Storage()
    }

    /// Lock, run `body`, apply the returned delta to `workAvailable`,
    /// unlock, then signal as indicated by the return value.
    @inlinable
    package func withLock<Result>(
        _ body: () -> (workDelta: Int, signal: Signal, result: Result)
    ) -> Result {
        self._storage.lock.lock()
        let (workDelta, signal, result) = body()
        self._storage.workAvailable += workDelta
        self._storage.lock.unlock()
        self._signal(signal)
        return result
    }

    /// Lock, run `body`, set `workAvailable` to the returned value if non-nil,
    /// unlock, then signal as indicated. Use for shutdown where an
    /// absolute value is needed rather than a delta.
    @inlinable
    package func withLockSettingWorkAvailable<Result>(
        _ body: () -> (workAvailable: Int?, signal: Signal, result: Result)
    ) -> Result {
        self._storage.lock.lock()
        let (workAvailable, signal, result) = body()
        if let workAvailable = workAvailable {
            self._storage.workAvailable = workAvailable
        }
        self._storage.lock.unlock()
        self._signal(signal)
        return result
    }