Commit Graph

986 Commits

Author SHA1 Message Date
Saleem Abdulrasool
a066a6ecfb Concurrency: support newer dispatch functionality on Windows
This addresses an unintended instance where new dispatch functionality
is not used on Windows as the lookup was never performed.  This limits
the runtime to shared linking which should generally be a safe
assumption on Windows.
2022-11-04 06:56:46 -07:00
John McCall
7f737d235d Synchronize with cancellation when removing a task from a task group
We were detaching the child by just modifying the list, but the cancellation path was assuming that that would not be done without holding the task status lock.

This patch just fixes the current runtime; the back-deployment side is complicated.

Fixes rdar://88398824
2022-10-29 00:10:28 -04:00
Nate Chandler
c574550073 [Freestanding] Removed bridging intrinsic. 2022-10-18 11:27:21 -07:00
Nate Chandler
c9d86eed75 [Freestanding] Disable child task priority spec.
Under the task-to-thread model, specifying a priority doesn't make
sense.

Here, variations of addTask and addTaskUnlessCancelled are introduced
which do not take a priority.  Additionally, the original functions are
made unavailable.
2022-10-18 11:25:10 -07:00
Nate Chandler
fa82280337 [Freestanding] Disable AsyncStream. 2022-10-18 11:25:10 -07:00
Nate Chandler
cbaa1b14c8 [Freestanding] Disable Task.sleep. 2022-10-18 11:25:10 -07:00
Mike Ash
96e965a359 [Concurrency] Use dispatch cooperative queues when available.
These queues are better suited to the concurrency runtime.
2022-09-29 15:46:35 -04:00
swift-ci
77903dece6 Merge pull request #61065 from apple/rokhinip/92348139-actors-as-locks
[Freestanding] In the task-to-thread model, actors should behave like actual locks
2022-09-21 15:32:15 -07:00
swift-ci
b28efc557d Merge pull request #61117 from apple/revert-60790-rdar99047747
Revert "[Freestanding] Disable checked continuations."
2022-09-21 11:06:49 -07:00
swift-ci
d93c022125 Merge pull request #61076 from mikeash/unsafe-continuation-validation
[Concurrency] Add an environment variable to validate unchecked continuation usage.
2022-09-16 18:27:22 -07:00
Mike Ash
afc5116ef0 [Concurrency] Add an environment variable to validate unchecked continuation usage.
When enabled, we track all active unchecked continuations in a global set, and fatal error if one is called twice.

rdar://97390481
2022-09-16 13:50:59 -04:00
Mike Ash
2a5d6a950b Merge pull request #61054 from mikeash/tsan-fix-task-cancellation
[Concurrency] Fix memory ordering around task cancellation.
2022-09-15 10:38:03 -04:00
Rokhini Prabhu
ab16a647d8 [Freestanding] In a task-to-thread model where we don't have the means
to create extra threads to process jobs, actors should really behave
like locks.
2022-09-14 18:33:54 -07:00
nate-chandler
9a7c0b9869 Revert "[Freestanding] Disable checked continuations." 2022-09-14 17:29:49 -07:00
Mike Ash
9c849e431c [Concurrency] Fix memory ordering around task cancellation.
Load task status with an acquire when canceling a task, to synchronize with the store-release that comes when updating a task's status.

Add explicit TSan calls in cancellation, as well as withStatusRecordLock and addStatusRecord, to avoid TSan complaining about data races when canceling a task.

Add a test that checks for TSan-reported data races when canceling a task.

rdar://93892417
2022-09-14 13:39:55 -04:00
Egor Zhdan
84a1ffcb33 [Shims] Include SwiftShims headers without ../
This replaces a number of `#include`-s like this:
```
#include "../../../stdlib/public/SwiftShims/Visibility.h"
```
with this:
```
#include "swift/shims/Visibility.h"
```

This is needed to allow SwiftCompilerSources to use C++ headers which include SwiftShims headers. Currently trying to do that results in errors:
```
swift/swift/include/swift/Demangling/../../../stdlib/public/SwiftShims/module.modulemap:1:8: error: redefinition of module 'SwiftShims'
module SwiftShims {
       ^
Builds.noindex/swift/swift/bootstrapping0/lib/swift/shims/module.modulemap:1:8: note: previously defined here
module SwiftShims {
       ^
```
This happens because the headers in both the source dir and the build dir refer to SwiftShims headers by relative path, and both the source root and the build root contain SwiftShims headers (which are equivalent, but since they are located in different dirs, Clang treats them as different modules).
2022-09-14 11:14:50 +01:00
Rokhini Prabhu
4c0f88bdee Move isDistributedRemote out of ActiveActorStatus since it does not need
to be modified in the atomic - it is set once and never changed again
2022-09-13 13:15:26 -07:00
Rokhini Prabhu
76e7cb1cd0 Merge pull request #60860 from apple/rokhinip/92347604-run-task-upon-await
[Freestanding] Only run tasks when they are awaited up on in task-to-thread model
2022-09-01 15:18:06 -07:00
Rokhini Prabhu
a606892155 Task group children should be executed on parent task at time of await 2022-09-01 10:02:25 -07:00
Rokhini Prabhu
cb6ffe831b In task-to-thread model, make sure that when we are waiting on an
async-let task, we run it in the context of the parent task.
2022-09-01 10:02:25 -07:00
Rokhini Prabhu
f2099c50cd Make sure that current thread's priority propagates to a runInline task 2022-09-01 10:02:25 -07:00
nate-chandler
1f38c4bb3b Merge pull request #60790 from nate-chandler/rdar99047747
[Freestanding] Disable checked continuations.
2022-08-31 07:39:21 -07:00
Nate Chandler
c20bd86007 [Freestanding] Disable checked continuations.
In the task-to-thread model, there are no threading mechanisms by which
work could be offloaded onto another thread.  As such, callback based
asynchronous APIs which are not Swift async do not make sense.

rdar://99047747
2022-08-25 16:55:45 -07:00
Alex Martini
8556e8337a Merge pull request #60178 from amartini51/doc_fixes
Documentation fixes
2022-08-24 13:53:24 -07:00
Doug Gregor
c42025a571 Merge pull request #60661 from DougGregor/concurrency-api-sendable-fixes
[Concurrency] Fix a few annotations for the concurrency library
2022-08-19 16:47:32 -07:00
Doug Gregor
ee4f6b1f75 Make CheckedContinuation and UnsafeContinuation unconditionally Sendable.
The continuation types were conditionally `Sendable` based on whether
the result type of the continuation was `Sendable`. However,
conceptually, the return value is never leaving the current task, so
it is never actually crossing concurrency boundaries. Therefore, we
can make the continuation types unconditionally `Sendable`.

Fixes rdar://98462858.
2022-08-19 13:06:41 -07:00
Doug Gregor
cf9a7478c3 Apply @_unsafeInheritExecutor to with(Throwing)TaskGroup.
Fixes rdar://98461630.
2022-08-19 13:06:41 -07:00
Konrad `ktoso` Malawski
ecccce6113 [Concurrency] Deprecate one of the cancellation handler overloads (#60569) 2022-08-18 11:14:30 +09:00
Ben Rimmington
ce0794cc31 [SE-0329] Update the Task.sleep(for:) method (#60400) 2022-08-12 11:29:07 +01:00
Alex Martini
f8b716947d Add missing 'try' for throwing async code.
Fixes <rdar://97022479>
2022-07-21 11:25:39 -07:00
Nate Chandler
34c08b8344 [TaskToThread] Add Task.runInline.
The new intrinsic, exposed via static functions on Task<T, Never> and
Task<T, Error> (rethrowing), begins an asynchronous context within a
synchronous caller's context.  This is only available for use under the
task-to-thread concurrency model, and even then only under SPI.
2022-07-08 08:44:18 -07:00
Nate Chandler
59f6cbc385 [NFC] Extracted function for header/context size.
So that this functionality is available elsewhere, extract it from
swift_task_create_commonImpl into a reusable static functions.
2022-07-06 11:51:16 -07:00
Nate Chandler
da45a67192 [NFC] Removed unused argument.
Previously getAsyncClosureEntryPointAndContextSize took both a pointer
for a function and a closure context.  That was a relic of a temporary
ABI where async function pointers were not used in all cases.  Now that
that ABI is long gone, async function pointers are always used.
Consequently, the closure context argument was unused.  Here, the
closure context argument is removed.
2022-07-06 11:51:16 -07:00
Nate Chandler
68eea8adec [TaskToThread] No unstructured tasks.
In this mode, the following are disabled:

- task creation
- global actors
- MainActor
- custom executors
2022-07-06 11:51:16 -07:00
Stephen Celis
8be672a8ae Add Task.sleep(for: Duration) (#59203)
* Add Task.sleep(for: Duration)

* Update TaskSleepDuration.swift

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>

* Fix suggestion.

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>

* Update stdlib/public/Concurrency/TaskSleepDuration.swift

Co-authored-by: Alex Martini <amartini@apple.com>
Co-authored-by: Xiaodi Wu <13952+xwu@users.noreply.github.com>
2022-06-27 09:05:36 -07:00
Karoy Lorentey
789545d1be Merge pull request #41843 from lorentey/adopt-primary-associated-types
[SE-0358][stdlib] Adopt primary associated types
2022-06-24 10:46:29 -07:00
Konrad `ktoso` Malawski
53168355f0 [Distributed] Explicitly ban a class extending AnyActor as well 2022-06-22 09:21:55 +09:00
Saleem Abdulrasool
a35c7b38dd Concurency: add Windows.h include
The Windows path uses `LoadLibrary` which should use `Windows.h` for the
declaration as per MSDN.  This avoids an unnecessary warning when
building for Windows.
2022-06-19 09:42:21 -07:00
Mike Ash
30cb7a20c3 Merge pull request #59349 from mikeash/concurrency-tracing-log-categories
[Concurrency] Set final subsystem/categories for concurrency tracing.
2022-06-15 11:14:25 -04:00
Mike Ash
72499c5753 [Concurrency] Set final subsystem/categories for concurrency tracing. 2022-06-13 11:36:09 -04:00
John McCall
ef80a315f8 When waiting on a task, escalate it before enqueuing the waiting task.
As soon as the waiting task is successfully enqueued on the blocking
task, both tasks have to be considered invalidated because the
blocking task can concurrently complete and resume its waiters:

- The waiting task ensures that the blocking task is valid while
  it's waiting.  However, that's measured from the perspective of
  the waiting task, not from the perspective of the thread that was
  previously executing it.  As soon as the waiting task is resumed,
  the wait call completes and the validity guarantee on the blocking
  task disappears, so the blocking task must be treated as
  invalidated.

- The waiting task ensures that it is valid as long as it isn't
  complete.  Since it's trying to wait, it must not be complete.
  However, as soon we resume it, it can complete, so the waiting
  task must also be treated as invalidated.

This is one of those things that's not really easy to test, and the
need for a fix is pretty urgent, so I'm submitting this patch without
a test.  I'll try to land a race test that demonstrates the bug in
the next few days.

@kavon deserves all the credit here for some truly heroic debugging
and finally recognizing the flaw in the code; I'm just popping in
at the last minute to sheepishly patch the bug.

Fixes rdar://92666987
2022-06-10 23:39:59 -04:00
Alastair Houghton
124581a810 [Build][Concurrency] Fix Concurrency build to work for Linux.
`SWIFT_BUILD_STATIC_STDLIB` is not mutually exclusive with
`SWIFT_BUILD_DYNAMIC_STDLIB`, and Linux sets both, so we can't use
`SWIFT_BUILD_STATIC_STDLIB` to conditionalise things.

The linker error about duplicate definitions for the threading error
handler was happening because we were forced to include the object
containing that symbol; moving it to another object should fix that.

And it turns out there's a way to get CMake to include the threading
object library only when building a shared object, so use that too.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
33f103f03e [Build] Fix some issues with the standalone library build.
When we're building static libraries, we don't need to include the
threading objects in both Concurrency and Core, and we also don't need
two copies of the threading library's fatal error handler.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
48255788dd [Concurrency] Remove redundant include.
Clean up an unnecesary include.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
eb4c81d60e [Threading] Don't use TLS keys as template arguments.
There's no guarantee that e.g. pthread_key_t is an integral type.  It could
be some kind of struct, or some other thing that isn't valid as a template
argument.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
c7c1c1be80 [Threading] Fix some problems with the C11 threading code.
These changes are needed to get things building with a C11 threads shim
header on macOS.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
ef2e40a937 [Concurrency][Windows] Remove unnecessary includes.
TaskLocal.cpp doesn't need <handleapi.h> or <processthreadsapi.h>, both of
which want <windows.h>, which isn't included any more.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
e305a7dcf6 [Concurrency][Windows] Add a couple of includes for Windows.
Actor.cpp does need <io.h> still, and Task.cpp should have been including
<windows.h>

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
14a4bd45b6 [Concurrency][Threading] Remove use of platform thread functions.
The concurrency library can use the new threading library functions,
which avoids the problem of including <windows.h>.

rdar://90776105
2022-06-07 07:39:52 +01:00