Skip to content

Commit

Permalink
Merge pull request #147 from bastion-rs/thread-yield-overburden-fix
Browse files Browse the repository at this point in the history
Fix extreme amount of syscalls to give time share to OS
  • Loading branch information
vertexclique committed Dec 31, 2019
2 parents c0f555c + 65afbad commit 957258f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 28 deletions.
12 changes: 0 additions & 12 deletions .github/workflows/discord.yml

This file was deleted.

16 changes: 5 additions & 11 deletions bastion/src/bastion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::message::{BastionMessage, Message};
use crate::path::BastionPathElement;
use crate::supervisor::{Supervisor, SupervisorRef};
use crate::system::SYSTEM;

use core::future::Future;

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

/// 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 @@ -533,6 +534,8 @@ impl Bastion {
debug!("Bastion: Cancelling system handle.");
system.cancel();
}

SYSTEM.notify_stopped();
}

/// Blocks the current thread until the system is stopped
Expand Down Expand Up @@ -564,16 +567,7 @@ impl Bastion {
/// [`Bastion::kill()`]: #method.kill
pub fn block_until_stopped() {
debug!("Bastion: Blocking until system is stopped.");
loop {
// FIXME: panics
let system = SYSTEM.handle().lock().wait().unwrap();
if system.is_none() {
debug!("Bastion: Unblocking because system is stopped.");
return;
}

thread::yield_now();
}
SYSTEM.wait_until_stopped();
}
}

Expand Down
34 changes: 29 additions & 5 deletions bastion/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ use fxhash::{FxHashMap, FxHashSet};
use lazy_static::lazy_static;
use lightproc::prelude::*;
use qutex::Qutex;
use std::sync::Arc;
use std::sync::{Arc, Condvar, Mutex};
use std::task::Poll;

lazy_static! {
pub(crate) static ref SYSTEM: GlobalSystem = System::init();
}

pub(crate) struct GlobalSystem {
sender: Sender,
supervisor: SupervisorRef,
dead_letters: ChildrenRef,
path: Arc<BastionPath>,
handle: Qutex<Option<RecoverableHandle<()>>>,
}

lazy_static! {
pub(crate) static ref SYSTEM: GlobalSystem = System::init();
running: Mutex<bool>,
stopping_cvar: Condvar,
}

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

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

Expand All @@ -78,6 +84,20 @@ impl GlobalSystem {
pub(crate) fn path(&self) -> &Arc<BastionPath> {
&self.path
}

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

pub(crate) fn wait_until_stopped(&self) {
// FIXME: panics
let mut running = self.running.lock().unwrap();
while *running {
running = self.stopping_cvar.wait(running).unwrap();
}
}
}

impl System {
Expand Down Expand Up @@ -376,6 +396,8 @@ impl System {
let mut system = SYSTEM.handle().lock_async().await.unwrap();
*system = None;

SYSTEM.notify_stopped();

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

SYSTEM.notify_stopped();

return;
}
}
Expand Down

0 comments on commit 957258f

Please sign in to comment.