Skip to content

Commit

Permalink
Clean up Runtime::block_on locks
Browse files Browse the repository at this point in the history
  • Loading branch information
LucioFranco committed Aug 27, 2020
1 parent cad3760 commit 099da4c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
2 changes: 1 addition & 1 deletion tokio/src/runtime/builder.rs
Expand Up @@ -364,7 +364,7 @@ impl Builder {
let blocking_spawner = blocking_pool.spawner().clone();

Ok(Runtime {
kind: Kind::Shell(Arc::new(Mutex::new(Shell::new(driver)))),
kind: Kind::Shell(Arc::new(Mutex::new(Some(Shell::new(driver))))),
handle: Handle {
spawner,
io_handle,
Expand Down
37 changes: 24 additions & 13 deletions tokio/src/runtime/mod.rs
Expand Up @@ -289,7 +289,7 @@ pub struct Runtime {
enum Kind {
/// Not able to execute concurrent tasks. This variant is mostly used to get
/// access to the driver handles.
Shell(Arc<Mutex<Shell>>),
Shell(Arc<Mutex<Option<Shell>>>),

/// Execute all tasks on the current-thread.
#[cfg(feature = "rt-core")]
Expand Down Expand Up @@ -439,25 +439,36 @@ impl Runtime {
pub fn block_on<F: Future>(&self, future: F) -> F::Output {
self.handle.enter(|| match &self.kind {
Kind::Shell(exec) => {
// TODO(lucio): refactor shell to have to ability to extract
// the parker and return it back.
let mut exec = exec.lock().unwrap();
exec.block_on(future)
// TODO(lucio): clean this up and move this impl into
// `shell.rs`, this is hacky and bad but will work for
// now.
let exec_temp = {
let mut lock = exec.lock().unwrap();
lock.take()
};

if let Some(mut exec_temp) = exec_temp {
let res = exec_temp.block_on(future);
exec.lock().unwrap().replace(exec_temp);
res
} else {
self.handle.enter(|| {
let mut enter = crate::runtime::enter(true);
enter.block_on(future).unwrap()
})
}
}
#[cfg(feature = "rt-core")]
Kind::Basic(exec) => {
// TODO(lucio): clean this up and move this impl into
// `basic_scheduler.rs`, this is hacky and bad but will work for
// now.
if let Some(mut exec_temp) = {
let exec_temp = {
let mut lock = exec.lock().unwrap();
let exec2 = lock.take();
// if lets have lovely semantics and love to fucking
// not drop locks for some reason so gotta do some
// manual clean!!!! YAY!
drop(lock);
exec2
} {
lock.take()
};

if let Some(mut exec_temp) = exec_temp {
let res = exec_temp.block_on(future);
exec.lock().unwrap().replace(exec_temp);
res
Expand Down

0 comments on commit 099da4c

Please sign in to comment.