Skip to content

Commit

Permalink
Revert "use AtomicBool"
Browse files Browse the repository at this point in the history
This reverts commit 34eb4d2.
  • Loading branch information
indutny committed May 22, 2021
1 parent 34eb4d2 commit 2974364
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions crates/neon-runtime/src/napi/tsfn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
use std::ffi::c_void;
use std::mem::MaybeUninit;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};

use crate::napi::bindings as napi;
use crate::raw::{Env, Local};
Expand Down Expand Up @@ -36,7 +35,7 @@ unsafe impl Sync for Tsfn {}
/// function for scheduling tasks to execute on a JavaScript thread.
pub struct ThreadsafeFunction<T> {
tsfn: Tsfn,
is_finalized: Arc<AtomicBool>,
is_finalized: Arc<Mutex<bool>>,
callback: fn(Option<Env>, T),
}

Expand Down Expand Up @@ -79,7 +78,7 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {
callback: fn(Option<Env>, T),
) -> Self {
let mut result = MaybeUninit::uninit();
let is_finalized = Arc::new(AtomicBool::new(false));
let is_finalized = Arc::new(Mutex::new(false));

assert_eq!(
napi::create_threadsafe_function(
Expand Down Expand Up @@ -157,9 +156,9 @@ impl<T: Send + 'static> ThreadsafeFunction<T> {
// Provides a C ABI wrapper for a napi callback notifying us about tsfn
// being finalized.
unsafe extern "C" fn finalize(_env: Env, data: *mut c_void, _hint: *mut c_void) {
let is_finalized = Arc::from_raw(data as *mut AtomicBool);
let is_finalized = Arc::from_raw(data as *mut Mutex<bool>);

is_finalized.store(true, Ordering::Relaxed);
*is_finalized.lock().unwrap() = true;
}

// Provides a C ABI wrapper for invoking the user supplied function pointer
Expand All @@ -182,7 +181,7 @@ impl<T> Drop for ThreadsafeFunction<T> {
fn drop(&mut self) {
// tsfn was already finalized by `Environment::CleanupHandles()` in
// Node.js
if self.is_finalized.load(Ordering::Relaxed) {
if *self.is_finalized.lock().unwrap() {
return;
}

Expand Down

0 comments on commit 2974364

Please sign in to comment.