Commit Graph

143 Commits

Author SHA1 Message Date
Mike Ash
a1dbdd59d1 [Concurrency] Remove useless comment about assert in AsyncTask::complete().
This comment is really about an assert that's no longer present, so it should just go away.
2025-05-01 11:22:39 -04:00
Mike Ash
325b66ab20 [Concurrency] Fix alreadyLocked in withStatusRecordLock.
If the preloaded status is locked, then we need to reload it in order to distinguish between the current thread holding the lock and another thread holding the lock. Without this, if another thread holds the lock, then we won't set the is-locked bit. We'll still actually hold the lock, but other threads may perform operations locklessly if the bit is not set, which can cause a crash. By reloading status in that case, we ensure that the bit is always set correctly.

This manifested as crashes in task cancellation but could cause other task-related issues as well.

Also remove an assert of !isStatusRecordLocked() in AsyncTask::complete(). We allow other threads to access tasks and take the lock for things like cancellation, so the lock may legitimately be held at that point.

rdar://150327908
2025-04-30 13:53:31 -04:00
Konrad 'ktoso' Malawski
94423da6d3 [Concurrency] Refactor _swift_taskGroup_cancelAllChildren -> _cancel
This way we do the right thing always when cancelling the group;
and we MAY visit the child tasks if we have to.
2025-04-14 17:30:28 +09:00
Alastair Houghton
ef3ea9bd27 [Concurrency] Fix size of AsyncTask::PrivateStorage.
Because `TaskAllocator` is not a round multiple of the machine word
size on 64-bit platforms, I think we end up with padding before the
`TaskLocal::Storage` following it, which makes the `PrivateStorage`
structure larger than the calculation in `ABI/Task.h`.

