Commit Graph

83 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
Dario Rexin
3daf37f221 [Concurrency] Use correct type for Task.Handle in Task.sleep (#36654) 2021-03-31 12:45:44 -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
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
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
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
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -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
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
Konrad `ktoso` Malawski
0ec038c449 [Concurrency] remove Task.hasTaskLocalValues, they all do 2021-03-05 10:11:29 +09:00
Dario Rexin
54e78cc015 Fix after rebase 2021-03-04 09:37:59 -08:00
Dario Rexin
4bb52ccfbd Workaround for rdar://74957357 2021-03-04 09:37:59 -08:00
Dario Rexin
af4b6bc068 [Concurrency] Add asynchronous Task.sleep function 2021-03-04 09:37:58 -08:00
Evan Wilde
2ee7a322a5 Merge pull request #36239 from etcwilde/ewilde/run-async-main-on-main-thread
[Concurrency] Run async main code on main thread
2021-03-03 09:47:18 -08:00
Evan Wilde
a643fe418e Run async main code on main thread
This patch moves the actual running of the main code onto the main
thread by making it part of the main actor. This should be more
consistent with what folks are expecting.
2021-03-02 22:36:12 -08:00
Konrad `ktoso` Malawski
550018574a [Concurrency] remove unimplemented Task APIs: yield, startingOn 2021-03-03 14:31:01 +09:00
Konrad `ktoso` Malawski
d7169edc21 [TaskLocals] Cleanly separate locals impl from Task, no need for fragment 2021-03-02 00:54:47 +09:00
Konrad `ktoso` Malawski
599572ba54 cleanups of tests after merge 2021-02-25 14:24:43 +09:00
Konrad `ktoso` Malawski
6e525d7469 Merge branch 'main' into wip-no-escape-group 2021-02-25 10:37:20 +09:00
Konrad `ktoso` Malawski
dd9a9a6436 [Concurrency] Task APIs use thread-local ActiveTask now 2021-02-24 19:19:44 +09:00
Konrad `ktoso` Malawski
6f4fca8721 Merge branch 'main' into wip-no-escape-group 2021-02-24 08:59:53 +09:00
John McCall
2012195cd5 Alter the runtime interface for awaiting futures and task groups.
First, just call an async -> T function instead of forcing the caller
to piece together which case we're in and perform its own copy.  This
ensures that the task is actually kept alive properly.

Second, now that we no longer implicitly depend on the waiting tasks
being run synchronously, go ahead and schedule them to run on the
global executor.

This solves some problems which were blocking the work on TLS-ifying
the task/executor state.
2021-02-21 23:48:13 -05:00
Konrad `ktoso` Malawski
a226259d84 [Concurrency] TaskGroup moves out of AsyncTask, non escaping body 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
40b6b18945 [Concurrency] implement withCancellationHandler via records 2021-02-18 17:27:15 +09:00
swift-ci
c19375790a Merge pull request #35977 from DougGregor/discardable-run-detached 2021-02-15 14:51:51 -08:00
Doug Gregor
6a5dfb8aa4 Mark Task.runDetached as @discardableResult.
Often, this API is used for its side effects and nobody cares about the
handle.
2021-02-15 12:21:49 -08:00
Konrad `ktoso` Malawski
d2bd6abe61 [Concurrency] TaskLocals allow configuring inheritance: never 2021-02-13 20:09:11 +09:00
Konrad `ktoso` Malawski
b811b12246 [Concurrency] TaskLocals lookup "skip" optimization 2021-02-13 10:39:22 +09:00
Konrad `ktoso` Malawski
1044723787 [Concurrency] Initial Task Local Values implementation 2021-02-13 10:39:22 +09:00
Evan
e813bbea76 Merge pull request #35911 from etcwilde/ewilde/deprecate-runAsyncAndBlock
Deprecate runAsyncAndBlock API
2021-02-12 09:51:04 -08:00
Konrad `ktoso` Malawski
a55ebff942 [Concurrency] deprecate not implemented Task functions, until they're implemented 2021-02-12 16:33:16 +09:00
Konrad `ktoso` Malawski
e7cfc8c018 [Concurency] Improve priority testing, including multi-task escalation 2021-02-12 09:06:17 +09:00
Konrad `ktoso` Malawski
08de933288 [Concurrency] Task revisions 3
- introduce UnsafeCurrentTask
- implement Hashable, Equatable on tasks
- assume we'll have a way to get a task from sync context
- Task.Handle now has a Failure type as well
- Task.Handle.getResult
2021-02-12 09:06:17 +09:00
Evan Wilde
e83b0a2a84 Deprecate runAsyncAndBlock API
We don't want people using this as a means of jumping from synchronous
code to async code. It blocks the thread that it's running on and spawns
a new thread to run the async code.

This has a couple of drawbacks. First, if it blocks a thread that is
needed by the async code (i.e, calling something on the main actor while
blocking the main thread), it is an immediate deadlock. This is a bad
day for everyone. Second, it can easily lead to thread-explosions.

The proper entry-point for async code is to use something of the
following form:

```
@main
struct Main {
  static func main() async {
    // ...
  }
}
```

This should take care of most new code. There are still places where
folks will need to jump from synchronous code to async code. That still
needs to be designed and implemented, but we don't want people to come
to rely on `runAsyncAndBlock`.
2021-02-10 21:33:03 -08:00
Doug Gregor
1f1f703afe Drop @concurrent from parameter to @asyncHandler entry point.
The async handler code doesn't really run concurrently with where it is
launched, despite the implementation currently using `runDetached`
internally. Drop the @concurrent and bitcase it back on when needed.
2021-01-29 09:53:22 -08:00
Doug Gregor
9efb44fde8 Adopt @concurrent in Task APIs 2021-01-28 10:03:09 -08:00
Konrad `ktoso` Malawski
80ee936a72 Revert "[Concurrency] isCanceled spelling to follow guidance" 2021-01-23 07:27:34 +09:00
Konrad `ktoso` Malawski
8b37455774 [Concurrency] isCanceled spelling to follow guidance 2021-01-22 12:09:19 +09: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
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