Commit Graph

177 Commits

Author SHA1 Message Date
John McCall
3cfda35b7c Remove the unused swift_asyncLet_{start,end,wait,wait_throwing} runtime
functions.

These were introduced in an early draft implementation of async let, but
never used by a released compiler. They are not used as symbols by any
app binaries. There's no reason to keep carrying them.

While I'm at it, dramatically improve the documentation of the remaining
async let API functions.
2025-11-03 13:45:18 -08:00
Philippe Hausler
694aebb1fb Add an additional case for clocks to sleep with a wallclock deadline (#79139)
This only modifies the runtime function `swift_task_enqueueGlobalWithDeadline` to take new clock primitive to
interoperate with existing dispatch wall clock values.
2025-06-09 09:01:27 -07:00
Pavel Yaskevich
001eab867d [stdlib] SE-0472: Rename Task and*TaskGroup APIs to match the proposal
`Task.startSynchronously` -> `Task.immediate`
`*TaskGroup.startTaskSynchronously{UnlessCancelled}` -> `*TaskGroup.addImmediateTask{UnlessCancelled}`
2025-05-09 23:59:30 -07:00
Konrad 'ktoso' Malawski
6872707574 [embedded][Concurrency] Further refine C-api boundary for isIsolating... 2025-04-29 22:29:26 +09:00
Konrad 'ktoso' Malawski
e1dc854f8f [Concurrency] Change isIsolatingCurrent... to return Bool?
This changes the isIsolatingCurrentContext function to return `Bool?`
and removes all the witness table trickery we did previously to detect
if it was implemented or not. This comes at a cost of trying to invoke
it always, before `checkIsolated`, but it makes for an simpler
implementation and more checkable even by third party Swift code which
may want to ask this question.

Along with the `withSerialExecutor` function, this now enables us to
check the isolation at runtime when we have an `any Actor` e.g. from
`#isolation`.

Updates SE-0471 according to
https://forums.swift.org/t/se-0471-improved-custom-serialexecutor-isolation-checking-for-concurrency-runtime/78834/
review discussions
2025-04-29 20:08:55 +09:00
Konrad 'ktoso' Malawski
a24a28c217 [Concurrency] Improve in order synchronous enqueue of startSynchronously
Previously there was still a sneaky hop which caused ordering issues.
This introduced a specific test startSynchronously_order which checks
that the task enqueues indeed are "immediate" and cleans up how we
handle this.

This also prepares for the being discussed in SE review direction of
this API that it SHOULD be ALLOWED to actually hop and NOT be
synchronous at all IF the isolation is specified on the closure and is
DIFFERENT than the callers dynamic isolation.

This effectively implements "synchronously run right now if dynamically
on the exact isolation as requested by the closure; otherwise enqueue
the task as usual".

resolves rdar://149284186
cc @drexin
2025-04-15 19:59:15 +09:00
Mike Ash
8fa76431e2 [Concurrency] Move SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION to ABI/Task.h.
We check it in Task.h so it needs to be defined by that point.
2025-04-09 16:16:36 -04:00
Alastair Houghton
47fa71787f Revert "Merge pull request #80224 from glessard/revert-79789-custom-executors"
This reverts commit 06f6358067, reversing
changes made to 033f6679e8.
2025-03-28 10:15:07 +00:00
Nate Chandler
f30d8a603c [CoroutineAccessors] Rehome runtime functions.
Put them where they're meant to be, in swiftCore.
2025-03-27 11:37:26 -07:00
Nate Chandler
0c2a38b10b [CoroutineAccessors] Directly reference allocators
Replace the call to a runtime function that looks up the allocator with
a direct reference to a just-emittedd sought-after global allocator.
2025-03-26 08:35:35 -07:00
Alastair Houghton
8b15b05c63 Revert "[Concurrency] Provide a Swift interface for custom main and global executors." 2025-03-22 02:38:11 -07:00
Alastair Houghton
8443b5f76c Merge pull request #79789 from al45tair/custom-executors
[Concurrency] Provide a Swift interface for custom main and global executors.
2025-03-21 09:05:03 +00:00
Konrad `ktoso` Malawski
85fcd69833 [Concurrency] Implement detecting isIsolatingCurrentContext user impls (#79946)
* [Concurrency] Detect non-default impls of isIsolatingCurrentContext

* [Concurrency] No need for trailing info about isIsolating... in conformance

* Apply changes from review
2025-03-18 09:39:11 +09:00
Alastair Houghton
090c375b7b [Concurrency] Swift interface for custom main and global executors.
Reorganise the Concurrency code so that it's possible to completely
implement executors (both main and global) in Swift.

Provide API to choose the desired executors for your application.

Also make `Task.Sleep` wait using the current executor, not the global
executor, and expose APIs on `Clock` to allow for conversion between
time bases.

rdar://141348916
2025-03-13 13:34:41 +00:00
Konrad `ktoso` Malawski
e056c63c89 [Concurrency] Implement isIsolatingCurrentContext requirement and mode (#79788) 2025-03-11 09:48:38 +09:00
Konrad `ktoso` Malawski
fda7f539fb Reapply "Task names" (#79562) (#79600) 2025-03-08 10:58:49 +09:00
Nate Chandler
dd8cbe3e0a [CoroutineAccessors] Use retcon.once variant.
Allocate a coroutine frame in the caller based on the size in the
corresponding "function pointer" and pass it along with an allocator to
the callee.
2025-02-27 07:53:58 -08:00
Konrad `ktoso` Malawski
f55964df26 [Concurrency] Initial steps for startSynchronously for Task (#79608)
* [Concurrency] Initial steps for startSynchronously for Task

* [Concurrency] Rename to _startSynchronously while in development

* [Concurrency] StartSynchronously special executor to avoid switching

* startSynchronously bring back more info output

* [Concurrency] startSynchronously with more custom executor tests

* add missing ABI additions to test for x86

* [Concurrency] gyb generate _startSynchronously

* [Concurrency] %import dispatch for Linux startSynchronously test

* [Concurrency] Add TaskGroup.startTaskSynchronously funcs

* [Concurrency] DispatchSerialQueue does not exist on linux still
2025-02-27 02:34:33 -08: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
67e6373c23 [Concurrency] minor renames of new API for adding escalation handlers 2025-02-08 17:18:58 +09:00
Konrad `ktoso` Malawski
655bd67f67 [Concurrency] Task priority escalation handler API 2025-02-08 17:18:58 +09:00
Michael Gottesman
5fa02d8711 [concurrency] Add a new entrypoint: swift_task_isCurrentExecutorWithFlags.
This entrypoint is similar to swift_task_isCurrentExecutor except that it
provides an ABI level option flag that enables one to configure its behavior in
a backwards deployable manner via the option flag.

I used this to expose at the ABI level the ability to check the current executor
without crashing on failure, while preserving the current behavior of
swift_task_isCurrentExecutor (which crashes on failure).

I am going to use this to implement swift_task_runOnMainActor.
2024-10-17 11:17:38 -07:00
Kuba (Brecka) Mracek
341d6305ff Merge pull request #76767 from kubamracek/embedded-concurrency-task-groups
[Concurrency] Enable TaskGroup/DiscardingTaskGroup in Embedded Swift
2024-10-03 19:29:54 -07:00
Konrad `ktoso` Malawski
dea3b59331 Merge pull request #76250 from ktoso/wip-experimental-isolated-deinit 2024-10-03 17:19:10 +09:00
Kuba Mracek
a2dcb6045c [embedded] Introduce a new swift_taskGroup_initializeWithOptions runtime entrypoint 2024-10-01 14:56:38 -07:00
Alastair Houghton
26b5fa697a [Concurrency] Rename ExecutorHooks.h, tidy up the interface.
`ExecutorHooks.h` is now nothing to do with hooks, so rename it.  Also
there are some additional functions it should declare, and a couple of
places where we've slightly messed up the boundary, for instance
`swift_task_asyncMainDrainQueue` was defined in `Task.cpp` rather than
in the executor implementations, which is wrong, so fix that too.

`CooperativeGlobalExecutor.cpp` now builds against the interface from
`ExecutorImpl.h`, rather than including the all the concurrency headers.

rdar://135380149
2024-09-30 17:15:40 +01:00
Alastair Houghton
efe5d660f2 [Concurrency] Refactor executors so they are in their own object module.
C++ executor implementations were `#include`ed into `GlobalExecutor.cpp`,
which makes it difficult to replace the global executor when using the
Embedded Concurrency library.  Refactor things so that they build into
separate objects, which means replacing them is just a matter of writing
the relevant functions yourself.

rdar://135380149
2024-09-30 17:15:38 +01:00
Konrad `ktoso` Malawski
7d1ce789ad Revert "Revert "Isolated synchronous deinit"" 2024-09-17 17:35:38 +09:00
Alex Hoppen
c5aa49ba64 Revert "Isolated synchronous deinit" 2024-09-03 18:11:26 -07:00
Mykola Pokhylets
816d62c972 Merge remote-tracking branch 'upstream/main' into mpokhylets/isolated-deinit
# Conflicts:
#	include/swift/Basic/Features.def
#	lib/SILGen/SILGenDestructor.cpp
#	test/Concurrency/flow_isolation.swift
#	test/abi/macOS/arm64/concurrency.swift
#	test/abi/macOS/x86_64/concurrency.swift
2024-07-11 13:11:59 +02:00
Mykola Pokhylets
ab4d338f6a Added flags to be able to control task-locals behaviour in the future 2024-07-11 13:09:08 +02:00
Mykola Pokhylets
35f0334eb6 Rename performOnExecutor into deinitOnExecutor.
It cannot be used for executing general-purpose work, because such function would need to have a different signature to pass isolated actor instance.

And being explicit about using this method only for deinit allows to use object pointer for comparison with executor identity.
2024-07-11 13:09:07 +02:00
Mykola Pokhylets
b7e23c2e2e Runtime support for isolated deinit 2024-07-11 13:09:04 +02:00
Konrad `ktoso` Malawski
b9369bf0b7 [Concurrency] Fix default actor isolation issue in face of TaskExecutor preference (#74658)
* [Concurrency] Fix task excutor handling of default actor isolation

The task executor API did not properly account for taking the default
actor locking into account when running code on it, we just took the job
and ran it without checking with the serial executor at all, which
resulted in potential concurrent executions inside the actor --
violating actor isolation.

Here we change the TaskExecutor enqueue API to accept the "target"
serial executor, which in practice will be either generic or a specific
default actor, and coordinate with it when we perform a
runSynchronously.

The SE proposal needs to be amended to showcase this new API, however
without this change we are introducing races so we must do this before
the API is stable.

* Remove _swift_task_enqueueOnTaskExecutor as we don't use it anymore

* no need for the new protocol requirement

* remove the enqueue(_ job: UnownedJob, isolatedTo unownedSerialExecutor: UnownedSerialExecutor)

Thankfully we dont need it after all

* Don't add swift_defaultActor_enqueue_withTaskExecutor and centralize the task executor getting to enqueue()

* move around extern definitions
2024-07-01 16:42:27 +09:00
Konrad `ktoso` Malawski
14b8546392 [Concurrency] Don't add new task locals copy runtime func 2024-06-03 13:55:02 +09:00
Konrad `ktoso` Malawski
0c44645832 [Concurrency] Implement defensive copying in task groups, rather than crashing (#73978) 2024-05-31 11:27:03 -07:00
Konrad `ktoso` Malawski
86f5441294 [SerialExecutor] SerialExecutor.checkIsolated() to check its own tracking for isolation checks (#71172) 2024-03-29 07:06:34 +09:00
Rokhini Prabhu
ed8ed32dba Clean up Concurrency build system so that all the logic around choice of
which executor for which type of setting, is consolidated and we have a
single knob we use to determine when to use dispatch as our global
executor.

Radar-Id: rdar://problem/119416196
2023-12-13 16:09:51 -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
Konrad `ktoso` Malawski
0586c14b60 [Concurrency] SerialExecutor.isSameExclusiveExecutionContext (#64604) 2023-03-28 15:56:28 +09:00
Konrad `ktoso` Malawski
e0877594da [Concurrency] Custom executors with move-only Job (#63569) 2023-03-18 14:02:43 +09:00
Konrad `ktoso` Malawski
41f99fc2ae [Executors][Distributed] custom executors for distributed actor (#64237)
* [Executors][Distributed] custom executors for distributed actor

* harden ordering guarantees of synthesised fields

* the issue was that a non-default actor must implement the is remote check differently

* NonDefaultDistributedActor to complete support and remote flag handling

* invoke nonDefaultDistributedActorInitialize when necessary in SILGen

* refactor inline assertion into method

* cleanup

* [Executors][Distributed] Update module version for NonDefaultDistributedActor

* Minor docs cleanup

* we solved those fixme's

* add mangling test for non-def-dist-actor
2023-03-15 23:42:55 +09:00
Evan Wilde
1bd1c468a0 Concurrency: Task.startOnMainActor
This patch adds an SPI to run the first partial function of a MainActor
asynchronous function on the MainActor synchronously. This is
effectively like the asynchronous program entrypoint behavior. The first
partial function is run synchronously. Following continuations are
enqueued for execution like any other asynchronous function.
2023-03-06 13:33:22 -08:00
Evan Wilde
841fda5dbe Add asyncMainDrainQueue hook
The swift_task_asyncMainDrainQueue function acts as the entrypoint into
driving the main queues, ultimately running the whole program and acting
as the backing driver of the main actor. Making the function hookable
means that custom concurrency runtimes can implement their own async
entrypoints, allowing async top-level code and async-main to "just
work".
2023-03-01 22:16:23 -08:00
Konrad `ktoso` Malawski
a2783b2e8d [Concurrency] UnownedJob.priority and description for TaskPriority 2023-02-28 16:17:48 +09:00
Rokhini Prabhu
babaa4bc2a Merge pull request #63019 from apple/rokhinip/88093007-priority-escalation-to-future
Provide support for live priority escalation in the task runtime
2023-02-11 11:33:41 -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
Konrad `ktoso` Malawski
6b95ff7a25 [Concurrenncy] add internal way to assert we're on expected executor 2023-02-08 15:41:25 +09:00