Commit Graph

259 Commits

Author SHA1 Message Date
Doug Gregor
c4e8844fdb Merge pull request #40616 from DougGregor/concurrency-sendable-frozen-types 2021-12-17 16:47:40 -08:00
Doug Gregor
400404cf3a Explicit Sendable conformances for @frozen types
Task groups and `UnsafeCurrentTask` are non-`Sendable`.
`UnownedSerialExecutor` is always `Sendable`.

Fixes rdar://86496341.
2021-12-17 11:10:29 -08: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
swift-ci
37ea254cc1 Merge pull request #40220 from DougGregor/predates-concurrency 2021-11-19 01:14:42 -08:00
Doug Gregor
089cc4f101 Remove last use of @_unsafeSendable in the concurrency library 2021-11-18 11:20:53 -08:00
Evan Wilde
1c852f9179 Adding @usableFromInline to needed API
getMainExecutor and asyncMainDrainQueue function declarations are needed
to compile programs with the asyn-main function. The functions are
declared internal, so they don't show up in the swift interface files.
2021-11-16 21:36:58 -08:00
Alex Martini
0f75fc7f51 Cherry pick Swift 5.5 doc changes in Task.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
19e3f640f7 Drop unintended @_transparent. 2021-10-13 11:44:20 -07:00
Doug Gregor
c8315682db Make sure that Task.isCancelled fix works on older OSs.
Part of rdar://84146091 & SR-15309.
2021-10-13 10:25:04 -07:00
Evan Wilde
d376ee9989 Run first thunk of Async main synchronously
This patch updates the asynchronous main function to run the first thunk
of the function synchronously through a call to `swift_job_run`.

The runloop is killed by exiting or aborting the task that it is running
on. As such, we need to ensure that the task contains an async function
that either calls exit explicitly or aborts. The AsyncEntryPoint, that
contains this code, was added in the previous patch. This patch adds the
pieces for the actual implementation of this behaviour as well as adding
the necessary code to start the runloop.

There are now four layers of main functions before hitting the "real"
code.

@main: This is the actual main entrypoint of the program. This
constructs the task containing @async_main, grabs the main executor,
runs swift_job_run to run the first part synchronously, and finally
kicks off the runloop with a call to _asyncMainDrainQueue. This is
generated in the call to `emitAsyncMainThreadStart`.

@async_main: This thunk exists to ensure that the main function calls
`exit` at some point so that the runloop stops. It also handles emitting
an error if the user-written main function throws.

e.g:

```
func async_main() async -> () {
  do {
    try await Main.$main()
    exit(0)
  } catch {
    _errorInMain(error)
  }
}
```

Main.$main(): This still has the same behaviour as with the
synchronous case. It just calls `try await Main.main()` and exists to
simplify typechecking.

Main.main(): This is the actual user-specified main. It serves the same
purpose as in the synchronous, allowing the programmer to write code,
but it's async!

The control flow in `emitFunctionDefinition` is a little confusing (to
me anyway), so here it is spelled out:

If the main function is synchronous, the `constant.kind` will be a
`SILDeclRef::Kind::EntryPoint`, but the `decl` won't be async, so it
drops down to `emitArtificalTopLevel` anyway.

If the main function is async and we're generating `@main`, the
`constant.kind` will be `SILDeclRef::Kind::AsyncEntryPoint`, so we also
call `emitArtificalTopLevel`. `emitArtificalTopLevel` is responsible for
detecting whether the decl is async and deciding whether to emit code to
extract the argc/argv variables that get passed into the actual main
entrypoint to the program. If we're generating the `@async_main` body,
the kind will be `SILDeclRef::Kind::EntryPoint` and the `decl` will be
async, so we grab the mainEntryPoint decl and call
`emitAsyncMainThreadStart` to generate the wrapping code.

Note; there is a curious change in `SILLocation::getSourceLoc()`
where instead of simply checking `isFilenameAndLocation()`, I change it
to `getStorageKind() == FilenameAndLocationKind`. This is because the
SILLocation returned is to a FilenameAndLocationKind, but the actual
storage returns true for the call to `isNull()` inside of the
`isFilenameAndLocation()` call. This results in us incorrectly falling
through to the `getASTNode()` call below that, which asserts when asked
to get the AST node of a location.

I also did a little bit of refactoring in the SILGenModule for grabbing
intrinsics. Previously, there was only a `getConcurrencyIntrinsic`
function, which would only load FuncDecls out of the concurrency
module. The `exit` function is in the concurrency shims module, so I
refactored the load code to take a ModuleDecl to search from.

