Commit Graph

231 Commits

Author SHA1 Message Date
Rokhini Prabhu
cb6ffe831b In task-to-thread model, make sure that when we are waiting on an
async-let task, we run it in the context of the parent task.
2022-09-01 10:02:25 -07:00
Konrad `ktoso` Malawski
53168355f0 [Distributed] Explicitly ban a class extending AnyActor as well 2022-06-22 09:21:55 +09:00
Alastair Houghton
eb4c81d60e [Threading] Don't use TLS keys as template arguments.
There's no guarantee that e.g. pthread_key_t is an integral type.  It could
be some kind of struct, or some other thing that isn't valid as a template
argument.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
0e9318cec5 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-06-07 07:39:53 +01:00
Alastair Houghton
e305a7dcf6 [Concurrency][Windows] Add a couple of includes for Windows.
Actor.cpp does need <io.h> still, and Task.cpp should have been including
<windows.h>

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
14a4bd45b6 [Concurrency][Threading] Remove use of platform thread functions.
The concurrency library can use the new threading library functions,
which avoids the problem of including <windows.h>.

rdar://90776105
2022-06-07 07:39:52 +01:00
Alastair Houghton
f5bdb858e0 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alastair Houghton
0cf687aa2b [Build][Runtime] Replace SWIFT_STDLIB_SINGLE_THREADED_RUNTIME.
SWIFT_STDLIB_SINGLE_THREADED_RUNTIME is too much of a blunt instrument here.
It covers both the Concurrency runtime and the rest of the runtime, but we'd
like to be able to have e.g. a single-threaded Concurrency runtime while
the rest of the runtime is still thread safe (for instance).

So: rename it to SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and make it just
control the Concurrency runtime, then add a SWIFT_STDLIB_THREADING_PACKAGE
setting at the CMake/build-script level, which defines
SWIFT_STDLIB_THREADING_xxx where xxx depends on the chosen threading package.

This is especially useful on systems where there may be a choice of threading
package that you could use.

rdar://90776105
2022-06-07 07:39:51 +01:00
Alex Hoppen
4aa2bbbf06 Revert "Merge pull request #42447 from al45tair/eng/PR-90776105"
This reverts commit 8bcb71140f, reversing
changes made to c4dd271d36.
2022-06-02 18:03:23 +02:00
Alastair Houghton
69f0b03a73 [Threading] Don't use TLS keys as template arguments.
There's no guarantee that e.g. pthread_key_t is an integral type.  It could
be some kind of struct, or some other thing that isn't valid as a template
argument.

rdar://90776105
2022-05-24 14:57:42 +01:00
Alastair Houghton
b5bd267ff1 [Threading] Put everything through git clang-format.
Just formatting changes.

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
1fc36cf1f9 [Concurrency][Windows] Add a couple of includes for Windows.
Actor.cpp does need <io.h> still, and Task.cpp should have been including
<windows.h>

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
c034d6c3da [Concurrency][Threading] Remove use of platform thread functions.
The concurrency library can use the new threading library functions,
which avoids the problem of including <windows.h>.

rdar://90776105
2022-05-24 14:57:41 +01:00
Alastair Houghton
63a09007a1 [Threading] Create new threading library and use it.
Moved all the threading code to one place.  Added explicit support for
Darwin, Linux, Pthreads, C11 threads and Win32 threads, including new
implementations of Once for Linux, Pthreads, C11 and Win32.

rdar://90776105
2022-05-24 14:57:39 +01:00
Alastair Houghton
dadcb04ae2 [Build][Runtime] Replace SWIFT_STDLIB_SINGLE_THREADED_RUNTIME.
SWIFT_STDLIB_SINGLE_THREADED_RUNTIME is too much of a blunt instrument here.
It covers both the Concurrency runtime and the rest of the runtime, but we'd
like to be able to have e.g. a single-threaded Concurrency runtime while
the rest of the runtime is still thread safe (for instance).

