Commit Graph

41 Commits

Author SHA1 Message Date
Konrad `ktoso` Malawski
ec8caf3cdd Remove the experimental @Task macro (#82437) 2025-07-03 10:15:21 -07:00
Konrad 'ktoso' Malawski
c00946189c [Concurrency] Deprecate extractIsolation 2025-06-13 11:47:37 +09:00
Michael Gottesman
9d59dbed17 [swift-settings] Now that we aren't using it immediately, remove it from tree.
We can always get it back from the git history.

rdar://150695113
2025-05-05 13:39:03 -07:00
Holly Borla
bac0a10ae2 [Macros] Update the name and argument list for the function body macro
that wraps a function or closure body in a new top-level task.
2025-03-21 06:21:45 -07:00
Michael Gottesman
f64dd5a8d5 [concurrency] Add initial support for SwiftSettings to control defaultIsolation at the file level.
We introduce a new macro called #SwiftSettings that can be used in conjunction
with a new stdlib type called SwiftSetting to control the default isolation at
the file level. It overrides the current default isolation whether it is the
current nonisolated state or main actor (when -enable-experimental-feature
UnspecifiedMeansMainActorIsolated is set).
2025-03-10 17:33:45 -07:00
Holly Borla
8be41edf6f [Concurrency] Add a macro for wrapping a function body in a new task. 2025-03-03 20:51:14 -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
Konrad `ktoso` Malawski
2182dce8b7 [Concurrency] Document custom executors in API docs a bit
Include review feedback and show how to customize a single actor by
passing an executor through initializer
2024-06-04 15:42:02 +09:00
Holly Borla
c20b0e0956 [Concurrency] Obsolete AnyActor in Swift 6 using a typealias. 2024-05-29 22:59:19 -07:00
Holly Borla
1011e4ddb0 [Concurrency] Only deprecate AnyActor, and update more tests.
Obsoleting `AnyActor` in Swift 6 blocks the Concurrency library itself
from migrating to Swift 6, because `Actor` and `DistributedActor` have to
preserve their refinement of `AnyActor` to avoid breaking code currently
using the marker protocol. There's no way to move protocol refinement into
an extension so that the use-site declaration can be obsoleted, so we're
stuck with just the deprecation of `AnyActor`.
2024-05-24 12:58:02 -07:00
Holly Borla
7af38860ed [Concurrency] Deprecate and obsolete AnyActor.
This marker protocol isn't useful for abstracting over actors and distributed
actors because it doesn't have any runtime requirements, so there's no way
to switch to the given actor. Instead, you should use `isolated (any Actor)?`
parameters, and the compiler will compute a local actor value from a
distributed actor using `DistributedActor.asLocalActor`.
2024-05-24 08:57:17 -07:00
Holly Borla
f1e14f8be7 [Concurrency] Mark extractIsolation as @_alwaysEmitIntoClient. 2024-02-27 18:29:28 -08:00
Holly Borla
59507ba361 [Concurrency] Add an API for extracting the isolation of a dynamically isolated
function value.
2024-02-26 17:59:27 -08:00
Holly Borla
ddf2fc44f4 [Concurrency] Allow #isolation to have a more specific contextual type. 2024-02-16 16:27:54 -08:00
Doug Gregor
8d67a37231 Only declare isolation macro when the compiler supports macros 2024-01-18 18:47:26 -08:00
Doug Gregor
24c2d1a49c Switch #isolation from AnyActor to Actor
We are currently lacking the ability to describe "normal or distributed
actor" with a single protocol in a manner that allows hopping to it,
because `AnyActor` is a marker protocol. Use `Actor` while we sort
this out.
2024-01-18 18:46:47 -08:00
Doug Gregor
255009dddb Implement #isolation macro to produce the isolation of the current context
Introduce a new expression macro that produces an value of type
`(any AnyActor)?` that describes the current actor isolation. This
isolation will be `nil` in non-isolated code, and refer to either the
actor instance of shared global actor in other cases.

This is currently behind the experimental feature flag
OptionalIsolatedParameters.
2024-01-16 14:25:51 -08:00
Konrad `ktoso` Malawski
41f99fc2ae [Executors][Distributed] custom executors for distributed actor (#64237)
* [Executors][Distributed] custom executors for distributed actor

* harden ordering guarantees of synthesised fields

* the issue was that a non-default actor must implement the is remote check differently

* NonDefaultDistributedActor to complete support and remote flag handling

* invoke nonDefaultDistributedActorInitialize when necessary in SILGen

* refactor inline assertion into method

* cleanup

* [Executors][Distributed] Update module version for NonDefaultDistributedActor

* Minor docs cleanup

* we solved those fixme's

* add mangling test for non-def-dist-actor
2023-03-15 23:42:55 +09:00
Konrad `ktoso` Malawski
84347e5d54 [Concurrency] Avoid redundant conformance warning on Actor
Since the `Actor` type is now `Sendable` via `AnyActor`:

```
/Users/ktoso/code/swift-project/swift/stdlib/public/Concurrency/Actor.swift:38:34: warning: redundant conformance constraint 'Self' : 'Sendable'
public protocol Actor: AnyActor, Sendable {
                                 ^
/Users/ktoso/code/swift-project/swift/stdlib/public/Concurrency/Actor.swift:38:24: note: conformance constraint 'Self' : 'Sendable' implied here
public protocol Actor: AnyActor, Sendable {
                       ^
```
2022-03-30 12:43:17 +09:00
Konrad `ktoso` Malawski
040a4a318c [Concurrency][Distributed] Move AnyActor to Concurrency module 2022-03-08 15:28:30 +09: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
John McCall
ca62a79079 Use &_dispatch_main_q as the identity of the main actor.
I added Builtin.buildMainActorExecutor before, but because I never
implemented it correctly in IRGen, it's not okay to use it on old
versions, so I had to introduce a new feature only for it.

The shim dispatch queue class in the Concurrency runtime is rather
awful, but I couldn't think of a reasonable alternative without
just entirely hard-coding the witness table in the runtime.
It's not ABI, at least.
2021-06-17 05:04:30 -04:00
Doug Gregor
8a068e0c08 [SE-0316] Introduce the GlobalActor protocol to describe global actors.
Based on the discussion in the first review of the global actors
proposal, introduce a `GlobalActor` protocol that describes types that
can be global actors. Introduce this protocol, make `@globalActor`
types implicitly conform to it, and remove all of the bespoke
validation logic that was used to check the "shared" member.

Addresses rdar://79339591
2021-06-15 06:39:12 -07:00
Chris Adamson
83ac78f1cf Remove symbol from Actor abstract. 2021-05-27 15:21:11 -04:00
Chris Adamson
6eebb2207a Apply suggestions from code review
Co-authored-by: Brian Lanier <blanier@apple.com>
2021-05-27 09:54:41 -04:00
Chris Adamson
12d5d8edef Correct placement of doc comments in AsyncSequence
Also remove stray characters in doc comments for Actor, MainActor.
2021-05-27 09:32:48 -04: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
Mike Ash
1173b737aa [Concurrency] Add availability to Concurrency APIs.
This allows programs to target older OSes while using Concurrency behind an availability check. When targeting older OSes, the symbols are weak-linked and the compiler will require the use of Concurrency features to be guarded by an availability check.

rdar://75850003
2021-04-01 10:42:08 -04:00
Evan Wilde
42b6918657 NFC: Expunge 'actor class' in comments 2021-03-23 15:06:40 -07:00
swift-ci
bdc048e541 Merge pull request #36511 from DougGregor/main-actor-run 2021-03-19 10:19:13 -07:00
Doug Gregor
3aa4fbec51 Add MainActor.run to execute a synchronous closure on the main thread.
It's important to be able to execute a synchronous closure on the main
thread to perform various main-thread updates. This is the async equivalent
to DispatchQueue.main.sync.
2021-03-18 23:52:58 -07:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Doug Gregor
47b0abfda4 Remove Actor.enqueue(partialTask:) and all its magic.
The backs out of some early decisions we made about actor layout
that we don't need. Custom actors will use a different approach.

This should suffice for the remainder of rdar://70146827.
2021-03-04 22:49:10 -08:00
Evan Wilde
748f15e0d5 Update standard library actor class
This patch updates the once instance of actor class in the concurrency
library to actor. This is the actor for the MainActor global actor.
2021-02-10 08:05:57 -08:00
Doug Gregor
27e0b1243e [Concurrency] Add the ConcurrentValue protocol.
The ConcurrentValue protocol indicates when a particular type is safe
to share in a concurrent context, e.g., that a copy of a given value
can be safely used concurrently with the value.
2021-02-03 16:43:33 -08:00
Kavon Farvardin
91e2246c19 quick-and-dirty implementation of MainActor in the runtime system
it is "dirty" in the sense that we don't have proper support for
custom executors right now.
2021-01-14 17:43:58 -08:00
Kavon Farvardin
a2c57d13c3 [concurrency] initial declaration of MainActor
Implementation is left as a TODO for now.

Resolves rdar://72161578
Partially resolves rdar://72105129
2021-01-06 15:01:22 -08:00
John McCall
945011d39f Handle default actors by special-casing layout in IRGen instead
of adding a property.

This better matches what the actual implementation expects,
and it avoids some possibilities of weird mismatches.  However,
it also requires special-case initialization, destruction, and
dynamic-layout support, none of which I've added yet.

In order to get NSObject default actor subclasses to use Swift
refcounting (and thus avoid the need for the default actor runtime
to generally use ObjC refcounting), I've had to introduce a
SwiftNativeNSObject which we substitute as the superclass when
inheriting directly from NSObject.  This is something we could
do in all NSObject subclasses; for now, I'm just doing it in
actors, although it's all actors and not just default actors.
We are not yet taking advantage of our special knowledge of this
class anywhere except the reference-counting code.

I went around in circles exploring a number of alternatives for
doing this; at one point I basically had a completely parallel
"ForImplementation" superclass query.  That proved to be a lot
of added complexity and created more problems than it solved.
We also don't *really* get any benefit from this subclassing
because there still wouldn't be a consistent superclass for all
actors.  So instead it's very ad-hoc.
2020-12-02 18:47:13 -05:00
Doug Gregor
c2b8565a10 [Concurrency] Implicitly synthesize actor queue storage and enqueue.
When an actor class has its `enqueue(partialTask:)` implicitly
synthesized, also synthesize a stored property for the actor's queue.
The type of the property is defined by the _Concurrency library
(`_DefaultActorQueue`), and it will be initialized with a call to
`_defaultActorQueueCreate` (also provided by the _Concurrency
library).

Also synthesize the body of the implicitly-generated
`enqueue(partialTask:)`, which will be a call to
`_defaultActorQueueEnqueuePartialTask(actor:queue:partialTask:)`.
Together, all of these allow us to experiment with the form of the
queue and the queue operation without affecting the type checker.

When `enqueue(partialTask:)` is not implicitly synthesized, the queue
storage is not synthesized either. In such cases, the user has taken
over the execution of tasks for the actor, rather than using the
default implementation.
2020-09-29 13:50:32 -07:00
Doug Gregor
ab3c5dee3e [Concurrency] Introduce Actor protocol to which actor classes all conform.
Introduce a new Actor protocol, which is a class-bound protocol with only
one requirement:

    func enqueue(partialTask: PartialAsyncTask)

All actor classes implicitly conform to this protocol, and will synthesize
a (currently empty) definition of `enqueue(partialTask:)` unless a suitable
one is provided explicitly.
2020-09-28 16:59:21 -07:00