Skip to content
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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

gottesmm
Copy link
Contributor

[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.

@gottesmm gottesmm requested review from rjmccall, a team and ktoso as code owners October 15, 2024 17:21
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm
Copy link
Contributor Author

@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.
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm
Copy link
Contributor Author

I ripped out the backwards compatibility stuff. There is more to it and we may need some more compiler functionality or a compatibility library.

@gottesmm
Copy link
Contributor Author

but this is just for a prototype, so it makes sense to just get it done.

Copy link
Contributor

@ktoso ktoso left a 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,
Copy link
Contributor

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)

Suggested change
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));
Copy link
Contributor

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);
}
}
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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?

Copy link
Contributor

@ktoso ktoso left a 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.

@ktoso ktoso self-requested a review October 16, 2024 00:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants