Commit Graph

1607 Commits

Author SHA1 Message Date
3405691582
7a87fa5cbc Add a platform executor module for OpenBSD.
This is basically the same as the one for Linux, but it would be
somewhat awkward to add the platform conditional on a file named for
Linux when OpenBSD is not Linux.

Important note: if Dispatch is disabled, then this will cause a
compilation error (probably not just for OpenBSD either), because
PlatformExecutorFactory is both defined in PlatformExecutorNone.swift
and PlatformExecutor<...>.swift in this case.

Because this only bites OpenBSD bootstrap builds, and since OpenBSD
support has been upstreamed to Dispatch, default to the Dispatch
implementation for now to get this in, and we'll refactor in a different
pr.
2025-05-08 17:46:30 -04:00
Michael Gottesman
e04e15f125 [swift-settings] Now that we aren't using it immediately, remove it from tree.
We can always get it back from the git history.

rdar://150695113
(cherry picked from commit 9d59dbed17)

Conflicts:
	include/swift/AST/DiagnosticsSema.def
	include/swift/Basic/Features.def
	test/abi/macOS/arm64/concurrency.swift
	test/abi/macOS/x86_64/concurrency.swift
2025-05-06 14:12:23 -07:00
Doug Gregor
68c7c85132 Merge pull request #81288 from swiftlang/revert-81127-pick-wip-inlinable-for-hashable-asyncstream
Revert "[6.2][Concurrency] Hashable funcs should be inlinable for AsyncStream"
2025-05-06 10:31:30 -07:00
Alastair Houghton
315d67c0e8 [Concurrency] Don't pass negative times to the Dispatch code.
Dispatch uses unsigned times, and cannot cope with times before its
clock started.  As such, passing negative times from Swift through to
the C++ code results in large unsigned values, which then causes us to
wait forever.  This is undesirable.

rdar://148899609
2025-05-06 16:31:05 +01:00
Alastair Houghton
418ae45ce2 [Concurrency] Make initial executor construction fully thread safe.
I had originally thought that we'd get away without this because the
executor construction generally happens in the async `main` function,
but of course if a program doesn't already use Swift Concurrency,
and someone uses it from a plug-in or library it loads, there's a
risk that we might have a race condition.

rdar://149346132
2025-05-06 11:50:07 +01:00
Doug Gregor
36a93dc91e Revert "[6.2][Concurrency] Hashable funcs should be inlinable for AsyncStream…"
This reverts commit ec7ed9ac39.
2025-05-05 10:02:38 -07:00
Evan Wilde
62c353a7f9 [6.2]: Add platform executor module for FreeBSD
Swift concurrency defines a default platform executor. This was not
defined for FreeBSD resulting in build failures. Defining it to use the
Dispatch executor.

Scope: This only impacts FreeBSD.
Risk: Low. This change only affects FreeBSD.
Reviewers: @compnerd, @ktoso, @al45tair
Testing: Local testing to ensure Concurrency builds on FreeBSD.
Fixes: rdar://150643436

(cherry picked from commit 4a41e50730)
2025-05-04 14:05:06 -07:00
Alastair Houghton
e767e7f705 Merge pull request #81196 from al45tair/eng/PR-150290165-6.2
[Concurrency] Fix copy-paste error.
2025-05-04 18:32:01 +01:00
Alastair Houghton
55cda7dd85 Merge pull request #80795 from al45tair/eng/PR-149058236-6.2
[Concurrency] Remove -executor-factory option and replace with magic type.
2025-05-04 18:29:30 +01:00
Konrad `ktoso` Malawski
f6a864a188 Merge pull request #81135 from ktoso/pick-22b20e731eaa70746d3c21bf8c44836aa49dfabb
[6.2][Concurrency] Change isIsolatingCurrent... to return Bool?
2025-05-02 07:53:12 +09:00
Konrad `ktoso` Malawski
413349a0e8 Merge pull request #81230 from ktoso/pick-wip-escalation-handler-priority-auth
[6.2][Concurrency] Fix ptr auth for task priority escalation handler
2025-05-02 05:51:49 +09:00
Mike Ash
23e21ff47d Merge pull request #81206 from mikeash/fix-statusRecordLock-6.2
[6.2][Concurrency] Fix alreadyLocked in withStatusRecordLock.
2025-05-01 11:21:46 -04:00
Konrad 'ktoso' Malawski
75a3cacaec [Concurrency] Hide the _withSerialExecutor, we will introduce executor 2025-05-01 22:08:18 +09:00
Konrad 'ktoso' Malawski
fe0889b285 [Concurrency] Offer way to get SerialExecutor from Actor 2025-05-01 22:07:13 +09:00
Alastair Houghton
c072992fe4 Merge pull request #81201 from al45tair/eng/PR-150310927-6.2
[Concurrency] Fix issue with using Dispatch queues as executors.
2025-05-01 11:57:24 +01:00
Konrad 'ktoso' Malawski
3615dbf398 [Concurrency] Fix ptr auth for task priority escalation handler
We missed to sign the handler. Along the way the signature of it
changed, so adjust for that.

