Commit Graph

1608 Commits

Author SHA1 Message Date
Konrad `ktoso` Malawski
b135ecd8f6 [TaskGroup] Must detach discarded task, THEN unlock before resume waiting 2023-08-09 08:44:11 +09:00
Konrad `ktoso` Malawski
9d9f8cb143 [DiscardingTaskGroup] Properly detach when LAST task is failed, and prior failure was already stored 2023-08-09 08:44:11 +09:00
Konrad `ktoso` Malawski
eaafe3f79e [TaskGroup] Stop using group.spawn in warning messages 2023-08-08 20:38:57 +09:00
Alex Martini
2676e261fd Fix typo; add missing word. 2023-08-07 11:35:42 -07:00
Konrad `ktoso` Malawski
d01abcb42e Unlock BEFORE resuming the waitAll waiting task, to avoid unlock() on
released memory.

Because the waitAll() group method is called before the group returns
from withTaskGroup and is used to ensure all tasks have completed before
we destroy the task group, this method is then immediately followed by
calling TaskGroup::destroy.

If we, as previously, first resume the waiting task and then attempt to
unlock the lock held by the group, we are in an unsafe situation with
racing the task group destroy() and the unlock().

Therefore, we must release the lock before we resume the waiting task.
2023-08-04 08:46:30 +09:00
Mike Ash
9f9929a1d0 [Concurrency] Fix crash when actor is dynamically subclassed.
Dynamic subclasses have a NULL type descriptor. Make sure isDefaultActorClass doesn't try to dereference that NULL descriptor.

rdar://112223265
2023-08-02 19:05:10 -04:00
Mike Ash
9f70da0726 Merge pull request #67457 from mikeash/task-cancellation-deadlock-docs
[Concurrency] Warn about deadlock potential in withTaskCancellationHandler docs.
2023-07-25 10:09:59 -04:00
Evan Wilde
669285fd17 Merge pull request #65534 from stephank/fix/cmake-3.25
build: fix accidental cmake expansions
2023-07-24 09:44:14 -07:00
Konrad `ktoso` Malawski
244c48974c Merge pull request #67463 from ktoso/wip-executor-docs 2023-07-24 10:46:54 +09:00
Franz Busch
8dda2274bc Replace custom Deque implementation with swift-collection Deque
# Motivation
For AsyncStream we created a custom internal Deque implementation to use for the buffering. This implementation was relatively bare-bones compared to the one in swift-collections. Furthermore, it lacked some methods that we need to implement the new `AsyncStream` APIs that support producer backpressure.

# Modification
This PR copies over the Deque implementation of swift-collections and makes it internal and non-inlinable.

# Result
We now have a fully functional Deque.
2023-07-23 08:33:03 +01:00
Konrad `ktoso` Malawski
3b9be36f90 docs: fix old spelling of assert/assume APIs in docs 2023-07-22 09:19:39 +09:00
Mike Ash
1c86195349 [Concurrency] Warn about deadlock potential in withTaskCancellationHandler docs.
withTaskCancellationHandler gets called with the task status record lock held, so care needs to be taken if the cancellation handler acquires any locks of its own. Update the docs comment to describe this.

rdar://111686260
2023-07-21 16:31:18 -04:00
Stéphan Kochen
7b460ce495 build: fix accidental cmake expansions
As of CMake 3.25, there are now global variables `LINUX=1`, `ANDROID=1`,
etc. These conflict with expressions that used these names as unquoted
strings in positions where CMake accepts 'variable|string', for example:

- `if(sdk STREQUAL LINUX)` would fail, because `LINUX` is now defined and
  expands to 1, where it would previously coerce to a string.

- `if(${sdk} STREQUAL "LINUX")` would fail if `sdk=LINUX`, because the
  left-hand side expands twice.

In this patch, I looked for a number of patterns to fix up, sometimes a
little defensively:

- Quoted right-hand side of `STREQUAL` where I was confident it was
  intended to be a string literal.

- Removed manual variable expansion on left-hand side of `STREQUAL`,
  `MATCHES` and `IN_LIST` where I was confident it was unintended.

Fixes #65028.
2023-07-17 21:50:50 +02:00
Konrad `ktoso` Malawski
6bfef94c9e [docs][Concurrency] Include required parameter in group docs
[docs][Concurrency] Fix typo in method signature in code snippets of groups
2023-06-30 12:17:57 +09:00
swift-ci
578ed42ba2 Merge pull request #66544 from kateinoigakukun/pr-30e1f2f61604200ebbd6a0a47877b209ab785d86
[Concurrency] Fix calling convention mismatch in AsyncStream
2023-06-29 03:04:15 -07:00
Konrad `ktoso` Malawski
0a2defed98 [Concurrency] Fix code snippet on task group docs
Code snippet was missing required of: parameter.
2023-06-24 10:59:13 +09:00
Alastair Houghton
4f4d0147ca Merge pull request #66721 from al45tair/eng/PR-110665213
[Threading][TSan] Fix TSan errors from lazy init on Linux.
2023-06-22 20:03:00 +01:00
Dimitri Bouniol
1544c39989 Added missing documentation to TaskLocal 2023-06-19 14:51:01 -07:00
Alastair Houghton
ee5efff5b1 [Threading][TSan] Update after review comments.
* Use the longer name ThreadSanitizer rather than TSan for the new files.
* Don't implement `tsan::consume` at all for now.
* Do the `tsan::release` for `ulock_unlock()` at the head of the function,
  not at the tail.
