File: PosixSingletons.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (117 lines) | stat: -rw-r--r-- 5,795 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//===----------------------------------------------------------------------===//
//
// 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 NIOCore

extension NIOSingletons {
    /// A globally shared, lazily initialized ``MultiThreadedEventLoopGroup``  that uses `epoll`/`kqueue` as the
    /// selector mechanism.
    ///
    /// The number of threads is determined by `NIOSingletons/groupLoopCountSuggestion`.
    public static var posixEventLoopGroup: MultiThreadedEventLoopGroup {
        return singletonMTELG
    }

    /// 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/blockingPoolThreadCountSuggestion`.
    public static var posixBlockingThreadPool: NIOThreadPool {
        return globalPosixBlockingPool
    }
}

extension MultiThreadedEventLoopGroup {
    /// A globally shared, singleton ``MultiThreadedEventLoopGroup``.
    ///
    /// 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 they 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.
    ///
    /// The loop count of this group is determined by `NIOSingletons/groupLoopCountSuggestion`.
    ///
    /// - note: Users who do not want any code to spawn global singleton resources may set
    ///         `NIOSingletons/singletonsEnabledSuggestion` to `false` which will lead to a forced crash
    ///         if any code attempts to use the global singletons.
    ///
    public static var singleton: MultiThreadedEventLoopGroup {
        return NIOSingletons.posixEventLoopGroup
    }
}

extension EventLoopGroup where Self == MultiThreadedEventLoopGroup {
    /// A globally shared, singleton ``MultiThreadedEventLoopGroup``.
    ///
    /// This provides the same object as ``MultiThreadedEventLoopGroup/singleton``.
    public static var singletonMultiThreadedEventLoopGroup: Self {
        return MultiThreadedEventLoopGroup.singleton
    }
}

extension NIOThreadPool {
    /// A globally shared, singleton ``NIOThreadPool``.
    ///
    /// SwiftNIO allows and encourages the precise management of all operating system resources such as threads and file descriptors.
    /// Certain resources (such as the main ``NIOThreadPool``) however are usually globally shared across the program. This means
    /// that many programs have to carry around an ``NIOThreadPool`` despite the fact they don't require the ability to fully return
    /// all the operating resources which would imply shutting down the ``NIOThreadPool``. 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.
    ///
    /// The thread count of this pool is determined by `NIOSingletons/suggestedBlockingPoolThreadCount`.
    ///
    /// - note: Users who do not want any code to spawn global singleton resources may set
    ///         `NIOSingletons/singletonsEnabledSuggestion` to `false` which will lead to a forced crash
    ///         if any code attempts to use the global singletons.
    public static var singleton: NIOThreadPool {
        return NIOSingletons.posixBlockingThreadPool
    }
}

private let singletonMTELG: MultiThreadedEventLoopGroup = {
    guard NIOSingletons.singletonsEnabledSuggestion else {
        fatalError("""
                   Cannot create global singleton MultiThreadedEventLoopGroup because the global singletons have been \
                   disabled by setting  `NIOSingletons.singletonsEnabledSuggestion = false`
                   """)
    }
    let threadCount = NIOSingletons.groupLoopCountSuggestion
    let group = MultiThreadedEventLoopGroup._makePerpetualGroup(threadNamePrefix: "NIO-SGLTN-",
                                                                numberOfThreads: threadCount)
    _ = Unmanaged.passUnretained(group).retain() // Never gonna give you up,
    return group
}()

private let globalPosixBlockingPool: NIOThreadPool = {
    guard NIOSingletons.singletonsEnabledSuggestion else {
        fatalError("""
                   Cannot create global singleton NIOThreadPool because the global singletons have been \
                   disabled by setting `NIOSingletons.singletonsEnabledSuggestion = false`
                   """)
    }
    let pool = NIOThreadPool._makePerpetualStartedPool(
        numberOfThreads: NIOSingletons.blockingPoolThreadCountSuggestion,
        threadNamePrefix: "SGLTN-TP-#"
    )
    _ = Unmanaged.passUnretained(pool).retain() // never gonna let you down.
    return pool
}()