Commit Graph

1685 Commits

Author SHA1 Message Date
Mike Ash
716f2bc0b7 [Concurrency] Emit async task running/waitingOn metadata inline into CPUTrace.
This allows tracking async code execution throughout a task's lifetime.

rdar://137230240
2025-10-10 17:46:22 +01:00
Eric Miotto
c9345a38c4 [Concurrency] Add availability to one of the ExecutorJob extensions
Without this, we may fail building Concurrency when enforcing strict
availability.

Addresses rdar://159473855
2025-08-29 15:09:46 -07:00
Eric Miotto
173c5ff2a4 [Concurrency] Change availability for a TaskPriority extension
We need the `init?(JobPriority)` constructor to be
`StdlibDeploymentTarget 5.9`
so the compiler will not complain when we reference it
 from `ExecutorJob.createTrampoline` when building the standard library
without strict availability.

Addresses rdar://159397287
2025-08-28 15:34:10 -07:00
Alastair Houghton
d6224d16d9 [Concurrency] Fix minimal stdlib build again.
Some mentions of `ExecutorJob` were not guarded by the required
`SWIFT_STDLIB_TASK_TO_THREAD_MODEL_CONCURRENCY` condition.
2025-08-26 09:38:14 +01:00
Alastair Houghton
ff8d2fa80c [Concurrency] Support continuous waits in CooperativeExecutor.
This fixes some WASM tests.
2025-08-26 09:38:14 +01:00
Alastair Houghton
bd27a14ea0 [Concurrency] Fix cooperative executor to return only after all jobs run.
We were terminating after the first set of jobs; if one of them scheduled
another job, and there were no timers running, we would terminate,
which was wrong.
2025-08-26 09:38:13 +01:00
Alastair Houghton
d0b3d14c68 [Concurrency] Don't expose Dispatch or CF executors directly.
Also add `final` to the `CooperativeExecutor` declaration.
2025-08-26 09:38:13 +01:00
Alastair Houghton
250458f9f6 [Concurrency] Add a couple of comments.
Add some documentation comments to the Dispatch and CF executors,
and update the comments for the allocation and private data APIs.
2025-08-26 09:38:13 +01:00
Alastair Houghton
0fbd382e9c [Concurrency] Fix CooperativeExecutor to not loop forever.
If a job enqueues another job on the executor, we might never leave
the inner `while` loop in the `run()` method.  Fix this by taking
the contents of the run queue and only running those jobs in the
queue at the time we enter the inner loop.
2025-08-26 09:38:13 +01:00
Alastair Houghton
871a062002 [Concurrency] Remove canonicalization code.
I don't think we actually need this.  If you have a non-canonical
(i.e. derived) clock, you can just implement `enqueue` and/or `run`
and call those methods on the clock you're wrapping.
2025-08-26 09:38:13 +01:00
Alastair Houghton
7af5b8d8fd [Concurrency] Add implementations of run and enqueue for built-in clocks.
The built-in clocks should have implementations of `run` and `enqueue`,
to allow derived clocks to call those implementations.
2025-08-26 09:38:13 +01:00
Alastair Houghton
6046286b58 [Concurrency] Updates after second SE pitch.
We no longer attempt to convert timestamps from the passed-in `Clock`
in order to allow any clock to work with any executor.  Instead,
executors that do not recognise a clock should call the `enqueue`
function on that `Clock`, which lets the `Clock` itself decide how
to proceed.

Additionally, rename `SchedulableExecutor` to `SchedulingExecutor`.
2025-08-26 09:38:11 +01:00
Gábor Horváth
35b5c817b2 Merge pull request #83520 from Xazax-hun/check-safety-function-types 2025-08-08 06:39:39 +01:00
Gabor Horvath
402ad33463 [StrictMemorySafety] Check the safety of return types of calls
Previously, we skipped checking the return type of a function for safety
as we expected to warn at the use of the returned value:

  let x = returnsUnsafe()
  usesUnsafe(x) // warn here

Unfortunately, this resulted in missing some unsafe constructs that can
introduce memory safety issues when the use of the return value had a
different shape resulting in false negatives for cases like:

  return returnsUnsafe()

or

  usesUnsafe(returnsUnsafe())

This PR changes the analysis to always take return types of function
calls into account.

rdar://157237301
2025-08-05 12:16:44 +01:00
Alastair Houghton
94058e5e68 [Concurrency] Fix availability problem with UnownedTaskExecutor.
a550080 added a call to the new initializer, but that call
came from a function annotated with `StdlibDeploymentTarget 6.2`,
rather than `SwiftStdlib 6.2`, while the initializer was set to
`SwiftStdlib 6.2`.  This will fail in some build configurations
(specifically where the target being built for is older than that
implied by `SwiftStdlib 6.2`).

