Skip to content

[Concurrency] Add missing runSynchronously to ExecutorJob #72899

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 1 commit into from
Apr 8, 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
87 changes: 87 additions & 0 deletions stdlib/public/Concurrency/PartialAsyncTask.swift
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ public struct UnownedJob: Sendable {
_swiftJobRun(self, executor)
}

/// Run this job isolated to the passed task executor.
///
/// This operation runs the job on the calling thread and *blocks* until the job completes.
/// The intended use of this method is for an executor to determine when and where it
/// wants to run the job and then call this method on it.
///
/// The passed in executor reference is used to establish the executor context for the job,
/// and should be the same executor as the one semantically calling the `runSynchronously` method.
///
/// This operation consumes the job, preventing it accidental use after it has been run.
///
/// Converting a `ExecutorJob` to an ``UnownedJob`` and invoking ``UnownedJob/runSynchronously(_:)` on it multiple times is undefined behavior,
/// as a job can only ever be run once, and must not be accessed after it has been run.
///
/// - Parameter executor: the task executor this job will be run on.
///
/// - SeeAlso: ``runSynchronously(isolatedTo:taskExecutor:)``
@_unavailableInEmbedded
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
Expand All @@ -117,6 +134,24 @@ public struct UnownedJob: Sendable {
_swiftJobRunOnTaskExecutor(self, executor)
}

/// Run this job isolated to the passed in serial executor, while executing it on the specified task executor.
///
/// This operation runs the job on the calling thread and *blocks* until the job completes.
/// The intended use of this method is for an executor to determine when and where it
/// wants to run the job and then call this method on it.
///
/// The passed in executor reference is used to establish the executor context for the job,
/// and should be the same executor as the one semantically calling the `runSynchronously` method.
///
/// This operation consumes the job, preventing it accidental use after it has been run.
///
/// Converting a `ExecutorJob` to an ``UnownedJob`` and invoking ``UnownedJob/runSynchronously(_:)` on it multiple times is undefined behavior,
/// as a job can only ever be run once, and must not be accessed after it has been run.
///
/// - Parameter serialExecutor: the executor this job will be semantically running on.
/// - Parameter taskExecutor: the task executor this job will be run on.
///
/// - SeeAlso: ``runSynchronously(on:)``
@_unavailableInEmbedded
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
Expand Down Expand Up @@ -286,6 +321,58 @@ extension ExecutorJob {
__consuming public func runSynchronously(on executor: UnownedSerialExecutor) {
_swiftJobRun(UnownedJob(self), executor)
}

/// Run this job on the passed in task executor.
///
/// This operation runs the job on the calling thread and *blocks* until the job completes.
/// The intended use of this method is for an executor to determine when and where it
/// wants to run the job and then call this method on it.
///
/// The passed in executor reference is used to establish the executor context for the job,
/// and should be the same executor as the one semantically calling the `runSynchronously` method.
///
/// This operation consumes the job, preventing it accidental use after it has been run.
///
/// Converting a `ExecutorJob` to an ``UnownedJob`` and invoking ``UnownedJob/runSynchronously(_:)` on it multiple times is undefined behavior,
/// as a job can only ever be run once, and must not be accessed after it has been run.
///
/// - Parameter executor: the executor this job will be run on.
///
/// - SeeAlso: ``runSynchronously(isolatedTo:taskExecutor:)``
@_unavailableInEmbedded
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@inlinable
__consuming public func runSynchronously(on executor: UnownedTaskExecutor) {
_swiftJobRunOnTaskExecutor(UnownedJob(self), executor)
}

/// Run this job isolated to the passed in serial executor, while executing it on the specified task executor.
///
/// This operation runs the job on the calling thread and *blocks* until the job completes.
/// The intended use of this method is for an executor to determine when and where it
/// wants to run the job and then call this method on it.
///
/// The passed in executor reference is used to establish the executor context for the job,
/// and should be the same executor as the one semantically calling the `runSynchronously` method.
///
/// This operation consumes the job, preventing it accidental use after it has been run.
///
/// Converting a `ExecutorJob` to an ``UnownedJob`` and invoking ``UnownedJob/runSynchronously(_:)` on it multiple times is undefined behavior,
/// as a job can only ever be run once, and must not be accessed after it has been run.
///
/// - Parameter serialExecutor: the executor this job will be semantically running on.
/// - Parameter taskExecutor: the task executor this job will be run on.
///
/// - SeeAlso: ``runSynchronously(on:)``
@_unavailableInEmbedded
@available(SwiftStdlib 6.0, *)
@_alwaysEmitIntoClient
@inlinable
__consuming public func runSynchronously(isolatedTo serialExecutor: UnownedSerialExecutor,
taskExecutor: UnownedTaskExecutor) {
_swiftJobRunOnTaskExecutor(UnownedJob(self), serialExecutor, taskExecutor)
}
}
#endif // !SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ final class NaiveQueueExecutor: TaskExecutor {
}
}

public func apiTest(serialExecutor: any SerialExecutor, _ job: consuming ExecutorJob) {
job.runSynchronously(
isolatedTo: serialExecutor.asUnownedSerialExecutor(),
taskExecutor: self.asUnownedTaskExecutor())
}

}

actor ThreaddyTheDefaultActor {
Expand Down