Commit Graph

1607 Commits

Author SHA1 Message Date
Saleem Abdulrasool
c5e99e453f Concurrency: include missing header
Include the new header for placement new.
2022-04-14 06:52:25 -07:00
Karoy Lorentey
0aadfb0444 [stdlib] Add Clock.Duration as an associated type requirement
As [discussed on the forum][forum], we'll likely want to use
`Duration` as the primary associated type on `protocol Clock`;
however, that protocol currently only has `Instant`.

[forum]: https://forums.swift.org/t/pitch-primary-associated-types-in-the-standard-library/56426/39

To support declaring it as the primary associated type,
`protocol Clock` needs to have `Duration` as an associated type
requirement:

```
@available(SwiftStdlib 5.7, *)
public protocol Clock: Sendable {
  associatedtype Duration: DurationProtocol
  associatedtype Instant: InstantProtocol where Instant.Duration == Duration

  var now: Instant { get }
  var minimumResolution: Instant.Duration { get }

  func sleep(until deadline: Instant, tolerance: Instant.Duration?) async throws
}
```

This setup is reminiscent of `Sequence.Element` vs.
`Sequence.Iterator.Element`.

SE-0329 has not yet shipped in an ABI stable release, so we still have
the opportunity to address this.

rdar://91591545
2022-04-11 15:15:26 -07:00
冀卓疌
0fbf9b9e2c [gardening] typo "beforet" → "before" 2022-04-10 15:40:56 +08:00
Mike Ash
43996706ae Merge pull request #42219 from mikeash/weak-actors
[Concurrency] Fix ObjC weak references to actors.
2022-04-08 14:24:27 -04:00
Yuta Saito
3c805eefed Merge pull request #41487 from kateinoigakukun/katei/fix-asynclet-get-sigs-upstream
Concurrency: fix inconsistent _asyncLet_get signatures
2022-04-08 18:53:00 +09: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
8a0079df3a Update TaskPrivate.h 2022-04-05 17:12:42 -07: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
3405691582
301a406bf9 [stdlib] Buildfix OpenBSD by adding clock types. (#41857)
Reading SE-0329 and this module, two clock types are broadly defined,
"continuous", which advances during suspension, and "suspending", which
does not.

SE-0329 notes the behavior of the clock types on "Darwin and BSD" is
that `CLOCK_MONOTONIC` is "continuous" and `CLOCK_UPTIME` is
"suspending". This is also true for OpenBSD (see
https://man.openbsd.org/clock_gettime).

Update code to match. This buildfixes this platform.
2022-03-30 10:00:10 -07:00
Yuta Saito
30b9fc90a8 Concurrency: fix inconsistent _asyncLet_get signatures 2022-03-30 20:58:40 +09:00
Konrad `ktoso` Malawski
84347e5d54 [Concurrency] Avoid redundant conformance warning on Actor
Since the `Actor` type is now `Sendable` via `AnyActor`:

```
/Users/ktoso/code/swift-project/swift/stdlib/public/Concurrency/Actor.swift:38:34: warning: redundant conformance constraint 'Self' : 'Sendable'
public protocol Actor: AnyActor, Sendable {
                                 ^
/Users/ktoso/code/swift-project/swift/stdlib/public/Concurrency/Actor.swift:38:24: note: conformance constraint 'Self' : 'Sendable' implied here
public protocol Actor: AnyActor, Sendable {
                       ^
```
2022-03-30 12:43:17 +09: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
Evan Wilde
90b279bc66 Merge pull request #41933 from etcwilde/ewilde/concurrency-reland-runasyncmain-cleanups
Cleaning up _runAsyncMain
2022-03-21 16:59:34 -07:00
Evan Wilde
9ab7a822df Cleaning up _runAsyncMain a bit
I'm making two cleanups here. First, the closure going into
`_runAsyncMain` needs to be `@Sendable` or passing it to the task is not
safe. This will also result in a warning being emitted.
Second, I'm making this @usableFromInline and `internal`. This function
is around for legacy reasons, but it's part of the ABI, so we can't pull
it out entirely, but we don't want folks using it.
2022-03-21 13:02:20 -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
a64b75e6c3 Merge pull request #41808 from mikeash/more-signposts
[Concurrency] Decode actor/task flags in signposts, make task_wait an interval.
2022-03-17 11:02:45 -04:00
Mike Ash
9fd97d5b52 Merge pull request #41676 from mikeash/swift-inspect-concurrency-flags
[swift-inspect][RemoteMirror] Decode job/task/actor flags.
2022-03-16 10:21:40 -04:00
Doug Gregor
41971284fa Fix variable name 2022-03-15 15:18:00 -07:00
Doug Gregor
83d9b68b8e Separate out Duration-based APIs so they aren't in the back-deployment libs
The back-deployed Swift Concurrency library should not contain anything
based on clocks or durations, which aren't always available in the
underlying system. Move that functionality to separate files that are
excluded from the back-deployed concurrency libraries.

This is a partial step to better approximate the back-deployment
libraries. At some point, we'll stop building the back-deployment
libraries entirely and instead use the binaries provided by the
toolchain.

Fixes rdar://89237163.
2022-03-15 15:14:41 -07:00
Konrad `ktoso` Malawski
591f7ed9d5 [Concurrency] Fix typo in internal variable name
deadine -> deadline
2022-03-15 12:04:36 +09:00
Mike Ash
5fd87a8ee3 [swift-inspect][RemoteMirror] Decode job/task/actor flags.
Have RemoteMirror internally decode these flags fields and return them as separate fields in the task/actor info. Handle the structures both with and without task escalation support.

Also show when a task is the current task on a thread in swift-inspect's task listing.

rdar://88598003
2022-03-14 16:20:05 -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
Alastair Houghton
0d87a1084d Merge pull request #41746 from al45tair/eng/PR-89787540
Add a build option for the concurrency tracing feature.
2022-03-10 10:29:00 +00:00
Mike Ash
d1b60c8ca6 Merge pull request #41670 from mikeash/signpost-task-flags-changed
[Concurrency] Use separate signpost name for task_flags_changed.
2022-03-09 12:45:47 -05:00
Alastair Houghton
f204f0634b Add a build option for the concurrency tracing feature.
Concurrency tracing was enabled depending on the presence of a header
file, but if a similarly named header was found somewhere in the path
that didn't do the same thing, that might cause problems.

Instead, we should have a build time configuration option.

rdar://89787540
2022-03-09 12:53:10 +00:00
Konrad `ktoso` Malawski
033b8bf0bd Merge pull request #41722 from ktoso/wip-move-anyactor 2022-03-09 07:09:37 +09:00
Philippe Hausler
d5f63ee638 Enable Sendability for AsyncStream and AsyncThrowingStream (#41713)
* Enable Sendability for AsyncStream and AsyncThrowingStream

* Move removeFirst to hit all cases where the continuations are not empty
2022-03-08 13:14:26 -08:00
Konrad `ktoso` Malawski
040a4a318c [Concurrency][Distributed] Move AnyActor to Concurrency module 2022-03-08 15:28:30 +09: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
Hamish Knight
a2cfbda850 Revert "Cleaning up _runAsyncMain a bit" 2022-03-05 13:14:36 +00:00
Evan Wilde
1ab41bf955 Merge pull request #40661 from etcwilde/ewilde/concurrency/cleanup-run-async-main
Cleaning up _runAsyncMain a bit
2022-03-04 21:25:18 -08:00
Philippe Hausler
b0f0cb257d Audit and correct AsyncSequence Sendability (#41253)
* Audit and correct AsyncSequence Sendability

* Annotate availability on Sendable conformances for AsyncSequences

* Fix typo of Sendable.Iterator

* Correct tests to use sendable builders

* Add @preconcurrency annotations and fixup one remaining missing @Sendable case

* Move preconcurrency to correct syntactical position

* Use unchecked Sendable conditional conformance instead of marking all internals as preconcurrency and sendable

* Use unchecked Sendable conditional conformance instead of marking all internals as preconcurrency and sendable for dropWhile

* claim ABI changes for marking of @preconcurrency as expected
2022-03-04 21:21:32 -08:00
Allan Shortlidge
1be8913c9b NFC: Fix a number of warnings emitted when building swift-frontend. 2022-03-04 16:15:33 -08:00
Mike Ash
837586f575 [Concurrency] Use separate signpost name for task_flags_changed. 2022-03-04 11:19:07 -05:00
Chris Adamson
87d0885088 AsyncStream: Explicit code-block fence around doc code snippet. (#41407)
* Explicit code-block fence around doc code snippet.

* Doc snippet: remove unnecessary indentation.

Co-authored-by: Chris Adamson <cadamson@apple.com>
2022-03-02 09:06:26 -05:00
John McCall
aca744b211 Remove the Flags field from AsyncContext.
Generated code has never actually initialized this field, so we
might as well remove it.  Doing so mostly doesn't impact the ABI
since we don't store anything for arguments or results in the
context as part of the normal call sequence.  We do need to adjust
some of the hard-coded contexts, however, such as continuation
contexts and the statically-sized context for special runtime
async functions.
2022-02-25 16:57:49 -05: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
John McCall
313165d3f3 In debug builds, assert that tasks are complete during deallocation. 2022-02-24 03:16:43 -05:00
Karoy Lorentey
d384a796eb Merge pull request #41512 from lorentey/clock-fixes
[stdlib] Some more clock adjustments
2022-02-22 18:13:35 -08:00
Rokhini Prabhu
d59866fb27 Merge pull request #41513 from apple/rokhinip/89236911-compare-exchange-weak
Account for the fact that compare_exchange_weak can fail sometimes.
2022-02-22 17:22:22 -08: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
Karoy Lorentey
3680ea6c87 [stdlib] swift_get_clock_res: call clock_getres on Darwin, not gettime 2022-02-22 11:00:11 -08:00
Karoy Lorentey
e0a411ab52 [stdlib] Standard clocks: Fix .now and .minimumResolution implementations
These used to set the clock id incorrectly, leading to zero results. Fix that and also make the underlying entry point abort if it gets an invalid ID, preventing this from reoccurring.
2022-02-19 18:43:32 -08:00
Evan Wilde
b6414b383b Merge branch 'main' into ewilde/concurrency/cleanup-run-async-main 2022-02-17 16:08:17 -08:00
Philippe Hausler
e675b310f8 [SE-0329] Clock/Instant/Duration (#40609)
* [WIP] Initial draft at v2 Clock/Instant/Duration

* Ensure the literal types for _DoubleWide are able to be at least 64 bits on 32 bit platforms

* static cast timespec members to long

* Remove runtime exports from clock functions

* Export clock functions in implementations as they are in headers

* Clean up internal properties by adding leading underscores, refine availability to a TBD marker macro, and break at 80 lines to match style

* Shift operators to concrete Instant types to avoid complexity in solver resolution

* Adjust diagnostic note and error expectation of ambiguities to reflect new potential solver (perhaps incorrect) solutions

* Update stdlib/public/Concurrency/TaskSleep.swift

Co-authored-by: Karoy Lorentey <klorentey@apple.com>

* [stdlib][NFC] Remove trailing whitespace

* [stdlib] Remove _DoubleWidth from stdlib's ABI

* [stdlib] Strip downd _DoubleWidth to _[U]Int128

* Additional adjustments to diagnostic notes and errors expectation of ambiguities to reflect new potential solver (perhaps incorrect) solutions

* Disable type checker performance validation for operator overload inferences (rdar://33958047)

* Decorate Duration, DurationProtocol, Instant and clocks with @available(SwiftStdlib 9999, *)

* Restore diagnostic ambiguity test assertion (due to availability)

* Add a rough attempt at implementing time accessors on win32

* Remove unused clock id, rename SPI for swift clock ids and correct a few more missing availabilities

* remove obsolete case of realtime clock for dispatch after callout

* Use the default implementation of ~ for Int128 and UInt128

* Ensure diagnostic ambiguitiy applies evenly to all platforms and their resolved types

* Restore the simd vector build modifications (merge damage)

* Update to latest naming results for Instant.Duration

* Updates to latest proposal initializers and accessors and adjust encoding/decoding to string based serialization

* Update availability for Clock/Instant/Duration methods and types to be 5.7

* Correct *Clock.now to report via the correct runtime API

* Ensure the hashing of Duration is based upon the attoseconds hashing

* Avoid string based encoding and resort back to high and low bit encoding/decoding but as unkeyed

* Adjust naming of component initializer to use suffixes on parameters

* Duration decoding should use a mutable container for decoding

* fix up components initializer and decode access

* Add platform base initializers for timespec and tiemval to and from Duration

* Add some first draft documentation for standard library types Duration, DurationProtocol and InstantProtocol

* Another round of documentation prose and some drive-by availability fixes

* InstantProtocol availability should be 5.7

* Correct linux timeval creation to be Int and not Int32

Co-authored-by: Karoy Lorentey <klorentey@apple.com>
2022-02-17 09:32:46 -08:00
Rokhini Prabhu
32e02fafad Merge pull request #41362 from apple/rokhinip/86100521-actor-runtime-escalation
Priority inversion avoidance in Actor Runtime
2022-02-16 17:10:56 -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