Commit Graph

95 Commits

Author SHA1 Message Date
Doug Gregor
3c38ffe0ea [Concurrency] await try -> try await
The `try await` ordering is both easier to read and indicates the order
of operations better, because the suspension point occurs first and
then one can observe a thrown error.
2020-12-23 13:21:59 -08:00
Erik Eckstein
29543d4997 Revert "[Concurrency] Workaround for _runAsyncHandler."
This reverts commit 5cac8d0152.
2020-12-17 16:33:03 +01:00
swift-ci
81ef1bd36f Merge pull request #35135 from DougGregor/concurrency-runasynchandler-workaround 2020-12-16 21:54:21 -08:00
Doug Gregor
5cac8d0152 [Concurrency] Workaround for _runAsyncHandler.
Work around an expected miscompile in _runAsyncHandler.
2020-12-16 19:37:50 -08:00
Konrad `ktoso` Malawski
b267778bf1 Rebased to use new global executor 2020-12-17 06:05:13 +09:00
Konrad `ktoso` Malawski
9e1ecc539c [Concurrency] guard offer/poll with a lock for now; cleanups 2020-12-17 06:05:13 +09:00
Konrad `ktoso` Malawski
7b37554096 [Concurrency] Initial TaskGroup implementation working 2020-12-17 06:05:13 +09:00
Konrad `ktoso` Malawski
e294c7cbad [Concurrency] Implement TaskGroup.isEmpty via readyQueue
before reversing order of fragments; future must be last since dynamic
size

offer fixed

before implementing poll
2020-12-17 06:05:13 +09:00
Konrad `ktoso` Malawski
520b513e8a [Concurrency] Task: isCancelled,checkCancelled implementation
move comments to the wired up continuations

remove duplicated continuations; leep the wired up ones

before moving to C++ for queue impl

trying to next wait via channel_poll

submitting works; need to impl next()
2020-12-17 06:05:13 +09:00
Konrad `ktoso` Malawski
9162d40cff +task Implement Task.currentPriority
cleanups
2020-12-17 06:05:13 +09:00
Doug Gregor
35acd40b1e +concurrency task groups initial work in progress 2020-12-17 06:05:13 +09:00
Doug Gregor
d6060153d2 Merge pull request #35059 from jckarter/integrate-objc-interop-with-task-runtime
Integrate ObjC interop intrinsics with task runtime.
2020-12-12 18:10:35 -08:00
Doug Gregor
7a7179d983 Merge pull request #35071 from DougGregor/with-one-unsafe-continuation
[Concurrency] Remove extraneous copies of withUnsafe(Throwing)Continuation
2020-12-11 20:59:38 -08:00
Doug Gregor
1a15eb1b7c [Concurrency] Remove extraneous copies of withUnsafe(Throwing)Continuation 2020-12-11 15:42:35 -08:00
Joe Groff
2ee6b70402 Integrate ObjC interop intrinsics with task runtime. 2020-12-11 09:20:02 -08:00
Doug Gregor
79e66210a0 [Concurrency] Schedule "async let" child tasks.
This provides runtime support for "async let". Add a simple test of
"async let" along with a simple test of the actor runtime.
2020-12-10 23:53:10 -08:00
John McCall
7cc63f9a28 Implement a cooperative global executor for single-threaded runtimes.
Currently, the only thing in the system that donates a thread
to run it is swift_runAndBlockThread, but we'll probably need
others.  Nothing in the concurrency runtime should block via a
semaphore in this configuration.

As an outrageous hack, work around the layering problems with
using libdispatch from the concurrency library on non-Darwin
systems by making those systems use the cooperative global
executor.  This is only acceptable as a temporary solution
for landing this change and setting things onto the right
long-term design.
2020-12-10 19:18:53 -05:00
John McCall
1177cde4e3 Use current public Dispatch API to schedule global work.
We expect to iterate on this quite a bit, both publicly
and internally, but this is a fine starting-point.

I've renamed runAsync to runAsyncAndBlock to underline
very clearly what it does and why it's not long for this
world.  I've also had to give it a radically different
implementation in an effort to make it continue to work
given an actor implementation that is no longer just
running all work synchronously.

