Commit Graph

1608 Commits

Author SHA1 Message Date
Evan
4f708fad7e Merge pull request #35215 from etcwilde/ewilde/async-main
Adding async-main support

Resolves: rdar://71828636
2021-01-15 10:57:14 -08:00
Kavon Farvardin
91e2246c19 quick-and-dirty implementation of MainActor in the runtime system
it is "dirty" in the sense that we don't have proper support for
custom executors right now.
2021-01-14 17:43:58 -08:00
Evan Wilde
13cc56b85c Build on Windows
Windows doesn't have dlsym. Since I don't have a Windows box, I'm just
if-def'ing it out for the time being. Since we don't want Windows going
off and doing dangerous things, I threw in an abort just to be safe.
2021-01-14 15:47:08 -08:00
Evan Wilde
81ae9d663f More tests and cleanup
Adding execution and death test to ensure that we crash appropriately
when the main function throws an uncaught exception, and that the async
main runs correctly.

Also switching to doing the CFRunLoopRun lookup with `RTLD_DEFAULT`
since `RTLD_SELF` isn't available on Linux.

Switching to `try await` since `await try` is no longer the right way to
do that.

Using exit(0) instead of EXIT_SUCCESS since the C++ importer doesn't
mark imported macros with @actorIndependent yet.
2021-01-14 13:28:58 -08:00
Evan Wilde
6b16657922 Explode on uncaught error thrown in main
This patch has two desirable effects for the price of one.
 1. An uncaught error thrown from main will now explode
 2. Move us off of using runAsyncAndBlock

The issue with runAsyncAndBlock is that it blocks the main thread
outright. UI and the main actor need to run on the main thread or bad
things happen, so blocking the main thread results in a bad day for
them.

Instead, we're using CFRunLoopRun to run the core-foundation run loop on
the main thread, or, dispatch_main if CFRunLoopRun isn't available.
The issue with just using dispatch_main is that it doesn't actually
guarantee that it will run the tasks on the main thread either, just
that it clears the main queue. We don't want to require everything that
uses concurrency to have to include CoreFoundation either, but dispatch
is already required, which supplies dispatch_main, which just empties
out the main queue.
2021-01-13 15:49:28 -08:00
Evan Wilde
c01b9f000e Adding async-main support
This patch adds the async-main start-point for programs.
When a `static func main() async` is inserted into the main program, it
gets called through `_runAsyncMain` instead of calling directly. This
starts the program in an async context, which is good because then we
can do async stuff from there.

The following code

```
@main struct MyProgram {
  static func main() async {
    // async code
  }
}
```

is turned into

```
@main struct MyProgram {
  static func $main() {
    _runAsyncMain(main)
  }

  static func main() async {
    // async code
  }
}
```

_runAsyncMain code-gen's to the same thing as runAsyncAndBlock, which
emits a call to `swift_task_runAndBlockThread`.
2021-01-13 13:17:35 -08:00
Joe Groff
49d4299ae0 Concurrency: Have CheckedContinuation trap on double resume. 2021-01-12 16:08:41 -08:00
Doug Gregor
4c5bd34b5d Merge pull request #35358 from DougGregor/concurrency-without-darwin
[Concurrency] Drop dependency on the Darwin overlay
2021-01-11 20:05:46 -08:00
Doug Gregor
2d5cdec2cd [Concurrency] Drop dependency on the Darwin overlay 2021-01-11 15:06:26 -08:00
Joe Groff
5f0974515d Make CheckedContinuation thread-safe and resilient. 2021-01-08 14:54:27 -08:00
Joe Groff
c151f4b02b Concurrency: Introduce a CheckedContinuation adapter.
To help catch runtime issues adopting `withUnsafeContinuation`, such as callback-based APIs that misleadingly
invoke their callback multiple times and/or not at all, provide a couple of classes that can take ownership of
a fresh `UnsafeContinuation` or `UnsafeThrowingContinuation`, and log attempts to resume the continuation multiple times
or discard the object without ever resuming the continuation.
2021-01-08 13:50:42 -08:00
Kavon Farvardin
a2c57d13c3 [concurrency] initial declaration of MainActor
Implementation is left as a TODO for now.

Resolves rdar://72161578
Partially resolves rdar://72105129
2021-01-06 15:01:22 -08:00
Kavon Farvardin
fabd45e7c3 [concurrency] add resume<E: Error>(with: Result<T, E>) to UnsafeThrowingContinuation
A handy overload that allows one to return a Result<> to
a continuation without needing to manually unwrap it in client code.

Resolves rdar://71870249
2021-01-06 15:01:14 -08:00
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