The emitBuiltinCreateAsyncTask function symbol is exposed from
SILGenBuiltin so that it is available from SILGenFunction. There is a
fair bit of work involved going from what is available at the SGF to
what is needed for actually calling the CreateAsyncTask builtin, so in
order to avoid additional maintenance, it's good to re-use that.
2021-10-02 16:53:06 -07:00
Doug Gregor
95d84d6581 Allow a Swift 5.5 compiler to process the new _Concurrency module.
Swift 5.5 does not implement support for structural types that are
Sendable, which breaks type checking of the newer _Concurrency module.
Work around the issue with an `#if` in the offending inline code.

Fixes rdar://82602353.
2021-09-02 12:22:32 -07:00
Doug Gregor
4a63884026 Add missing Sendable requirements and a conformance 2021-08-26 10:56:48 -07:00
Doug Gregor
3723c2f685 Adopt Sendable in the Task APIs 2021-08-14 08:13:10 -07:00
Doug Gregor
d9e3259ff6 [SE-0304] Clean up handling of task priorities. 2021-07-30 12:53:03 -07:00
Doug Gregor
8884e3a510 [SE-0304] Rename Task.suspend() back to Task.yield(). 2021-07-27 12:19:55 -07:00
Andrew Trick
982b47d829 Add Builtin.hopToActor to the feature list.
This builtin never occurs in @inlinable code. But apparently we still
need to add a language feature for every builtin. This must allow
older compilers to reparse the library source (though I don't know why
that would ever happen!)

Fixes rdar://80525569 error: module 'Builtin' has no member named 'hopToActor')
2021-07-13 16:28:38 -07:00
Doug Gregor
4a0723f68c [SE-0304] Add UnsafeCurrentTask.cancel(). 2021-07-08 13:22:33 -07:00
Doug Gregor
59d1e61ac4 [SE-0304] Implement cancellable Task.sleep(nanoseconds:). 2021-07-08 09:46:07 -07:00
Doug Gregor
b7e2136e4a [SE-0304] Rename Task.yield() to Task.suspend() 2021-07-07 20:30:57 -07:00
Doug Gregor
f692faea6a [SE-0304] Rename TaskPriority.default to medium 2021-07-07 18:06:52 -07:00
Doug Gregor
819615eab1 Make Swift task-creation entry points always-emit-into-client.
The `swift_task_create` entry point is our general runtime ABI for
launching tasks. Make the various Swift APIs sitting on top of it
always-emit-into-client to take them out of the ABI. This reduces the
number of ABI entry points and allows us to make more ABI-compatible
changes to the Swift side.
2021-06-24 07:53:19 -07:00
Doug Gregor
ba0644cf81 Move priority adjustments for async task creation into the runtime.
We're not actually performing the adjustments at the moment due to an
unrelated bug, and will want to perform them within
`swift_task_create_common` based on inheritContext and the given
priority.
2021-06-24 07:53:18 -07:00
Doug Gregor
7def279cdb Drop task options from the createAsyncTask SIL builtin.
We'll keep the task options entirely as a contract between IRGen and
the concurrency runtime, so Swift code need not deal with them.
2021-06-24 07:53:18 -07:00
Doug Gregor
e7e922ea77 Introduce a createAsyncTaskInGroup SIL builtin.
Rather than using group task options constructed from the Swift parts
of the _Concurrency library and passed through `createAsyncTask`'s
options, introduce a separate builtin that always takes a group. Move
the responsibility for creating the options structure into IRGen, so
we don't need to expose the TaskGroupTaskOptionRecord type in Swift.
2021-06-24 07:53:18 -07:00
Doug Gregor
95e38839ab Hollow out swift_asyncLet_startImpl to just a swift_task_create call.
Introduce a task option record to capture the async-let storage, so
that `swift_task_create` can perform the appropriate initialization.
2021-06-24 07:53:18 -07:00
Doug Gregor
8388a92ee9 Correct naming of "CopyThreadLocals" to "CopyTaskLocals" 2021-06-24 07:53:18 -07:00
Doug Gregor
dafb574a35 Switch task group child task creation over to Builtin.createAsyncTask().
Extend the behavior of `swift_task_create_common` to also encompass
adding the pending group task (when requested) and attaching it to the
group.
2021-06-24 07:53:18 -07:00
Doug Gregor
c7edfa3ba9 Centralize non-group task creation on swift_task_create[_f].
Introduce a builtin `createAsyncTask` that maps to `swift_task_create`,
and use that for the non-group task creation operations based on the
task-creation flags. `swift_task_create` and the thin function version
`swift_task_create_f` go through the dynamically-replaceable
`swift_task_create_common`, where all of the task creation logic is
present.

While here, move copying of task locals and the initial scheduling of
the task into `swift_task_create_common`, enabling by separate flags.
2021-06-24 07:53:17 -07:00
Konrad `ktoso` Malawski
8536100354 [Concurrency] introduce task options, and change ABI to accept them
introduce new options parameter to all task spawning

[Concurrency] ABI for asynclet start to accept options

[Concurrency] fix unittest usages of changed task creation ABI