The major remaining bit of actor-scheduling work is to
make swift_task_enqueue actually do something sensible
based on the executor it's been given; currently it's
expecting a flag that IRGen simply doesn't know to set.
2020-12-10 19:18:53 -05:00
eeckstein
ad58de3a94 Merge pull request #34880 from eeckstein/task-allocator
[concurrency] Implement the Task allocator as bump-pointer allocator.
2020-12-10 08:37:06 +01:00
Joe Groff
291b75fcc4 Merge pull request #34953 from jckarter/async-native-to-foreign-thunk
[WIP] SILGen: Implement native-to-foreign thunks for async methods.
2020-12-09 17:00:36 -08:00
Erik Eckstein
ec1490d06e [concurrency] Implement the Task allocator as bump-pointer allocator.
Use the StackAllocator as task allocator.

TODO: we could pass an initial pre-allocated first slab to the allocator, which is allocated on the stack or with the parent task's allocator.

rdar://problem/71157018
2020-12-09 22:22:28 +01:00
Saleem Abdulrasool
a1d8819fc3 Concurrency: repair the build after #34902
libdispatch is not part of the system on Linux and Windows, and dispatch
has not been used for the standard library up until this point.  The
current usage is limited to the Apple platforms, so rather than adding
another build of dispatch, conditionally include the header instead.
2020-12-09 09:09:53 -08:00
Joe Groff
5087e411c2 SILGen: Implement native-to-foreign thunks for async methods.
Bridging an async Swift method back to an ObjC completion-handler-based API requires
that the ObjC thunk spawn a task on which to execute the Swift async API and pass
its results back on to the completion handler.
2020-12-08 10:04:40 -08:00
Doug Gregor
cc2e32f2fa Merge pull request #34902 from jckarter/continuation-resume-operations
Concurrency: Implement `resume(returning:)` and `resume(throwing:)` for Unsafe*Continuation.
2020-12-07 20:59:03 -08:00
Joe Groff
185f44e2d3 Concurrency: Implement resume(returning:) and resume(throwing:) for Unsafe*Continuation. 2020-12-07 07:47:47 -08:00
Doug Gregor
06a712bff6 Merge pull request #34957 from rjmccall/partial-async-task-is-a-job
Freeze PartialAsyncTask as a Job*
2020-12-04 15:43:44 -08:00
Doug Gregor
105a27322d [Concurrency] Traffic in underlying C type rather than JobFlags.
Suppresses a warning about returning a C++ type from a C entrypoint,
fixing rdar://71731181.
2020-12-04 12:29:18 -08:00
Doug Gregor
18ef1869f3 [Concurrency] Diagnose "try await" with a Fix-It 2020-12-04 00:57:18 -08:00
John McCall
ee0bb0a2d5 Freeze PartialAsyncTask as a Job*.
Also fix the extra inhabitants of Builtin.RawUnsafeContinuation
and try to make adding types like this a little less boiler-plate
in the future.
2020-12-04 01:06:29 -05:00
Slava Pestov
c133e13efd Merge pull request #34916 from slavapestov/with-unsafe-continuation
Concurrency: Implement withUnsafe[Throwing]Continuation
2020-12-03 11:18:49 -05:00
Erik Eckstein
89007e2e04 runtime: work around the missing task-enqueue implementation
Instead of assert, just run the job immediately.
Needed to make the hop_to_executor IRGen test of the following commit working.
2020-12-03 11:56:44 +01: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
John McCall
853a8657dd Add a basic default-actor implementation with support for
eagerly adopting and abandoning threads.
2020-12-02 18:47:02 -05:00
John McCall
ca16548470 Add a swift::atomic<T> which uses a better ABI than MSVC's 128-bit std::atomic. 2020-12-02 18:47:02 -05:00
John McCall
0555a86f82 Extract executor stuff out into a separate header and
start introducing the idea of a swiftasync CC.
2020-12-02 18:47:02 -05:00
Slava Pestov
9b3cd280b5 Concurrency: Implement high-level withUnsafe[Throwing]Continuation entry points
These functions call the corresponding built-ins,
wrapping the Builtin.RawUnsafeContinuation value
in an Unsafe[Throwing]Continuation<T>.
2020-12-01 20:05:17 -05:00
Slava Pestov
361eef2af0 Concurrency: Change Unsafe[Throwing]Continuation<T> to store a RawUnsafeContinuation 2020-12-01 20:04:11 -05:00
Erik Eckstein
863dcec142 [concurrency] stdlib: add a _runAsyncHandler compiler intrinsic.
It just calls Task.runDetatched.
It's more efficient to have a non-generic compiler intrinsic than to let the compiler call the generic Task.runDetatched.
The _runAsyncHandler doesn't have to be generic because the return value of the run function is defined to be Void.
2020-12-01 08:42:56 +01:00
Joe Groff
822f8577e6 Merge pull request #34817 from jckarter/coalesce-unsafe-continuations
Coalesce multiple `Unsafe*Continuation` definitions.
2020-11-30 10:30:48 -08:00
Doug Gregor
dab6fb7098 [Concurrency] Implement SIL generation for "async let".
Implement SIL generation for "async let" constructs, which involves:

