Commit e0bed0a2 authored by Ralf's avatar Ralf
Browse files

cargo fmt

parent 7a20cf34
...@@ -72,12 +72,11 @@ impl State for OpenState { ...@@ -72,12 +72,11 @@ impl State for OpenState {
buzz_actor buzz_actor
}); });
let r = handle_events!(env, let r = handle_events!(env,
EventHandler::new_safe(|ev| { EventHandler::new_safe(|ev| {
if let Event::Bell(true) = ev { if let Event::Bell(true) = ev {
buzzer.spawn(Duration::from_secs(2)); buzzer.spawn(Duration::from_secs(2));
} }
}) }));
);
actors.buzz = buzzer.terminate(); actors.buzz = buzzer.terminate();
if r == EventsResult::ShuttingDown { if r == EventsResult::ShuttingDown {
return None; return None;
...@@ -85,4 +84,3 @@ impl State for OpenState { ...@@ -85,4 +84,3 @@ impl State for OpenState {
None // FIXME None // FIXME
} }
} }
...@@ -21,37 +21,39 @@ enum WakeableState<T> { ...@@ -21,37 +21,39 @@ enum WakeableState<T> {
pub struct WakeableHandle<T, F> { pub struct WakeableHandle<T, F> {
w_state: WakeableState<T>, // invariant: This is never empty when control is to the user w_state: WakeableState<T>, // invariant: This is never empty when control is to the user
fun: Arc<F> fun: Arc<F>,
} }
pub struct WakeableThread { pub struct WakeableThread {
state: Arc<ThreadState>, state: Arc<ThreadState>,
} }
pub fn prepare_wakeable<D, F, T>(t: T, f: F) -> WakeableHandle<T, F> where pub fn prepare_wakeable<D, F, T>(t: T, f: F) -> WakeableHandle<T, F>
D: Send + 'static, where D: Send + 'static,
T: Send + 'static, T: Send + 'static,
F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static
{ {
WakeableHandle { w_state: WakeableState::Lazy(t), fun: Arc::new(f) } WakeableHandle {
w_state: WakeableState::Lazy(t),
fun: Arc::new(f),
}
} }
pub fn spawn_wakeable<D, F, T>(t: T, d: D, f: F) -> WakeableHandle<T, F> where pub fn spawn_wakeable<D, F, T>(t: T, d: D, f: F) -> WakeableHandle<T, F>
D: Send + 'static, where D: Send + 'static,
T: Send + 'static, T: Send + 'static,
F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static, F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static
{ {
let mut handle = prepare_wakeable(t, f); let mut handle = prepare_wakeable(t, f);
handle.spawn(d); handle.spawn(d);
handle handle
} }
impl<T, F> WakeableHandle<T, F> impl<T, F> WakeableHandle<T, F> {
{ pub fn spawn<D>(&mut self, d: D)
pub fn spawn<D>(&mut self, d: D) where where D: Send + 'static,
D: Send + 'static, T: Send + 'static,
T: Send + 'static, F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static
F: Fn(WakeableThread, T, D) -> T + Send + Sync + 'static,
{ {
// get ahand of the t, wherever it is // get ahand of the t, wherever it is
let t = self.get_t(); let t = self.get_t();
...@@ -67,7 +69,7 @@ impl<T, F> WakeableHandle<T, F> ...@@ -67,7 +69,7 @@ impl<T, F> WakeableHandle<T, F>
// store everything back into our state // store everything back into our state
self.w_state = WakeableState::Running(handle, state); self.w_state = WakeableState::Running(handle, state);
} }
/// This is "unsafe" because it leaves the handle in the empty state, which /// This is "unsafe" because it leaves the handle in the empty state, which
/// can cause panics. /// can cause panics.
fn get_t(&mut self) -> T { fn get_t(&mut self) -> T {
...@@ -77,11 +79,11 @@ impl<T, F> WakeableHandle<T, F> ...@@ -77,11 +79,11 @@ impl<T, F> WakeableHandle<T, F>
WakeableState::Running(handle, state) => { WakeableState::Running(handle, state) => {
state.wakeup(); state.wakeup();
handle.join().unwrap() // panics only if the thread paniced handle.join().unwrap() // panics only if the thread paniced
}, }
WakeableState::Empty => panic!("The WakeableState can not be empty"), WakeableState::Empty => panic!("The WakeableState can not be empty"),
} }
} }
pub fn terminate(mut self) -> T { pub fn terminate(mut self) -> T {
self.get_t() // This is fine to call, because the handle will be destroyed self.get_t() // This is fine to call, because the handle will be destroyed
} }
...@@ -90,12 +92,12 @@ impl<T, F> WakeableHandle<T, F> ...@@ -90,12 +92,12 @@ impl<T, F> WakeableHandle<T, F>
impl<T, F> Drop for WakeableHandle<F, T> { impl<T, F> Drop for WakeableHandle<F, T> {
fn drop(&mut self) { fn drop(&mut self) {
match self.w_state { match self.w_state {
WakeableState::Empty => {}, // all right, the user called "terminate" WakeableState::Empty => {} // all right, the user called "terminate"
WakeableState::Lazy(_) => {}, // we were never used :( TODO: emit a warning? WakeableState::Lazy(_) => {} // we were never used :( TODO: emit a warning?
WakeableState::Running(_, ref state) => { WakeableState::Running(_, ref state) => {
// the thread could still be running. emit a warning? Also, speed up the shutdown // the thread could still be running. emit a warning? Also, speed up the shutdown
state.wakeup(); state.wakeup();
}, }
}; };
} }
} }
...@@ -107,7 +109,7 @@ impl ThreadState { ...@@ -107,7 +109,7 @@ impl ThreadState {
*should_run = ShouldThreadRun::No; *should_run = ShouldThreadRun::No;
self.cond.notify_one(); self.cond.notify_one();
} }
pub fn sleep(&self, d: Duration) -> ShouldThreadRun { pub fn sleep(&self, d: Duration) -> ShouldThreadRun {
// We panic if the lock is poisened, which only happens if someone paniced // We panic if the lock is poisened, which only happens if someone paniced
let should_run = self.mutex.lock().unwrap(); let should_run = self.mutex.lock().unwrap();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment