Commit Graph

1607 Commits

Author SHA1 Message Date
Saleem Abdulrasool
8040430bc7 Concurrency: silence some unused variable warnings
This cleans up some warnings from the unused variables. The variables
were referenced by `assert` cases only.
2025-02-03 09:22:50 -08:00
Allan Shortlidge
4799f1e295 Concurrency: Suppress some warnings. 2025-02-01 13:52:43 -08:00
Kuba Mracek
aea2bb563c [embedded] Prepare the Embedded Concurrency library for building on armv7m/armv7em 2025-01-30 10:48:09 -08:00
Saleem Abdulrasool
f1b859c9a5 Concurrency: centralise the definition for std::bit_cast
Remove the multiple definitions of `std::bit_cast` into a header. While
this is still not great, it does reduce the duplication. This also
silently works towards reducing a bit of the UB introduced here by
adding an inline namespace for `std` which you are not technically
allowed to use. However, by doing this, we have a clear migration path
away from this once we adopt C++20.
2025-01-28 11:14:15 -08:00
Saleem Abdulrasool
c9df7c57a9 Concurrency: silence some -Wcast-function-type-mismatch warnings
The C++ standard does not guarantee that the code and data pointers are
interchangeable. Recent enhancements to clang now properly identify the
improper `reinterpret_cast` between function types. Silence the warning
by switching to `std::bit_cast`. Unfortunately, this is a C++20 feature
and we are still on C++17. In the case that the compiler doesn't have a
`bit_cast` implementation, fallback with some UB to a local definition.
2025-01-25 10:37:01 -08:00
Mike Ash
29efab6ccc [Concurrency] Fix Task.sleep on values greater than Int64.max.
In the Dispatch implementation, clamp the delay to INT64_MAX. Swift's nanoseconds value is unsigned, but we ultimately use it with dispatch_time, which takes a signed int64_t. Extremely large values get interpreted as negative, which results in not sleeping.

INT64_MAX nanoseconds is about 292 years, so it should be difficult to notice a practical effect from sleeping for less time than requested due to the clamping.

rdar://143278824
2025-01-22 12:28:18 -05:00
Tony Allevato
3bd71c9d62 [NFC] Include <cstdlib> and <cstdint> where necessary.
Our Bazel builds have become more strict about libc++
dependencies recently, so these are required to pick up
declarations of `malloc` and `uint32_t`, respectively.
2025-01-22 07:47:16 -05:00
Doug Gregor
3ab5456eae Mark SerialExecutor.asUnownedSerialExecutor unsafe so conformances don't need to be @unsafe
What we should actually do with SerialExecutor conformances is not yet clear, though.
2024-12-20 23:16:26 -08:00
Doug Gregor
9e09513514 Mark UnownedSerialExecutor as @unsafe 2024-12-20 07:35:35 -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
Mykola Pokhylets
5ac1cba8d1 Handle versioning of the IsolatedDeinit feature 2024-12-12 16:41:02 +09:00
Mike Ash
e727252733 [Concurrency] Fix start of version ranges in install name magic symbols.
The magic symbols specify a version range where clients should reference a @rpath relative path to libswift_Concurrency.dylib instead of the standard absolute path. This version range started at macOS 10.15 and aligned versions, which is the oldest target supported by Concurrency. However, clients that use Concurrency can target earlier OSes as long as they availability-check their use of Concurrency. When targeting something earlier than 10.15, they'd reference the absolute path, then fail to find the back-deployment Concurrency runtime on OS versions that need it.

Fix this by setting the start of the range to macOS 10.9 and aligned, which is the oldest target supported by Swift.

