Skip to content

Commit

Permalink
feat: ExternalOpsTracker (#770)
Browse files Browse the repository at this point in the history
Allows an embedder to track "external" operations which should still
keep the event loop alive. Will be used to ref napi threadsafe
functions.
  • Loading branch information
devsnek authored Jun 6, 2024
1 parent ef3deea commit eeecea2
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
1 change: 1 addition & 0 deletions core/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ pub use crate::modules::SourceCodeCacheInfo;
pub use crate::modules::StaticModuleLoader;
pub use crate::modules::ValidateImportAttributesCb;
pub use crate::normalize_path::normalize_path;
pub use crate::ops::ExternalOpsTracker;
pub use crate::ops::OpId;
pub use crate::ops::OpMetadata;
pub use crate::ops::OpState;
Expand Down
36 changes: 36 additions & 0 deletions core/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr::NonNull;
use std::rc::Rc;
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use std::sync::Arc;
use v8::fast_api::CFunctionInfo;
use v8::fast_api::CTypeInfo;
Expand Down Expand Up @@ -250,12 +252,43 @@ impl OpCtx {
}
}

/// Allows an embedder to track operations which should
/// keep the event loop alive.
#[derive(Debug, Clone)]
pub struct ExternalOpsTracker {
counter: Arc<AtomicUsize>,
}

impl ExternalOpsTracker {
pub fn ref_op(&self) {
self.counter.fetch_add(1, Ordering::Relaxed);
}

pub fn unref_op(&self) {
let _ =
self
.counter
.fetch_update(Ordering::Relaxed, Ordering::Relaxed, |x| {
if x == 0 {
None
} else {
Some(x - 1)
}
});
}

pub(crate) fn has_pending_ops(&self) -> bool {
self.counter.load(Ordering::Relaxed) > 0
}
}

/// Maintains the resources and ops inside a JS runtime.
pub struct OpState {
pub resource_table: ResourceTable,
pub(crate) gotham_state: GothamState,
pub waker: Arc<AtomicWaker>,
pub feature_checker: Arc<FeatureChecker>,
pub external_ops_tracker: ExternalOpsTracker,
}

impl OpState {
Expand All @@ -265,6 +298,9 @@ impl OpState {
gotham_state: Default::default(),
waker: Arc::new(AtomicWaker::new()),
feature_checker: maybe_feature_checker.unwrap_or_default(),
external_ops_tracker: ExternalOpsTracker {
counter: Arc::new(AtomicUsize::new(0)),
},
}
}

Expand Down
4 changes: 4 additions & 0 deletions core/runtime/jsrealm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::modules::ModuleCodeString;
use crate::modules::ModuleId;
use crate::modules::ModuleMap;
use crate::modules::ModuleName;
use crate::ops::ExternalOpsTracker;
use crate::ops::OpCtx;
use crate::stats::RuntimeActivityTraces;
use crate::tasks::V8TaskSpawnerFactory;
Expand Down Expand Up @@ -68,6 +69,7 @@ pub struct ContextState {
pub(crate) exception_state: Rc<ExceptionState>,
pub(crate) has_next_tick_scheduled: Cell<bool>,
pub(crate) get_error_class_fn: GetErrorClassFn,
pub(crate) external_ops_tracker: ExternalOpsTracker,
}

impl ContextState {
Expand All @@ -76,6 +78,7 @@ impl ContextState {
isolate_ptr: *mut v8::OwnedIsolate,
get_error_class_fn: GetErrorClassFn,
op_ctxs: Box<[OpCtx]>,
external_ops_tracker: ExternalOpsTracker,
) -> Self {
Self {
isolate: Some(isolate_ptr),
Expand All @@ -91,6 +94,7 @@ impl ContextState {
task_spawner_factory: Default::default(),
timers: Default::default(),
unrefed_ops: Default::default(),
external_ops_tracker,
}
}
}
Expand Down
6 changes: 6 additions & 0 deletions core/runtime/jsruntime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,7 @@ impl JsRuntime {
isolate_ptr,
options.get_error_class_fn.unwrap_or(&|_| "Error"),
op_ctxs,
op_state.borrow().external_ops_tracker.clone(),
));

// TODO(bartlomieju): factor out
Expand Down Expand Up @@ -1809,6 +1810,7 @@ impl JsRuntime {
|| pending_state.has_pending_dyn_imports
|| pending_state.has_pending_dyn_module_evaluation
|| pending_state.has_pending_background_tasks
|| pending_state.has_pending_external_ops
|| pending_state.has_tick_scheduled
{
// pass, will be polled again
Expand All @@ -1823,6 +1825,7 @@ impl JsRuntime {
if pending_state.has_pending_ops
|| pending_state.has_pending_dyn_imports
|| pending_state.has_pending_background_tasks
|| pending_state.has_pending_external_ops
|| pending_state.has_tick_scheduled
{
// pass, will be polled again
Expand Down Expand Up @@ -2011,6 +2014,7 @@ pub(crate) struct EventLoopPendingState {
has_pending_background_tasks: bool,
has_tick_scheduled: bool,
has_pending_promise_events: bool,
has_pending_external_ops: bool,
}

impl EventLoopPendingState {
Expand Down Expand Up @@ -2053,6 +2057,7 @@ impl EventLoopPendingState {
has_pending_background_tasks: scope.has_pending_background_tasks(),
has_tick_scheduled: state.has_next_tick_scheduled.get(),
has_pending_promise_events,
has_pending_external_ops: state.external_ops_tracker.has_pending_ops(),
}
}

Expand All @@ -2071,6 +2076,7 @@ impl EventLoopPendingState {
|| self.has_pending_background_tasks
|| self.has_tick_scheduled
|| self.has_pending_promise_events
|| self.has_pending_external_ops
}
}

Expand Down

0 comments on commit eeecea2

Please sign in to comment.