Commit Graph

231 Commits

Author SHA1 Message Date
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
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
Allan Shortlidge
4799f1e295 Concurrency: Suppress some warnings. 2025-02-01 13:52:43 -08:00
Alex Lorenz
b3ec0ff245 [stdlib] fix android build
ptrauth isn't available when stdlib is built with NDK's clang
2024-12-15 21:33:33 -08:00
Konrad `ktoso` Malawski
55603ec2d4 Use __ptrauth_swift_deinit_work_function in IsolatedDeinitJob 2024-12-12 16:41:04 +09:00
Konrad `ktoso` Malawski
9317f12bd0 Add pointer auth for isolated deinit work function 2024-12-12 16:41:03 +09:00
Kuba (Brecka) Mracek
8f66bf170e Merge pull request #77270 from kubamracek/embedded-concurrency-arm64e
[Concurrency] Fix arm64e ptrauth violations in Embedded Swift Concurrency
2024-10-30 10:17:32 -07:00
Kuba Mracek
cf7ea301e1 [Concurrency] Fix arm64e ptrauth violations in Embedded Swift Concurrency 2024-10-28 21:16:48 -07:00
Mykola Pokhylets
67ab0d7404 Implement disabling task-local values in isolated deinit
As requested by the LSG during the proposal review.
See https://forums.swift.org/t/accepted-with-modifications-se-0371-isolated-synchronous-deinit/74042
2024-10-28 16:15:21 +01: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
Mike Ash
763666ed6c Merge pull request #77031 from mikeash/actor-enqueue-retain
[Concurrency] Retain the actor around the CAS in enqueue() when necessary.
2024-10-17 13:38:04 -04:00
Mike Ash
6b01fed9c4 [Concurrency] Retain the actor around the CAS in enqueue() when necessary.
It's possible that the job we enqueue holds the last strong reference to the actor. If that job runs on another thread after we enqueue it, then it's possible for `this` to be destroyed while we're still in this function. We need to use `this` after the enqueue when the priorities don't match. When it looks like that will happen, retain `this` before the enqueue to ensure it stays alive until we're done with it.

Introduce a defensive retain helper class that makes it easy to do a single retain under certain conditions even in a loop, and does RAII to balance it with a release when the scope exits.

rdar://135400933
2024-10-16 16:51:29 -04:00
Kuba Mracek
d2dac4ed72 [Concurrency] Avoid relying on stderr, fprintf(), write() in Embedded Concurrency runtime 2024-10-14 13:36:14 -07:00
Alastair Houghton
54a495527e [Concurrency] Remove C++ runtime references from embedded Concurrency.
In embedded mode, we mustn't have references to the C++ library, because
some embedded platforms don't include the C++ library.

Additionally, it's good to avoid use of global operator new and operator
delete, because they can be globally overridden and this has bitten us
in the past.

rdar://137286187
2024-10-07 10:30:27 +01:00
Konrad `ktoso` Malawski
dea3b59331 Merge pull request #76250 from ktoso/wip-experimental-isolated-deinit 2024-10-03 17:19:10 +09:00
Alastair Houghton
f8d6012d9a Merge pull request #76483 from al45tair/eng/PR-135380149
[Concurrency] Build executors as their own separate object modules.
2024-10-02 09:39:46 +01:00
Michael Gottesman
cb8e8b505a Fix syntax highlighting by changing how we include COMPATIBILITY_OVERRIDE_INCLUDE_PATH.
The way that we include COMPATIBILITY_OVERRIDE_INCLUDE_PATH freaks out the
syntax highlighting of editors like emacs. It causes the whole file to be
highlighted like it is part of the include string.

To work around this, this patch creates a separate file called
CompatibilityOverrideIncludePath.h that just includes
COMPATIBILITY_OVERRIDE_INCLUDE_PATH. So its syntax highlighting is borked, but
at least in the actual files that contain real code, the syntax highlighting is
restored.
2024-10-01 16:17:16 -07:00
Alastair Houghton
837513dc16 [Concurrency] Fix test failures, add struct layout checks.
We were missing a field in `SwiftJob`, which broke various things.  To
avoid that problem in future, this PR adds a set of static asserts to
check the layout of various structures and that we're using the same
values as well.