rdar://157217460
2025-08-04 14:30:20 +01:00
Max Desiatov
78c85945f6 Actor.cpp: Fix unused variables for non-embedded path 2025-07-31 21:16:40 +01:00
Max Desiatov
7ec0837778 Embedded WASI: fix concurrency-deleted-method.swift test
The test was crashing due to `swift_unreachable("custom executors not supported in embedded Swift")` line in `swift_task_enqueueImpl`, as the corresponding non-embedded codepath was relying on an unspecialized generic function `_swift_task_enqueueOnExecutor` defined in `Executor.swift`. Unspecialized generics are unavailable in Embedded Swift, and such `@silgen_name` function can't be specialized when used from concurrency runtime code written in C/C++. We can redefine this function for Embedded Swift as using a class-bound existential instead, and re-enable this codepath with a slightly different call that avoids the use of unavailable `swift_getObjectType` function from the non-embedded runtime.
2025-07-31 16:00:40 +01:00
Alastair Houghton
a55008066d [Concurrency] Further improve performance.
Remove some reference counting traffic by using `Unowned*Executor`s.

Also, add a test to make sure we stay on the fast path.

rdar://156701386
2025-07-30 13:41:04 +01:00
Alastair Houghton
2b4082e4f9 [Concurrency] Fix task switch performance issue.
When using the new custom default executors, sometimes we end up
taking a long time to do a task switch.  This is happening because
the path the new code takes sometimes results in a concrete pointer
to the default global executor being in the executor tracking
information in `swift_task_switch()`, and if we try to switch to
a `nil` task executor (which _also_ means the default global executor),
we aren’t spotting that and we’re taking the slow path.

Essentially, we want to take the fast path in cases where we switch
from `nil` to the concrete default global executor and vice-versa.

rdar://156701386
2025-07-28 15:49:46 +01:00
Alastair Houghton
d0cf535664 Merge pull request #83330 from al45tair/eng/PR-156710569
[Concurrency] Update availability of UnimplementedExecutor.
2025-07-28 12:42:12 +01:00
Pavel Yaskevich
95d4ef5756 [Concurrency] Remove deprecated Task.startSynchronously API
This is the original spelling which was not accepted. We kept
it for a bit to give adopters time to switch but it's time to
remove it now.
2025-07-25 13:15:02 -07:00
Alastair Houghton
f27e20a3e7 [Concurrency] Update availability of UnimplementedExecutor.
We need it to be `StdlibDeploymentTarget 6.2` rather than
`SwiftStdlib 6.2`, so that we can update the availability definitions.