How to get the number:

```
func PROPER(bar: (TaskPriority, TaskPriority) -> Void) {
    let p = TaskPriority.default
    bar(p, p)
}
```

```
-> % swiftc -target arm64e-apple-macos13 example.swift -S -o - | swift demangle | grep -a3 autda
	stur	x8, [x29, #-64]
	mov	x17, x8
	movk	x17, #11839, lsl #48 <<<<<<<<<
	autda	x16, x17
	ldr	x8, [x16, #64]
	lsr	x8, x8, #0
	add	x8, x8, #15
```

Resolves rdar://150378890
2025-05-01 12:13:29 +09:00
Doug Gregor
fcedb0b2a4 Merge pull request #81125 from DougGregor/unsafe-call-effects-6.2
[6.2] [Strict memory safety] Provide argument-specific diagnostics for calls
2025-04-30 15:09:50 -07:00
Mike Ash
a44dadd0e0 [6.2][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
(cherry picked from commit 325b66ab20)
2025-04-30 13:56:29 -04:00
Alastair Houghton
7f5f45ced2 [Concurrency] Fix issue with using Dispatch queues as executors.
We were failing to switch executors to Dispatch queues, where those
were being used as executors, which caused a variety of unusual
symptoms.

rdar://150310927
2025-04-30 14:29:14 +01:00
Alastair Houghton
ae47fd7140 [Concurrency] Fix copy-paste error.
The `__builtin_add_overflow` should have been adding to `leeway`, not to
`deadline`.

rdar://150290165
2025-04-30 10:19:04 +01:00
Konrad `ktoso` Malawski
ec7ed9ac39 [6.2][Concurrency] Hashable funcs should be inlinable for AsyncStream (#81127)
Small review followup for https://github.com/swiftlang/swift/pull/81064/

The impls should be @inlinable.

Original PR: https://github.com/swiftlang/swift/pull/81126
Radar: rdar://149914179
2025-04-30 00:58:01 -07:00
Konrad 'ktoso' Malawski
25413d2ef4 [embedded][Concurrency] Further refine C-api boundary for isIsolating... 2025-04-29 22:29:54 +09:00
Konrad 'ktoso' Malawski
a63356ed0d [Concurrency] adjust how we fail creating an IsIsolatingCurrentContextDecision 2025-04-28 20:26:17 +09:00
Konrad 'ktoso' Malawski
eaf0b15ea3 [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-28 19:17:58 +09:00
Max Desiatov
c51eaa336a Revert "Revert "Concurrency: Move code between Executor{Bridge,Impl}.cpp"" (#80692)
* Revert "Revert "Concurrency: Move code between `Executor{Bridge,Impl}.cpp`""

* Update CMakeLists.txt
2025-04-28 19:16:32 +09:00
Doug Gregor
7f29a27e3d [Strict memory safety] Update standard library for unsafe treated as a call effect
(cherry picked from commit 050a514588)
2025-04-26 07:45:32 -07:00
Mykola Pokhylets
3fdcb49fb2 Use concrete SwiftStdlib version 2025-04-24 13:17:04 +09:00
Mykola Pokhylets
ee2e86c7fc Added explicit availability attribute for each protocol requirement
To fix failing test/api-digester/stability-concurrency-abi.test
2025-04-24 13:17:00 +09:00
Mykola Pokhylets
d1b3cca1d9 Added Hashable conformance to Async(Throwing)Stream.Continuation 2025-04-24 13:16:54 +09:00
Mike Ash
36d67e990f Merge pull request #80949 from mikeash/function-cast-6.2
[6.2][Runtime] Add function_cast, switch from std::bit_cast.
2025-04-22 16:25:06 -04:00
Mike Ash
b2d6514b63 [6.2][Runtime] Add function_cast, switch from std::bit_cast.
Function types aren't always trivially copyable, e.g. with address-discriminated signed pointers on ARM64e. Introduce a function_cast helper and use that instead.

(cherry picked from commit 185b739cf1)
2025-04-21 12:47:48 -04:00
Doug Gregor
19975bc78f Remove extraneous unsafe from the concurrency library
(cherry picked from commit 1c9875e14a)
2025-04-21 09:03:34 -07:00
Doug Gregor
469b30334c [Strict memory safety] Remove now-extraneous "unsafe" from the standard libraries
Now that we aren't propagating "unsafe" to nested types, remove
unnecessary "unsafe" keywords from the standard library.

(cherry picked from commit fe6856726e)
2025-04-21 09:03:23 -07:00
Eric Miotto
a948f794c9 Merge pull request #80883 from edymtt/emiotto/mark-executorimpl_cpp-optional-6.2
[6.2] CMake Embedded Stdlib: mark `ExecutorImpl.cpp` as optional
2025-04-17 14:37:26 -07:00
Eric Miotto
d542e5c728 [6.2] CMake Embedded Stdlib: mark ExecutorImpl.cpp as optional
This is needed for internal Apple configurations that build the Embedded
Stdlib on its own.

Addresses rdar://149410405

(cherry picked from commit a249e7fd8d)
2025-04-17 06:52:43 -07:00
Konrad `ktoso` Malawski
361ebea8d7 Merge pull request #80797 from ktoso/pick-wip-parent-cancel-must-cancel-group
[6.2][Concurrency] Parent task cancellation must cancel task group itself
2025-04-17 11:33:11 +09:00
Doug Gregor
15ecd6654d Merge pull request #80845 from DougGregor/relative-witness-tables-fixes-6.2
[6.2] Relative witness table fixes
2025-04-16 17:37:51 -07:00
Doug Gregor
2de64b666f [Runtime] Handle relative witness tables in _getIsolationCheckingOptionsFromExecutorWitnessTable
Part of rdar://148687148.
2025-04-15 18:15:06 -07:00
Konrad 'ktoso' Malawski
29b8e271e1 [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:44:00 +09:00
Konrad 'ktoso' Malawski
059e5dd15e [Concurrency] Parent task cancellation must cancel task group itself.
Seems that during refactorings of child cancellations we somehow missed
also cancelling the group itself. It seems we did not have good test
coverage of the addTaskUnlessCancelled somehow and thus this slipped
through.

This adds a regression test for addTaskUnlessCancelled and fixes how we
handle the cancellation effect in TaskStatus.

resolves #80789
resolves rdar://149177600
2025-04-14 17:43:55 +09:00
Alastair Houghton
55ec37253e [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-14 09:28:50 +01:00
Alastair Houghton
670be7df63 [Concurrency] Remove -executor-factory option and replace with magic type.
We decided that using a magic typealias to set the executor factory was better
than using a compiler option. Remove the `-executor-factory` option, and replace
by looking up the `DefaultExecutorFactory` type, first in the main module, and
then if that fails in Concurrency.

rdar://149058236
2025-04-14 09:23:03 +01:00
Mike Ash
7edc799b16 [6.2][RemoteMirror] Fix AsyncTask child iteration.
Iterating child tasks depends on knowing the size of AsyncTask, and changing the size of the task broke it. Instead of relying on mirroring the full structure in our out-of-process definitions, add a debug variable to libswift_Concurrency that contains the size of AsyncTask.

While we're there, add some more validation to child task enumeration. Check each child task's metadata pointer to make sure that it actually points to the AsyncTask metadata, and have the inner loop also increment and check ChildTaskLoopCount to stop runaway iteration in that loop.

rdar://148836760

(cherry picked from commit e3057031da)
2025-04-08 15:23:05 -04:00
Doug Gregor
06c0efbce6 [stdlib] Reinstate AllowUnsafeAttribute experimental feature on libraries
We need this so that older compilers can handle the .swiftinterface
files we generate. It's unnecessary for newer compilers and can be
removed later.

Fixes rdar://148529962.

(cherry picked from commit 0100104ff7)
2025-04-04 15:18:25 -07:00
Artem Chikin
69b4ea3469 Merge pull request #80421 from swiftlang/revert-80246-playing-with-pointy-pointers
Revert "Concurrency: remove workaround for silencing UB"
2025-04-03 16:12:53 -07:00
Alastair Houghton
a9ed11ca96 [Concurrency] Fix race condition in _runAsyncMain.
As of the custom main/global executor changes, there is a race in
`_runAsyncMain()` to construct the main executor; if this goes the wrong way,
the IRGen async tests, which use this function, can fail.

Fix by explicitly constructing a task and enqueing it on the main executor,
instead of detaching a task and trying to hop to it.

rdar://148506256
2025-04-03 12:07:52 +01:00
Alastair Houghton
941991b9e1 Merge pull request #80439 from al45tair/eng/PR-148337712
[Concurrency] Use the correct Dispatch clock.
2025-04-02 16:11:37 +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
Alastair Houghton
5b5f486a31 [Concurrency] Use the correct Dispatch clock.
This caused a hard-to-reproduce bug where a machine that had slept for
a long time would take a very long time to run some of the tests because
we'd be scheduling Dispatch executions with the wrong clock.

rdar://148337712
2025-04-01 16:07:12 +01:00
Kuba (Brecka) Mracek
a1cdd336f0 Merge pull request #80420 from kubamracek/embedded-drop-mach-apple
[embedded] Stop using -D__APPLE__ and -D__MACH__
2025-04-01 06:25:44 -07:00