So: rename it to SWIFT_STDLIB_SINGLE_THREADED_CONCURRENCY and make it just
control the Concurrency runtime, then add a SWIFT_STDLIB_THREADING_PACKAGE
setting at the CMake/build-script level, which defines
SWIFT_STDLIB_THREADING_xxx where xxx depends on the chosen threading package.

This is especially useful on systems where there may be a choice of threading
package that you could use.

rdar://90776105
2022-05-24 14:57:38 +01:00
Jonathan Grynspan
b749dd395d Revert "runtime: allow over-aligned types in the runtime"
This reverts commit b694ce4634.
2022-05-05 11:10:12 -04:00
Mike Ash
e5c61b3945 [Concurrency] Minor tweaks to continuation, task, and job tracing.
Change continuation signposts to emit an interval for init/resume.

Fix task_create to take the decoded flags as separate parameters, matching other calls.

Move job_run trace calls into runJobInEstablishedExecutorContext. swift_job_runImpl didn't catch everything.

rdar://92149411
2022-04-22 15:50:10 -04:00
Josh Soref
7a57e7ce99 Spelling stdlib/public/concurrency (#42443)
* spelling: already

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: appropriate

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: asynchronous

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: cancel

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: divisible

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: execution

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: initialized

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: normally

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: preprocessed

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: priority

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: some

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: success

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: suspending

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: throws

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 11:12:57 -07:00
Rokhini Prabhu
2c2aa05d47 Remove the inline job in actors and make them use OOL jobs always. This is because we need 16 bytes of atomic state in the actor which is not available to us on arm64_32 since the inline job takes up 40 of the 48 bytes for use.
Enable priority escalation support on arm64_32.

Radar-Id: rdar://problem/90725051
2022-04-15 11:30:03 -07:00
Saleem Abdulrasool
a8b0ee24dc runtime: blanket application of namespacing and inclusion of new
Apply a blanket pass of including `new` for the placement new allocation
and namespacing the call to the global placement new allocator.  This
should repair the Android ARMv7 builds.
2022-04-14 14:21:12 -07:00
冀卓疌
0fbf9b9e2c [gardening] typo "beforet" → "before" 2022-04-10 15:40:56 +08:00
Mike Ash
b00397c7eb [Concurrency] Fix ObjC weak references to actors.
Change actor destruction to call swift_deallocClassInstance instead of swift_deallocObject. When ObjC interop is enabled, swift_deallocClassInstance will check the "pure swift deallocation" bit and call into the ObjC runtime to destruct the instance when needed. This is what clears weak references and associated objects.

rdar://91270492
2022-04-06 15:57:01 -04:00
Saleem Abdulrasool
b694ce4634 runtime: allow over-aligned types in the runtime
Not all targets have a 16-byte type alignment guarantee.  For the types
which are not naturally aligned, provide a type specific `operator new`
overload to ensure that we are properly aligning the type on allocation
as we run the risk of under-aligned allocations otherwise.

This should no longer be needed with C++17 and newer which do a two
phase `operator new` lookup preferring
`operator new(std::size, std::align_val_t)` if needed.  The base type
would be fully pre-processed away.  The empty base class optimization
should help ensure that we do not pay any extra size costs for the
alignment fixes.

As we are a C++14 codebase, we must locally implement some of the
standard type_traits utilities, namely `void_t`.  We take the minimal
definition here, assuming that the compiler is up-to-date with C++14 DR
reports which fixed an issue in SFINAE.  We use the SFINAE for detecting
the presence of the `operator new` overload to guide the over-alignment,
which is inherited through the new `swift::overaligned_type<>`  base
type.

Annotate the known classes which request explicit alignment which is
non-pointer alignment.  This list was identified by
`git grep ' alignas(.*) '`.
2022-04-04 18:26:20 -07:00
Alastair Houghton
3251df866c [Concurrency] Don't use getenv() unless SWIFT_STDLIB_HAS_ENVIRON is set.
We don't necessarily have environment variables, which is why we have
SWIFT_STDLIB_HAS_ENVIRON.  Calling getenv() outside of a check of this
means Concurrency won't work in environments where getenv() cannot
be used.

rdar://91050064
2022-03-31 16:04:31 +01:00
Rokhini Prabhu
cea9de700b Disable Actor priority escalation support on arm64_32 as temporary
workaround while we investigate crashes we're seeing

Radar-Id: rdar://problem/90724683
2022-03-23 14:36:16 -07:00
Mike Ash
ae2b5140ed [RemoteMirror][swift-inspect] Decode locks in priority-escalation concurrency runtime.
When possible, decode the DrainLock/ExecutionLock fields of tasks and actors in concurrency runtimes built with priority escalation, and show the corresponding thread info in swift-inspect output.

We weren't properly decoding actor flags previously, so fix that up as well and have Remote Mirror split them out into separate fields so clients don't have to. We were missing the Job Storage field from the definition of DefaultActorImpl in RuntimeInternals.h, fix that so we actually read the right data.

rdar://88598003
2022-03-18 15:41:16 -04:00
Mike Ash
b555b6083b [Concurrency] Decode actor/task flags in signposts, make task_wait an interval, signpost continuations.
Decode all fields from the various flags values, pass each field as a separate argument to the various signposts. We were just passing the raw value of the flags and requiring the signpost client to decode them, which was ugly and required the client to know details they shouldn't need to know.

Strip ptrauth bits from the task resume function when signposting, when ptrauth is supported.

Add signpost events for continuation init/await/resume events.

We also make task_wait into an interval, rather than a single event. The interval ends when the task resumes. As part of this change, we also skip emitting the interval when the wait completed immediately and the task didn't have to suspend.

While we're in there, clean up a few SWIFT_TASK_DEBUG_LOG lines that emitted warnings when built with the logging enabled.

rdar://88658803
2022-03-14 15:46:09 -04:00
Allan Shortlidge
58fe2e1a05 Merge pull request #41682 from tshortli/fix-warnings
NFC: Fix a number of warnings emitted when building swift-frontend
2022-03-07 15:11:36 -08:00
Allan Shortlidge
a8ff646b6e NFC: Fix unintialized variable warnings and C++20 extension warnings in concurrency related code. 2022-03-07 11:59:08 -08:00
Saleem Abdulrasool
8e0cc413c2 Concurrency: clean up a log message (NFC)
This simply cleans up the logged message when debug tracing is enabled.
2022-03-06 10:17:47 -08:00
John McCall
c70d73cb7e Fix a use-after-free with task tracing.
We cannot access the executed job after it has finished executing:

- If it's a non-task job, it is always invalidated; such jobs are
  self-owning, and they are expected to destroy themselves after
  execution.

- If it's a task, and it completes during execution, it will invalidate
  itself synchronously, e.g. by releasing itself.  At this point, it
  must be assumed that the task memory has been releaed.

- If it's a task, and it hasn't completed during execution, we are
  now racing with whatever event *does* complete the task.

Any information we want to log about the job must be recorded when it
starts to run.

rdar://88817560
2022-02-24 13:58:30 -05:00
Rokhini Prabhu
78a8fc8e64 The asserts in preprocessQueue assume that the only reason we are
preprocessing is due to running into a concurrent enqueuer while
draining. However, sometimes we can go into this code path because
compare_exchange_weak failed spuriously. We need to account the fact
that compare_exchange_weak can fail spuriously. Also add more asserts.

Radar-Id: rdar://problem/89236911
2022-02-22 12:28:31 -08:00
Rokhini Prabhu
ac4fab89f1 Priority escalation support when actors run into contention
Radar-Id: rdar://problem/86100521
2022-02-16 10:22:18 -08:00
Rokhini Prabhu
b318f651c0 Track the identity of the task draining the actor in the
ActiveActorStatus

Radar-Id: rdar://problem/86100521
2022-02-16 10:22:17 -08:00
Rokhini Prabhu
4c736b0507 Create the notion of an ActiveActorStatus which captures the atomic
state of the actor similar to the ActiveTaskStatus. Refactor the default
actor runtime implementation to set us up for priority escalation
support

Radar-Id: rdar://problem/86100521
2022-02-16 10:21:40 -08:00
Rokhini Prabhu
a5fa349f88 Merge pull request #41281 from apple/rokhinip/88600541-task-priority-escalation-arm64_32
Task Escalation support on arm64_32 Apple platforms
2022-02-10 17:02:38 -08:00
Rokhini Prabhu
6beb11ed7d Rename flagAsEnqueuedOnExecutor to flagAsAndEnqueueOnExecutor to reflect
what it actually does.

Radar-Id: rdar://problem/88600541
2022-02-10 11:50:31 -08:00
Rokhini Prabhu
c07aa9c425 PR #41088 triggered a machine verifier failure in optimized mode for
arm64 causing Actor.cpp to fail to compile. Workaround this bug (88700717) for now.
2022-02-09 15:47:49 -08:00
Kuba (Brecka) Mracek
ffb1cacac3 [Concurrency] Avoid using asl_log when not available (#41277) 2022-02-09 09:40:53 -08:00
Rokhini Prabhu
66d4af0b01 Task priority escalation on Apple platforms
A task can be in one of 4 states over its lifetime:

    (a) suspended
    (b) enqueued
    (c) running
    (d) completed

This change provides priority inversion avoidance support if a task gets
escalated when it is in state (a), (c), (d).

Radar-Id: rdar://problem/76127624
2022-02-07 16:34:46 -08:00
Pavel Yaskevich
624b27257a [Distributed] Move some of the swift_distributed_* accessors to a dedicated file
Add a new `DistributedActor.cpp` and move some of the logic related to
swift_distributed_execute_target there to avoid exporting it from Concurrency.
2022-01-24 18:22:37 -08:00
Pavel Yaskevich
59ff689f28 [Distributed] IRGen: Teach distributed thunk accessor to load witness tables (when needed) 2022-01-24 10:18:00 -08:00
Pavel Yaskevich
406af83329 [Distributed] Runtime/IRGen: Allow passing substitutions to distributed accessor
Accept an optional list of substitutions which are required to form
a call to a generic distributed thunk.
2022-01-24 10:18:00 -08:00
Pavel Yaskevich
925b59826d [Distributed] IRGen: Drop result type for accessor parameters 2022-01-24 10:18:00 -08:00
Pavel Yaskevich
e7ceddc564 [Distributed] IRGen: Provide argument/result types to the accessor 2022-01-24 10:17:59 -08:00
Pavel Yaskevich
d54e3294c3 [Distributed] Runtime: Implement a way to access generic env of distributed target 2022-01-24 10:17:59 -08:00
Max Desiatov
372ada0e24 test: add handling for Wasm/WASI (#39519)
This change adds support for WASI in stdlib tests. Some tests that expect a crash to happen had to be disabled, since there's currently no way to observe such crash from a WASI host.
2022-01-12 14:24:50 +00:00
Pavel Yaskevich
680698e099 [Distributed] Fix _executeDistributedFunction resume and result buffer (#40814)
* [Distributed] Adjust interface of `swift_distributed_execute_target`

Since this is a special function, `calleeContext` doesn't point to
a direct parent but instead both parent context (uninitialized)
and resume function are passed as last arguments which means that
`callContext` has to act as an intermediate context in call to accessor.

* [Distributed] Drop optionality from result buffer in `_executeDistributedTarget`

`RawPointer?` is lowered into a two arguments since it's a struct,
to make it easy let's just allocate an empty pointer for `Void` result.

* [Distributed] NFC: Update _remoteCall test-case to check multiple different result types
2022-01-12 23:18:40 +09:00
Mike Ash
e9356928d6 Merge pull request #40070 from mikeash/concurrency-tracing
[Concurrency] Add tracing for major operations in the concurrency runtime.
2022-01-11 14:42:45 -05:00
Saleem Abdulrasool
7972139464 Merge pull request #40740 from compnerd/unsupported
Concurrency: avoid unnecessary dependency on LLVMSupport
2022-01-10 08:31:20 -08:00