[Concurrency] introduce constants for parameter indexes in ownership

[Concurrency] fix test/SILOptimizer/closure_lifetime_fixup_concurrency.swift
2021-06-21 13:03:50 +09:00
Arnold Schwaighofer
5e9d1b52e2 Merge pull request #37939 from aschwaighofer/workaround_runtime_bug
Workaround the current actor runtime's unwillingness to deal with priorities properly
2021-06-16 14:21:50 -07:00
Arnold Schwaighofer
c4bd90f6c4 Workaround the actor runtimes unwillingness to deal with priorities properly
rdar://79378627
2021-06-16 07:39:32 -07:00
Joe Groff
e39dca4dbf SILGen: Hop to the executor for actor-constrained methods in their @objc async thunks.
If a Swift async method is actor-constrained, then when it's projected into ObjC, it should still
run its task on the correct actor, along with the completion handler that ObjC passes into it.
Fixes rdar://76415650.
2021-06-15 12:09:47 -07:00
Andrew Trick
d3ac3d4d84 Use Builtin.hopToActor in _runAsyncMain
If the optimizer is doing it's job, then @MainActor can't be used to
force another actor-independent async callee onto the main actor.

Instead, explicitly hop, which communicates the actual intent and is robust.
2021-06-13 23:44:30 -07:00
Doug Gregor
7d2ce77e4d [Concurrency] Add async properties Task.value and Task.result 2021-05-18 20:03:26 -07:00
Doug Gregor
8750f209fc [Concurrency] Rename TaskPriority members.
Based on feedback from the second review, we decided to go with
high/default/low/background, with aliases for the Dispatch-inspired
names. While here, make TaskPriority be backed by a UInt8 to better
describe the actual restrictions, and start removing userInteractive,
because clients shouldn't be able to specify it.
2021-05-18 16:37:40 -07:00
Doug Gregor
6aefea0716 Fix up some documentation for the async/detach renamings 2021-05-18 15:33:39 -07: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
Doug Gregor
9788cc2cf4 [Concurrency] Provide an alternative implementation when "async { }" is unusable
There exist Swift builds that support `Sendable` but not the attributes
used on `async`. Make sure that have an implementation of
`_runTaskForBridgedAsyncMethod` that does something.

Fixes rdar://77637570.
2021-05-17 13:11:53 -07:00
Doug Gregor
2b9ca315fe [Concurrency] Remove asyncHandler attribute.
The `asyncHandler` attribute turned out to be the wrong solution
to the problem of creating a sync->async bridge. Remove it.
2021-05-13 17:01:39 -07:00
Fred Riss
bbda706393 [Concurrency] Add a unique Task ID to AsyncTask
This commit changes JobFlags storage to be 32bits, but leaves the runtime
API expressed in terms of size_t. This allows us to pack an Id in the
32bits we freed up.

The offset of this Id in the AsyncTask is an ABI constant. This way
introspection tools can extract the currently running task identifier
without any need for special APIs.
2021-05-11 08:28:17 -07:00
Konrad `ktoso` Malawski
6cbb792f92 [TaskLocals] Propagate task-locals through async{} 2021-05-11 11:06:17 +09:00
Dario Rexin
efe6973293 [Concurrency] Reduce overhead of Task.yield and Task.sleep (#37090)
* [Concurrency] Reduce overhead of Task.yield and Task.sleep

Instead of creating a new task, we create a simple job that wraps a Builtin.RawUnsafeContinuation and resumes the continuation when it is executed. The job instance is allocated on the task local allocator, meaning we don't malloc anything.

* Update stdlib/public/Concurrency/Task.swift

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
2021-05-10 11:51:20 -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
cc2863c136 Merge pull request #36878 from rjmccall/custom-executors
Introduce basic support for custom executors
2021-04-30 13:54:02 -04: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
Doug Gregor
cdc6dbe11d [Concurrency] Deprecate Task.Priority.unspecified and use optionals instead 2021-04-29 22:16:02 -07:00
Doug Gregor
92dd8ef4df [Concurrency] Make async operation return a task handle.
Per updates to the Structured Concurrency protocol, make the `async`
operation (1) overloaded on throwing-ness and (2) return an appropriate
`Task.Handle`.
2021-04-29 20:57:53 -07:00
Doug Gregor
67ed43730d [Concurrency] Have Task.currentPriority query the system as a fallback.
When `Task.currentPriority` is evaluated outside of an actual task,
query the system to determine the priority at which the code is
currently executing. This is the behavior that `async` specifies.
Extend that to `currentPriority` rather than guessing `.default`.
2021-04-29 11:01:43 -07:00
Doug Gregor
2000bf9b5f Add @_implicitSelfCapture to asyncDetached.
Like `async`, `asyncDetached` should not require `self.` because it is
unlikely to introduce cycles.
2021-04-27 17:03:03 -07:00