rdar://156710569
2025-07-25 17:02:48 +01:00
Max Desiatov
31e92db5b2 Executor.swift: only enable MainActor in Embedded Swift for WASI 2025-07-24 16:03:06 +01:00
Max Desiatov
5c5358b5df Enable more MainActor code paths for Embedded Swift 2025-07-24 14:58:21 +01:00
Justice Adams
d454771ee2 [CMAKE] Fix layering issue between Concurrency and Darwin overlay (#82973)
Concurrency from the Core project is importing the Darwin platform
overlay, which in turn depends on SwiftCore from the Core project,
breaking the project layering.

Concurrency only needs the Clang module, but Swift does not have a
mechanism to only import a clang module. For now import the
functionality needed from Darwin by importing and wrapping the
associated functions from `<dlfcn.h>` within `CFExecutor.cpp`

Also remove Darwin import from `AsyncStreamBuffer.swift` because it is
not used
2025-07-23 16:04:14 -07:00
Max Desiatov
9cdfc28117 Embedded: exclude ExecutorImpl.swift for non-Wasm platforms 2025-07-21 15:57:22 +01:00
Max Desiatov
05f3f1ffc5 Embedded: only include ExecutorImpl.cpp for Wasm 2025-07-21 15:52:59 +01:00
Max Desiatov
5150a6b1eb Only enable MainActor for WASI with Embedded Swift 2025-07-21 15:42:15 +01:00
Max Desiatov
959e75bac0 Enable MainActor in embedded concurrency, add ExecutorImpl.cpp
`ExecutorImpl.cpp` should be moved from `SWIFT_RUNTIME_CONCURRENCY_NONEMBEDDED_C_SOURCES` to `SWIFT_RUNTIME_CONCURRENCY_C_SOURCES`. This way we can also include `ExecutorImpl.swift` and use `PlatformExecutorCooperative.swift` in embedded concurrency for WASI.
2025-07-21 15:21:54 +01:00
Evan Wilde
48fb7cd874 Merge pull request #83087 from etcwilde/ewilde/op-precedence
Fix warning in DispatchGlobalExecutor
2025-07-17 00:41:02 -07:00
Yuta Saito
af85e061dd Merge pull request #83086 from kateinoigakukun/yt/fix-isolated-conformance-static-link 2025-07-17 08:35:52 +09:00
Konrad `ktoso` Malawski
197530d264 Merge pull request #82943 from ktoso/wip-remove-task-startsynchronously-symbol 2025-07-17 06:48:40 +09:00
Yuta Saito
30e0fa5133 [Concurrency] Fix runtime isolated-conformance checks with static stdlib
Most of linkers pull object files from static archives only if any
symbol from that object file is referenced, even if the object contains
a ctor code. `Setup.cpp` didn't have any symbols referenced from
other code, so it was not linked in when the concurrency runtime was
linked in statically. This commit moves the ctor code to `Task.cpp`
to ensure that it is always linked in.
2025-07-16 06:28:04 +00:00
Evan Wilde
38c3ac1d22 Fix warning in DispatchGlobalExecutor
Fixing "warning: '&&' within '||'" -Wlogical-op-parentheses warning
2025-07-15 20:00:32 -07:00
Saleem Abdulrasool
326f18d113 Runtimes: detect the existence of dispatch_async_swift_job
This function is used by the executor to help schedule the async job.
While not currently available on Windows on main, this allows correctly
handling the presence of the symbol for static linking.
2025-07-11 11:28:07 -07:00
Konrad `ktoso` Malawski
47db5ade76 [Concurrency] Align Task.isolated with proposal (add task executor) (#82913) 2025-07-11 08:42:19 +09:00
Saleem Abdulrasool
817922175d Merge pull request #82932 from compnerd/static-flags
utils: build static runtime with static dispatch and closure
2025-07-10 14:46:09 -07:00
Konrad 'ktoso' Malawski
5996da0c0c [Concurrency] Remove symbols of startSynchronously, make it AEIC
In preparation for removal. This spelling of Task.immediate was before
SE-review and was NOT accepted and therefore we should try to remove it
entirely.
2025-07-10 17:34:29 +09:00
Konrad 'ktoso' Malawski
664be9bd1e [Concurrency] Add missing Task.immediateDetached, which drops task locals 2025-07-10 14:57:44 +09:00
Konrad 'ktoso' Malawski
358c3e9dcf [Concurrency] a few missing overloads for immediate tasks 2025-07-10 12:40:41 +09:00
Saleem Abdulrasool
8b74c09b8f utils: build static runtime with static dispatch and closure
Although we would like these to be dynamic, we currently cannot build
them properly due to limitations of the build system. Adjust the build
to pass in additional flags to properly build the runtimes for that
environment as a temporary measure.
2025-07-09 14:41:59 -07:00
Pavel Yaskevich
72267bb1d4 Merge pull request #82892 from swiftlang/revert-82833-remove-startSynchronously
Revert "[Concurrency] Remove deprecated `Task.startSynchronously` API"
2025-07-09 00:19:08 -07:00
Alex Martini
74c3dbcd24 Revise doc comment for style & clarity
- Move the definition of 'structured concurrency' to the beginning of
  the section.
- Avoid future tense.  Fix some mixed future+present tense errors.
- Write bullet points as individual sentences, not as the clauses of one
  long sentence.
- Apple Style: Use 'since' only in the sense of 'after'.
- Apple Style: Use 'you' instead of 'one'.
- Apple Style: Close up 'non-' except when ambiguous.
- DevPubs style: Avoid bare 'this' for clarity.  Here, 'this guarantee'
  tells the reader what 'this' refers to.
- DevPubs style: Avoid 'may' which can be ambiguous between possibility
  and permission.
2025-07-08 14:31:29 -07:00
Pavel Yaskevich
8c4f7e97c8 Revert "[Concurrency] Remove deprecated Task.startSynchronously API" 2025-07-08 13:54:23 -07:00
Alex Martini
51b2033eeb Fix more 'cancel(l)ed' style issues 2025-07-08 10:50:43 -07:00
Alex Martini
4a7ac28bcb Remove stray spaces
These looks like the side effect of a find-and-replace for PR 82558
fixing 'cancelled' vs 'canceled'.
2025-07-08 10:45:36 -07:00
Changhyun Kang
bf61199e88 [Concurrency] fix documentation typos in TaskGroup.swift (#82291) 2025-07-08 21:02:19 +09:00
Konrad `ktoso` Malawski
fe86091cc1 Merge pull request #82558 from ktoso/wip-cleanup-group-docs 2025-07-08 01:06:51 -07:00
Pavel Yaskevich
2b05dc630b Merge pull request #82833 from xedin/remove-startSynchronously
[Concurrency] Remove deprecated `Task.startSynchronously` API
2025-07-07 16:47:15 -07:00
Alastair Houghton
79f746e62d [Concurrency] Don't use ExecutorJob for task-to-thread model.
Task-to-thread model doesn't have `ExecutorJob`, so we can't use it;
however, we probably also don't need `_swift_createJobForTestingOnly`
there so just `#if` it out in that case.

rdar://155054460
2025-07-07 11:43:53 +01:00