-
Notifications
You must be signed in to change notification settings - Fork 656
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
345 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#if canImport(Darwin) | ||
import Darwin | ||
#elseif os(Windows) | ||
import ucrt | ||
import WinSDK | ||
#elseif canImport(Glibc) | ||
import Glibc | ||
#elseif canImport(Musl) | ||
import Musl | ||
#else | ||
#error("Unsupported C library") | ||
#endif | ||
import NIOCore | ||
|
||
/// Singleton resources provided by SwiftNIO for programs & libraries that don't need full control over all operating system resources. | ||
/// | ||
/// SwiftNIO allows and encourages the precise management of all operating system resources such as threads and file descriptors. | ||
/// Certain resources (such as the main ``EventLoopGroup``) however are usually globally shared across the program. This means | ||
/// that many programs have to carry around an ``EventLoopGroup`` despite the fact the don't require the ability to fully return | ||
/// all the operating resources which would imply shutting down the ``EventLoopGroup``. This type is the global handle for singleton | ||
/// resources that applications (and some libraries) can use to obtain never-shut-down singleton resources. | ||
/// | ||
/// Programs and libraries that do not use these singletons will not incur extra resource usage, these resources are lazily initialized on | ||
/// first use. | ||
public enum NIOSingletons {} | ||
|
||
extension NIOSingletons { | ||
/// How many threads will be used for the multi-threaded ``EventLoopGroup``s. | ||
/// | ||
/// The thread count is ``System/coreCount`` unless the environment variable `NIO_SINGLETONS_MULTI_THREADED_ELG_LOOP_COUNT` | ||
/// is set. | ||
public static var suggestedMultiThreadedEventLoopGroupLoopCount: Int { | ||
return globalSuggestedMultiThreadedEventLoopGroupLoopCount | ||
} | ||
|
||
/// How many threads will be used for the blocking `NIOThreadPool`s. | ||
/// | ||
/// The thread count is determined automatically unless the environment variable `NIO_SINGLETONS_BLOCKING_POOL_THREAD_COUNT` is set. | ||
public static var suggestedBlockingPoolThreadCount: Int { | ||
return globalSuggestedBlockingPoolThreadCount | ||
} | ||
} | ||
|
||
private let globalSuggestedMultiThreadedEventLoopGroupLoopCount: Int = { | ||
if let threadCountEnv = getenv("NIO_SINGLETONS_MULTI_THREADED_ELG_LOOP_COUNT"), | ||
let threadCount = Int(String(cString: threadCountEnv)), threadCount > 0 { | ||
return threadCount | ||
} else { | ||
return System.coreCount | ||
} | ||
}() | ||
|
||
private let globalSuggestedBlockingPoolThreadCount: Int = { | ||
if let threadCountEnv = getenv("NIO_SINGLETONS_BLOCKING_POOL_THREAD_COUNT"), | ||
let threadCount = Int(String(cString: threadCountEnv)), threadCount > 0 { | ||
return threadCount | ||
} else { | ||
return System.coreCount * 2 | ||
} | ||
}() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@_exported import NIOSingletonResources | ||
import NIOPosix | ||
import NIOCore | ||
|
||
extension NIOSingletons { | ||
/// A globally shared, lazily initialized ``MultiThreadedEventLoopGroup`` that uses `epoll` as the selector mechanism. | ||
/// | ||
/// The number of threads is determined by ``NIOSingletons.suggestedMultiThreadedEventLoopGroupThreadCount``. | ||
public static var multiThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup { | ||
return globalMultiThreadedPosixEventLoopGroup | ||
} | ||
|
||
/// A globally shared, lazily initialized ``MultiThreadedEventLoopGroup`` with just one thread that uses `epoll` as the selector mechanism. | ||
public static var singleThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup { | ||
return globalSingleThreadedPosixEventLoopGroup | ||
} | ||
|
||
/// A globally shared, lazily initialized ``EventLoop`` that uses `epoll` as the selector mechanism. Backed by `NIOSingletons.singleThreadedPosixEventLoopGroup`. | ||
public static var posixEventLoop: EventLoop { | ||
return Self.singleThreadedPosixEventLoopGroup.next() | ||
} | ||
|
||
/// A globally shared, lazily initialized ``NIOThreadPool`` that can be used for blocking I/O and other blocking operations. | ||
/// | ||
/// /// The number of threads is determined by ``NIOSingletons.suggestedBlockingPoolThreadCount``. | ||
public static var posixBlockingPool: NIOThreadPool { | ||
return globalPosixBlockingPool | ||
} | ||
} | ||
|
||
private let globalMultiThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup = { | ||
let threadCount = NIOSingletons.suggestedMultiThreadedEventLoopGroupLoopCount | ||
let group = MultiThreadedEventLoopGroup._makePerpetualGroup(threadNamePrefix: "NIO-MSGTN-", | ||
numberOfThreads: threadCount) | ||
_ = Unmanaged.passUnretained(group).retain() // Never gonna give you up, | ||
return group | ||
}() | ||
|
||
private let globalSingleThreadedPosixEventLoopGroup: MultiThreadedEventLoopGroup = { | ||
let group = MultiThreadedEventLoopGroup._makePerpetualGroup(threadNamePrefix: "NIO-SSGTN-", | ||
numberOfThreads: 1) | ||
_ = Unmanaged.passUnretained(group).retain() // never gonna let you down. | ||
return group | ||
}() | ||
|
||
private let globalPosixBlockingPool: NIOThreadPool = { | ||
let pool = NIOThreadPool._makePerpetualStartedPool(numberOfThreads: NIOSingletons.suggestedBlockingPoolThreadCount) | ||
_ = Unmanaged.passUnretained(pool).retain() // Make sure this is never deallocated (not strictly necessary). | ||
return pool | ||
}() | ||
|
||
|
Oops, something went wrong.