Commit Graph

76 Commits

Author SHA1 Message Date
Doug Gregor
177d16ced8 Enable strict memory safety in the Concurrency module 2025-02-26 14:28:24 -08:00
Allan Shortlidge
ba3cd79b6f Concurrency: Remove superflous _SwiftConcurrencyShims imports.
The `_SwiftConcurrencyShims` module was imported `@_implementationOnly` which
was causing warnings to be emitted during the stdlib build. The module
currently serves no purpose; the only declaration it contains is a defunct
`_SwiftContext` struct which is not referenced by anything. The module needs to
continue to exist for source compatibility, though, since it is part of the
toolchain and imported publicly from other modules.
2024-09-09 12:20:13 -07:00
Doug Gregor
8378562e12 Adopt @unsafe throughout the standard library
Annotate all of the `Unsafe*` types and `unsafe` functions in the standard
library (including concurrency, synchronization, etc.) as `@unsafe`. Add a
few tests to ensure that we detect uses of these types in clients that
have disabled unsafe code.
2024-08-19 14:33:09 -07:00
Kavon Farvardin
2557ba96c8 Noncopyable: deprecate '@_moveOnly' attribute
This hidden attribute is a leftover from before the '~Copyable' syntax.

rdar://130526083
2024-07-23 11:05:33 -07:00
Doug Gregor
a2038f92f8 Address code review feedback and fix rebase typo in a test 2024-07-09 08:13:24 -07:00
Doug Gregor
a2b2324e7f Use @_unsafeInheritExecutor forms of with*Continuation from @_unsafeInheritExecutor functions
The move from `@_unsafeInheritExecutor` to `#isolation` for the
with*Continuation breaks code that is using `@_unsafeInheritExecutor` and
calling these APIs. This originally caused silent breakage (which manifest
as runtime crashes), and is now detected by the compiler as an error.

However, despite `@_unsafeInheritExecutor` being an unsafe,
not-intended-to-be-user-facing feature, it is indeed being used, along
with these APIs. Introduce _unsafeInheritExecutor_-prefixed versions of
the `with*Continuation` and `withTaskCancellationHandler` APIs into
the _Concurrency library that use `@_unsafeInheritExecutor`. Then,
teach the type checker to swap in these
_unsafeInheritExecutor_-prefixed versions in lieu of the originals
when they are called from an `@_unsafeInheritExecutor` function. This
allows existing code using `@_unsafeInheritExecutor` with these APIs
to continue working as it has before, albeit with a warning that
`@_unsafeInheritExecutor` has been removed.

