Commit Graph

104 Commits

Author SHA1 Message Date
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
Kuba Mracek
db94dc7d36 [Concurrency] Fix memory leak around mismatched refcounting in Concurrency 2024-10-31 21:43:02 -07: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
Mike Ash
bc6c027a1f [Concurrency] Add a debug variable that holds the current version of the concurrency library.
This allows lldb to know which version of the internals layout it needs to use.

rdar://135886268
2024-09-16 09:14:03 -04: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
Takumi Muraishi
b2becb7b7e fix typos 2024-03-26 14:44:41 +09:00
Allan Shortlidge
3338722e21 NFC: Suppress an unused private member warning in ABI/Task.h. 2024-01-19 15:43:35 -08: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
a1f0782c0c [embedded] Add null checks of futureResultType back (allow non-future tasks) 2023-10-06 20:04:03 -07:00
Kuba Mracek
5d8c55eacb [embedded] Initial Swift Concurrency for embedded Swift 2023-10-06 20:04:03 -07:00
Konrad `ktoso` Malawski
e0877594da [Concurrency] Custom executors with move-only Job (#63569) 2023-03-18 14:02:43 +09:00
Konrad `ktoso` Malawski
ac0c3b75c6 [Concurrency] Freeze JobPriority, comment that Flags location is assumed (#64267) 2023-03-10 16:49:37 +09: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
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
Rokhini Prabhu
b2f51dd3de Implement continuations in task-to-thread model.
This is done using a condition variable upon which the awaiting thread
will block if the continuation has not be resumed by the point of await.
The resuming thread will signal this condition variable, thereby
unblocking the awaiting thread.

Rdar://99977665
2022-11-09 15:58:53 -08:00
Josh Soref
871634b9f3 Spelling include (#42616)
* spelling: accessible

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: are

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: assume

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: attempt

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: children

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: convenience

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: creation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: default

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: dereference

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: deserialization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: embedded

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: enriched

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: excluding

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: for

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: global

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: guarantee

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialization

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialize

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: label

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: lifting

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: mangled

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: metadata

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: minimum

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: offset

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: only

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: otherwise

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: output

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: overall

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: parameter

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: passed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: performance

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: referenced

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: standard

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: syntax

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: that

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trivia

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: truncate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: undesirable

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: uniformly

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: uninitialized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: value

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: verification

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-25 09:00:59 -07:00
John McCall
aca744b211 Remove the Flags field from AsyncContext.
Generated code has never actually initialized this field, so we
might as well remove it.  Doing so mostly doesn't impact the ABI
since we don't store anything for arguments or results in the
context as part of the normal call sequence.  We do need to adjust
some of the hard-coded contexts, however, such as continuation
contexts and the statically-sized context for special runtime
async functions.
2022-02-25 16:57:49 -05:00
Rokhini Prabhu
6beb11ed7d Rename flagAsEnqueuedOnExecutor to flagAsAndEnqueueOnExecutor to reflect
what it actually does.

Radar-Id: rdar://problem/88600541
2022-02-10 11:50:31 -08:00
Rokhini Prabhu
66d4af0b01 Task priority escalation on Apple platforms
A task can be in one of 4 states over its lifetime:

    (a) suspended
    (b) enqueued
    (c) running
    (d) completed

This change provides priority inversion avoidance support if a task gets
escalated when it is in state (a), (c), (d).

Radar-Id: rdar://problem/76127624
2022-02-07 16:34:46 -08:00
Rokhini Prabhu
519e60c43e Allow concurrent modifications to the ActiveTaskStatus while another
thread has the task status record lock.

Today, if a thread is holding the StatusRecordLock, then no other
modification of the task status is possible - including a thread
starting to execute the task or stopping execution of the task.
However, the TaskStatusRecordLock is really about protecting the linked
list that is maintained in the ActiveTaskStatus. As such, other
operations which don't need to look at that linked list of us records
really shouldn't have to block on the StatusRecordLock.

This change allows for concurrent modification of the veTaskStatus while
the TaskStatusRecordLock is held. In particular, a task can cancelled,
escalated, start and stop running, all while another ad is holding onto
the task's StatusRecordLock. In the event of cancellation and
escalation, the task's StatusRecordLock must be n in order to propagate
cancellation and escalation to its child tasks is not needed to cancel
or escalate the task itself.

Radar-Id: rdar://problem/76127624
2022-02-07 16:34:46 -08:00
Rokhini Prabhu
9df1c9a135 Track base priority separately from max priority whereby base priority
is set at creation time. Create a new API for accessing this state

Radar-Id: rdar://problem/86100376
2022-01-24 07:50:27 -08:00
Mike Ash
0977920217 [Concurrency] Add tracing for major operations in the concurrency runtime.
Each trace point is declared as a function in the new `Tracing.h` header. These functions are called from the appropriate places in the concurrency runtime.

On Darwin, an implementation of these functions is provided which uses the `os/signpost.h` API to emit signpost events/intervals.

When the signpost API is not available, no-op stub implementations are provided. Implementations for other OSes can be provided by providing implementations of the trace functions for that OS.

rdar://81858487
2022-01-07 11:35:40 -05:00
Mike Ash
bac6965946 [Concurrency] Make task IDs 64-bit.
The 32-bit identifier in Job is locked down at this point, so we expand the ID by storing the top 32 bits separately inside AsyncTask::PrivateStorage.

rdar://85167409
2021-11-19 15:24:35 -05:00
John McCall
d7cae26171 Better comments, fix a calculation. 2021-10-20 13:11:03 -04:00
Arnold Schwaighofer
68b9ccc4ad Make sure that the future fragment's storage pointer is properly aligned 2021-10-20 00:52:07 -04:00
Mike Ash
a80a19b1e1 [Concurrency] Propagate Darwin vouchers across async tasks.
Darwin OSes support vouchers, which are key/value sets that can be adopted on a thread to influence its execution, or sent to another process. APIs like Dispatch propagate vouchers to worker threads when running async code. This change makes Swift Concurrency do the same.

The change consists of a few different parts:

1. A set of shims (in VoucherShims.h) which provides declarations for the necessary calls when they're not available from the SDK, and stub implementations for non-Darwin platforms.
2. One of Job's reserved fields is now used to store the voucher associated with a job.
3. Jobs grab the current thread's voucher when they're created.
4. A VoucherManager class manages adoption of vouchers when running a Job, and replacing vouchers in suspended tasks.
5. A VoucherManager instance is maintained in ExecutionTrackingInfo, and is updated as necessary throughout a Job/Task's lifecycle.

rdar://76080222
2021-09-02 17:45:28 -04:00
Michael Gottesman
ff6b67c585 [concurrency] Add AsyncTask::flagAsCompleted() and behind SWIFT_TASK_PRINTF_DEBUG allow it to be used to log Task completion.
This should let people debug Task lifetime easier.
2021-07-22 13:25:09 -07:00
John McCall
29434257dd Allow swift_continuation_await to be forced to honor an executor. 2021-07-14 20:24:01 -04:00
John McCall
3aa04db87b Track whether a task is actively running.
Tracking this as a single bit is actually largely uninteresting
to the runtime.  To handle priority escalation properly, we really
need to track this at a finer grain of detail: recording that the
task is running on a specific thread, enqueued on a specific actor,
or so on.  But starting by tracking a single bit is important for
two reasons:

- First, it's more realistic about the performance overheads of
  tasks: we're going to be doing this tracking eventually, and
  the cost of that tracking will be dominated by the atomic
  access, so doing that access now sets the baseline about right.

- Second, it ensures that we've actually got runtime involvement
  in all the right places to do this tracking.

A propos of the latter: there was no runtime involvement with
awaiting a continuation, which is a point at which the task
potentially transitions from running to suspended.  We must do
the tracking as part of this transition, rather than recognizing
in the run-loops that a task is still active and treating it as
having suspended, because the latter point potentially races with
the resumption of the task.  To do this, I've had to introduce
a runtime function, swift_continuation_await, to do this awaiting
rather than inlining the atomic operation on the continuation.

As part of doing this work, I've also fixed a bug where we failed
to load-acquire in swift_task_escalate before walking the task
status records to invoke escalation actions.

I've also fixed several places where the handling of task statuses
may have accidentally allowed the task to revert to uncancelled.
2021-07-14 20:24:01 -04:00
John McCall
e223a4f955 Greatly increase the amount of private storage in a task.
It's already nearly full, and there's plenty of stuff we're not
tracking properly yet.
2021-07-14 16:01:00 -04:00
Konrad `ktoso` Malawski
8536100354 [Concurrency] introduce task options, and change ABI to accept them
introduce new options parameter to all task spawning

[Concurrency] ABI for asynclet start to accept options

[Concurrency] fix unittest usages of changed task creation ABI

[Concurrency] introduce constants for parameter indexes in ownership

[Concurrency] fix test/SILOptimizer/closure_lifetime_fixup_concurrency.swift
2021-06-21 13:03:50 +09:00
Mike Ash
c2df2448ec [Concurrency] Reserve two more words in Job.
Keep Task the same size by pulling two words out of OpaquePrivateStorage.

rdar://79313908
2021-06-16 16:59:25 -04:00
Arnold Schwaighofer
0b8e7169e3 swift_task_future_waitImpl: Sink context intialization into slow path 2021-06-10 12:43:08 -07:00
John McCall
db3967f41d Future-proof the layout of AsyncTask. 2021-05-20 15:30:48 -04:00
Varun Gandhi
35b9d01cb3 [Runtime] Remove FIXMEs and hacks for tail calls.
Reverts hack from 9b4f7d62cd.
2021-05-17 12:46:58 -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
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
Konrad `ktoso` Malawski
6f3dac190a [TaskLocals] remove TaskLocalInheritance, we'll introduce when needed 2021-04-29 15:48:22 +09:00
Konrad `ktoso` Malawski
3d96d05546 [TaskLocals] property wrapper keys 2021-04-29 15:48:09 +09: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
Erik Eckstein
93367ed587 concurrency: make the startAsyncLet closure no-escaping
The closure does not escape the startAsyncLet - endAsyncLet scope. Even though it's (potentially) running on a different thread.

The substantial change in the runtime is to not call swift_release on the closure context if it's a non-escaping closure.
2021-04-20 21:57:19 +02:00
Mike Ash
2dc06c5fe2 [Concurrency] Fix Dispatch linkage offset on 32-bit.
rdar://76703675
2021-04-15 12:18:38 -04:00
Konrad `ktoso` Malawski
0857d0f62b remove last mention of spawnDetached 2021-04-03 10:08:11 +09:00
Konrad `ktoso` Malawski
6e746304e1 [Concurrency] Update Task and Group APIs based on Review 1 2021-04-03 09:52:03 +09:00