* Add a comment to test/Sanitizers/tsan/once.swift to explain the test a
  little more clearly.

rdar://110665213
2023-06-19 15:32:50 +01:00
Alastair Houghton
41f46ec085 [Threading][TSan] Fix TSan errors from lazy init on Linux.
Move the TSan functionality from Concurrency into Threading.  Use it
in the Linux `ulock` implementation so that TSan knows about `ulock`
and will tolerate the newer `swift_once` implementation that uses it.

rdar://110665213
2023-06-19 15:32:50 +01:00
Yuta Saito
97ac3985e2 [Concurrency] Fix signature mismatch of _startTaskOnMainActor
The function is defined in Task.cpp with a void return type, but
referenced in Task.swift with an pointer return type.
2023-06-11 12:29:11 +00:00
Yuta Saito
00705b4b2a [Concurrency] Fix calling convention mismatch in AsyncStream
Functions defined in AsyncStream.cpp are called from Swift with swiftcc
but are defined with the C calling convention.
2023-06-11 12:26:52 +00:00
Yuta Saito
0ceab44032 [DiscardingTG] Fix runtime signature mismatch for statically known -> Void closure
Since https://github.com/apple/swift/pull/65613, DiscardingTG started to
accept `() -> Void` instead of `() -> T`, but it also changed the number
of arguments accepted by the closure from 3 to 2. So it should use
`non_future_adapter` instead of `future_adapter` to avoid runtime signature
mismatch.
2023-05-30 18:15:12 +00:00
swift-ci
9698128a36 Merge pull request #65613 from ktoso/wip-discarding-error-task-leak
[DiscardingTG] Undo pointer auth workaround; fix memory leaks
2023-05-25 19:06:48 -07:00
Mike Ash
dfd313cd6c [Runtime] Fast lookups of Concurrency types with standard manglings.
Have the Concurrency runtime register a struct containing pointers to all of its type descriptors with standard manglings. Then have the runtime use that struct to quickly look up those types, instead of using the standard, slower type lookup code.

rdar://109783861
2023-05-24 16:08:23 -04:00
Konrad `ktoso` Malawski
760cb44987 remove extra debug statements 2023-05-24 18:59:18 +02:00
Konrad `ktoso` Malawski
d82de55ae4 less code duplication 2023-05-24 18:46:57 +02:00
Konrad `ktoso` Malawski
9f5a707a30 back to println tests 2023-05-24 18:03:46 +02:00
Konrad `ktoso` Malawski
cad608eb81 [Discarding] Don't leak retained "first error" task when retaining it 2023-05-24 18:03:28 +02:00
Doug Gregor
3f0d61cc5d [Concurrency runtime] Don't read from the actor after transitioning state
Once we have transitioned the actor into a new state, we report the
state change as a trace event so it can be noted by tools (e.g.,
Instruments). However, the act of transitioning to a new state can mean
that there is an opportunity for another thread to deallocate the
actor. This means that the tracing call cannot depend on dereferencing
the actor pointer.

