Commit Graph

240 Commits

Author SHA1 Message Date
Mykola Pokhylets
1b69068105 Keep current task executor when taking fast path in isolated deinit 2024-07-11 13:09:08 +02:00
Mykola Pokhylets
ab4d338f6a Added flags to be able to control task-locals behaviour in the future 2024-07-11 13:09:08 +02:00
Mykola Pokhylets
35f0334eb6 Rename performOnExecutor into deinitOnExecutor.
It cannot be used for executing general-purpose work, because such function would need to have a different signature to pass isolated actor instance.

And being explicit about using this method only for deinit allows to use object pointer for comparison with executor identity.
2024-07-11 13:09:07 +02:00
Mykola Pokhylets
a043ebf97a Don't switch threads when running isolated deinit of the default actor. 2024-07-11 13:09:07 +02:00
Mykola Pokhylets
411cdb7f73 Added unit-tests for (not)copying task-local values. Fixed release after deallocation. 2024-07-11 13:09:06 +02:00
Alastair Houghton
b6e7a5107a Merge pull request #75024 from Sajjon/cyon_typo_batch__stdlib
Typos: `stdlib`
2024-07-08 10:57:54 +01:00
Konrad `ktoso` Malawski
5ad2291ac0 [Concurrency] Fix build in actors as locks configuration (#75004) 2024-07-07 10:37:33 +09:00
Alexander Cyon
c21b1e68fd [stdlib] Fix typos 2024-07-06 13:09:57 +02:00
Konrad `ktoso` Malawski
b9369bf0b7 [Concurrency] Fix default actor isolation issue in face of TaskExecutor preference (#74658)
* [Concurrency] Fix task excutor handling of default actor isolation

The task executor API did not properly account for taking the default
actor locking into account when running code on it, we just took the job
and ran it without checking with the serial executor at all, which
resulted in potential concurrent executions inside the actor --
violating actor isolation.

Here we change the TaskExecutor enqueue API to accept the "target"
serial executor, which in practice will be either generic or a specific
default actor, and coordinate with it when we perform a
runSynchronously.

The SE proposal needs to be amended to showcase this new API, however
without this change we are introducing races so we must do this before
the API is stable.

* Remove _swift_task_enqueueOnTaskExecutor as we don't use it anymore

* no need for the new protocol requirement

* remove the enqueue(_ job: UnownedJob, isolatedTo unownedSerialExecutor: UnownedSerialExecutor)

Thankfully we dont need it after all

* Don't add swift_defaultActor_enqueue_withTaskExecutor and centralize the task executor getting to enqueue()

* move around extern definitions
2024-07-01 16:42:27 +09:00
Rokhini Prabhu
1f466c2312 Merge pull request #70603 from swiftlang/rokhinip/clear-scheduler-private-fields-executor
Clear out the SchedulerPrivate fields before enqueueing the task
2024-06-27 09:52:31 -07:00
Konrad `ktoso` Malawski
a43abe6a90 [Concurrency] Remove OPTNONE workaround for old clang version
This reverts c07aa9c425 which was done to
avoid a crash in optimnized caused by this PR:
https://github.com/apple/swift/pull/41088

Since this was almost 2 years ago, we probably don't have this issue
anymore as far as I can see other resolved issues, so try to remove the
workaround.

Resolves rdar://88711954
2024-06-20 20:03:33 +09:00
Konrad `ktoso` Malawski
083fdaa0de Merge pull request #73998 from nickolas-pohilets/mpokhylets/fix-32-build 2024-06-04 13:49:19 +09:00
Konrad `ktoso` Malawski
0c44645832 [Concurrency] Implement defensive copying in task groups, rather than crashing (#73978) 2024-05-31 11:27:03 -07:00
Mykola Pokhylets
987b641a7c Updated HeaderFooterLayout to correctly handle empty Header or Footer 2024-05-30 21:56:28 +02:00
Mykola Pokhylets
a891b8f0fb Move isDistributedRemoteActor outside #if 2024-05-30 21:55:58 +02:00
Mykola Pokhylets
b5e039d1e0 Fixed 32-bit build with SWIFT_CONCURRENCY_ENABLE_PRIORITY_ESCALATION 2024-05-30 13:15:39 +02:00
Mykola Pokhylets
6298d41edf Reapply "Fix quadratic performance of the ListMerger in specific usage pattern"
This reverts commit 2640ff613b.
2024-05-30 13:08:42 +02:00
Konrad `ktoso` Malawski
7c0e1f599a [Concurrency] Fix embedded, cannot refer to runtime/env vars from Actor.cpp 2024-05-28 18:19:55 +09:00
Konrad `ktoso` Malawski
153806b71a [Concurrency] Reshape initial values of executor checking so tests pass on Linux 2024-05-27 17:02:26 +09:00
Konrad `ktoso` Malawski
d36f99540c [Concurrency] Always assume that if expecting main actor and are on main thread that this is equal 2024-05-27 17:02:26 +09:00
Konrad `ktoso` Malawski
7790609fc8 [Concurrency] Futher prevent crashes in legacy mode of isCurrentExecutor 2024-05-27 17:02:26 +09:00
Konrad `ktoso` Malawski
481c928d67 [Concurrency] Fix missing return in legacy isSameExecutor mode detection
resolves rdar://128425368
resolves rdar://127400013
2024-05-27 17:02:25 +09:00
Andrew Trick
2640ff613b Revert "Fix quadratic performance of the ListMerger in specific usage pattern" 2024-05-21 16:23:57 -07:00
Mykola Pokhylets
9ba09ff63d Process incoming queue when obtaining drainer lock 2024-05-10 11:05:22 +02:00
Mykola Pokhylets
40c38f9803 Using multiple insertion points to ensure all jobs are always inserted in O(1)
Fully separated unprocessed jobs and processed jobs
Reverse jobs after updating status to minimise contention
2024-05-10 11:05:22 +02:00
Mykola Pokhylets
21a70e10c7 Fixed quadratic performance of ListMerger when each executed job creates 2+ new jobs of the same priority
See https://forums.swift.org/t/quadratic-performance-of-the-listmerger-in-specific-use-case/69393
2024-05-10 11:05:22 +02:00
Konrad `ktoso` Malawski
b2bd17635f [Concurrency] Fix too eager early return in checkIsolation mode detecting (#73495) 2024-05-09 03:12:37 -07:00
Konrad `ktoso` Malawski
86f5441294 [SerialExecutor] SerialExecutor.checkIsolated() to check its own tracking for isolation checks (#71172) 2024-03-29 07:06:34 +09:00
John McCall
b0cee67d04 Properly save and restore the current task in the runtime so that tasks
can be reentrantly executed.

I don't think doing this is *actually a good idea*, but corrupting the
runtime is an even worse idea, and the overhead here is very low.
2024-03-15 00:40:54 -04:00
Pavel Yaskevich
87878461a5 [stdlib] Make expected actor isolation checking crash by default
Only affects projects compiled with `-enable-actor-data-race-checks` flag.
2024-02-02 23:53:18 -08:00
Konrad `ktoso` Malawski
bd19d937f0 [TaskExecutor] Remove underscore from new APIs (#71145) 2024-01-30 06:27:54 +09:00
Allan Shortlidge
79d0ecafaa NFC: Ignore deprecation of asl_log in the runtime and demangling library.
ASL is deprecated in macOS 10.12. It may be time to transition to os_log now
that deployment targets have been raised to 10.12, but until that project
starts these warnings are just pollution.

Filed rdar://121066531 to track adoption of `os_log()` if appropriate.
2024-01-16 13:27:55 -08:00
Rokhini Prabhu
61bec0d786 Always clear out the SchedulerPrivate fields before enqueueing the task
into the executor
2023-12-21 19:22:05 -08:00
Rokhini Prabhu
ed8ed32dba Clean up Concurrency build system so that all the logic around choice of
which executor for which type of setting, is consolidated and we have a
single knob we use to determine when to use dispatch as our global
executor.

Radar-Id: rdar://problem/119416196
2023-12-13 16:09:51 -08:00
Konrad `ktoso` Malawski
828f589be4 Initial Task Executor implementation Task(on:), addTask(on:) etc. (#68793)
Co-authored-by: John McCall <rjmccall@gmail.com>
2023-12-12 17:14:24 +09:00
Konrad `ktoso` Malawski
5546f4da7b Rename ExecutorRef to SerialExecutorRef 2023-11-01 18:39:44 +09:00
Kuba Mracek
5d8c55eacb [embedded] Initial Swift Concurrency for embedded Swift 2023-10-06 20:04:03 -07:00
Allan Shortlidge
b4f3a9fb06 NFC: Address warnings about unused functions. 2023-09-07 18:09:19 -07: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
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
0586c14b60 [Concurrency] SerialExecutor.isSameExclusiveExecutionContext (#64604) 2023-03-28 15:56:28 +09:00
Konrad `ktoso` Malawski
e0877594da [Concurrency] Custom executors with move-only Job (#63569) 2023-03-18 14:02:43 +09:00
Konrad `ktoso` Malawski
5d2a311dda [Executors] assumeOnLocalDistributedActorExecutor (isolated Act) -> 2023-03-16 13:23:47 +09:00
Konrad `ktoso` Malawski
41f99fc2ae [Executors][Distributed] custom executors for distributed actor (#64237)
* [Executors][Distributed] custom executors for distributed actor

* harden ordering guarantees of synthesised fields

* the issue was that a non-default actor must implement the is remote check differently

* NonDefaultDistributedActor to complete support and remote flag handling

* invoke nonDefaultDistributedActorInitialize when necessary in SILGen

* refactor inline assertion into method

* cleanup

* [Executors][Distributed] Update module version for NonDefaultDistributedActor

* Minor docs cleanup

* we solved those fixme's

* add mangling test for non-def-dist-actor
2023-03-15 23:42:55 +09:00
Kavon Farvardin
c100fe01bc guard use of enqueueStealer when using actors-as-locks
skip escalation in this case.
2023-03-08 14:55:36 -08:00
Rokhini Prabhu
0bb5a89d56 Fixup isEnqueued call on actor state which really is isScheduled
Radar-Id: rdar://problem/101864092
2023-03-03 11:44:56 -08:00
Rokhini Prabhu
9b6c873239 Escalate an actor if a job in the queue of the actor has been escalated.
Radar-Id: rdar://problem/101864092
2023-02-28 13:52:26 -08:00
Rokhini Prabhu
4515654a62 Remove redundant call to swift_dispatch_lock_override_end() since that
is only needed for acknowledging asynchronous overrides from other
threads, onto the thread itself. It is not needed for self-overrides.

Radar-Id: rdar://problem/101864092
2023-02-28 13:51:46 -08:00
Rokhini Prabhu
d63ce1372c Change memory barriers in actor runtime so that we have acquire upon
lock() and release on unlock() and use consume when are relying on
address dependency HW load ordering. Adjust tsan annotations to match.

Radar-Id: rdar://problem/101864092
2023-02-28 13:51:46 -08:00
Rokhini Prabhu
534811c3dc When a task gets enqueued on an executor, start tracking which executor
it is enqueued on. This way, we have the necessary bookkeeping to
escalate an executor when a task that is enqueued, is escalated.

Radar-Id: rdar://problem/101864092
2023-02-25 17:01:03 -08:00