rdar://149067144
2025-04-11 17:12:16 +01:00
Konrad `ktoso` Malawski
28c4930f4f [Concurrency] Avoid inserting handler record in already cancelled task. (#80456)
This avoids the potential to race with the triggering coming from
task_cancel, because we first set the cancelled flag, and only THEN
take the lock and iterate over the inserted records. Because of this we
could: T1 flip the cancelled bit; T2 observes that, and triggers
"immediately" during installing the handler record. T1 then proceeds to
lock records and trigger it again, causing a double trigger of the
cancellation handler.

resolves https://github.com/swiftlang/swift/issues/80161
resolves rdar://147493150
2025-04-02 19:21:18 +09:00
Mike Ash
6397e30856 [Concurrency] Eliminate StatusRecordLockRecord.
Move to a recursive lock inline in the Task. This avoids the need to allocate a lock record and simplifies the code somewhat.

Change Task's OpaquePrivateStorage to compute its size at build time based on the sizes of its components, rather than having it be a fixed size. It appears that the fixed size was intended to be part of the ABI, but that didn't happen and we're free to change this size. We need to expand it slightly when using pthread_mutex as the recursive lock, as pthread_mutex is pretty big. Other recursive locks allow it to shrink slightly.

We don't have a recursive mutex in our Threading support code, so add a RecursiveMutex type.

rdar://113898653
2025-03-26 14:52:37 -04:00
Mike Ash
1e265c24d7 Merge pull request #80096 from mikeash/task-group-cancellation-lock-fix
[Concurrency] Fix a race when using cancelAll on a task group concurrently with child tasks being removed.
2025-03-20 11:53:31 -04:00
Mike Ash
38c4fce1a8 [Concurrency] Fix a race when using cancelAll on a task group concurrently with child tasks being removed.
_swift_taskGroup_cancelAllChildren relies on there being no concurrent modification when called from the owning task, but this is not guaranteed.

Rearrange things to always take the owning task's status record lock when walking the group's children. Split _swift_taskGroup_cancelAllChildren into two functions, one which assumes/requires the lock is already held, and one which acquires the lock. We don't have the owning task in this case, but we can either get it from the current task, or by looking at the parent of the child task we're working on.

rdar://147172991
2025-03-18 14:52:07 -04:00
Konrad `ktoso` Malawski
85e4685f90 [Concurrency] Adjust task escalation APIs to SE accepted shapes (#79988)
* [Concurrency] Adjust task escalation APIs to SE accepted shapes

* adjust test a little bit

* Fix closure lifetime in withTaskPriorityEscalationHandler

* avoid bringing workaround func into abi by marking AEIC
2025-03-18 07:05:11 +09:00
Konrad `ktoso` Malawski
fda7f539fb Reapply "Task names" (#79562) (#79600) 2025-03-08 10:58:49 +09:00
Konrad `ktoso` Malawski
09003d6f11 Revert "Merge pull request #77609 from ktoso/wip-task-names" (#79562)
This reverts commit 4ab5d2604f.
2025-02-23 22:59:21 -08:00
Konrad `ktoso` Malawski
4ab5d2604f Merge pull request #77609 from ktoso/wip-task-names
[Concurrency] Task names
2025-02-21 22:28:33 +09:00
Allan Shortlidge
4ab3aae7c5 Gardening: Suppress -Winvalid-offsetof warnings in static asserts.
There cannot be undefined behavior at compile time, so these warnings can be
ignored when `offsetof` is used in a static assert:

```
warning: offset of on non-standard-layout type 'AsyncTask' [-Winvalid-offsetof]
```
2024-10-29 15:52:53 -07:00
Mykola Pokhylets
cb4c880af1 Move TaskLocal.h out of ABI folder, because it describes implementation details 2024-10-06 14:37:11 +02:00
Kuba Mracek
f1c2334c9a [Concurrency] Enable TaskGroup/DiscardingTaskGroup in Embedded Swift 2024-09-28 17:52:05 -07:00
Konrad `ktoso` Malawski
2ec717b115 [Concurrency] TaskExecutor ownership fixes (#74000) 2024-06-14 22:56:33 +09:00
Mike Ash
0f226b6667 [Concurrency] Refine getResumeFunctionForLogging to avoid reading invalid future contexts.
When using a future adapter, the resume context may not be valid after the task starts running. Only peer through the adapter when we're starting to run.

rdar://126298035
2024-05-21 16:22:40 -04:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Konrad `ktoso` Malawski
5546f4da7b Rename ExecutorRef to SerialExecutorRef 2023-11-01 18:39:44 +09:00
Kuba Mracek
5d8c55eacb [embedded] Initial Swift Concurrency for embedded Swift 2023-10-06 20:04:03 -07:00
Konrad `ktoso` Malawski
8aa70dc733 cleanup for freestanding mode 2023-08-09 13:08:04 +09:00
Alastair Houghton
ee5efff5b1 [Threading][TSan] Update after review comments.
* Use the longer name ThreadSanitizer rather than TSan for the new files.
* Don't implement `tsan::consume` at all for now.
* Do the `tsan::release` for `ulock_unlock()` at the head of the function,
  not at the tail.
* Add a comment to test/Sanitizers/tsan/once.swift to explain the test a
  little more clearly.

rdar://110665213
2023-06-19 15:32:50 +01:00
Alastair Houghton
41f46ec085 [Threading][TSan] Fix TSan errors from lazy init on Linux.
Move the TSan functionality from Concurrency into Threading.  Use it
in the Linux `ulock` implementation so that TSan knows about `ulock`
and will tolerate the newer `swift_once` implementation that uses it.

rdar://110665213
2023-06-19 15:32:50 +01:00
Konrad `ktoso` Malawski
0586c14b60 [Concurrency] SerialExecutor.isSameExclusiveExecutionContext (#64604) 2023-03-28 15:56:28 +09:00
Rokhini Prabhu
4c92890a42 Remove overly strict assert that only the thread which was previously running the task, can mark the task as no-longer running. 2023-03-03 18:04:26 -08:00
Rokhini Prabhu
534811c3dc When a task gets enqueued on an executor, start tracking which executor
it is enqueued on. This way, we have the necessary bookkeeping to
escalate an executor when a task that is enqueued, is escalated.

Radar-Id: rdar://problem/101864092
2023-02-25 17:01:03 -08:00
Rokhini Prabhu
d77306729b Improve TaskDependencyStatusRecord so that it can now track the executor
you are enqueued on

Radar-Id: rdar://problem/101864092
2023-02-25 16:58:02 -08:00
Rokhini Prabhu
d0aa55670a Provide an updateStatusRecord function which allows clients to update a
status record that is already registered with the task. Provide more
versatile removeStatusRecord functions and update clients to use them

Radar-Id: rdar://problem/101864092
2023-02-25 16:55:38 -08:00
Rokhini Prabhu
e8d9fd91f7 Tell TSAN that we know that we're doing with CONSUME semantics so that
it doesn't flag false positive issues

Radar-Id: rdar://problem/105634683
2023-02-23 17:42:00 -08:00
Rokhini Prabhu
3c0f4f79fe Provide a convenience for addStatusRecord for clients who have already
done the load or who need the oldStatus information after adding the
status record.

Change some of the memory barrier logic since we can take advantage of
load-through HW address dependency.

Radar-Id: rdar://problem/105634683
2023-02-20 23:14:47 -08:00
Rokhini Prabhu
e052ccef31 Create the notion of a TaskDependencyStatusRecord which tracks what a
task is blocked on. We can use this information to then perform live
priority escalation to a task future.

Radar-Id: rdar://problem/88093007
2023-02-08 17:29:55 -08:00
Rokhini Prabhu
2e6baf279d Add support to addStatusRecord and removeStatusRecord so that the flags
field of an ActiveTaskStatus can also be modified while the
TaskStatusRecord list is being modified. Make the StatusRecordLock
reentrant.

Radar-Id: rdar://problem/88093007
2023-02-08 17:29:05 -08:00
Rokhini Prabhu
4152834319 Don't destroy the task private storage until task is destroyed since
other threads/tasks which have a reference to the task might need to
access parts of it even if the task itself is completed.

Radar-Id: rdar://problem/88093007
2023-02-07 14:34:29 -08:00
swift-ci
93a0a6d905 Merge pull request #62914 from ktoso/wip-task-group-twotypes
[Concurrency] DiscardingTaskGroup (rev 3)
2023-01-11 01:37:33 -08:00
Alejandro Alonso
382510fa50 Rename Reflection library to RemoteInspection (#62846) 2023-01-06 13:21:32 -05:00
Konrad `ktoso` Malawski
e37b998c56 implement simple rethrowing logic, however body throw always wins 2023-01-05 21:42:20 +09:00
Konrad `ktoso` Malawski
7ac5b407a6 move to DiscardingTaskGroup 2023-01-05 16:19:05 +09:00
Konrad `ktoso` Malawski
2652862229 prepare for cancellation handling 2023-01-05 16:19:05 +09:00
Konrad `ktoso` Malawski
54dec38a34 initial complete impl 2023-01-05 16:19:05 +09:00
Konrad `ktoso` Malawski
f8b85015c1 prepare flags
wip on options

implement discardResults as a flag passed to grout init
2023-01-05 16:19:05 +09: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
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
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
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
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
Alastair Houghton
66f6eb6bc6 [Runtime][Windows] A couple of files need to include <windows.h>
We shouldn't include <windows.h> implicitly from .cpp files, but should
do it directly so that we know it's there.

Also, if we're including <windows.h>, do it at the top of the file.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
0cf687aa2b [Build][Runtime] Replace SWIFT_STDLIB_SINGLE_THREADED_RUNTIME.
SWIFT_STDLIB_SINGLE_THREADED_RUNTIME is too much of a blunt instrument here.
It covers both the Concurrency runtime and the rest of the runtime, but we'd
like to be able to have e.g. a single-threaded Concurrency runtime while
the rest of the runtime is still thread safe (for instance).

So: rename it to SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and make it just
control the Concurrency runtime, then add a SWIFT_STDLIB_THREADING_PACKAGE
setting at the CMake/build-script level, which defines
SWIFT_STDLIB_THREADING_xxx where xxx depends on the chosen threading package.

This is especially useful on systems where there may be a choice of threading
package that you could use.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton
b5bd267ff1 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-05-24 14:57:41 +01:00