Commit Graph

45 Commits

Author SHA1 Message Date
Konrad 'ktoso' Malawski
2c99a669d3 Correct priority cancellation handler signing once more
Resolves rdar://150378890 for real this time, verified on arm64e myself
2025-06-02 21:32:57 +09: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
Dave Lee
8ad295aa02 Fix a comment reference to TaskGroupBase (#79622)
Update a reference to `TaskGroupImpl` to `TaskGroupBase`.
2025-03-10 13:16:03 -07: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
Konrad `ktoso` Malawski
655bd67f67 [Concurrency] Task priority escalation handler API 2025-02-08 17:18:58 +09:00
Konrad `ktoso` Malawski
2ec717b115 [Concurrency] TaskExecutor ownership fixes (#74000) 2024-06-14 22:56:33 +09: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
Rokhini Prabhu
758b38f5b6 Fix warnings around initialization of union
Radar-Id: rdar://problem/106095698
2023-03-03 18:55:09 -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
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
62852262b4 Remove dead code with task nearest deadline logic
Radar-Id: rdar://problem/88093007
2023-02-07 14:34:29 -08:00
Konrad `ktoso` Malawski
4b90cdbe5c Revert "cleanup"
This reverts commit 128e2b2099.
2023-01-11 13:03:22 +09:00
Konrad `ktoso` Malawski
128e2b2099 cleanup 2023-01-11 12:40:25 +09:00
Konrad `ktoso` Malawski
6d63cf4994 fix cast type in getting task record 2023-01-11 11:55:52 +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
David Smith
1707ffebfe Review feedback 2022-02-02 16:28:53 -08:00
David Smith
df577f2c5b Fix SR-15785 by adding a cached reference to the last linked list node, avoiding n^2 append scaling 2022-02-02 14:02:12 -08:00
Rokhini Prabhu
1947102ebd Change the logic for adding new task status records to a task
This change has two parts to it:

1. Add in a new interface (addStatusRecordWithChecks) for adding task
status records that also takes in a function ref. This function ref will
be used to evaluate if current state of the parent task has any changes
that need to be propagated to the child task that has been created.

This is necessary to prevent the following race between task creation
and concurrent cancellation and escalation:

a. Parent task create child task. It does lazy relaxed loads on its own
   state while doing so and propagates this state to the child.
b. Child task is created but has not been attached to the parent
   task/task group.
c. Parent task gets cancelled by another thread.
d. Child task gets linked into the parent’s task status records but no
   reevaluation has happened to account for changes that might have happened to
   the parent after (a).

2. Move status record management functions from the
Runtime/Concurrency.h to TaskPrivate.h. Remove any corresponding
overrides that are no longer needed. Remove unused tryAddStatusRecord
method whose functionality is provided by addStatusRecordWithChecks.

Radar-Id: rdar://problem/86347801
2021-12-31 03:23:52 -08:00
Konrad `ktoso` Malawski
f336404ae9 [Concurrency] TaskGroup children remove their records as they complete
If we didn't do this (and we didn't), the tasks get released as we
perform the next() impl, and move the value from the ready task to the
waiting task. Then, the ready task gets destroyed.

But as the task group exists, it performs a cancelAll() and that
iterates over all records. Those records were not removed previously
(!!!) which meant we were pointing at now deallocated tasks.

Previously this worked because we didn't deallocate the tasks, so they
leaked, but we didn't crash. With the memory leak fixed, this began to
crash since we'd attempt to cancel already destroyed tasks.

Solution:
- Remove task records whenever they complete a waiting task.
- This can ONLY be done by the "group owning task" itself, becuause
  the contract of ONLY this task being allowed to modify records. o
  It MUST NOT be done by the completing tasks as they complete, as it
  would race with the owning task modifying this linked list of child
  tasks in the group record.
2021-09-04 20:09:55 +09:00
Konrad `ktoso` Malawski
e8888f7965 [Concurrency] prevent races in task cancellation 2021-07-16 08:29:00 +09:00
John McCall
db3967f41d Future-proof the layout of AsyncTask. 2021-05-20 15:30:48 -04:00
Joe Groff
4150b31954 Fix calling convention for withCompletionHandler runtime calls.
rdar://75370240
2021-03-25 14:45:47 -07:00
Konrad `ktoso` Malawski
aedbbe615d [TaskGroup] Towards ABI stability of groups 2021-03-02 20:25:22 +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
07d6715d9f cleanup: remove all println debugging from task infra 2021-02-22 13:26:38 +09:00
Konrad `ktoso` Malawski
3132009929 [Concurrency] Track group children in task record + invasive links 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
1c4655df6a [Concurrency] track child tasks from group in Task records 2021-02-22 13:26:33 +09:00
Konrad `ktoso` Malawski
b1e5c4a2c4 test cleanups, additional test to check we dont cancel other groups 2021-02-22 13:26:28 +09:00
Konrad `ktoso` Malawski
1860b20cb7 [Concurrency] cancel all child tasks of a group on cancelAll 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
854cade5d6 cleanups, cancellation 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
edbcb1ea9e [Concurrency] task cancellation propagate through children 2021-02-22 13:26:27 +09:00
Konrad `ktoso` Malawski
1970dbfdae fix over releasing the current task by group dealloc 2021-02-22 13:26:27 +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
Konrad `ktoso` Malawski
49c6ae3da5 [Concurrency] Nurseries are now Task.Groups 2020-11-06 10:58:51 +09:00
John McCall
c31dab2fe3 Add a missing inline. 2020-10-22 00:53:16 -04:00
John McCall
8ac4362754 Implement a simple library for task cancellation and status management.
There are things about this that I'm far from sold on.  In
particular, I'm concerned that in order to implement escalation
correctly, we're going to have to add a status record for the
fact that the task is being executed, which means we're going
to have to potentially wait to acquire the status lock; overall,
that means making an extra runtime function call and doing some
atomics whenever we resume or suspend a task, which is an
uncomfortable amount of overhead.

The testing here is pretty grossly inadequate, but I wanted to
lay down the groundwork here.
2020-10-15 00:36:36 -04:00