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

Refactor MacOS and provide a coroutine-driven runloop #237

Closed
wants to merge 24 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
78a85d6
Refactor Cocoa event handling
willglynn Jul 12, 2017
bf48df1
Add context::Context-based Cocoa event dispatching
willglynn Jul 16, 2017
278f13d
Add a runloop observer
willglynn Jul 16, 2017
f3d0958
Refactor the MacOS event loop
willglynn Jul 17, 2017
0f95f20
Switch to thread-local context storage
willglynn Jul 17, 2017
87b7e70
Remove unnecessary UserCallback struct
willglynn Jul 17, 2017
b464e5f
Add a timer to assist with waking the runloop
willglynn Jul 17, 2017
ed6a225
Small fixes
willglynn Jul 17, 2017
bf4dcb3
Context switch from enqueue_event() to get_event() directly
willglynn Jul 17, 2017
230e3f7
Spike: runloop refactor
willglynn Jul 18, 2017
b245574
Add a Runloop that runs in a coroutine
willglynn Jul 18, 2017
84e036f
Link enqueue_event() to Runloop::wake()
willglynn Jul 18, 2017
9b97567
Move receive_event_from_cocoa()/forward_event_to_cocoa() to nsevent
willglynn Jul 18, 2017
f777fa3
Hook up a runloop observer and a timer for the runloop coroutine
willglynn Jul 18, 2017
7d02e3d
Minor refactorings to better separate concerns
willglynn Jul 18, 2017
6253368
Ask Travis to test --features context too
willglynn Jul 18, 2017
cf4c6d6
The blocking runloop should wake itself by posting an event
willglynn Jul 23, 2017
9c4b2f8
Clean up unused functions
willglynn Jul 23, 2017
90559d6
Add wakeup latency tool
willglynn Jul 23, 2017
6b0cd65
Remove dead code and mark conditionally-dead code
willglynn Jul 23, 2017
cbe2be9
Replace runloop observer with dispatch
willglynn Aug 3, 2017
51c64f6
Replace the timer with dispatch after_ms()
willglynn Aug 3, 2017
0bb7811
Add doc comments
willglynn Aug 3, 2017
cb5df71
Avoid overlap in guard_against_lengthy_operations()
willglynn Aug 18, 2017
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ script:
- cargo build --verbose
- if [ $TRAVIS_OS_NAME = osx ]; then cargo build --target x86_64-apple-ios --verbose; fi
- cargo test --verbose
- if [ $TRAVIS_OS_NAME = osx ]; then cargo test --features context --verbose; fi

os:
- linux
Expand Down
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ objc = "0.2"
[target.'cfg(target_os = "macos")'.dependencies]
objc = "0.2"
cocoa = "0.9"
dispatch = "0.1"
core-foundation = "0.4"
core-graphics = "0.8"
context = { version = "2.0", optional = true }

[target.'cfg(target_os = "windows")'.dependencies]
winapi = "0.2"
Expand Down
102 changes: 102 additions & 0 deletions examples/wakeup_latency.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
extern crate winit;

use std::thread;
use std::time::{Duration,Instant};
use std::sync::mpsc;
use std::collections::VecDeque;

enum Action {
WakeupSent(Instant),
AwakenedReceived(Instant),
}

fn calculate_latency(rx: mpsc::Receiver<Action>) {
thread::spawn(move || {
let mut wakeups_sent: VecDeque<Instant> = VecDeque::new();
let mut awakeneds_received: VecDeque<Instant> = VecDeque::new();

let mut latency_history: Vec<u64> = Vec::with_capacity(1000);

println!("wakeup() -> Event::Awakened latency (all times in µs)");
println!("mean\tmax\t99%\t95%\t50%\t5%\t1%\tmin");

while let Ok(action) = rx.recv() {
match action {
Action::WakeupSent(instant) => wakeups_sent.push_back(instant),
Action::AwakenedReceived(instant) => awakeneds_received.push_back(instant),
}

while wakeups_sent.len() > 0 && awakeneds_received.len() > 0 {
let sent = wakeups_sent.pop_front().unwrap();
let recvd = awakeneds_received.pop_front().unwrap();
if recvd > sent {
let latency = recvd.duration_since(sent);
let latency_us = latency.as_secs() * 1_000_000
+ (latency.subsec_nanos() / 1_000) as u64;
latency_history.push(latency_us);
}
}

if latency_history.len() > 300 {
latency_history.sort();

{
let mean = latency_history.iter()
.fold(0u64, |acc,&u| acc + u) / latency_history.len() as u64;
let max = latency_history.last().unwrap();
let pct99 = latency_history.get(latency_history.len() * 99 / 100).unwrap();
let pct95 = latency_history.get(latency_history.len() * 95 / 100).unwrap();
let pct50 = latency_history.get(latency_history.len() * 50 / 100).unwrap();
let pct5 = latency_history.get(latency_history.len() * 5 / 100).unwrap();
let pct1 = latency_history.get(latency_history.len() * 1 / 100).unwrap();
let min = latency_history.first().unwrap();
println!("{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}", mean, max, pct99, pct95, pct50, pct5, pct1, min);
}

latency_history.clear();
}
}
});
}

fn send_wakeups(tx: mpsc::Sender<Action>, proxy: winit::EventsLoopProxy) {
thread::spawn(move || {
loop {
let sent_at = Instant::now();
proxy.wakeup().expect("wakeup");
tx.send(Action::WakeupSent(sent_at)).unwrap();

thread::sleep(Duration::from_secs(1) / 60);
}
});
}

fn main() {
let mut events_loop = winit::EventsLoop::new();

let _window = winit::WindowBuilder::new()
.with_title("A fantastic window!")
.build(&events_loop)
.unwrap();

let (tx,rx) = mpsc::channel::<Action>();

calculate_latency(rx);
send_wakeups(tx.clone(), events_loop.create_proxy());

events_loop.run_forever(|event| {
match event {
winit::Event::Awakened { .. } => {
// got awakened
tx.send(Action::AwakenedReceived(Instant::now())).unwrap();

winit::ControlFlow::Continue
}

winit::Event::WindowEvent { event: winit::WindowEvent::Closed, .. } => {
winit::ControlFlow::Break
},
_ => winit::ControlFlow::Continue,
}
});
}
5 changes: 5 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,17 @@ extern crate cocoa;
extern crate core_foundation;
#[cfg(target_os = "macos")]
extern crate core_graphics;
#[cfg(target_os = "macos")]
extern crate dispatch;
#[cfg(any(target_os = "linux", target_os = "dragonfly", target_os = "freebsd", target_os = "openbsd"))]
extern crate x11_dl;
#[cfg(any(target_os = "linux", target_os = "freebsd", target_os = "dragonfly", target_os = "openbsd"))]
#[macro_use]
extern crate wayland_client;

#[cfg(feature="context")]
extern crate context;

pub use events::*;
pub use window::{AvailableMonitorsIter, MonitorId, get_available_monitors, get_primary_monitor};
pub use native_monitor::NativeMonitorId;
Expand Down
Loading