Skip to content

Commit

Permalink
Context switch from enqueue_event() to get_event() directly
Browse files Browse the repository at this point in the history
  • Loading branch information
willglynn committed Jul 17, 2017
1 parent c9be7ef commit 1442d3a
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 11 deletions.
13 changes: 9 additions & 4 deletions src/platform/macos/events_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,15 @@ impl Shared {
pub fn enqueue_event(&self, event: Event) {
self.pending_events.lock().unwrap().push_back(event);

// attempt to wake the runloop
self.timer.trigger();
unsafe {
runloop::CFRunLoopWakeUp(runloop::CFRunLoopGetMain());
// attempt to hop back
if unsafe { super::send_event::try_resume(1) } {
// success!
} else {
// attempt to wake the runloop
self.timer.trigger();
unsafe {
runloop::CFRunLoopWakeUp(runloop::CFRunLoopGetMain());
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/platform/macos/send_event.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use cocoa;
use cocoa::appkit::{NSApp,NSApplication};

pub unsafe fn try_resume(value: usize) -> bool {
false
}

// The `SendEvent` struct encapsulates the idea of calling [NSApp sendEvent:event].
// This is a separate struct because, in the case of resize events, dispatching an event can enter
// an internal runloop, and we don't want to get stuck there.
Expand Down
2 changes: 2 additions & 0 deletions src/platform/macos/send_event_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ unsafe fn resume(value: usize) {
}
}

// Attempt to hop back to the "normal" stack frame, yielding `value`. Returns false if we're not
// inside a coroutine.
pub unsafe fn try_resume(value: usize) -> bool {
if let Some(context) = INNER_CONTEXT.with(|c| { c.take() }) {
// resume it, getting a new context
Expand Down
8 changes: 1 addition & 7 deletions src/platform/macos/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,13 @@ pub struct Timer {
timer: CFRunLoopTimerRef,
}

#[cfg(feature="context")]
extern "C" fn timer_callback(_timer: CFRunLoopTimerRef, _info: *mut c_void) {
// attempt to yield back to the caller
use super::send_event_context::try_resume;
unsafe {
try_resume(1);
super::send_event::try_resume(1);
}
}

#[cfg(not(feature="context"))]
extern "C" fn timer_callback(timer: CFRunLoopTimerRef, info: *mut c_void) {
// nothing to do
}

impl Timer {
pub fn new() -> Timer {
Expand Down

0 comments on commit 1442d3a

Please sign in to comment.