-
Notifications
You must be signed in to change notification settings - Fork 10.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[concurrency] Implement swift_task_runOnMainActor. #77028
base: main
Are you sure you want to change the base?
Conversation
@swift-ci smoke test |
0d60b99
to
2364626
Compare
@swift-ci smoke test |
This entrypoint is similar to swift_task_isCurrentExecutor except that it provides an ABI level option flag that enables one to configure its behavior in a backwards deployable manner via the option flag. I used this to expose at the ABI level the ability to check the current executor without crashing on failure, while preserving the current behavior of swift_task_isCurrentExecutor (which crashes on failure). I am going to use this to implement swift_task_runOnMainActor.
This routine takes a synchronous non-throwing main actor isolated closure without a result. If we are dynamically on the main actor, we just run the closure synchronously. Otherwise, we run a new task on the main actor and call the closure on that. This builds on top of the previous commit by using swift_task_isCurrentExecutor_v2 in the implementation of this function. To backwards deploy this function on Darwin, I used some tricks from libdispatch to validate that we are on the main queue.
2364626
to
0404c2e
Compare
@swift-ci smoke test |
I ripped out the backwards compatibility stuff. There is more to it and we may need some more compiler functionality or a compatibility library. |
but this is just for a prototype, so it makes sense to just get it done. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is nicer now, by reusing the existing infra -- now it'd be great if we can get an SPI to make this checking more reliable -- Thanks for reusing the existing infra 👍
|
||
SWIFT_EXPORT_FROM(swift_Concurrency) | ||
SWIFT_CC(swift) | ||
bool swift_task_isCurrentExecutor_v2(SerialExecutorRef executor, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's avoid a _v2 name like this, it'll show up in backtraces in crashing mode and isn't a naming we really do anywhere.
In other APIs where we did add new versions that take flags we name those ...WithFlags
so I'd suggest doing the same here. (e.g. createTaskGroupWithFlags did so)
bool swift_task_isCurrentExecutor_v2(SerialExecutorRef executor, | |
bool swift_task_isCurrentExecutorWithFlags(SerialExecutorRef executor, |
swift_task_checkIsolated(expectedExecutor); // will crash if not same context | ||
|
||
// checkIsolated did not crash, so we are on the right executor, after all! | ||
return true; | ||
} | ||
|
||
assert(checkMode == Legacy_NoCheckIsolated_NonCrashing); | ||
assert(!options.contains(swift_task_is_current_executor_flag::Assert)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
*checkMode = swift_task_is_current_executor_flag( | ||
*checkMode | swift_task_is_current_executor_flag::Assert); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
// we instead just compile a shim in a .c file that actually calls | ||
// swift_task_getCurrent and avoid the collision. | ||
@_silgen_name("getCurrentTaskShim") | ||
func _getCurrentTaskShim() -> UInt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Huh I see that's the problem you talked about, unfortunate but ok I guess
@_silgen_name("swift_task_isCurrentExecutor_v2") | ||
@usableFromInline | ||
internal func _taskIsCurrentExecutor( | ||
executor: Builtin.Executor, flags: UInt64) -> Bool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we put this one immediately next to the existing overload please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer we rename the swift_task_isCurrentExecutor_v2
-> swift_task_isCurrentExecutorWithFlags
though to follow existing conventions, if this were to be merged.
[concurrency] Implement swift_task_runOnMainActor.
This routine takes a synchronous non-throwing main actor isolated closure
without a result. If we are dynamically on the main actor, we just run the
closure synchronously. Otherwise, we run a new task on the main actor and call
the closure on that.
This builds on top of the previous commit by using
swift_task_isCurrentExecutor_v2 in the implementation of this function.
To backwards deploy this function on Darwin, I used some tricks from libdispatch
to validate that we are on the main queue.