Skip to content

Commit

Permalink
Simplify the condvar check
Browse files Browse the repository at this point in the history
  • Loading branch information
vertexclique committed Dec 31, 2019
1 parent d4420d0 commit 65afbad
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
9 changes: 3 additions & 6 deletions bastion/src/bastion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,10 @@ use crate::message::{BastionMessage, Message};
use crate::path::BastionPathElement;
use crate::supervisor::{Supervisor, SupervisorRef};
use crate::system::SYSTEM;
use bastion_executor::blocking;
use bastion_executor::run::run;

use core::future::Future;
use lightproc::proc_stack::ProcStack;

use std::fmt::{self, Debug, Formatter};
use std::thread;
use std::time::Duration;

/// A `struct` allowing to access the system's API to initialize it,
/// start, stop and kill it and to create new supervisors and top-level
Expand Down Expand Up @@ -538,7 +535,7 @@ impl Bastion {
system.cancel();
}

SYSTEM.stopped_notify();
SYSTEM.notify_stopped();
}

/// Blocks the current thread until the system is stopped
Expand Down
33 changes: 14 additions & 19 deletions bastion/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub(crate) struct GlobalSystem {
dead_letters: ChildrenRef,
path: Arc<BastionPath>,
handle: Qutex<Option<RecoverableHandle<()>>>,
stopped: Mutex<bool>,
sync: Condvar,
running: Mutex<bool>,
stopping_cvar: Condvar,
}

#[derive(Debug)]
Expand All @@ -51,17 +51,17 @@ impl GlobalSystem {
let handle = Some(handle);
let handle = Qutex::new(handle);
let path = Arc::new(BastionPath::root());
let stopped = Mutex::new(false);
let sync = Condvar::new();
let running = Mutex::new(true);
let stopping_cvar = Condvar::new();

GlobalSystem {
sender,
supervisor,
dead_letters,
path,
handle,
stopped,
sync,
running,
stopping_cvar,
}
}

Expand All @@ -85,22 +85,17 @@ impl GlobalSystem {
&self.path
}

pub(crate) fn stopped_notify(&self) {
pub(crate) fn notify_stopped(&self) {
// FIXME: panics
*self.stopped.lock().unwrap() = false;
self.sync.notify_all();
*self.running.lock().unwrap() = false;
self.stopping_cvar.notify_all();
}

pub(crate) fn wait_until_stopped(&self) {
// FIXME: panics
let mut stopped = self.stopped.lock().unwrap();
loop {
if *stopped {
return;
}

// FIXME: panics
stopped = self.sync.wait(stopped).unwrap();
let mut running = self.running.lock().unwrap();
while *running {
running = self.stopping_cvar.wait(running).unwrap();
}
}
}
Expand Down Expand Up @@ -401,7 +396,7 @@ impl System {
let mut system = SYSTEM.handle().lock_async().await.unwrap();
*system = None;

SYSTEM.stopped_notify();
SYSTEM.notify_stopped();

return;
}
Expand All @@ -418,7 +413,7 @@ impl System {
let mut system = SYSTEM.handle().lock_async().await.unwrap();
*system = None;

SYSTEM.stopped_notify();
SYSTEM.notify_stopped();

return;
}
Expand Down

0 comments on commit 65afbad

Please sign in to comment.