Also added some functions to the ABI, and fixed things so that if you
enable the debug logging the library still builds (there was an extra
`_` in `Actor.cpp` that caused a build failure).

Finally, renamed `Hooks.cpp` to `ConcurrencyHooks.cpp`.

rdar://135380149
2024-09-30 17:16:33 +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
45b97f146b Merge branch 'main' into wip-experimental-isolated-deinit 2024-09-30 13:47:39 +09:00
Mykola Pokhylets
aeb71c4cb4 Disable retain count checks in actor deinit for embedded runtime 2024-09-30 03:41:40 +01:00
Mike Ash
efa62e7c33 [Concurrency] Fix crash from calling fromTaskExecutorPreference after enqueueing job.
After we've enqueued a job, another thread may run it and destroy it. Don't try to get the job's task executor preference when we try to schedule it. Instead, get the task executor preference before enqueueing the job, then use that preference when scheduling if necessary. Since getting the executor preference is potentially somewhat expensive (we need to search the status records for an executor preference record), only do this if the pre-compare-and-swap states look like they'll need it.

rdar://136281920
2024-09-27 17:32:29 -04: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
Allan Shortlidge
29f172f3c1 Concurrency: Suppress a -Wc++23-extensions warning.
Fixes the following warning:

```
warning: label at end of compound statement is a C++23 extension [-Wc++23-extensions]
```
2024-09-03 13:09:28 -07:00
Mykola Pokhylets
d6673c78bd Implemented zombie actor state in SWIFT_CONCURRENCY_ACTORS_AS_LOCKS mode using additional reference counter 2024-08-26 19:41:17 +02:00
Mykola Pokhylets
a7ac32fc8f Disabled isolated deinit for SWIFT_CONCURRENCY_ACTORS_AS_LOCKS in the runtime 2024-08-26 17:46:47 +02:00
Mykola Pokhylets
50b1313175 Merge branch 'main' into mpokhylets/isolated-deinit
# Conflicts:
#	lib/SILGen/SILGenDistributed.cpp
#	lib/Sema/TypeCheckConcurrency.cpp
2024-08-15 16:58:43 +02:00
Mykola Pokhylets
66871b922d Deterministically crash in DefaultActorImpl::destroy() when self escapes in deinit 2024-08-15 12:43:48 +02:00
Mykola Pokhylets
6151731d26 Updated implementation to handle SWIFT_CONCURRENCY_ACTORS_AS_LOCKS 2024-08-13 12:30:21 +02:00
Mike Ash
75fa43ebd8 [Concurrency] Clear current task TSD before enqueuing task in swift_task_switch. (#74736)
The executor may execute and free the task while the enqueue code is still finishing up. If that code tries to get an async backtrace (for example, if it calls malloc/free with malloc stack logging enabled) then it will find a dangling pointer in the current task TSD, and dereferencing it may crash.

rdar://130125017
2024-07-24 12:29:01 +09: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
e9173ff2f4 Make sure legacy executor checking mode is always used in swift_task_deinitOnExecutor() 2024-07-11 13:09:08 +02:00
Mykola Pokhylets
8dbddec82e Don't read current task executor to avoid extra access of the task-local storage. 2024-07-11 13:09:08 +02:00
Mykola Pokhylets
1b69068105 Keep current task executor when taking fast path in isolated deinit 2024-07-11 13:09:08 +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
a043ebf97a Don't switch threads when running isolated deinit of the default actor. 2024-07-11 13:09:07 +02:00
Mykola Pokhylets
411cdb7f73 Added unit-tests for (not)copying task-local values. Fixed release after deallocation. 2024-07-11 13:09:06 +02:00
Alastair Houghton
b6e7a5107a Merge pull request #75024 from Sajjon/cyon_typo_batch__stdlib
Typos: `stdlib`
2024-07-08 10:57:54 +01:00
Konrad `ktoso` Malawski
5ad2291ac0 [Concurrency] Fix build in actors as locks configuration (#75004) 2024-07-07 10:37:33 +09:00
Alexander Cyon
c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +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