1
+ //===----------------------------------------------------------------------===//
2
+ //
3
+ // This source file is part of the Swift.org open source project
4
+ //
5
+ // Copyright (c) 2020 Apple Inc. and the Swift project authors
6
+ // Licensed under Apache License v2.0 with Runtime Library Exception
7
+ //
8
+ // See https://swift.org/LICENSE.txt for license information
9
+ // See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10
+ //
11
+ //===----------------------------------------------------------------------===//
12
+
13
+ import Swift
14
+ @_implementationOnly import _SwiftConcurrencyShims
15
+
16
+ // None of _TaskExecutor APIs are available in task-to-thread concurrency model.
17
+ #if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY
18
+
19
+ /// The global concurrent executor that is used by default for Swift Concurrency
20
+ /// tasks.
21
+ ///
22
+ /// The executor's implementation is platform dependent.
23
+ /// By default it uses a fixed size pool of threads and should not be used for
24
+ /// blocking operations which do not guarantee forward progress as doing so may
25
+ /// prevent other tasks from being executed and render the system unresponsive.
26
+ ///
27
+ /// You may pass this executor explicitly to a ``Task`` initializer as a task
28
+ /// executor preference, in order to ensure and document that task be executed
29
+ /// on the global executor, instead e.g. inheriting the enclosing actor's
30
+ /// executor. Refer to ``_withTaskExecutorPreference(_:operation:)`` for a
31
+ /// detailed discussion of task executor preferences.
32
+ ///
33
+ /// Customizing the global concurrent executor is currently not supported.
34
+ @available ( SwiftStdlib 9999 , * )
35
+ nonisolated ( unsafe)
36
+ public var globalConcurrentExecutor : any _TaskExecutor {
37
+ get {
38
+ _DefaultGlobalConcurrentExecutor. shared
39
+ }
40
+ // TODO: introduce a set {} once we are ready to allow customizing the
41
+ // default global executor. This should be done the same for main actor
42
+ }
43
+
44
+ /// A task executor which enqueues all work on the default global concurrent
45
+ /// thread pool that is used as the default executor for Swift concurrency
46
+ /// tasks.
47
+ @available ( SwiftStdlib 9999 , * )
48
+ internal final class _DefaultGlobalConcurrentExecutor : _TaskExecutor {
49
+ public static let shared : _DefaultGlobalConcurrentExecutor = . init( )
50
+
51
+ private init ( ) { }
52
+
53
+ public func enqueue( _ job: consuming ExecutorJob ) {
54
+ _enqueueJobGlobal ( job. context)
55
+ }
56
+
57
+ public func asUnownedTaskExecutor( ) {
58
+ // The "default global concurrent executor" is simply the "undefined" one.
59
+ // We represent it as the `(0, 0)` ExecutorRef and it is handled properly
60
+ // by the runtime, without having to call through to the
61
+ // `_DefaultGlobalConcurrentExecutor` declared in Swift.
62
+ UnownedTaskExecutor ( _getUndefinedTaskExecutor ( ) )
63
+ }
64
+ }
65
+
66
+ #endif
0 commit comments