1. Creating a child task future at the point of declaration of the "async let",
which runs the initializer in an async closure.
2. Entering a cleanup to destroy the child task.
3. Entering a cleanup to cancel the child task.
4. Waiting for the child task when any of the variables is reference.
5. Decomposing the result of the child task to write the results into the
appropriate variables.

Implements rdar://71123479.
2020-11-27 22:50:39 -08:00
Konrad `ktoso` Malawski
fd8097c2d2 not owned 2020-11-20 00:46:40 -08:00
Konrad `ktoso` Malawski
7126f32f22 fix properly, by using Task.JobFlags which does conversions 2020-11-20 00:46:40 -08:00
Konrad `ktoso` Malawski
7615c707d6 [Concurrency] Fix priority implementation 2020-11-20 00:46:40 -08:00
Konrad `ktoso` Malawski
261f0d2dcd +task Implement Task.currentPriority 2020-11-20 09:00:41 +09:00
Joe Groff
f922eb237c Coalesce multiple Unsafe*Continuation definitions.
We somehow ended up with a set hidden in `Task` as well as a set at top level. SILGen currently
hooks into the top-level ones, so shed the `Task`-namespaced versions for now.
2020-11-19 09:05:11 -08:00
Doug Gregor
894528062d [Concurrency] Implement Task.Handle.get() in terms of an async runtime call.
Switch the contract between the runtime operation `swift_future_task_wait`
and Task.Handle.get() pver to an asynchronous call, so that the
compiler will set up the resumption frame for us. This allows us to
correctly wait on futures.

Update our "basic" future test to perform both normal returns and
throwing returns from a future, either having to wait on the queue or
coming by afterward.
2020-11-18 22:02:14 -08:00
Doug Gregor
a1284d062c [Concurrency] Stop runDetached from actually printing/running the task.
Rather than immediately running the task synchronously within
runDetached, return the handle to the newly-created task. Add a method
task.Handle.run() to execute the task. This is just a temporary hack
that should not persist in the API, but it lets us launch tasks on a
particular Dispatch queue:

```swift
extension DispatchQueue {
  func async<R>(execute: @escaping () async -> R) -> Task.Handle<R> {
    let handle = Task.runDetached(operation: execute)

    // Run the task
    _ = { self.async { handle.run() } }()

    return handle
  }
}
```

One can pass asynchronous work to DispatchQueue.async, which will
schedule that work on the dispatch queue and return a handle. Another
asynchronous task can then read the result.

Yay for rdar://71125519.
2020-11-17 10:04:26 -08:00
Doug Gregor
cd8029a079 Merge pull request #34759 from DougGregor/concurrency-future-builtin
[Concurrency] Add Builtin.createAsyncTaskFuture and implement runDetached on it
2020-11-16 13:28:56 -08:00
Arnold Schwaighofer
9dad3aff53 Merge pull request #34746 from aschwaighofer/reinstate_task_allocator_size_alignment_requirement
Reinstate task allocator size alignment requirement
2020-11-16 12:49:02 -08:00
Doug Gregor
2a41920c00 [Concurrency] Implement basic runDetached on top of createAsyncTaskFuture. 2020-11-15 22:37:13 -08:00