-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[TaskExecutors] Task initializer and withTaskExecutor parameter changes #70783
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ktoso
merged 6 commits into
swiftlang:main
from
ktoso:wip-cleanup-task-executor-parameters
Jan 20, 2024
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
38a951f
make TaskExecutor params not optional; add DefaultConcurrentExecutor
ktoso ea36007
move to `some TaskExecutor` now that we can avoid the optional
ktoso 7152811
bring back optional TaskExecutor param; introduce global var executor
ktoso dc59517
update some missed docs
ktoso 19470fa
further semantics and docs cleanups for accepting nil
ktoso 07f31c3
add missing nonisolated(unsafe) to global concurrent executor var
ktoso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 Swift.org open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Swift | ||
@_implementationOnly import _SwiftConcurrencyShims | ||
|
||
// None of _TaskExecutor APIs are available in task-to-thread concurrency model. | ||
#if !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY | ||
|
||
/// The global concurrent executor that is used by default for Swift Concurrency | ||
/// tasks. | ||
/// | ||
/// The executor's implementation is platform dependent. | ||
/// By default it uses a fixed size pool of threads and should not be used for | ||
/// blocking operations which do not guarantee forward progress as doing so may | ||
/// prevent other tasks from being executed and render the system unresponsive. | ||
/// | ||
/// You may pass this executor explicitly to a ``Task`` initializer as a task | ||
/// executor preference, in order to ensure and document that task be executed | ||
/// on the global executor, instead e.g. inheriting the enclosing actor's | ||
/// executor. Refer to ``_withTaskExecutorPreference(_:operation:)`` for a | ||
/// detailed discussion of task executor preferences. | ||
/// | ||
/// Customizing the global concurrent executor is currently not supported. | ||
@available(SwiftStdlib 9999, *) | ||
nonisolated(unsafe) | ||
public var globalConcurrentExecutor: any _TaskExecutor { | ||
get { | ||
_DefaultGlobalConcurrentExecutor.shared | ||
} | ||
// TODO: introduce a set {} once we are ready to allow customizing the | ||
// default global executor. This should be done the same for main actor | ||
} | ||
|
||
/// A task executor which enqueues all work on the default global concurrent | ||
/// thread pool that is used as the default executor for Swift concurrency | ||
/// tasks. | ||
@available(SwiftStdlib 9999, *) | ||
internal final class _DefaultGlobalConcurrentExecutor: _TaskExecutor { | ||
public static let shared: _DefaultGlobalConcurrentExecutor = .init() | ||
|
||
private init() {} | ||
|
||
public func enqueue(_ job: consuming ExecutorJob) { | ||
_enqueueJobGlobal(job.context) | ||
} | ||
|
||
public func asUnownedTaskExecutor() { | ||
// The "default global concurrent executor" is simply the "undefined" one. | ||
// We represent it as the `(0, 0)` ExecutorRef and it is handled properly | ||
// by the runtime, without having to call through to the | ||
// `_DefaultGlobalConcurrentExecutor` declared in Swift. | ||
UnownedTaskExecutor(_getUndefinedTaskExecutor()) | ||
} | ||
} | ||
|
||
#endif |
This file contains hidden or 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 |
---|---|---|
|
@@ -121,7 +121,7 @@ public struct UnownedJob: Sendable { | |
@available(SwiftStdlib 9999, *) | ||
@_alwaysEmitIntoClient | ||
@inlinable | ||
public func runSynchronously(isolated serialExecutor: UnownedSerialExecutor, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
public func runSynchronously(isolatedTo serialExecutor: UnownedSerialExecutor, | ||
taskExecutor: UnownedTaskExecutor) { | ||
_swiftJobRunOnTaskExecutor(self, serialExecutor, taskExecutor) | ||
} | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so here's the new global @DougGregor