|
| 1 | +//===----------------------------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the SwiftNIO open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors |
| 6 | +// Licensed under Apache License v2.0 |
| 7 | +// |
| 8 | +// See LICENSE.txt for license information |
| 9 | +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors |
| 10 | +// |
| 11 | +// SPDX-License-Identifier: Apache-2.0 |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#if canImport(Network) |
| 16 | +import NIOCore |
| 17 | + |
| 18 | +@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) |
| 19 | +extension NIOTSEventLoopGroup { |
| 20 | + /// A globally shared, lazily initialized, singleton ``NIOTSEventLoopGroup``. |
| 21 | + /// |
| 22 | + /// SwiftNIO allows and encourages the precise management of all operating system resources such as threads and file descriptors. |
| 23 | + /// Certain resources (such as the main `EventLoopGroup`) however are usually globally shared across the program. This means |
| 24 | + /// that many programs have to carry around an `EventLoopGroup` despite the fact they don't require the ability to fully return |
| 25 | + /// all the operating resources which would imply shutting down the `EventLoopGroup`. This type is the global handle for singleton |
| 26 | + /// resources that applications (and some libraries) can use to obtain never-shut-down singleton resources. |
| 27 | + /// |
| 28 | + /// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on |
| 29 | + /// first use. |
| 30 | + /// |
| 31 | + /// The loop count of this group is determined by `NIOSingletons.groupLoopCountSuggestion`. |
| 32 | + /// |
| 33 | + /// - note: Users who do not want any code to spawn global singleton resources may set |
| 34 | + /// `NIOSingletons.singletonsEnabledSuggestion` to `false` which will lead to a forced crash |
| 35 | + /// if any code attempts to use the global singletons. |
| 36 | + public static var singleton: NIOTSEventLoopGroup { |
| 37 | + return NIOSingletons.transportServicesEventLoopGroup |
| 38 | + } |
| 39 | + |
| 40 | +} |
| 41 | + |
| 42 | +extension NIOSingletons { |
| 43 | + /// A globally shared, lazily initialized, singleton ``NIOTSEventLoopGroup``. |
| 44 | + /// |
| 45 | + /// SwiftNIO allows and encourages the precise management of all operating system resources such as threads and file descriptors. |
| 46 | + /// Certain resources (such as the main `EventLoopGroup`) however are usually globally shared across the program. This means |
| 47 | + /// that many programs have to carry around an `EventLoopGroup` despite the fact they don't require the ability to fully return |
| 48 | + /// all the operating resources which would imply shutting down the `EventLoopGroup`. This type is the global handle for singleton |
| 49 | + /// resources that applications (and some libraries) can use to obtain never-shut-down singleton resources. |
| 50 | + /// |
| 51 | + /// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on |
| 52 | + /// first use. |
| 53 | + /// |
| 54 | + /// The loop count of this group is determined by `NIOSingletons.groupLoopCountSuggestion`. |
| 55 | + /// |
| 56 | + /// - note: Users who do not want any code to spawn global singleton resources may set |
| 57 | + /// `NIOSingletons.singletonsEnabledSuggestion` to `false` which will lead to a forced crash |
| 58 | + /// if any code attempts to use the global singletons. |
| 59 | + @available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) |
| 60 | + public static var transportServicesEventLoopGroup: NIOTSEventLoopGroup { |
| 61 | + return globalTransportServicesEventLoopGroup |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +@available(OSX 10.14, iOS 12.0, tvOS 12.0, watchOS 6.0, *) |
| 66 | +private let globalTransportServicesEventLoopGroup: NIOTSEventLoopGroup = { |
| 67 | + guard NIOSingletons.singletonsEnabledSuggestion else { |
| 68 | + fatalError(""" |
| 69 | + Cannot create global singleton NIOThreadPool because the global singletons have been \ |
| 70 | + disabled by setting `NIOSingletons.singletonsEnabledSuggestion = false` |
| 71 | + """) |
| 72 | + } |
| 73 | + |
| 74 | + let group = NIOTSEventLoopGroup._makePerpetualGroup(loopCount: NIOSingletons.groupLoopCountSuggestion, |
| 75 | + defaultQoS: .default) |
| 76 | + _ = Unmanaged.passUnretained(group).retain() // Never gonna give you up, never gonna let you down. |
| 77 | + return group |
| 78 | +}() |
| 79 | +#endif |
0 commit comments