A refactoring a few months ago to move the bit that indicates when a
distributed actor is remote from inside the atomic actor state out to a
separate field (because it's constant for a given actor instance),
which introduced a dereference of the actor instance in forming the
tracing call. This introduced a narrow window in which a race
condition could occur: the actor transitions to an idle state, and is
then deallocate before the trace event for the actor transition occurs,
leading to a use-after-free.

Fetch this bit of information earlier in the process, before any state
changes and when we know the actor is still allocated, and pass it
through to the tracing code.

Fixes rdar://108497870.
2023-05-18 15:57:27 -07:00
Konrad `ktoso` Malawski
da6f08a24e [Concurrency] Adopt stable keyword consuming instead of __owned
add test for warnings emitted when missing consuming attribute

use -emit-sil in mock SDK tests for better coverage
2023-05-11 15:18:54 +02:00
Allan Shortlidge
1bf94248a0 Merge pull request #65777 from tshortli/clean-up-back-deployed-concurrency-apis
Concurrency: Revert workarounds for `@backDeployed` condfails
2023-05-09 08:53:25 -07:00
Konrad `ktoso` Malawski
ca2987cd83 [Concurrency] Easier to handle deprecation of Job and friends 2023-05-09 14:38:15 +09:00
Allan Shortlidge
860b8cdf48 Concurrency: Revert workarounds for @backDeployed condfails.
All compilers that must be able to compile the standard library's textual
interface accept the following:
- back deployed functions declared without `@available`
- `@inlinable` back deployed functions
- `defer` blocks in back deployed functions

We can therefore revert the workarounds added in
https://github.com/apple/swift/pull/62946/ and
https://github.com/apple/swift/pull/62928/.

Resolves rdar://104085810
2023-05-08 18:21:08 -07:00
Evan Wilde
08c4646a0a Merge pull request #65719 from etcwilde/ewilde/discarding-task-group-availability
Fix DiscardingTaskGroup availability
2023-05-07 18:15:02 -07:00
Konrad `ktoso` Malawski
c7fedd4fb9 [Executors] Fix delegation chain of Excecutor.enqueue for Job specifically 2023-05-06 10:50:24 +09:00
Evan Wilde
c6f551c683 Fix discarding task-group availability
Discarding task groups aren't available in the 5.8-aligned OS's,
resulting in a loader error at launch if you try to use them.

Bumping the availability attributes to 5.9 on everything in
DiscardingTaskGroup.swift
2023-05-05 15:01:43 -07:00
Kavon Farvardin
8511317a97 Replace additional uses of noncopyable metatypes. 2023-05-04 11:44:27 -07:00
Kavon Farvardin
103bd043cd Revert "Revert "Prevent noncopyable metatypes from being converted to Any""
This reverts commit 1a3b42cd63.
2023-05-04 11:38:00 -07:00
Hamish Knight
1a3b42cd63 Revert "Prevent noncopyable metatypes from being converted to Any" 2023-05-04 12:10:10 +01:00
Kavon Farvardin
1df3912794 Merge pull request #65606 from kavon/noncopyable-metatypes
Prevent noncopyable metatypes from being converted to `Any`
2023-05-03 22:05:17 -07:00
Konrad `ktoso` Malawski
0bdb39f51e Merge pull request #65455 from ktoso/wip-bincompat-job-rename 2023-05-04 07:34:45 +09:00
Kavon Farvardin
4710fc675b Prevent noncopyable metatypes from being converted to Any
These metatypes are a gateway to more incorrect
uses of these noncopyable values because we don't
yet have the corresponding runtime support yet.
The other use cases of using metatypes of
noncopyable types in generics is not high enough to
warrant people using them yet.

resolves rdar://106452518
2023-05-03 10:23:11 -07:00
Allan Shortlidge
c0ee3ac866 Concurrency: Make _Concurrency module interface parsable by older compilers.
Revert a few unnecessary changes have been made to the _Concurrency module
recently that make its swiftinterface un-parseable by older compilers that we
need to support.

- The `consume` keyword is not understood by older compilers, so including it
  in inlinable code is a problem. It has no actual effect on lifetimes where it
  was used, so just omit it.
- Don't build the _Concurrency module with -enable-experimental-feature
  MoveOnly. The MoveOnly feature is now enabled by default in recent compilers,
  so the flag is superfluous when building the dylib. When emitting the
  interface, having the feature enabled explicitly exposes code guarded by
  `$MoveOnly` to older compilers that do not accept all of the code.

Resolves rdar://108793606
2023-05-02 15:27:05 -07:00
Konrad `ktoso` Malawski
8d6980ec10 [Executors] Make move to ExecutorJob binary compatible; deprecate Job 2023-05-02 14:44:11 +09:00
Konrad `ktoso` Malawski
fe90d44e4c [Executors] Remove accidentally added availability on asUnownedSerialExecutor (#65407) 2023-04-26 06:14:12 +09:00
nate-chandler
d20b8d26b8 Merge pull request #65274 from nate-chandler/rdar107275872
[Concurrency] Nest stack traffic in withValue.
2023-04-19 11:52:41 -07:00
Evan Wilde
b8396e6449 Merge pull request #65271 from etcwilde/ewilde/fix-startOnMainActor
Fix main executor check in `startOnMainActor`
2023-04-19 07:49:18 -07:00
Nate Chandler
4fe988b7c2 [Concurrency] Nest stack traffic in withValue.
Because `_taskLocalValuePush` and `_taskLocalValuePop` can result in calls
to `swift_task_alloc` and `swift_task_dealloc` respectively, and because
the compiler hasn't been taught about that (e.g.
`SILInstruction::isAllocatingStack`,
`SILInstruction::isDeallocatingStack`, etc), calling them (push and pop)
from a function which makes use the stack for dynamically sized
allocations can result in violations of stack discipline of the form

```
swift_task_alloc // allocates %ptr_1
copy_value_witness // copies into %ptr_1
swift_task_localValuePush // calls swift_task_alloc and allocates %ptr_2
swift_task_dealloc // deallocates %ptr_1
swift_task_localValuePop // calls swift_task_dealloc and deallocates %ptr_2
```

Avoid the problem by not allocating dynamically sized stack space in the
function which calls `_taskLocalValuePush` and `_taskLocalValuePop`.
Split the calls to those functions into `withValueImpl` function which
takes its argument `__owned`.  Call that function from `withValue`,
ensuring that the necessary copy (to account for the fact that withValue
takes its argument `__guaranteed` but `_taskLocalValuePush` takes its
`__owned`) and associated stack traffic occur in `withValue`.

Still, allow `withValueImpl` to be inlined.  The stack nesting will be
preserved across it.

rdar://107275872
2023-04-18 19:51:34 -07:00
Evan Wilde
ad81f8d557 Allow discarding startOnMainActor results
Adding discardableResult to startOnMainActor
2023-04-18 16:40:54 -07:00