Commit 0aa78c01 authored by Ralf's avatar Ralf
Browse files

cargo fmt

parent 834190fc
...@@ -101,8 +101,13 @@ macro_rules! handle_events_timeout { ...@@ -101,8 +101,13 @@ macro_rules! handle_events_timeout {
impl Environment { impl Environment {
pub fn new() -> (Environment, EventSender) { pub fn new() -> (Environment, EventSender) {
let (tx, rx) = channel(); let (tx, rx) = channel();
( Environment { events: rx, sender: tx.clone(), sensors: SensorStates::new(), cur_id: 0 }, (Environment {
EventSender { sender: tx } ) events: rx,
sender: tx.clone(),
sensors: SensorStates::new(),
cur_id: 0,
},
EventSender { sender: tx })
} }
pub fn handle_events(&mut self, handlers: Vec<EventHandler>) -> EventsResult { pub fn handle_events(&mut self, handlers: Vec<EventHandler>) -> EventsResult {
...@@ -113,11 +118,13 @@ impl Environment { ...@@ -113,11 +118,13 @@ impl Environment {
pub fn handle_events_timeout(&mut self, pub fn handle_events_timeout(&mut self,
d: Duration, d: Duration,
handlers: Vec<EventHandler>) handlers: Vec<EventHandler>)
-> EventsResult -> EventsResult {
{
let sender = self.sender.clone(); let sender = self.sender.clone();
// make sure we ignore old timeouts // make sure we ignore old timeouts
let id = { self.cur_id = u32::wrapping_add(self.cur_id, 1); self.cur_id }; let id = {
self.cur_id = u32::wrapping_add(self.cur_id, 1);
self.cur_id
};
// spawn a thread that sends a timeout event with this id in [d] time // spawn a thread that sends a timeout event with this id in [d] time
thread::spawn(move || { thread::spawn(move || {
thread::sleep(d); thread::sleep(d);
...@@ -127,9 +134,8 @@ impl Environment { ...@@ -127,9 +134,8 @@ impl Environment {
// now we are ready to listen for events // now we are ready to listen for events
self.handle_events_cur_id(handlers) self.handle_events_cur_id(handlers)
} }
fn handle_events_cur_id(&mut self, mut handlers: Vec<EventHandler>) -> EventsResult fn handle_events_cur_id(&mut self, mut handlers: Vec<EventHandler>) -> EventsResult {
{
for ev in self.events.iter() { for ev in self.events.iter() {
match ev { match ev {
EventOrTimer::Timeout(id) => { EventOrTimer::Timeout(id) => {
...@@ -137,8 +143,8 @@ impl Environment { ...@@ -137,8 +143,8 @@ impl Environment {
return EventsResult::Timeout; return EventsResult::Timeout;
} }
// old timeout event, ignore // old timeout event, ignore
}, }
EventOrTimer::Ev(ev) => { EventOrTimer::Ev(ev) => {
// a regular event // a regular event
self.sensors.record_state(&ev); self.sensors.record_state(&ev);
// forward to all handlers // forward to all handlers
...@@ -153,7 +159,7 @@ impl Environment { ...@@ -153,7 +159,7 @@ impl Environment {
} }
return EventsResult::ShuttingDown; return EventsResult::ShuttingDown;
} }
pub fn sensors(&self) -> &SensorStates { pub fn sensors(&self) -> &SensorStates {
&self.sensors &self.sensors
} }
......
...@@ -19,7 +19,10 @@ impl UnLocking { ...@@ -19,7 +19,10 @@ impl UnLocking {
} }
impl Situation for UnLocking { impl Situation for UnLocking {
fn run(&mut self, mut actors: Actors, env: &mut Environment) -> (Actors, Option<Box<Situation>>) { fn run(&mut self,
mut actors: Actors,
env: &mut Environment)
-> (Actors, Option<Box<Situation>>) {
// TODO: abort if already locked // TODO: abort if already locked
let locking = self.locking; let locking = self.locking;
for _ in 0..3 { for _ in 0..3 {
...@@ -46,7 +49,8 @@ impl Situation for UnLocking { ...@@ -46,7 +49,8 @@ impl Situation for UnLocking {
})); }));
actors.lock = locker.terminate(); actors.lock = locker.terminate();
if r != EventsResult::Timeout { if r != EventsResult::Timeout {
assert!(r == EventsResult::ShuttingDown || env.sensors().door_locked() == Some(locking)); assert!(r == EventsResult::ShuttingDown ||
env.sensors().door_locked() == Some(locking));
break; // the retries break; // the retries
} }
} }
...@@ -64,7 +68,10 @@ impl SpaceOpen { ...@@ -64,7 +68,10 @@ impl SpaceOpen {
} }
impl Situation for SpaceOpen { impl Situation for SpaceOpen {
fn run(&mut self, mut actors: Actors, env: &mut Environment) -> (Actors, Option<Box<Situation>>) { fn run(&mut self,
mut actors: Actors,
env: &mut Environment)
-> (Actors, Option<Box<Situation>>) {
let mut buzzer = Wakeable::new(actors.buzz, move |t, mut buzz_actor, d| { let mut buzzer = Wakeable::new(actors.buzz, move |t, mut buzz_actor, d| {
buzz_actor.act(true); buzz_actor.act(true);
t.sleep(d); t.sleep(d);
...@@ -79,7 +86,11 @@ impl Situation for SpaceOpen { ...@@ -79,7 +86,11 @@ impl Situation for SpaceOpen {
})); }));
actors.buzz = buzzer.terminate(); actors.buzz = buzzer.terminate();
// hand over to next state // hand over to next state
(actors, if r == EventsResult::ShuttingDown { None } (actors,
else { None /* FIXME */ }) if r == EventsResult::ShuttingDown {
None
} else {
None /* FIXME */
})
} }
} }
...@@ -26,7 +26,7 @@ pub struct Wakeable<T, F> { ...@@ -26,7 +26,7 @@ pub struct Wakeable<T, F> {
/// a way to talk to it (the ThreadState). /// a way to talk to it (the ThreadState).
enum WakeableState<T> { enum WakeableState<T> {
Lazy(T), Lazy(T),
Running(thread::JoinHandle<T>, Arc<ThreadState>) Running(thread::JoinHandle<T>, Arc<ThreadState>),
} }
impl<T, F> Wakeable<T, F> { impl<T, F> Wakeable<T, F> {
...@@ -44,7 +44,7 @@ impl<T, F> Wakeable<T, F> { ...@@ -44,7 +44,7 @@ impl<T, F> Wakeable<T, F> {
fun: Arc::new(f), fun: Arc::new(f),
} }
} }
/// Like [new], but immediately spawns the thread /// Like [new], but immediately spawns the thread
pub fn spawn<D>(t: T, d: D, f: F) -> Wakeable<T, F> pub fn spawn<D>(t: T, d: D, f: F) -> Wakeable<T, F>
where D: Send + 'static, where D: Send + 'static,
...@@ -79,7 +79,7 @@ impl<T, F> Wakeable<T, F> { ...@@ -79,7 +79,7 @@ impl<T, F> Wakeable<T, F> {
WakeableState::Running(handle, state) WakeableState::Running(handle, state)
}); });
} }
/// Returns the T managed by the Wakeable, potentially termianting a still /// Returns the T managed by the Wakeable, potentially termianting a still
/// running thread. /// running thread.
pub fn terminate(self) -> T { pub fn terminate(self) -> T {
...@@ -95,7 +95,7 @@ impl<T> WakeableState<T> { ...@@ -95,7 +95,7 @@ impl<T> WakeableState<T> {
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
}, }
} }
} }
} }
...@@ -122,7 +122,7 @@ impl WakeableThread { ...@@ -122,7 +122,7 @@ impl WakeableThread {
pub fn sleep(&self, d: Duration) -> ShouldThreadRun { pub fn sleep(&self, d: Duration) -> ShouldThreadRun {
self.state.sleep(d) self.state.sleep(d)
} }
/// Returns the current "should we terminate" state. /// Returns the current "should we terminate" state.
pub fn should_terminate(&self) -> bool { pub fn should_terminate(&self) -> bool {
self.state.should_terminate() self.state.should_terminate()
...@@ -157,11 +157,11 @@ impl ThreadState { ...@@ -157,11 +157,11 @@ impl ThreadState {
let (should_run, _) = self.cond.wait_timeout(should_run, d).unwrap(); let (should_run, _) = self.cond.wait_timeout(should_run, d).unwrap();
return *should_run; return *should_run;
} }
/// Returns whether the thread should terminate. /// Returns whether the thread should terminate.
fn should_terminate(&self) -> bool { fn should_terminate(&self) -> bool {
// 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();
return *should_run == ShouldThreadRun::No return *should_run == ShouldThreadRun::No;
} }
} }
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