Skip to content
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

[Concurrency] Introduce Actor protocol to which actor classes all conform #34111

Merged
merged 9 commits into from Sep 30, 2020

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Sep 29, 2020

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 definition of enqueue(partialTask:) along with storage for the runtime
to manager in the actor.

This design also lets us loosen the constraints on actors somewhat, so that
an actor class can be @objc and inherit directly from NSObject. Allow this,
and tighten up @objc checking to maintain actor isolation across the
language boundary.

…form.

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.
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.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

…bles.

Actor classes never have non-actor superclasses, so we can ensure that
all actor classes have a common vtable prefix for the
`enqueue(partialTask:)` operation. This allows us to treat all actor
classes uniformly, without having to go through the Actor witness
table every time.
…minator.

For actor class's implementations of `enqueue(partialTask:)`, use a
fixed ptrauth discriminator. Paired with the fixed vtable location,
this allows one to invoke this operation on any native actor instance
without knowing the specific type.
Actor-isolated operations must not be directly accessible from anywhere
that is not already guaranteed to be running within the actor context.
Prevent such operations from being `@objc`, because that would allow
Objective-C code to violate actor isolation.
NSObject is guaranteed to have no state and no Swift vtable, and is
necessary for Swift classes to implement the NSObject protocol. Allow
it (and only it) as the superclass of an actor class, so that actor
classes can be exposed to Objective-C.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit 75b18ad into apple:main Sep 30, 2020
@DougGregor DougGregor deleted the concurrency-actor-protocol branch September 30, 2020 20:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants