Commit Graph

1608 Commits

Author SHA1 Message Date
Konrad `ktoso` Malawski
34f8e768b5 spawnDetached to detach, non-suspending group.spawn, spawnUnlessCancelled 2021-04-03 09:53:32 +09:00
Konrad `ktoso` Malawski
6e746304e1 [Concurrency] Update Task and Group APIs based on Review 1 2021-04-03 09:52:03 +09:00
Mike Ash
1173b737aa [Concurrency] Add availability to Concurrency APIs.
This allows programs to target older OSes while using Concurrency behind an availability check. When targeting older OSes, the symbols are weak-linked and the compiler will require the use of Concurrency features to be guarded by an availability check.

rdar://75850003
2021-04-01 10:42:08 -04:00
Doug Gregor
e69df4ef4f Merge pull request #36668 from DougGregor/feature-check-inlined-text 2021-03-31 20:51:25 -07:00
Dario Rexin
3daf37f221 [Concurrency] Use correct type for Task.Handle in Task.sleep (#36654) 2021-03-31 12:45:44 -07:00
Mike Ash
e98fc3e304 Merge pull request #36572 from mikeash/async-task-dispatch-integration2
[Concurrency] Make Job objects work as Dispatch objects.
2021-03-31 09:42:58 -04:00
Doug Gregor
492bca113d [Module interface] Retain #if's with feature checks in inline bodies. 2021-03-30 17:56:42 -07:00
Doug Gregor
46c9de8ba8 [Concurrency] Work around issue with old compilers and newer _Concurrency lib
Fixes rdar://75996690.
2021-03-30 16:27:08 -07:00
John McCall
98711fd628 Revise the continuation ABI.
The immediate desire is to minimize the set of ABI dependencies
on the layout of an ExecutorRef.  In addition to that, however,
I wanted to generally reduce the code size impact of an unsafe
continuation since it now requires accessing thread-local state,
and I wanted resumption to not have to create unnecessary type
metadata for the value type just to do the initialization.

Therefore, I've introduced a swift_continuation_init function
which handles the default initialization of a continuation
and returns a reference to the current task.  I've also moved
the initialization of the normal continuation result into the
caller (out of the runtime), and I've moved the resumption-side
cmpxchg into the runtime (and prior to the task being enqueued).
2021-03-28 12:58:16 -04:00
Mike Ash
0989524338 [Concurrency] Make Job objects work as Dispatch objects.
Fill out the metadata for Job to have a Dispatch-compatible vtable. When available, use the dispatch_enqueue_onto_queue_4Swift to enqueue Jobs directly onto queues. Otherwise, keep using dispatch_async_f as we have been.

rdar://75227953
2021-03-26 18:31:00 -04:00
Doug Gregor
4ca8a66fc5 Merge pull request #36590 from jckarter/task-cancellation-handler-abi
Fix calling convention for withCompletionHandler runtime calls.
2021-03-25 21:49:29 -07:00
Joe Groff
698a975a23 Merge pull request #36586 from jckarter/owned-task-spawn-closures
Allow closures to be `__owned` or `__shared`, and make task spawning closures `__owned`
2021-03-25 16:02:07 -07:00
Joe Groff
4150b31954 Fix calling convention for withCompletionHandler runtime calls.
rdar://75370240
2021-03-25 14:45:47 -07:00
Joe Groff
c6691f3919 Allow closures to be __owned or __shared, and make task spawning closures __owned 2021-03-25 11:36:33 -07:00
Nate Cook
86b69b98cc Fix license headers in concurrency source files 2021-03-25 10:12:02 -05:00
Mike Ash
c4e4e44f71 Merge pull request #36390 from mikeash/concurrency-compatibility-overrides
[Concurrency] Add compatibility overrides to Concurrency library.
2021-03-24 13:07:07 -04:00
Evan Wilde
42b6918657 NFC: Expunge 'actor class' in comments 2021-03-23 15:06:40 -07:00
Evan Wilde
8f974c1a79 Merge pull request #36524 from etcwilde/ewilde/fix-after-main-crash
[Concurrency] Make asyncMainDrainQueue noreturn
2021-03-22 23:00:33 -07:00
Joe Groff
1a9dc145a6 Concurrency runtime: Fix location of tsan_release edges for actors.
The TSAN release edge should appear before the actual cmpxchg-release.
2021-03-22 14:35:48 -07:00
Mike Ash
6aab257c33 [Concurrency] Add compatibility overrides to Concurrency library.
Take the existing CompatibilityOverride mechanism and generalize it so it can be used in both the runtime and Concurrency libraries. The mechanism is preprocessor-heavy, so this requires some tricks. Use the SWIFT_TARGET_LIBRARY_NAME define to distinguish the libraries, and use a different .def file and mach-o section name accordingly.

We want the global/main executor functions to be a little more flexible. Instead of using the override mechanism, we expose function pointers that can be set by the compatibility library, or by any other code that wants to use a custom implementation.

rdar://73726764
2021-03-22 11:09:06 -04:00
Konrad `ktoso` Malawski
92933a17f2 Merge pull request #36297 from apple/ktoso-patch-3
[Concurrency] remove Task.hasTaskLocalValues, they all do
2021-03-21 22:58:24 +09:00
Arnold Schwaighofer
98a674bf12 Merge pull request #36518 from aschwaighofer/fix_pushAsyncContext_assert
Fix pushAsyncContext
2021-03-19 15:17:01 -07:00
Evan Wilde
b0b4cb67a1 Make asyncMainDrainQueue noreturn
CFRunLoopRun returns once it finishes, though the kernel may clean us up
before we get there. We effectively have a race condition between the
kernel cleaning us up and returning from a never returning function.

Small programs likely get cleaned up before reaching the ud2 instruction
emitted after the never returning function in swift, so they don't
notice, but programs of a sufficient size do. At that size, the program
will crash after what the programmer expects to be the end of their
program.
2021-03-19 12:33:26 -07:00
swift-ci
bdc048e541 Merge pull request #36511 from DougGregor/main-actor-run 2021-03-19 10:19:13 -07:00
Doug Gregor
cfa07b60ee Merge pull request #36477 from jckarter/executor-hop-back
Concurrency: Hop back to the previous executor after actor calls.
2021-03-19 10:17:20 -07:00
Arnold Schwaighofer
fc91036bf7 Fix pushAsyncContext
We no longer store arguments in the async context (like the closure context). The assert is no longer true
2021-03-19 09:25:46 -07:00
Doug Gregor
3aa4fbec51 Add MainActor.run to execute a synchronous closure on the main thread.
It's important to be able to execute a synchronous closure on the main
thread to perform various main-thread updates. This is the async equivalent
to DispatchQueue.main.sync.
2021-03-18 23:52:58 -07:00
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -07:00
Doug Gregor
9579390024 [SE-0304] Rename ConcurrentValue to Sendable 2021-03-18 22:48:20 -07:00
Joe Groff
b9ee090605 Add tsan edges when actors transition in and out of running state. 2021-03-18 18:16:33 -07:00
Joe Groff
79fb05b362 Concurrency: Hop back to the previous executor after actor calls.
Tasks shouldn't normally hog the actor context indefinitely after making a call that's bound to
that actor, since that prevents the actor from potentially taking on other jobs it needs to
be able to address. Set up SILGen so that it saves the current executor (using a new runtime
entry point) and hops back to it after every actor call, not only ones where the caller context
is also actor-bound.

The added executor hopping here also exposed a bug in the runtime implementation while processing
DefaultActor jobs, where if an actor job returned to the processing loop having already yielded
the thread back to a generic executor, we would still attempt to make the actor give up the thread
again, corrupting its state.

rdar://71905765
2021-03-18 11:47:50 -07:00
Arnold Schwaighofer
cf55973973 Remove unused ResumeParentExecutor 2021-03-18 09:05:45 -07:00
Arnold Schwaighofer
3bdd5cb99a IRGen: async error ABI
Throwing functions pass the error result in `swiftself` to the resume
partial function.
Therefore, `() async -> ()` to `() async throws -> ()` is not ABI compatible.

TODO: go through remaining failing IRGen async tests and replace the
illegal convert_functions.
2021-03-17 17:17:12 -07:00
Arnold Schwaighofer
f75fbb7594 IRGen: Async ABI passing parameter and results directly
The error is still passed in the async context. I will fix this in a
follow-up.
2021-03-17 07:41:01 -07:00
John McCall
6c879d6fd3 Change the async ABI to not pass the active task and executor.
Most of the async runtime functions have been changed to not
expect the task and executor to be passed in.  When knowing the
task and executor is necessary, there are runtime functions
available to recover them.

The biggest change I had to make to a runtime function signature
was to swift_task_switch, which has been altered to expect to be
passed the context and resumption function instead of requiring
the caller to park the task.  This has the pleasant consequence
of allowing the implementation to very quickly turn around when
it recognizes that the current executor is satisfactory.  It does
mean that on arm64e we have to sign the continuation function
pointer as an argument and then potentially resign it when
assigning into the task's resume slot.

rdar://70546948
2021-03-16 22:52:54 -04:00
swift-ci
549923513a Merge pull request #36216 from apple/tsan-swift-concurrency2 2021-03-16 19:11:20 -07:00
Julian Lettner
8abf12fbe6 Add swift_task_set_tsan_hooks()
This change will be sequenced:
  1. Call this hook from the TSan runtime
  2. Remove `dlsym()` lookup from Swift runtime
2021-03-16 13:43:55 -07:00
Nate Chandler
ee63777332 [IRGen] Fix ABI for thick async functions.
Previously, thick async functions were represented sometimes as a pair
of (AsyncFunctionPointer, nullptr)--when the thick function was produced
via a thin_to_thick_function, e.g.--and sometimes as a pair of
(FunctionPointer, ThickContext)--when the thick function was produced by
a partial_apply--with the size stored in the slot of the ThickContext.

That optimized for the wrong case: partial applies of dynamic async
functions; in that case, there is no appropriate AsyncFunctionPointer to
form when lowering the partial_apply instruction.  The far more common
case is to know exactly which function is being partially applied.  In
that case, we can form the appropriate AsyncFunctionPointer.

Furthermore, the previous representation made calling a thick function
more complex: it was always necessary to check whether the context was
in fact null and then proceed along two different paths depending.

Here, that behavior is corrected by creating a thunk in a mandatory
IRGen SIL pass in the case that the function that is being partially
applied is dynamic.  That new thunk is then partially applied in place
of the original partial_apply of the dynamic function.
2021-03-15 13:37:40 -07:00
Julian Lettner
99bcd87af6 Use swift_once instead of C++ static initializers 2021-03-12 14:52:56 -08:00
Mike Ash
bedca5ed61 Merge pull request #36139 from mikeash/async-task-dispatch-integration
[Concurrency] Make Job/AsyncTask minimally compatible with dispatch object layout
2021-03-11 18:04:04 -05:00
Arnold Schwaighofer
a17de090b6 SILGen: Fix emitBuiltinCreateAsyncTaskFuture
We need to re-abstract the function argument.
I think this should fix rdar://74957357.
2021-03-11 06:44:02 -08:00
Mike Ash
bd62fdb2db [Concurrency] Make Job/AsyncTask minimally compatible with dispatch object layout
Create a TargetDispatchClassMetadata for Swift metadata that also has a dispatch-compatible vtable. Dispatch leaves room for ObjC class metadata so the two regions don't overlap. (The vtable currently consists of a single dummy entry; this will be filled out later.)

Rearrange the Job and AsyncTask hierarchy so that AsyncTask inherits only from Job, which in turn inherits from HeapObject. This gives all Job instances a dispatch-compatible isa field. It also gives them a refcount word, which is wasted on instances that aren't AsyncTask instances. Maybe we can find some use for that space in the future.

rdar://75227953
2021-03-10 10:04:30 -05:00
Kavon Farvardin
01b1043ce5 Fix a memory issue with actors in the runtime system, by @phausler 2021-03-09 14:47:36 -08:00
Joe Groff
872afda50b Merge pull request #36298 from jckarter/created-task-closure-context-leak
SIL: Clean up ownership handling in `createAsyncTask` builtins.
2021-03-09 14:17:13 -08:00
Joe Groff
d9798c0868 Concurrency: Redo non-_f variants of swift_task_create to accept closures as is.
In their previous form, the non-`_f` variants of these entry points were unused, and IRGen
lowered the `createAsyncTask` builtins to use the `_f` variants with a large amount of caller-side
codegen to manually unpack closure values. Amid all this, it also failed to make anyone responsible
for releasing the closure context after the task completed, causing every task creation to leak.
Redo the `swift_task_create_*` entry points to accept the two words of an async closure value
directly, and unpack the closure to get its invocation entry point and initial context size
inside the runtime. (Also get rid of the non-future `swift_task_create` variant, since it's unused
and it's subtly different in a lot of hairy ways from the future forms. Better to add it later
when it's needed than to have a broken unexercised version now.)
2021-03-08 16:54:19 -08:00
Konrad `ktoso` Malawski
2467f6aaea [Concurrency] Task.withGroup can be rethrows 2021-03-08 13:39:12 +09:00
Doug Gregor
84cdeb7372 Merge pull request #36322 from DougGregor/resurrect-unsafe-throwing-continuation 2021-03-05 17:18:30 -08:00
Mike Ash
0f7f060abe Merge pull request #36290 from mikeash/cache-global-dispatch-queues
[Concurrency] Cache the queue returned from dispatch_get_global_queue.
2021-03-05 18:43:19 -05:00
Doug Gregor
32702203c5 Add UnsafeThrowingContinuation back as a deprecated typealias.
This helps migrate clients that reference this type.
Fixes rdar://75104903.
2021-03-05 12:21:54 -08:00
Joe Groff
016496384e Concurrency: Remove currently-unused non-_f variants of swift_task_create
I'm about to replace these with new variations that correctly handle spawning a task with
a closure as its initial entry point, without leaking or requiring large amounts of caller-side
code generation.
2021-03-05 12:01:06 -08:00