Fixes rdar://131151376.
2024-07-08 23:35:37 -07:00
Konrad `ktoso` Malawski
b9369bf0b7 [Concurrency] Fix default actor isolation issue in face of TaskExecutor preference (#74658)
* [Concurrency] Fix task excutor handling of default actor isolation

The task executor API did not properly account for taking the default
actor locking into account when running code on it, we just took the job
and ran it without checking with the serial executor at all, which
resulted in potential concurrent executions inside the actor --
violating actor isolation.

Here we change the TaskExecutor enqueue API to accept the "target"
serial executor, which in practice will be either generic or a specific
default actor, and coordinate with it when we perform a
runSynchronously.

The SE proposal needs to be amended to showcase this new API, however
without this change we are introducing races so we must do this before
the API is stable.

* Remove _swift_task_enqueueOnTaskExecutor as we don't use it anymore

* no need for the new protocol requirement

* remove the enqueue(_ job: UnownedJob, isolatedTo unownedSerialExecutor: UnownedSerialExecutor)

Thankfully we dont need it after all

* Don't add swift_defaultActor_enqueue_withTaskExecutor and centralize the task executor getting to enqueue()

* move around extern definitions
2024-07-01 16:42:27 +09:00
Michael Gottesman
5db3b70c65 [concurrency] Update withUnsafe{,Throwing}Continuation to have a sending result.
rdar://129299180
2024-06-05 12:50:59 -07:00
Konrad `ktoso` Malawski
fb4ffb43db [Concurrency] Add missing runSynchronously to ExecutorJob (#72899) 2024-04-08 13:41:08 +09:00
Konrad `ktoso` Malawski
dfcf1054dd [Concurrency] Remove _unsafeInheritExecutor from public APIs, use #isolation (#72578) 2024-04-05 03:57:54 -07:00
Holly Borla
5d9ad87a82 [Concurrency] Task executors are available in Swift 6.0. 2024-03-13 09:56:24 -07:00
Konrad `ktoso` Malawski
1dec00a420 [TaskExecutors] Task initializer and withTaskExecutor parameter changes (#70783) 2024-01-20 11:03:26 +09:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Konrad `ktoso` Malawski
f58a46fece Merge pull request #69671 from Jager-yoo/typos-concurrency 2023-11-27 13:38:23 +09:00
Choo Hyun Ho
36e01c23ae stdlib/Concurrency/PartialAsyncTask.swift: Correct typos (#69880) 2023-11-15 07:01:57 -08:00
Jaeho Yoo
391eee0625 [Concurrency] Fix some typos in PartialAsyncTask 2023-11-07 01:16:37 +09:00
Kuba Mracek
df5879746c [embedded] Use async main in the test 2023-10-06 20:32:21 -07:00
Alex Martini
2676e261fd Fix typo; add missing word. 2023-08-07 11:35:42 -07:00
Konrad `ktoso` Malawski
da6f08a24e [Concurrency] Adopt stable keyword consuming instead of __owned
add test for warnings emitted when missing consuming attribute

use -emit-sil in mock SDK tests for better coverage
2023-05-11 15:18:54 +02:00
Konrad `ktoso` Malawski
ca2987cd83 [Concurrency] Easier to handle deprecation of Job and friends 2023-05-09 14:38:15 +09:00
Kavon Farvardin
8511317a97 Replace additional uses of noncopyable metatypes. 2023-05-04 11:44:27 -07:00
Kavon Farvardin
103bd043cd Revert "Revert "Prevent noncopyable metatypes from being converted to Any""
This reverts commit 1a3b42cd63.
2023-05-04 11:38:00 -07:00
Hamish Knight
1a3b42cd63 Revert "Prevent noncopyable metatypes from being converted to Any" 2023-05-04 12:10:10 +01:00
Kavon Farvardin
1df3912794 Merge pull request #65606 from kavon/noncopyable-metatypes
Prevent noncopyable metatypes from being converted to `Any`
2023-05-03 22:05:17 -07:00
Kavon Farvardin
4710fc675b Prevent noncopyable metatypes from being converted to Any
These metatypes are a gateway to more incorrect
uses of these noncopyable values because we don't
yet have the corresponding runtime support yet.
The other use cases of using metatypes of
noncopyable types in generics is not high enough to
warrant people using them yet.

resolves rdar://106452518
2023-05-03 10:23:11 -07:00
Konrad `ktoso` Malawski
8d6980ec10 [Executors] Make move to ExecutorJob binary compatible; deprecate Job 2023-05-02 14:44:11 +09:00
Kavon Farvardin
8f13679582 add missing rename of Job -> ExecutorJob 2023-04-12 13:37:43 -07:00
Konrad `ktoso` Malawski
80b93863a9 [Concurrency] Rename Job to ExecutorJob, ease transition with typealias (#65006) 2023-04-11 14:52:10 +09:00
Kavon Farvardin
363448a0d7 bring back the noncopyable Job type in the _Concurrency lib
This reverts commit abe377ce68.

Co-authored-by: Konrad `ktoso` Malawski <konrad_malawski@apple.com>
2023-03-24 08:58:40 -07:00
Konrad `ktoso` Malawski
abe377ce68 [Workaround][Concurrency] Mark Job as NOT moveonly until we fix moveonly types (#64536) 2023-03-23 07:43:51 +09:00
Konrad `ktoso` Malawski
e0877594da [Concurrency] Custom executors with move-only Job (#63569) 2023-03-18 14:02:43 +09:00
Konrad `ktoso` Malawski
ac0c3b75c6 [Concurrency] Freeze JobPriority, comment that Flags location is assumed (#64267) 2023-03-10 16:49:37 +09:00
Konrad `ktoso` Malawski
c446f4cb12 review followup 2023-03-03 13:37:57 +09:00
Konrad `ktoso` Malawski
8b2253cfbe [Concurrency] Fix continuation documentation about execution semantics 2023-03-03 09:45:55 +09:00
Konrad `ktoso` Malawski
98c33a7b1e new init in extension must be @available explicitly 2023-03-01 00:08:17 +09:00
Konrad `ktoso` Malawski
a2783b2e8d [Concurrency] UnownedJob.priority and description for TaskPriority 2023-02-28 16:17:48 +09:00
Doug Gregor
ee4f6b1f75 Make CheckedContinuation and UnsafeContinuation unconditionally Sendable.
The continuation types were conditionally `Sendable` based on whether
the result type of the continuation was `Sendable`. However,
conceptually, the return value is never leaving the current task, so
it is never actually crossing concurrency boundaries. Therefore, we
can make the continuation types unconditionally `Sendable`.

Fixes rdar://98462858.
2022-08-19 13:06:41 -07:00
John McCall
84c74941dc Make with*Continuation inherit the caller's executor.
Not inheriting the caller's executor is a major problem for
these functions.  Under SE-0338, treating them as non-isolated
means that it's illegal to pass them anytthing non-Sendable
from a different isolation context; since the function is
declared to take a non-sendable function parameter, effectively,
SE-0338 means that these functions can only be safely called
from non-isolated contexts.  That's not really acceptable, but
it gets worse: since we haven't implemented the sendability
rule for that yet, we're immediately bypassing isolation safety
when using these functions with no warning.

The `withCheckedContinuation` functions are not
`@_alwaysEmitIntoClient` (an oversight in the initial release),
and so we need to use the unsafe attribute on them.  When we
eventually implement a safe mechanism for this, we should make
`@_alwaysEmitIntoClient` variants of these functions which use
the new feature, and we can demote the existing functions to
`internal @availableFromInline`.

I've gone ahead and made `withChecked*Continuation` `@inlinable`.
2022-02-14 20:46:03 -05:00
najacque
57ad1f1c7d Merge pull request #40115 from amartini51/concurrency_docs_to_main
Cherry-pick concurrency docs
2021-12-07 11:59:56 -08:00
Doug Gregor
5a99ae485e Make (Unsafe|Checked)Continuation's Sendable conformance conditional.
Make the continuation type's conformances to the `Sendable` protocol
conditional on the sendability of the result yielded when the
resumption is performed. This ensures that one cannot silently escape
a continuation's result out of a task or actor, closing a safety hole
in Sendable checking.

Fixes rdar://85419546.
2021-11-15 16:41:07 -08:00
Alex Martini
699421c79b Cherry pick Swift 5.5 doc changes in PartialAsyncTask.swift 2021-11-09 17:23:38 -08:00
Karoy Lorentey
47956908b7 [Concurrency] SwiftStdlib 5.5 ⟹ SwiftStdlib 5.1 (usages)
The concurrency runtime now deploys back to macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, which corresponds to the 5.1 release of the stdlib.

Adjust macro usages accordingly.
2021-10-28 14:36:36 -07:00
Doug Gregor
60763a07e2 CheckedContinuation, UnsafeContinuation, and UnownedJob are Sendable
Fixes rdar://82909334.
2021-09-29 09:51:44 -07:00
Michael Gottesman
fc4d6c86ef [concurrency] Add UnownedJob._runSynchronously(on:) to support running a job synchronously on an executor.
I am going to use this to test that we propagate synchronous accesses into
asynchronous tasks access sets.

To ensure this is not ABI, I underscored/marked this as alwaysEmitIntoClient.
2021-07-22 13:25:10 -07:00
John McCall
0ce82d0755 Stage the swift_continuation_await ABI change.
Introduce a fake (but non-ABI) declaration to the swiftinterface
which marks that an SDK support swift_continuation_await, and then
only call it if that declaration exists, otherwise falling back
on the old atomic sequence.  Using that sequence will badly mess
up the runtime's tracking of task state, but it might still work,
and more importantly things will still build, which solves the
short-term problem.  Hopefully we can remove this hack soon.

Fixes rdar://problem/80787731.
2021-07-20 20:31:40 -04:00
Doug Gregor
1a024e96c8 Remove UnownedJob.run(). It never did anything anyway 2021-07-08 13:23:42 -07:00
Ben Rimmington
d3d87e8447 [Concurrency] Deprecate typealias PartialAsyncTask (#38042)
Deprecate typealias PartialAsyncTask, and move it into the "SourceCompatibilityShims.swift" file.
2021-06-23 21:16:17 +01:00
Doug Gregor
eda5b4daad [Concurrency] Alternative Task API.
The `Task` type has oscillated somewhat from being purely a namespace,
to having instances that are used (albeit rarely), back to purely
being a namespace that isn't used for all that many names. Many of the
names that used to be on Task have already been moved out, e.g., for
creating new detached tasks, creating new task groups, adding
cancellation handlers, etc.

Collapse `Task.Handle<Success, Failure>` into `Task<Success, Failure>`.
`Task.Handle` is the type that is most frequently referenced in the
concurrency library, so giving it the short name `Task` is most
appropriate. Replace the top-level async/detach functions with a
`Task` initializer and `Task.detached`, respectively.

The `Task` type can still act as a namespace for static operations
such as, e.g., `Task.isCancelled`. Do this with an extension of the
form:

    extension Task where Success == Never, Failure == Never { ... }

We've been accruing a number of compatibility shims. Move them all
into their own source file, deprecate them, and make them
always-emit-into-client so they don't have any ABI impact.
2021-05-18 14:36:21 -07:00
Alexis Laferrière
10d115bd5c [Concurrency] Use the SwiftStdlib 5.5 macro instead of 9999 versions 2021-05-04 09:30:58 -07:00
John McCall
186c53000d Introduce basic support for custom executors.
- Introduce an UnownedSerialExecutor type into the concurrency library.
- Create a SerialExecutor protocol which allows an executor type to
  change how it executes jobs.
- Add an unownedExecutor requirement to the Actor protocol.
- Change the ABI for ExecutorRef so that it stores a SerialExecutor
  witness table pointer in the implementation field.  This effectively
  makes ExecutorRef an `unowned(unsafe) SerialExecutor`, except that
  default actors are represented without a witness table pointer (just
  a bit-pattern).
- Synthesize the unownedExecutor method for default actors (i.e. actors
  that don't provide an unownedExecutor property).
- Make synthesized unownedExecutor properties `final`, and give them
  a semantics attribute specifying that they're for default actors.
- Split `Builtin.buildSerialExecutorRef` into a few more precise
  builtins.  We're not using the main-actor one yet, though.

Pitch thread:
  https://forums.swift.org/t/support-custom-executors-in-swift-concurrency/44425
2021-04-30 03:11:56 -04:00