1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-10 16:11:13 -05:00

Make ntasks an atomic counter.

Fixes #919.
This commit is contained in:
Ryan Dahl 2018-10-06 15:00:49 -04:00
parent f1989c68a0
commit 0514f54a22

View file

@ -44,7 +44,12 @@ pub struct Isolate {
ptr: *const libdeno::isolate,
dispatch: Dispatch,
rx: mpsc::Receiver<(i32, Buf)>,
ntasks: i32,
// Although Isolate is only accessed on the main thread, we use an atomic
// variable here to workaround an issue probably caused by our poor usage
// of Box::leak in Isolate::from_c()
// https://github.com/denoland/deno/issues/919
// ntasks ought to be i32.
ntasks: atomic::AtomicIsize,
pub timeout_due: Option<Instant>,
pub state: Arc<IsolateState>,
}
@ -86,7 +91,7 @@ impl Isolate {
ptr: 0 as *const libdeno::isolate,
dispatch,
rx,
ntasks: 0,
ntasks: atomic::AtomicIsize::new(0),
timeout_due: None,
state: Arc::new(IsolateState {
dir: deno_dir::DenoDir::new(flags.reload, None).unwrap(),
@ -189,25 +194,18 @@ impl Isolate {
}
fn ntasks_increment(&mut self) {
assert!(self.ntasks >= 0);
self.ntasks = self.ntasks + 1;
let previous_ntasks = self.ntasks.fetch_add(1, atomic::Ordering::SeqCst);
assert!(previous_ntasks >= 0);
}
fn ntasks_decrement(&mut self) {
// Do something that has no effect. This is done to work around a spooky
// bug that happens in release mode only (presumably a compiler bug), that
// causes nsize to unexpectedly contain zero.
// TODO: remove this workaround when no longer necessary.
#[allow(unused)]
static UNUSED: atomic::AtomicIsize = atomic::AtomicIsize::new(0);
UNUSED.fetch_add(self.ntasks as isize, atomic::Ordering::AcqRel);
// Actually decrement the tasks counter here.
self.ntasks = self.ntasks - 1;
assert!(self.ntasks >= 0);
let previous_ntasks = self.ntasks.fetch_sub(1, atomic::Ordering::SeqCst);
assert!(previous_ntasks >= 1);
}
fn is_idle(&self) -> bool {
self.ntasks == 0 && self.timeout_due.is_none()
let n = self.ntasks.load(atomic::Ordering::SeqCst);
n == 0 && self.timeout_due.is_none()
}
}