rdar://140476764
2024-12-09 15:57:46 -05:00
Kyle
7c6cc140ad Fix TaskLocal macro link issue 2024-12-04 15:17:10 +08:00
Kyle
a3e8d1068f Fix missing curly brace in TaskLocal documentation 2024-12-04 15:16:42 +08:00
John McCall
d8d70d9aac Add support for creating a task with a self-consuming task function.
Not used yet.
2024-11-15 22:51:57 -05:00
Allan Shortlidge
1ee4924542 Concurrency: Suppress -Wcast-function-type-mismatch warnings. 2024-11-13 09:51:29 -08:00
Allan Shortlidge
3f42ec25f9 Concurrency: Fix warnings about unused functions. 2024-11-13 09:51:29 -08:00
Mike Ash
fcdec961b0 Merge pull request #77306 from swiftlang/fix-getTaskId
[Concurrency] Fix getTaskId.
2024-11-04 20:03:19 -05:00
Allan Shortlidge
f4df1a8e52 Merge pull request #77316 from tshortli/unavailable-in-embedded
Sema: Allow calls to @_unavailableInEmbedded functions in compatible contexts
2024-11-01 17:16:22 -07:00
Allan Shortlidge
686ef29e55 Concurrency: Avoid string interpolation in argument to fatalError().
In Embedded Swift, the `String` type is unavailable. This means that the
overload of `fatalError()` that takes a `String` is also unavailable. Specify a
`StaticString` in the setter for `TaskLocal.projectedValue` to satisfy this
constraint.
2024-11-01 13:03:29 -07:00
Kuba Mracek
d78be484fc [Concurrency] Avoid reinterpret_cast in Concurrency/Task.cpp, fixes crashes in libdispatch 2024-11-01 10:31:58 -07:00
Kuba Mracek
db94dc7d36 [Concurrency] Fix memory leak around mismatched refcounting in Concurrency 2024-10-31 21:43:02 -07:00
Mike Ash
bb09297598 [Concurrency] Fix getTaskId.
We should build a 64-bit value from the two 32-bit Id fields, but we were shifting one field by the other. Coincidentally, this managed to produce the correct value until the ID goes beyond 2^32, but after that it's weird and wrong.
2024-10-30 16:53:48 -04: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
Konrad `ktoso` Malawski
432c9d4549 Merge pull request #77249 from nickolas-pohilets/mpokhylets/stop-task-locals-in-isolated-deinit 2024-10-30 22:21:34 +09: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
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
Mykola Pokhylets
2acf5b6b6b Refactoring of the TaskLocal::Item
Replace NextLinkType with Item::Kind with more clear definition.
Use llvm::PointerIntPair<>.
Use hierarchy of classes instead of optional fields inside Item.
Combined Storage::copyToOnlyOnlyFromCurrentGroup() into Storage::initializeLinkParent().
Also create parent task marker when copying items created inside withTaskGroup().
Removed Storage::peekHeadLinkType().
2024-10-27 12:32:21 +01:00
Kuba Mracek
4c58842f7a [Concurrency] Fix typo (Impll->Impl) in NonDispatchGlobalExecutor.cpp, resolve build failure when using task-to-thread Concurrency model 2024-10-25 13:01:20 -07:00
Michael Gottesman
b1750bad1a [concurrency] Implement swift_task_runOnMainActor.
This routine takes a synchronous non-throwing main actor isolated closure
without a result. If we are dynamically on the main actor, we just run the
closure synchronously. Otherwise, we run a new task on the main actor and call
the closure on that.

This builds on top of the previous commit by using
swift_task_isCurrentExecutorWithFlags in the implementation of this function.

To backwards deploy this function on Darwin, I used some tricks from libdispatch
to validate that we are on the main queue.
2024-10-17 11:17:38 -07: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
Kuba Mracek
2358cc973d [Concurrency] Split out the default cooperative executor into a separate opt-in library 2024-10-16 22:49:22 -07: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
Konrad `ktoso` Malawski
bd554ba524 Merge pull request #76883 from nickolas-pohilets/mpokhylets/move-task-locals-header 2024-10-10 19:38:35 +09:00
Alastair Houghton
a0a77fa401 Merge pull request #76870 from al45tair/eng/PR-137286187
[Concurrency] Remove C++ runtime references from embedded Concurrency.
2024-10-09 12:33:26 +01:00
Allan Shortlidge
0c09c6d778 Concurrency: Fix warnings in the runtime.
Addresses an unused variable warning and an unreachable code warning.
2024-10-08 14:49:17 -07:00
Alex Lorenz
e6461882c8 Merge pull request #76841 from hyp/eng/stdlib-pragma-clang-diag
[stdlib][android] ignore the -Wgnu-offsetof-extensions in ExecutorChe…
2024-10-07 08:04:43 -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
Mykola Pokhylets
cb4c880af1 Move TaskLocal.h out of ABI folder, because it describes implementation details 2024-10-06 14:37:11 +02: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 (Brecka) Mracek
762e7898de Merge pull request #76760 from kubamracek/embedded-asyncstream
[Concurrency] Include more pieces of Concurrency in Embedded (AsyncStream, continuations)
2024-10-02 22:23:17 -07:00
Alex Lorenz
f72ea6526f [stdlib][android] ignore the -Wgnu-offsetof-extensions in ExecutorChecks.cpp
ExecutorChecks.cpp no longer builds for android as -Wgnu -Werror is used for stdlib
2024-10-02 17:31:37 -07:00
Kuba (Brecka) Mracek
4b68c3fa6f Merge pull request #76766 from kubamracek/embedded-dont-avoid-any-actor
[Concurrency] Drop the cloned code for Embedded around 'any Actor'
2024-10-02 10:47:49 -07:00
Kuba Mracek
ed441eb1bf [Concurrency] Include more pieces of Concurrency in Embedded (AsyncStream, continuations)
We have been only including a subset of files and functionality on Embedded Concurrency, let's instead include all the
source files, and have a fine grained opt out on things that don't yet work. Namely, this is still avoiding clocks, task
sleeping and custom executors.

Add a test for AsyncStream and continuations on Embedded Concurrency.
2024-10-02 09:22:36 -07:00