Commit Graph

35 Commits

Author SHA1 Message Date
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
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
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
Joe Groff
2ee6b70402 Integrate ObjC interop intrinsics with task runtime. 2020-12-11 09:20:02 -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
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
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
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
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
Konrad `ktoso` Malawski
7126f32f22 fix properly, by using Task.JobFlags which does conversions 2020-11-20 00:46:40 -08:00
Konrad `ktoso` Malawski
261f0d2dcd +task Implement Task.currentPriority 2020-11-20 09:00:41 +09: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
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
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
John McCall
a06d18ce81 Add API for creating unscheduled tasks.
Make all tasks into heap objects.
2020-10-22 00:53:16 -04:00