Commit Graph

1608 Commits

Author SHA1 Message Date
Chris Adamson
af4ebb4c46 Provide doc comments for AsyncSequence and related types (#37383)
* First draft docs for iterator, sequence type.

Also drop(while:), to get a feel for the AsyncSequence->AsyncSequence cases.

* Source docs for contains and reduce.

* Docs for methods in AsyncSequence.

This covers all the methods that return a single value. Still working on the extension methods that return new sequences.

* Early source docs for dropFirst(_:) and its type

* Correct doc format for dropFirst(_:).

* Prefix, plus some fixes.

* Docs for prefix(while:).

* Apply Philippe's snippet fixes.

* First draft docs on the map sequences.

Plus miscellaneous fixes throughout.

* Show result of flatMap example.

* More explicit cancellation guidance.

* Convert snippets to trailing closures.

* Correct misplaced doc comments.

* Apply suggestions from code review

Co-authored-by: Philippe Hausler <phausler@apple.com>

* Apply editorial feedback.

* Apply additional editorial feedback.

* Apply suggestions from code review

Co-authored-by: bjlanier <blanier@apple.com>

* Apply further editorial feedback.

Co-authored-by: Chris Adamson <cadamson@apple.com>
Co-authored-by: Philippe Hausler <phausler@apple.com>
Co-authored-by: bjlanier <blanier@apple.com>
2021-05-14 10:16:36 -04:00
Doug Gregor
2b9ca315fe [Concurrency] Remove asyncHandler attribute.
The `asyncHandler` attribute turned out to be the wrong solution
to the problem of creating a sync->async bridge. Remove it.
2021-05-13 17:01:39 -07:00
Mike Ash
e4a1d2bbed [Concurrency] Expose Job's metadata as a debug variable 2021-05-13 15:57:03 -04:00
3405691582
eb945c81f2 [concurrency] Provide missing header file. (#37004)
HAVE_PTHREAD_H is supplied in this config file.

The `__has_include` pattern is discouraged, but there is no other way of
conditioning on `pthread_np.h`, which is required for `pthread_main_np`
on OpenBSD. Therefore, use it, but call it out in a comment.
2021-05-13 09:13:36 -07:00
Philippe Hausler
4886bd56e4 [Concurrency] AsyncStream and AsyncThrowingStream
* Rework YieldingContinuation to service values in a buffered fashion

* Fix word size calculation for locks

* Handle terminal states and finished/failed storage

* Wrap yielding continuation into a more featureful type for better ergonomics

* Hope springs eternal, maybe windows works with this?

* Prevent value overflows at .max limits

* Add a cancellation handler

* Fix series tests missing continuation parameters

* Fix series tests for mutable itertaors

* Rename to a more general name for Series's inner continuation type

* Whitespace fixes and add more commentary about public functions on Series

* Restore YieldingContinuation for now with deprecations to favor Series

* Ensure onCancel is invoked in deinit phases, and eliminate a potential for double cancellation

* Make sure ThrowingSeries has the same nonmutating setter for onCancel as Series

* Add a swath of more unit tests that exersize cancellation behavior as well as throwing behaviors

* Remove work-around for async testing

* Fixup do/catch range to properly handle ThrowingSeries test

* Address naming consistency of resume result function

* Adopt the async main test setup

* More migration of tests to new async mechanisms

* Handle the double finish/throw case

* Ensure the dependency on Dispatch is built for the series tests (due to semaphore usage)

* Add import-libdispatch to run command for Series tests

* Use non-combine based timeout intervals (portable to linux) for dispatch semaphore

* Rename Series -> AsyncStream and resume functions to just yield, and correct a missing default Element.self value

* Fix missing naming change issue for yielding an error on AsyncThrowingStream

* Remove argument label of buffering from tests

* Extract buffer and throwing variants into their own file

* Slightly refactor for only needing to store the producer instead of producer and cancel

* Rename onCancel to onTermination

* Convert handler access into a function pair

* Add finished states to the termination handler event pipeline and a disambiguation enum to identify finish versus cancel

* Ensure all termination happens before event propigation (and outside of the locks) and warn against requirements for locking on terminate and enqueue

* Modified to use Deque to back the storage and move the storage to inner types; overall perf went from 200kE/sec to over 1ME/sec

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Doug Gregor <dgregor@apple.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Doug Gregor <dgregor@apple.com>

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Update stdlib/public/Concurrency/AsyncThrowingStream.swift

Co-authored-by: Joseph Heck <heckj@mac.com>

* Remove local cruft for overlay disabling

* Remove local cruft for Dispatch overlay work

* Remove potential ABI impact for adding Deque

Co-authored-by: Doug Gregor <dgregor@apple.com>
Co-authored-by: Joseph Heck <heckj@mac.com>
2021-05-11 21:41:33 -07:00
Fred Riss
bbda706393 [Concurrency] Add a unique Task ID to AsyncTask
This commit changes JobFlags storage to be 32bits, but leaves the runtime
API expressed in terms of size_t. This allows us to pack an Id in the
32bits we freed up.

The offset of this Id in the AsyncTask is an ABI constant. This way
introspection tools can extract the currently running task identifier
without any need for special APIs.
2021-05-11 08:28:17 -07:00
Konrad `ktoso` Malawski
6cbb792f92 [TaskLocals] Propagate task-locals through async{} 2021-05-11 11:06:17 +09:00
Konrad `ktoso` Malawski
82e91b7785 [TaskLocals] Enable sync functions to bind task-locals; Keep Storage in TLS 2021-05-11 11:06:16 +09:00
Dario Rexin
efe6973293 [Concurrency] Reduce overhead of Task.yield and Task.sleep (#37090)
* [Concurrency] Reduce overhead of Task.yield and Task.sleep

Instead of creating a new task, we create a simple job that wraps a Builtin.RawUnsafeContinuation and resumes the continuation when it is executed. The job instance is allocated on the task local allocator, meaning we don't malloc anything.

* Update stdlib/public/Concurrency/Task.swift

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>

Co-authored-by: Konrad `ktoso` Malawski <konrad.malawski@project13.pl>
2021-05-10 11:51:20 -07:00
Doug Gregor
38b652db94 Add "async" aliases to task group "spawn" operations 2021-05-07 00:23:09 -07:00
Doug Gregor
220e29d674 Reinstate "async let", with "spawn let" as an alias. 2021-05-07 00:13:56 -07:00
Max Desiatov
7be99e4cbf Merge pull request #37084 from kateinoigakukun/katei/share-fatalerror-concurrency
Rename duplicated `swift::fatalError` in `swiftRuntime` and `swift_Concurrency`.

Both `swiftRuntime` and `swift_Concurrency` had `swift::fatalError` implementation, but it causes symbol conflict with the `-static-stdlib` flag.

This patch removes one of the implementations in `swift_Concurrency` to avoid conflicts. Also added a test case to ensure that linking the Concurrency module with `-static-stdlib` works.

This issue was found by SwiftWasm test cases.
2021-05-06 09:46:15 +01:00
Alexis Laferrière
10d115bd5c [Concurrency] Use the SwiftStdlib 5.5 macro instead of 9999 versions 2021-05-04 09:30:58 -07:00
Alexis Laferrière
0233b94dcd [Concurrency] Define the availability macro SwiftStdlib 5.5
The use of an availability macro will make it easier to update those
versions to the marketing versions when they are announced. Plus
internal branches can redefine only the macro for customized
availability.
2021-05-04 09:30:54 -07:00
Konrad `ktoso` Malawski
1cd7180382 [Concurrency] don't require Sendable (yet) on groups and locals 2021-05-03 10:52:06 +09:00
John McCall
cc2863c136 Merge pull request #36878 from rjmccall/custom-executors
Introduce basic support for custom executors
2021-04-30 13:54:02 -04:00
Doug Gregor
5cc451cae8 Merge pull request #37159 from DougGregor/concurrency-priority-cleanup 2021-04-30 07:01:56 -07:00
Konrad `ktoso` Malawski
b92f058b3e Merge pull request #37167 from apple/locals-windows-fix
[TaskLocals] Windows: fix missing io.h
2021-04-30 22:54:56 +09:00
swift-ci
e152c14c0a Merge pull request #37162 from DougGregor/spawn-let 2021-04-30 02:19:16 -07:00
Konrad `ktoso` Malawski
5ff76c6a0d [TaskLocals] Windows: fix missing io.h
TaskLocals print a verbose error before crashing if API is abused within a task-group.
This printing needs `io.h` on windows.
2021-04-30 17:00:24 +09:00
John McCall
186c53000d Introduce basic support for custom executors.
- Introduce an UnownedSerialExecutor type into the concurrency library.
- Create a SerialExecutor protocol which allows an executor type to
  change how it executes jobs.
- Add an unownedExecutor requirement to the Actor protocol.
- Change the ABI for ExecutorRef so that it stores a SerialExecutor
  witness table pointer in the implementation field.  This effectively
  makes ExecutorRef an `unowned(unsafe) SerialExecutor`, except that
  default actors are represented without a witness table pointer (just
  a bit-pattern).
- Synthesize the unownedExecutor method for default actors (i.e. actors
  that don't provide an unownedExecutor property).
- Make synthesized unownedExecutor properties `final`, and give them
  a semantics attribute specifying that they're for default actors.
- Split `Builtin.buildSerialExecutorRef` into a few more precise
  builtins.  We're not using the main-actor one yet, though.

Pitch thread:
  https://forums.swift.org/t/support-custom-executors-in-swift-concurrency/44425
2021-04-30 03:11:56 -04:00
Doug Gregor
5d8174da57 [Concurrency] Introduce spawn let 2021-04-29 22:42:40 -07:00
Doug Gregor
cdc6dbe11d [Concurrency] Deprecate Task.Priority.unspecified and use optionals instead 2021-04-29 22:16:02 -07:00
Doug Gregor
92dd8ef4df [Concurrency] Make async operation return a task handle.
Per updates to the Structured Concurrency protocol, make the `async`
operation (1) overloaded on throwing-ness and (2) return an appropriate
`Task.Handle`.
2021-04-29 20:57:53 -07:00
swift-ci
bf6c673fd3 Merge pull request #36959 from ktoso/wip-task-locals-revisions 2021-04-29 20:50:01 -07:00
Konrad `ktoso` Malawski
2fc1ed8f50 [TaskLocals] rename 'do body' to 'operation' parameter 2021-04-30 09:38:31 +09:00
Fred Riss
28a23e5b41 Fix Windows build
```
S:\jenkins\workspace\swift-PR-windows\swift\stdlib\public\Concurrency\Actor.cpp(1612,22): error: expected unqualified-id
              : std::max(oldPriority, job->getPriority());
                     ^
C:\Program Files (x86)\Windows Kits\10\\Include\10.0.18362.0\shared\minwindef.h(193,29): note: expanded from macro max
#define max(a,b)            (((a) > (b)) ? (a) : (b))
                            ^
1 error generated.
```
2021-04-29 17:09:44 -07:00
Doug Gregor
67ed43730d [Concurrency] Have Task.currentPriority query the system as a fallback.
When `Task.currentPriority` is evaluated outside of an actual task,
query the system to determine the priority at which the code is
currently executing. This is the behavior that `async` specifies.
Extend that to `currentPriority` rather than guessing `.default`.
2021-04-29 11:01:43 -07:00
Fred Riss
8f1a73cf09 [Concurrency] Use a stable TSD on Darwin to store the current Task
This gives low-level tools (e.g. backtracers) an ABI way to access
the current task.
2021-04-29 09:55:43 -07:00
Konrad `ktoso` Malawski
38a43a481e [TaskLocals] Enforce @TaskLocal may only be used on static props 2021-04-29 21:44:48 +09:00
Konrad `ktoso` Malawski
070f9a773b [TaskLocals] review 2: projected value wrapper 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
df5ff42d79 [TaskLocals] set task local value in synchronous function 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
f0781b1f8b [TaskLocals] prettier API thanks to default inits 2021-04-29 15:48:23 +09:00
Konrad `ktoso` Malawski
6f3dac190a [TaskLocals] remove TaskLocalInheritance, we'll introduce when needed 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
664936cd03 [TaskLocal] crash on illegal withValue 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
58ea749304 [TaskLocal] Crash on inapropriate use within task group 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
3d96d05546 [TaskLocals] property wrapper keys 2021-04-29 15:48:09 +09:00
Mike Ash
b144a71d06 [Concurrency] Push an autorelease pool around job runs.
This ensures that job code that calls out to ObjC code will see a consistent autorelease environment.

rdar://75329282
2021-04-28 16:19:03 -04:00
Yuta Saito
709359cfab Rename duplicated swift::fatalError definition in swiftRuntime and swift_Concurrency
Both swiftRuntime and swift_Concurrency had swift::fatalError
implementation, but it causes symbol conflict when -static-stdlib.

This patch renames one of the impl in swift_Concurrency to avoid
conflict.
2021-04-28 23:52:18 +09:00
Doug Gregor
2000bf9b5f Add @_implicitSelfCapture to asyncDetached.
Like `async`, `asyncDetached` should not require `self.` because it is
unlikely to introduce cycles.
2021-04-27 17:03:03 -07:00
Doug Gregor
ee5864c309 Merge pull request #37072 from DougGregor/user-initiated-async
[Concurrency] Perform userInitiated adjustment after determining priority
2021-04-27 12:26:39 -07:00
eeckstein
056024a2e9 Merge pull request #36993 from eeckstein/task-allocation
concurrency: alloc an async-let task with the parent's allocator.
2021-04-27 10:23:34 +02:00
Doug Gregor
b882a12e59 Merge pull request #37070 from DougGregor/bridged-async-via-async 2021-04-26 20:59:07 -07:00
Doug Gregor
fba8ccadfc [Concurrency] Perform userInitiated adjustment after determining priority.
Based on feedback, perform the adjustment from userInteractive to
userInitiated all the time, and rely on `qos_class_self` whenever we
don't have a task.
2021-04-26 15:15:29 -07:00
Doug Gregor
ace5f49790 [Concurrency] Switch _runTaskForBridgedAsyncMethod over to using 'async' 2021-04-26 15:02:49 -07:00
Doug Gregor
ff4b0f335e [Concurrency] Speculatively add asyncDetached as a synonym for async.
Given that `async` is the dominant way to initiate asynchronous work from
a synchronous function, speculatively rename `detach` to `asyncDetached`
to both fit into the naming scheme and clearly bias toward `async.
2021-04-26 12:49:07 -07:00
Max Desiatov
d9ef216779 Concurrency: Avoid importing Dispatch with no global executor (#37059)
Dispatch is not available on platforms where `SWIFT_CONCURRENCY_COOPERATIVE_GLOBAL_EXECUTOR` is defined `0`, e.g. WebAssembly/WASI..
2021-04-26 12:10:03 +01:00
Erik Eckstein
075ad87a40 Concurrency: allocate an async-let task with its parent's stack allocator.
Also, do this for the initial slab for the task's allocator itself.
This avoids memory allocations for async-lets.
In case the async-task's memory demand does not exceed the initial slab size, it is now completely malloc-free.

The refcount bits of an async-let task are initialized to "immortal" so that ARC operations don't have an effect on the task.
2021-04-26 13:07:32 +02:00
Konrad `ktoso` Malawski
fd8ec39f69 Merge branch 'main' into wip-main-remove-task-current 2021-04-26 17:39:50 +09:00
Konrad `ktoso` Malawski
e0eb940027 [Concurrency] Remove Task.current because it prevents task-local alloc #36993 2021-04-26 17:37:45 +09:00