diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-10-06 15:00:49 -0400 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-10-06 20:24:47 -0400 |
commit | 0514f54a225059c42caf28b583bad630b82f6128 (patch) | |
tree | 78cf52bd301f6c21b7b1f3b81e9bf734f6e3ea1c /src/isolate.rs | |
parent | f1989c68a075804a99d5b820b42643b7c43099da (diff) |
Make ntasks an atomic counter.
Fixes #919.
Diffstat (limited to 'src/isolate.rs')
-rw-r--r-- | src/isolate.rs | 28 |
1 files changed, 13 insertions, 15 deletions
diff --git a/src/isolate.rs b/src/isolate.rs index ddda96f99..ebcef77fc 100644 --- a/src/isolate.rs +++ b/src/isolate.rs @@ -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() } } |