Skip to content

[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
merged 6 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions stdlib/public/Concurrency/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ set(SWIFT_RUNTIME_CONCURRENCY_SWIFT_SOURCES
AsyncThrowingMapSequence.swift
AsyncThrowingPrefixWhileSequence.swift
GlobalActor.swift
GlobalConcurrentExecutor.swift
MainActor.swift
PartialAsyncTask.swift
SourceCompatibilityShims.swift
Expand Down
9 changes: 4 additions & 5 deletions stdlib/public/Concurrency/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@ public protocol SerialExecutor: Executor {
/// requirements.
///
/// By setting a task executor preference, either with a
/// ``_withTaskExecutor(_:operation:)``, creating a task with a preference
/// (`Task(_on:)`, or `group.addTask(on:)`), the task and all of its child
/// ``_withTaskExecutorPreference(_:operation:)``, creating a task with a preference
/// (`Task(_executorPreference:)`, or `group.addTask(executorPreference:)`), the task and all of its child
/// tasks (unless a new preference is set) will be preferring to execute on
/// the provided task executor.
///
Expand Down Expand Up @@ -375,9 +375,8 @@ internal func _task_serialExecutor_getExecutorRef<E>(_ executor: E) -> Builtin.E
@_unavailableInEmbedded
@available(SwiftStdlib 9999, *)
@_silgen_name("_task_executor_getTaskExecutorRef")
internal func _task_executor_getTaskExecutorRef<E>(_ executor: E) -> Builtin.Executor
where E: _TaskExecutor {
return executor.asUnownedTaskExecutor().executor
internal func _task_executor_getTaskExecutorRef(_ taskExecutor: any _TaskExecutor) -> Builtin.Executor {
return taskExecutor.asUnownedTaskExecutor().executor
}

// Used by the concurrency runtime
Expand Down
66 changes: 66 additions & 0 deletions stdlib/public/Concurrency/GlobalConcurrentExecutor.swift
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
}
Copy link
Contributor Author

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


/// 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
2 changes: 1 addition & 1 deletion stdlib/public/Concurrency/PartialAsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public struct UnownedJob: Sendable {
@available(SwiftStdlib 9999, *)
@_alwaysEmitIntoClient
@inlinable
public func runSynchronously(isolated serialExecutor: UnownedSerialExecutor,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aligning this with some discussions on forums in other threads about "isolated" parameters.

FYI @rjmccall @hborla

(this is new API coming in for the TaskExecutors)

public func runSynchronously(isolatedTo serialExecutor: UnownedSerialExecutor,
taskExecutor: UnownedTaskExecutor) {
_swiftJobRunOnTaskExecutor(self, serialExecutor, taskExecutor)
}
Expand Down
Loading