Skip to content

[Concurrency] Global actors #34201

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
Oct 10, 2020

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Oct 6, 2020

Global actor types can be used as attributes on various kinds of declarations to indicate that those declarations are part of the isolated state of that global actor. This pull request introduces support for global actors into the concurrency model:

  • The @globalActor attribute indicates a type that can act as a global actor. Such types must have a static property named shared that provides a globally shared actor instance identified by the type. Add the attribute and check for this condition.
  • A global actor can be used as an attribute on various declarations to indicate that they run on the actor indicated by the type. Most kinds of declarations (functions, properties, subscripts, types, extensions) can indicate which global actor they are associated with. Check basic correctness in the use of global actors as attributes.
  • Implement propagation and inference of global actor attributes from superclasses, overrides, protocol conformances, etc.
  • Extend the actor isolation model to incorporate global actors.

Implements most of rdar://70101562.

@DougGregor DougGregor marked this pull request as draft October 6, 2020 16:43
The globalActor attribute indicates that a particular type describes a
global actor. Global actors allow the notion of actor state isolation
to be spread across various declarations throughout a program, rather
than being centered around a single actor class. There are useful
primarily for existing notions such as "main thread" or subsystems
accessed through global/singleton state.
Global actor types can be used as attributes on various kinds of
declarations to indicate that those declarations are part of the
isolated state of that global actor. Allow such annotation and perform
basic correctness checks.
Extend the actor isolation checking rules to account for global
actors. For example, a function annotated with a given global actor
can invoke synchronous methods from the same global actor, but not
from a different global actor or a particular actor instance.
Similarly, a method of an (instance) actor that is annotated with a
global actor attribute is not part of the (instance) actor and,
therefore, cannot operate on its actor-isolated state.
The "privileged" cases for actor instance and global actor isolation
covered the case where the entity itself is within the particular
actor but can be freely used from outside the actor, e.g., because it
is asynchronous or an asynchronous handler. This is the wrong modeling
for the problem, because it's only the first part of that---what the
isolation of the particular entity is---that is needed for most
clients. There will be a different abstraction for that.
@DougGregor DougGregor force-pushed the concurrency-global-actor branch from 54a1a7a to c6f4be2 Compare October 9, 2020 22:48
Rename IsolationRestriction to ActorIsolationRestriction. This type
governs the *use* of a potentially actor-isolated entity, which
includes the notion that (e.g.) an asychronous function can be used
from any context even when it is actor-isolated. Use this for @objc
checking and protocol conformance checking, in addition to its
original use for enforcing actor isolation in statements and
expressions.
@DougGregor DougGregor marked this pull request as ready for review October 9, 2020 23:17
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test source compatibility

@DougGregor DougGregor merged commit 4419f87 into swiftlang:main Oct 10, 2020
@DougGregor DougGregor deleted the concurrency-global-actor branch October 10, 2020 07:19
@DougGregor
Copy link
Member Author

This was getting big enough, so I went ahead and merged it. The global actor propagation rules, as well as checking for consistency of witnesses and requirements, is still to come.

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.

1 participant