Commit Graph

1607 Commits

Author SHA1 Message Date
Doug Gregor
623e54f45c Merge pull request #37007 from DougGregor/async-operation
[Concurrency] Add "async" operation for continuing work asynchronously.
2021-04-22 12:00:16 -07:00
Doug Gregor
181ffaf733 Fix a typo in async comment 2021-04-22 09:12:37 -07:00
Doug Gregor
fbc62373e0 Work around lack of qos_class_self in non-Darwin libdispatch 2021-04-22 09:11:00 -07:00
Doug Gregor
3c3f216be3 [Concurrency] Add "async" operation for continuing work asynchronously.
The `async` operation is a global function that initiates asynchronous
work on behalf of the synchronous code that calls it. Unlike `detach`,
`async` inherits priority, actor context, and other aspects of the
synchronous code that initiates it, making it a better "default"
operation for creating asynchronous work than `detach`. The `detach`
operation is still important for creating truly detached tasks that
can later be `await`'d or cancelled if needed.

Implements the main entry point for rdar://76927008.
2021-04-21 22:21:24 -07:00
Erik Eckstein
93367ed587 concurrency: make the startAsyncLet closure no-escaping
The closure does not escape the startAsyncLet - endAsyncLet scope. Even though it's (potentially) running on a different thread.

The substantial change in the runtime is to not call swift_release on the closure context if it's a non-escaping closure.
2021-04-20 21:57:19 +02:00
Konrad `ktoso` Malawski
a5469c438a [AsyncLet] remove some commented out code 2021-04-19 10:06:23 +09:00
Konrad `ktoso` Malawski
fae3c96874 [AsyncLet] ending an async let cancels the task 2021-04-19 10:06:23 +09:00
Konrad `ktoso` Malawski
eed96d21bb [AsyncLet] remove runChild; it is now asyncLetStart 2021-04-19 10:06:23 +09:00
Konrad `ktoso` Malawski
d3c5ebc9b7 [AsyncLet] reimplemented with new ABI and builtins 2021-04-19 10:06:23 +09:00
Konrad `ktoso` Malawski
ba615029c7 [Concurrency] Store child record when async let child task spawned 2021-04-19 10:06:23 +09:00
Evan Wilde
9b82fec131 Merge pull request #36457 from etcwilde/ewilde/delete-runAsyncAndBlock
[Concurrency] Delete runAsyncAndBlock
2021-04-16 08:40:30 -07:00
Mike Ash
2dc06c5fe2 [Concurrency] Fix Dispatch linkage offset on 32-bit.
rdar://76703675
2021-04-15 12:18:38 -04:00
Evan Wilde
9b15a9ffad Delete runAsyncAndBlock once and for all! 2021-04-14 15:04:56 -07:00
Konrad `ktoso` Malawski
5e0593f1d3 [Concurrency] detach should not take Failure type param 2021-04-15 06:59:35 +09:00
Evan Wilde
6c7b2af580 Remove await from spawnUnlessCancelled
spawnUnlessCancelled is not an asynchronous function, so awaiting on it
in the standard library resulted in warnings.
2021-04-13 18:29:17 -07:00
Doug Gregor
b5e818b266 Don't use CompareObjectHandles on Windows 2021-04-13 10:29:00 -07:00
Doug Gregor
159ffcc5fd Fix silly error in our attempt at Windows support 2021-04-13 09:39:49 -07:00
Doug Gregor
f25369d69b Minor fixes 2021-04-13 08:35:44 -07:00
Doug Gregor
a722748681 More Windows workarounds 2021-04-13 00:52:26 -07:00
Doug Gregor
95ee2f231e Portability fix 2021-04-12 22:23:49 -07:00
Doug Gregor
28fa56df02 Portability for data race runtime reporting 2021-04-12 21:39:56 -07:00
Doug Gregor
e77a27e8ed [Concurrency] Introduce runtime detection of data races.
Through various means, it is possible for a synchronous actor-isolated
function to escape to another concurrency domain and be called from
outside the actor. The problem existed previously, but has become far
easier to trigger now that `@escaping` closures and local functions
can be actor-isolated.

Introduce runtime detection of such data races, where a synchronous
actor-isolated function ends up being called from the wrong executor.
Do this by emitting an executor check in actor-isolated synchronous
functions, where we query the executor in thread-local storage and
ensure that it is what we expect. If it isn't, the runtime complains.
The runtime's complaints can be controlled with the environment
variable `SWIFT_UNEXPECTED_EXECUTOR_LOG_LEVEL`:

  0 - disable checking
  1 - warn when a data race is detected
  2 - error and abort when a data race is detected

At an implementation level, this introduces a new concurrency runtime
entry point `_checkExpectedExecutor` that checks the given executor
(on which the function should always have been called) against the
executor on which is called (which is in thread-local storage). There
is a special carve-out here for `@MainActor` code, where we check
against the OS's notion of "main thread" as well, so that `@MainActor`
code can be called via (e.g.) the Dispatch library's
`DispatchQueue.main.async`.

The new SIL instruction `extract_executor` performs the lowering of an
actor down to its executor, which is implicit in the `hop_to_executor`
instruction. Extend the LowerHopToExecutor pass to perform said
lowering.
2021-04-12 15:19:51 -07:00
Arnold Schwaighofer
ae2a4cca14 Merge pull request #36842 from aschwaighofer/fix_withCancellationHandler_arm64e_windows
Fix with cancellation handler arm64e windows
2021-04-10 14:10:12 -07:00
swift-ci
5db3caf9cc Merge pull request #36855 from apple/ktoso-patch-3 2021-04-09 19:53:03 -07:00
swift-ci
63b4cf0c9a Merge pull request #36689 from apple/tsan-task-groups 2021-04-09 18:23:38 -07:00
Konrad `ktoso` Malawski
34d1e27565 [Concurrency] runDetached shim must be static 2021-04-10 08:54:41 +09:00
Julian Lettner
29107180e5 Teach TSan about TaskGroups
`TaskGroup::offer(completedTask)`:
* Called from `AsyncTask::completeFuture()` which already takes care of
  `release(completedTask)`.  No additional edge is required if no task
  is waiting and completed task is stored so it can be retrieved by
  `group.poll()`.
* If group has waiting task, it will be dequeued and scheduled, add
  `acquire(waitingTask)`.

`TaskGroupImpl::poll(waitingTask)`:
* If no pending tasks, do nothing.
* If returning finished task, `acquire(finishedTask)`.
* If enqueuing waiting task, `release(waitingTask)`.

Note: `release()` should go before, and `acquire()` after the annotated
      synchronization operation.

Radar-Id: rdar://75910632
2021-04-09 14:44:53 -07:00
Arnold Schwaighofer
141634aae7 Fix withCancellationHandler on arm64e 2021-04-09 05:30:24 -07:00
John McCall
efeb818161 Clean up the TaskGroup ABI:
- stop storing the parent task in the TaskGroup at the .swift level
- make sure that swift_taskGroup_isCancelled is implied by the parent
  task being cancelled
- make the TaskGroup structs frozen
- make the withTaskGroup functions inlinable
- remove swift_taskGroup_create
- teach IRGen to allocate memory for the task group
- don't deallocate the task group in swift_taskGroup_destroy

To achieve the allocation change, introduce paired create/destroy builtins.

Furthermore, remove the _swiftRetain and _swiftRelease functions and
several calls to them.  Replace them with uses of the appropriate builtins.
I should probably change the builtins to return retained, since they're
working with a managed type, but I'll do that in a separate commit.
2021-04-09 03:06:31 -04:00
John McCall
82c190af21 Merge pull request #36751 from rjmccall/two-word-executor
Change ExecutorRef and fix the actor runtime
2021-04-08 16:49:37 -04:00
Philippe Hausler
eb0bd6afd3 [Concurrency] YieldingContinuation (#36730)
* Implement a YieldingContinuation type suitable for emitting values more than once via a yielding family of functions and awaiting production via next

* Add availability for YieldingContinuation and tests

* remove UnsafeConcurrentValue

* use UnsafeSendable for now

* Ensure the testing contexts are actually async

* Change the usages of Task.runDetached to Task.detach

* Change the usages of Task.detach to detach

* Transition to a external storage class outside of the generic, move to acqrel atomics, and change the error type to be enforced to Error existentials for next.

* Apply suggestions from code review

Co-authored-by: Nate Cook <natecook@apple.com>

* Remove inlines to allow for resilient changes

* Add unreachable cases in testing

Co-authored-by: Nate Cook <natecook@apple.com>
2021-04-08 12:12:04 -07:00
John McCall
0242d7572e Delay deallocation of default actors when they're currently running.
For ordinary memory-management reasons, this should only ever
happen when there will be no more uses of the actor outside of the
actor runtime.  The actor runtime, meanwhile, doesn't care about
anything except the default-actor control state of the actor.  So
we can just allow the rest of the actor to be destructed when it
isn't needed anymore, then destroy the actor state and deallocate
the object when we get around to switching off the executor.

This does assume that the task doesn't do anything which semantically
detects the executor it's on before switching off it, since doing so
might read a bogus executor.  However, we should only get an executor
in a zombie state like this when a hop has been removed or reordered,
and detection events should count as inhibiting that and forcing the
true executor to be switched to (and thus detected).

(But maybe lifetime optimization can make this happen?  Maybe we
need semantic detection to filter out zombie executors.)
2021-04-08 12:57:12 -04:00
John McCall
be8d2c83fa Give up default actors in swift_job_run if we switch to them.
Previously, if this happened, we simply left the actor in a running
state, causing any further jobs submitted to it to never be executed.
I can only speculate why this wasn't showing up in testing.

Also, change swift_job_run so that it prevents switching if the executor
passed in is not generic.  This is an entrypoint for arbitrary executors
and generally should not allow unexpected switching (if someday custom
executors participate in that scheme).  This infrastructure will also
be useful for implementing the `async let` semantics of running
synchronously until the task reaches a suspension point.

Finally, improve the #if'ed logging code throughout the task/actor runtime.
2021-04-08 12:57:12 -04:00
John McCall
156264f8e8 Make ExecutorRef two words. 2021-04-08 12:57:12 -04:00
nate-chandler
b293ce7953 Merge pull request #36729 from nate-chandler/rdar76118522
[ptrauth] Sign AsyncFunctionPointers as data.
2021-04-06 13:29:57 -07:00
Julian Lettner
e1b82d852d Remove dlsym() lookup for TSan functions from common path (#36478)
* Move TSan function lookup out of the common path

Move TSan function lookup via `dlsym()` out of the common path.  The
TSan runtime will now call `__tsan_on_initialize()` which we can use to
initialize the TSan functions in the Swift runtime.

This avoids paying the cost of `dlsym()` in the common, non-TSan case.

Depends on: https://reviews.llvm.org/D98810

rdar://75493372

* Remove Windows code

Thread Sanitizer is not supported on Windows.

Co-authored-by: Julian Lettner <julian.lettner@apple.com>
2021-04-06 09:16:47 -07:00
swift-ci
56b86b6cf1 Merge pull request #36766 from ktoso/wip-add-deprecated-shims-old-task-api 2021-04-06 00:50:33 -07:00
Konrad `ktoso` Malawski
e6b7e5e989 Merge pull request #36307 from ktoso/wip-yield
[Concurrency] Simple Task.yield implementation
2021-04-06 13:19:34 +09:00
Konrad `ktoso` Malawski
74eee6a79c [Concurrency] shim for deprecated Task.withCancellationHandler 2021-04-06 13:07:11 +09:00
Konrad `ktoso` Malawski
d8b4e3c304 [Concurrency] add deprecated shim for Task.withGroup 2021-04-06 12:51:55 +09:00
Konrad `ktoso` Malawski
39e97c57f2 [Concurrency] add deprecated shim for runDetached 2021-04-06 11:17:25 +09:00
Nate Chandler
d3e0e7af07 [ptrauth] Signed AsyncFunctionPointers as data.
Previously, AsyncFunctionPointer constants were signed as code.  That
was incorrect considering that these constants are in fact data.  Here,
that is fixed.

rdar://76118522
2021-04-05 18:24:41 -07:00
nate-chandler
b9f38446b9 Merge pull request #36707 from nate-chandler/rdar76061892
[Concurrency] Added swift_deletedAsyncMethodError.
2021-04-05 17:01:53 -07:00
Konrad `ktoso` Malawski
9949bf440d updated to latest Task APIs 2021-04-06 07:20:07 +09:00
Nate Chandler
e2d341fb74 [Concurrency] Added swift_deletedAsyncMethodError.
The new async function will be used as a placeholder  in VTables and
WTables for async functions which have been deleted.

rdar://76061892
2021-04-05 08:15:31 -07:00
Konrad `ktoso` Malawski
fde8098a37 [Concurrency] remove workaround for void task handles rdar://74957357 2021-04-05 16:59:00 +09:00
Konrad `ktoso` Malawski
a180f532b5 [Concurrency] Simple Task.yield implementation 2021-04-05 16:59:00 +09:00
Konrad `ktoso` Malawski
0857d0f62b remove last mention of spawnDetached 2021-04-03 10:08:11 +09:00
Konrad `ktoso` Malawski
a5ac6f06fa [Concurrency] detach, spawnUnlessCancelled, priority param cleanup 2021-04-03 09:54:42 +09:00
Konrad `ktoso` Malawski
34f8e768b5 spawnDetached to detach, non-suspending group.spawn, spawnUnlessCancelled 2021-04-03 09:53:32 +09:00