Commit Graph

1608 Commits

Author SHA1 Message Date
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
Doug Gregor
4fb5f05f37 [Concurrency] More cleanups for futures 2020-11-14 15:18:54 -08:00
Doug Gregor
c0fd27fbe5 [Concurrency] Minor cleanups to futures. 2020-11-14 15:18:54 -08:00
Doug Gregor
7f3db7fbde [Concurrency] Have future functions write their results directly.
Introduce `FutureAsyncContext` to line up with the async context formed
by IR generation for the type `<T> () async throws -> T`. When allocating
a future task, set up the context with the address of the future's storage
for the successful result and null out the error result, so the caller
will directly fill in the result. This eliminates a bunch of extra
complexity and a copy.
2020-11-14 15:18:54 -08:00
Doug Gregor
19d9e0f4d4 [Concurrency] Clarify/fix error object handling in futures. 2020-11-14 15:18:54 -08:00
Doug Gregor
5538aafed8 [Concurrency] Inline AsyncTask::FutureFragment::fragmentSize. 2020-11-14 15:18:54 -08:00
Doug Gregor
91360f0785 [Concurrency] Turn "simple task" into just "task".
There isn't a big advantage to keeping "simple" task as a separate
concept from a task. Tasks may or may not have futures.
2020-11-14 15:18:54 -08:00
Doug Gregor
0ae6c6337e [Concurrency] Use SchedulerPrivate for the "next waiting task" link.
Reduce the size of AsyncTask by using the first slot of SchedulerPrivate
for the next waiting task. Thanks, John!
2020-11-14 15:18:54 -08:00
Doug Gregor
4b924673ce [Concurrency] Use a single atomic for future wait queue.
Use a single atomic for the wait queue that combines the status with
the first task in the queue. Address race conditions in waiting and
completing the future.

Thanks to John for setting the direction here for me.
2020-11-14 15:18:54 -08:00
Doug Gregor
85d003ef9b [Concurrency] Implement basic runtime support for task futures.
Extend AsyncTask and the concurrency runtime with basic support for
task futures. AsyncTasks with futures contain a future fragment with
information about the type produced by the future, and where the
future will put the result value or the thrown error in the initial
context.

We still don't have the ability to schedule the waiting tasks on an
executor when the future completes, so this isn't useful for anything
just test, and we can only test limited code paths.
2020-11-14 15:18:54 -08:00
Arnold Schwaighofer
b1c94a631b Revert "Silence an assert"
This reverts commit e51575b1df.
2020-11-14 10:12:33 -08:00
Arnold Schwaighofer
e51575b1df Silence an assert 2020-11-13 15:59:43 -08:00
Arnold Schwaighofer
193a3d5a8f Temporarily add runAsync to run a task synchronously in the current thread
Add a temporary runtime entry to run a task synchronous.
2020-11-13 10:41:42 -08:00
Joe Groff
0ca29b504f Merge pull request #34525 from jckarter/foreign-async-silgen
SILGen: Caller-side codegen for invoking foreign async functions
2020-11-12 06:55:04 -08:00
Mike Ash
768a085de8 Merge pull request #34598 from mikeash/os-unfair-lock-mutex
[Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin.
2020-11-11 11:37:12 -05:00
Joe Groff
368dc0f401 SILGen: Generate bodies for completion handler block impls 2020-11-10 16:36:50 -08:00
Mike Ash
e82d9e8c7b Move ConditionMutex to ConditionVariable::Mutex and move various other Mutex.h types to be nested. 2020-11-10 14:44:59 -05:00
Mike Ash
dd6c235a2d [Runtime] Use os_unfair_lock for Mutex and StaticMutex on Darwin.
os_unfair_lock is much smaller than pthread_mutex_t (4 bytes versus 64) and a bit faster.

However, it doesn't support condition variables. Most of our uses of Mutex don't use condition variables, but a few do. Introduce ConditionMutex and StaticConditionMutex, which allow condition variables and continue to use pthread_mutex_t.

On all other platforms, we continue to use the same backing mutex type for both Mutex and ConditionMutex.

rdar://problem/45412121
2020-11-06 13:05:37 -05:00
Doug Gregor
a2d7c701a4 Merge pull request #34600 from DougGregor/async-task-builtins
[Concurrency] Add cancelAsyncTask() builtin.
2020-11-05 20:53:25 -08:00
Konrad `ktoso` Malawski
49c6ae3da5 [Concurrency] Nurseries are now Task.Groups 2020-11-06 10:58:51 +09:00
Doug Gregor
c291eb596b [Concurrency] Add cancelAsyncTask() builtin.
Implement a new builtin, `cancelAsyncTask()`, to cancel the given
asynchronous task. This lowers down to a call into the runtime
operation `swift_task_cancel()`.

Use this builtin to implement Task.Handle.cancel().
2020-11-05 13:50:17 -08:00
Konrad `ktoso` Malawski
873125806d [Concurrency] Introduce initial (minimal, incomplete) Nursery APIs 2020-11-05 19:09:51 +09:00
Konrad `ktoso` Malawski
27d734a709 [Concurrency] Include Task.sleep(until:) and Task.yield() placeholders 2020-11-02 20:51:00 +09:00
Konrad `ktoso` Malawski
9e8f2cc031 [Concurrency] Task cancellation and deadline stubs 2020-11-02 20:51:00 +09:00
Joe Groff
f72afc8110 Merge pull request #34519 from jckarter/async-sil-tweaks
Async sil tweaks
2020-10-30 16:49:26 -07:00
Joe Groff
a9221219ab Make Unsafe*Continuation @frozen, so we can assume they're trivial 2020-10-30 11:08:24 -07:00
Konrad `ktoso` Malawski
107bc27c96 [Concurrency] Remove Handle.Failure, since we do not use it as get() is always throwing currently;
There is no meaningful way to restrict the error type (or expect it for that matter.
2020-10-28 17:04:06 +09:00
Konrad `ktoso` Malawski
3e261781e3 [concurrency] task is only a namespace after all; remove Task.current() 2020-10-28 16:54:52 +09:00
Konrad `ktoso` Malawski
d6d3e957d0 [Concurrency] add currentPriority to task 2020-10-28 14:15:20 +09:00
Konrad `ktoso` Malawski
b5fd2a5c56 Address review comments; get() must throw, formatting 2020-10-27 11:30:53 +09:00
Konrad `ktoso` Malawski
579c89c222 [Concurrency] More documentation of Task.Priority 2020-10-26 19:02:07 +09:00
Konrad `ktoso` Malawski
d6adac3172 [Concurrency] Stubs for Task.current() 2020-10-26 19:02:07 +09:00
Konrad `ktoso` Malawski
07f80bed64 [Concurrency] API stubs: Task.Handle, priority and runDetached 2020-10-26 19:02:07 +09:00
Konrad `ktoso` Malawski
9023e56bd9 [Concurrency] Add minimal placeholders for Task and UnsafeContinuation 2020-10-26 19:02:02 +09:00
John McCall
a06d18ce81 Add API for creating unscheduled tasks.
Make all tasks into heap objects.
2020-10-22 00:53:16 -04:00
John McCall
76d8f03ba4 Make the task allocator verify stack discipline in the laziest possible way. 2020-10-22 00:53:16 -04:00
John McCall
b717c7d823 Prepare for a more real task-local alloocator implementation. 2020-10-22 00:53:16 -04:00
John McCall
c18331c837 Move swift_task_alloc/dealloc into the Concurrency library.
Also, rename them to follow the general namespacing scheme.
2020-10-22 00:53:15 -04:00