Repository: apple/swift-nio
File: Sources/NIOPosix/PosixSingletons+ConcurrencyTakeOver.swift (Swift)

Code before:
import Atomics
import NIOCore

private let _haveWeTakenOverTheConcurrencyPool = ManagedAtomic(false)
extension NIOSingletons {
    /// Install ``MultiThreadedEventLoopGroup/singleton`` as Swift Concurrency's global executor.
    ///
    /// This allows to use Swift Concurrency and retain high-performance I/O alleviating the otherwise necessary thread switches between
    /// Swift Concurrency's own global pool and a place (like an `EventLoop`) that allows to perform I/O
    ///
    /// This method uses an atomic compare and exchange to install the hook (and makes sure it's not already set). This unilateral atomic memory
    /// operation doesn't guarantee anything becuase another piece of code could have done the same without using atomic operations. But we
    /// do our best.
    ///
    /// - warning: You may only call this method from the main thread.
    /// - warning: You may only call this method once.
    @discardableResult
    public static func unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor() -> Bool {
        #if compiler(>=5.9)
        guard #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) else {
            return false
        }

        typealias ConcurrencyEnqueueGlobalHook = @convention(thin) (
            UnownedJob, @convention(thin) (UnownedJob) -> Void
        ) -> Void

        guard
            _haveWeTakenOverTheConcurrencyPool.compareExchange(
                expected: false,
                desired: true,
                ordering: .relaxed
            ).exchanged
        else {
            fatalError("Must be called only once")
        }

        #if canImport(Darwin)
        guard pthread_main_np() == 1 else {
            fatalError("Must be called from the main thread")
        }
        #endif

        let concurrencyEnqueueGlobalHookPtr = dlsym(

Review comment (suggestion):
```suggestion
    /// operation doesn't guarantee anything because another piece of code could have done the same without using atomic operations. But we
```

Code after:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Atomics
import NIOCore

#if compiler(>=5.9)
private protocol SilenceWarning {
    @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
    func enqueue(_ job: UnownedJob)
}
@available(macOS 14, *)
extension SelectableEventLoop: SilenceWarning {}
#endif

private let _haveWeTakenOverTheConcurrencyPool = ManagedAtomic(false)
extension NIOSingletons {
    /// Install ``MultiThreadedEventLoopGroup/singleton`` as Swift Concurrency's global executor.
    ///
    /// This allows to use Swift Concurrency and retain high-performance I/O alleviating the otherwise necessary thread switches between
    /// Swift Concurrency's own global pool and a place (like an `EventLoop`) that allows to perform I/O
    ///
    /// This method uses an atomic compare and exchange to install the hook (and makes sure it's not already set). This unilateral atomic memory
    /// operation doesn't guarantee anything because another piece of code could have done the same without using atomic operations. But we
    /// do our best.
    ///
    /// - warning: You may only call this method from the main thread.
    /// - warning: You may only call this method once.
    @discardableResult
    public static func unsafeTryInstallSingletonPosixEventLoopGroupAsConcurrencyGlobalExecutor() -> Bool {
        #if /* minimum supported */ compiler(>=5.9) && /* maximum tested */ swift(<5.11)
        guard #available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *) else {
            return false