-
Notifications
You must be signed in to change notification settings - Fork 69
Add a way to mark things that can only be done on the main thread #27
Copy link
Copy link
Closed
Labels
enhancementNew feature or requestNew feature or request
Milestone
Description
Many things on NSApplication (e.g. -run, ...) require being done on the main thread; this is something we should clearly communicate in the type system!
See the winit code for more examples of where it's required (NSWindow, ...).
See also the following links:
- Main Thread Only APIs on OS X
- Thread Safety Summary
- Are the Cocoa Frameworks Thread Safe?
- Technical Note TN2028 - Threading Architectures
- Thread Management
Idea:
// No lifetime information needed; the main thread is static and available through the entire program!
// And even if it wasn't, the marker can't be passed to another thread
struct MainThreadMarker {
_priv: (),
}
impl !Send for MainThreadMarker {}
impl !Sync for MainThreadMarker {}
impl MainThreadMarker {
fn new() -> Option<Self> {
if is_main_thread!() {
Some(Self {
_priv: ()
})
} else {
None
}
}
fn new_unchecked() -> Self {
...
}
}
// This is valid to clone because it's still `!Send` and `!Sync`.
impl Clone for MainThreadMarker {...}
impl Copy for MainThreadMarker {}
// Usage
impl NSWindow {
fn do_thing(_mt: MainThreadMarker) {
// This action requires the main thread, so we take one as a parameter.
// It signals clearly to users "hey, this requires the main thread"
}
}
impl dispatch::Queue {
fn run_on_main_async(f: impl FnOnce(mt: MainThreadMarker) + Send) {}
// fn run_on_main_sync ...
}
Queue::run_on_main_async(|mt| {
ns_window.do_thing(mt);
})Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or request