-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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
proposal: runtime/mainthread: add mainthread.Do for mediating access to the main thread #70089
Comments
Related Issues and Documentation
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.) |
(I see that earlier versions of this proposal were already discussed at length elsewhere and I did try to catch up on it first, but I apologize if I'm asking a question that's redundant from earlier discussions.) If this function will panic when called from an environment where the Go runtime does not "own" the main thread, is it justified to also offer a function to test whether a call to this function is possible? That could, for example, allow a caller to choose to treat "I'm running in the wrong mode" as an error to be handled gracefully, rather than as an exception to be handled by panicking. package mainthread
// CanDo returns true if and only if a subsequent call to [Do] would not panic.
func CanDo() bool (Another variation of this would be for |
Just so you know, the current merge window closes 21 11, this would be a quick turn around time. There is the option of getting exceptions but theses are rare and usually limited to very low dangerous community impact. |
Does this API mean that if the main package imports a package that calls runtime.LockOSThread in init (for event loop in main thread) , If so, that means we may need to modify existing valid code when using the mainthread package, which I don't think is backward-compatible,see #64777 (comment). |
@qiulaidongfeng I believe your comment is addressed by #64777 (comment). In short, |
@apparentlymart the original proposal says to panic in c-shared/c-archive mode, but I'm not against CanDo or the like. |
Change https://go.dev/cl/628815 mentions this issue: |
Proposal Details
This is #64777 (comment) in proposal form. It is a reduced and compatible variant of #64777 (comment).
I propose to add a new package,
mainthread
, with a single function,Do
, that allows Go programs to execute a function on the main thread.The larger proposal (#64777 (comment)) adds
Yield
andWaiting
to support sharing the main thread in a Go program. However, the Go runtime doesn't always have control over the main thread, most notably in c-shared or c-archive mode on platforms such as Android. In those cases, the platform facility for mediating main thread access are strictly superior tomainthread.Do
. See #64777 (comment) for a detailed analysis and assumptions.In short, I believe it's better to accept this simpler proposal to only allow Go programs access to the main thread when the Go runtime has control over it, and let other cases be handled by platform API.
I hope this can be implemented in Go 1.24.
The text was updated successfully, but these errors were encountered: