From 93b0ec617cde7f8e1c7f3b8abe1100f07d438b12 Mon Sep 17 00:00:00 2001 From: Syrus Akbary Date: Thu, 5 Nov 2026 21:55:51 -0800 Subject: [PATCH] Wake the host when V8 queues a task only a checkpoint runs With no foreground task target bound, EdgeV8Platform puts V8's foreground tasks in the default platform's queue, which only the guest's next event-loop checkpoint runs (PumpPendingForegroundTasks). Guests running through wasmer_napi never bind a target. V8 posts such tasks from any thread: to resolve an Atomics.waitAsync that another thread's Atomics.notify woke, to finish a WebAssembly.compile, to run FinalizationRegistry callbacks. Nothing told a guest waiting for host events that one was due, so the task ran only once something unrelated woke the guest. A host can now install a process-wide waker with wasmer_napi::set_foreground_task_waker (bridge: snapi_bridge_set_foreground_task_waker; platform: EdgeV8Platform::SetForegroundTaskWaker). The platform calls it on the posting thread right after it queues an immediate task there, so the guest's wait ends and its next turn takes the checkpoint that runs the task. Delayed tasks, and tasks handed to a bound target, don't call it. The Rust side keeps the waker in an RwLock and calls it outside the lock, so a waker may replace or remove itself. A panic in the waker, or in dropping it, is caught before it can unwind into V8. The call has a guard of its own, so the waker's last reference is never dropped while its panic unwinds. Without a waker the platform calls nothing, and posting a task costs one more atomic load. Measured in WebkitWasm, whose browser binds no target and sleeps until its next GUI event between turns: a page running a nested engine, whose GPU owner it wakes with Atomics.waitAsync about 31 times a frame, went from 19.8 to 59.7 fps, and a Worker-to-page waitAsync round trip from 14 ms to 0.03 ms at p90. Idle CPU did not change. tests/src/v8_foreground_task_waker_test.cc checks the platform: an immediate task posted from another thread calls the waker once, on that thread, after the task is queued, and the next pump runs it; a delayed task, a cleared waker and a bound target don't call it. tests/foreground_task_waker.rs checks the Rust API through Wasmer dispatch: a guest's Atomics.notify on another thread calls the waker for the waiting guest's task, which resolves at that guest's next checkpoint; a removed waker isn't called; neither a waker that panics nor one that removes itself, panics, and holds a capture that panics when dropped takes the process down. The napi-native-concurrency workflow now runs it. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/napi-native-concurrency.yml | 5 +- src/lib.rs | 89 ++++++ src/napi_bridge_init.cc | 7 + src/snapi.rs | 3 + tests/CMakeLists.txt | 18 ++ tests/foreground_task_waker.rs | 230 ++++++++++++++ tests/src/v8_foreground_task_waker_test.cc | 291 ++++++++++++++++++ v8/src/edge_v8_platform.cc | 15 + v8/src/edge_v8_platform.h | 21 ++ 9 files changed, 678 insertions(+), 1 deletion(-) create mode 100644 tests/foreground_task_waker.rs create mode 100644 tests/src/v8_foreground_task_waker_test.cc diff --git a/.github/workflows/napi-native-concurrency.yml b/.github/workflows/napi-native-concurrency.yml index f1910e11d..26c0b5c29 100644 --- a/.github/workflows/napi-native-concurrency.yml +++ b/.github/workflows/napi-native-concurrency.yml @@ -14,6 +14,7 @@ on: - "lib/**" - "rust-toolchain.toml" - "src/**" + - "tests/foreground_task_waker.rs" - "tests/concurrent_*.rs" - "tests/wat_guest/**" - "v8/**" @@ -28,6 +29,7 @@ on: - "lib/**" - "rust-toolchain.toml" - "src/**" + - "tests/foreground_task_waker.rs" - "tests/concurrent_*.rs" - "tests/wat_guest/**" - "v8/**" @@ -75,7 +77,8 @@ jobs: # WAT guests, each in its own Wasmer store and thread, against the real # C++ bridge and V8. No WASIX C toolchain is needed. - - name: Test concurrent environments and stores + - name: Test concurrent environments and stores, and the foreground task waker run: >- ./cargo-standalone.sh test --locked + --test foreground_task_waker --test concurrent_envs --test concurrent_stores diff --git a/src/lib.rs b/src/lib.rs index d9b195c34..7c027a624 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,6 +104,95 @@ impl Display for WorkerThreadsAlreadyFixed { impl std::error::Error for WorkerThreadsAlreadyFixed {} +/// What [`set_foreground_task_waker`] installed. +#[cfg(not(all(target_arch = "wasm32", feature = "js")))] +type ForegroundTaskWaker = std::sync::Arc; + +#[cfg(not(all(target_arch = "wasm32", feature = "js")))] +static FOREGROUND_TASK_WAKER: std::sync::RwLock> = + std::sync::RwLock::new(None); + +/// What the V8 platform calls while a waker is installed. +#[cfg(not(all(target_arch = "wasm32", feature = "js")))] +extern "C" fn wake_for_foreground_task() { + // A panic must not unwind into V8, so the clone and its drop run inside + // this guard. The call has a guard of its own: the clone can be the last + // reference to the waker, and if its drop ran while a panic in the waker + // unwound, a panicking destructor among its captures would abort the + // process. + let result = std::panic::catch_unwind(|| { + // The lock is released before the call, so the waker may replace + // itself. + let waker = match FOREGROUND_TASK_WAKER.read() { + Ok(slot) => slot.clone(), + Err(poisoned) => poisoned.into_inner().clone(), + }; + if let Some(waker) = waker { + let called = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| waker())); + // Dropping a payload could panic in turn: leak it instead. + if let Err(payload) = called { + std::mem::forget(payload); + } + } + }); + if let Err(payload) = result { + std::mem::forget(payload); + } +} + +/// Sets what is called, process-wide, when V8 queues a foreground task that +/// only a guest's next event-loop checkpoint runs. +/// +/// A guest's foreground tasks wait in a queue that +/// `unofficial_napi_event_loop_checkpoint` runs, one task per checkpoint. A +/// guest can't have them handed to a hook of its own instead: the bridge gives +/// `unofficial_napi_attach_env` no native hooks. V8 posts these tasks from any +/// thread: to resolve an `Atomics.waitAsync` that another thread's +/// `Atomics.notify` woke, to finish a `WebAssembly.compile`, to run +/// `FinalizationRegistry` callbacks. A guest that sleeps until its next host +/// event is not told, so such a task waits until something else wakes the +/// guest. A host passes a waker that ends that sleep; the guest's next turn +/// takes the checkpoint that runs the task. +/// +/// The waker is called after an immediate task is queued, on the thread that +/// posted it: another guest's thread, a V8 worker thread, or the guest's own +/// thread in the middle of a guest call or a garbage collection. V8 may hold +/// its own locks then. So the waker must be quick, must not call into V8 or +/// run guest code, and must not take a lock that the host holds around guest +/// calls. It is called for every isolate's tasks, so a host running several +/// guests wakes them all, and a guest with nothing to run waits again. Delayed +/// tasks don't call it: each runs at the first checkpoint after its delay. A +/// panic in the waker is caught, and its payload leaked, so that it never +/// unwinds into V8. +/// +/// `None` removes the waker. A waker may call this function itself. A call +/// that was already under way may still run the waker it replaced after this +/// returns, and then drop the last reference to it on the posting thread, so +/// what a waker captures must not call into V8 when it is dropped either. +#[cfg(not(all(target_arch = "wasm32", feature = "js")))] +pub fn set_foreground_task_waker(waker: Option>) { + let installed = waker.is_some(); + let previous = { + let mut slot = match FOREGROUND_TASK_WAKER.write() { + Ok(slot) => slot, + Err(poisoned) => poisoned.into_inner(), + }; + let previous = std::mem::replace(&mut *slot, waker); + // Under the same lock, so that concurrent calls leave the slot and the + // platform's pointer in agreement. Without a waker, the platform gets + // no function at all and calls nothing. + // Safety: a plain function that lives as long as the process. + unsafe { + snapi::snapi_bridge_set_foreground_task_waker( + installed.then_some(wake_for_foreground_task as extern "C" fn()), + ); + } + previous + }; + // Dropped once unlocked: what it captured may call back in. + drop(previous); +} + #[derive(Debug, Clone, Copy, PartialEq, Eq, Sequence)] pub enum NapiVersion { V10, diff --git a/src/napi_bridge_init.cc b/src/napi_bridge_init.cc index e82dfd724..e601d3d76 100644 --- a/src/napi_bridge_init.cc +++ b/src/napi_bridge_init.cc @@ -668,6 +668,13 @@ extern "C" int snapi_bridge_init() { return 1; } +// Host-only: the function to call, from any thread, after V8 queues a +// foreground task that only an env's next event-loop checkpoint runs +// (EdgeV8Platform::SetForegroundTaskWaker). NULL clears it. +extern "C" void snapi_bridge_set_foreground_task_waker(void (*waker)()) { + EdgeV8Platform::SetForegroundTaskWaker(waker); +} + // ============================================================ // Value creation // ============================================================ diff --git a/src/snapi.rs b/src/snapi.rs index 5d6a79c7b..b886430ba 100644 --- a/src/snapi.rs +++ b/src/snapi.rs @@ -54,6 +54,9 @@ pub struct SnapiUnofficialHeapCodeStatistics { // through `snapi_js::host_js_*` entry points or direct guest memory access. unsafe extern "C" { pub fn snapi_bridge_init() -> i32; + // Host only: the JS backend has no V8 platform queue to wake for. + #[cfg(not(all(target_arch = "wasm32", feature = "js")))] + pub fn snapi_bridge_set_foreground_task_waker(waker: Option); pub fn snapi_bridge_set_v8_worker_thread_count(count: u32) -> i32; pub fn snapi_bridge_unofficial_configure_runtime( engine_flags: *const c_char, diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index e1196916e..14e7952aa 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -382,3 +382,21 @@ if(_napi_test_engine STREQUAL "v8") set_tests_properties(napi_v8.background_job_limit napi_v8.background_job_limit_single_worker PROPERTIES TIMEOUT 120) endif() + +if(_napi_test_engine STREQUAL "v8") + add_executable(napi_v8_foreground_task_waker_test src/v8_foreground_task_waker_test.cc) + target_include_directories(napi_v8_foreground_task_waker_test PRIVATE + "${NAPI_INCLUDE_ROOT}" + "${${_napi_engine_include_var}}" + ) + target_link_libraries(napi_v8_foreground_task_waker_test PRIVATE + ${_napi_test_engine_target} + "${${_napi_engine_library_var}}" + ) + if(NAPI_V8_DEFINES) + target_compile_definitions(napi_v8_foreground_task_waker_test PRIVATE ${NAPI_V8_DEFINES}) + endif() + add_test(NAME napi_v8.foreground_task_waker + COMMAND napi_v8_foreground_task_waker_test) + set_tests_properties(napi_v8.foreground_task_waker PROPERTIES TIMEOUT 120) +endif() diff --git a/tests/foreground_task_waker.rs b/tests/foreground_task_waker.rs new file mode 100644 index 000000000..64e780d9b --- /dev/null +++ b/tests/foreground_task_waker.rs @@ -0,0 +1,230 @@ +//! `wasmer_napi::set_foreground_task_waker` reaches the V8 platform through +//! the bridge. A guest's `Atomics.waitAsync`, woken by another guest's +//! `Atomics.notify` on another thread, resolves only at the waiting guest's +//! next checkpoint, so the host's waker must run, on the notifying thread, +//! when the notify queues that task. The platform side is covered by +//! `tests/src/v8_foreground_task_waker_test.cc`. +//! +//! The waker is process-wide, so this test needs a process to itself, which +//! is what a separate integration test binary with one test gives it. + +#![cfg(not(target_arch = "wasm32"))] + +use std::{ + mem::ManuallyDrop, + sync::{Arc, Mutex, mpsc}, + thread::{self, ThreadId}, + time::Duration, +}; + +use wasmer_napi::NapiCtx; + +mod wat_guest; +use wat_guest::{Guest, compile}; + +const TIMEOUT: Duration = Duration::from_secs(10); +/// More checkpoints than an idle env needs to run what V8 queued for it. +const MAX_CHECKPOINTS: usize = 64; + +/// The threads the installed waker ran on, in order. +type WakerLog = Arc>>; + +fn calls(log: &WakerLog) -> usize { + log.lock().unwrap().len() +} + +fn calls_on(log: &WakerLog, thread: ThreadId) -> usize { + log.lock() + .unwrap() + .iter() + .filter(|&&id| id == thread) + .count() +} + +/// Starts an `Atomics.waitAsync` on the shared cell; `woken` becomes 1 once +/// its promise resolves. +fn wait_async(page: &mut Guest) { + assert_eq!( + page.eval( + "globalThis.woken = 0; \ + Atomics.waitAsync(new Int32Array(shared), 0, 0).value.then(() => { woken = 1; });" + ), + 0 + ); +} + +/// Runs checkpoints until the waitAsync promise has resolved. +fn checkpoint_until_woken(page: &mut Guest) -> bool { + for _ in 0..MAX_CHECKPOINTS { + assert_eq!(page.checkpoint(), 0); + if page.number("woken") == 1 { + return true; + } + } + false +} + +/// Records a waker call like the installed wakers do, and panics when +/// dropped. +struct PanicsOnDrop(WakerLog); + +impl PanicsOnDrop { + fn record(&self) { + self.0.lock().unwrap().push(thread::current().id()); + } +} + +impl Drop for PanicsOnDrop { + fn drop(&mut self) { + panic!("a waker's capture that panics when dropped"); + } +} + +/// What the notifying guest saw: how many waiters `Atomics.notify` woke, and +/// how many times the waker ran on its thread meanwhile. +struct Notified { + woke: i32, + waker_calls: usize, +} + +#[test] +fn waker_runs_when_another_thread_queues_a_task_for_a_waiting_guest() { + let (engine, module) = compile(); + let ctx = NapiCtx::default(); + let (signal_tx, _signal_rx) = mpsc::channel(); + let log: WakerLog = Arc::default(); + + // Dropped only once the test has passed. A regression can leave the + // notifying guest stuck inside V8 (a waker that deadlocks, say), and + // dropping this guest while the test fails would then wait on it forever. + let mut page = ManuallyDrop::new(Guest::new(&ctx, &engine, &module, signal_tx.clone())); + page.create(); + assert_eq!( + page.eval("globalThis.shared = new SharedArrayBuffer(8); shared"), + 0 + ); + assert_eq!(page.message(), 0); + let token = page.token(); + + // A guest on another thread that notifies the shared cell on request. + let (notify_tx, notify_rx) = mpsc::channel::<()>(); + let (notified_tx, notified_rx) = mpsc::channel::(); + let notifier = { + let (ctx, engine, module, log) = (ctx.clone(), engine.clone(), module.clone(), log.clone()); + thread::spawn(move || { + let mut guest = Guest::new(&ctx, &engine, &module, signal_tx); + guest.create(); + assert_eq!(guest.take(token), 0); + assert_eq!(guest.install(), 0); + let me = thread::current().id(); + while notify_rx.recv().is_ok() { + let before = calls_on(&log, me); + let woke = guest.number("Atomics.notify(new Int32Array(shared), 0)"); + let waker_calls = calls_on(&log, me) - before; + if notified_tx.send(Notified { woke, waker_calls }).is_err() { + break; + } + } + assert_eq!(guest.release(), 0); + }) + }; + let notify = || { + notify_tx.send(()).expect("the notifying guest is gone"); + notified_rx + .recv_timeout(TIMEOUT) + .expect("the notifying guest did not answer") + }; + + // Run what V8 queued while the env started, before the waker is set. + for _ in 0..MAX_CHECKPOINTS { + assert_eq!(page.checkpoint(), 0); + } + + { + let log = log.clone(); + wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || { + log.lock().unwrap().push(thread::current().id()); + }))); + } + wait_async(&mut page); + let notified = notify(); + assert_eq!(notified.woke, 1, "the waitAsync was not waiting"); + assert!( + notified.waker_calls >= 1, + "the waker did not run on the notifying thread when the notify queued \ + the waiting guest's task" + ); + // Only a checkpoint runs the task that resolves the promise. + assert_eq!(page.number("woken"), 0); + assert!( + checkpoint_until_woken(&mut page), + "the waitAsync promise did not resolve" + ); + + // Without a waker nothing is called, and a checkpoint still runs the task. + wasmer_napi::set_foreground_task_waker(None); + let before = calls(&log); + wait_async(&mut page); + assert_eq!(notify().woke, 1); + assert_eq!(calls(&log), before, "a removed waker ran"); + assert!( + checkpoint_until_woken(&mut page), + "the waitAsync promise did not resolve without a waker" + ); + + // A waker that panics: the panic stays out of V8, and the task still runs. + { + let log = log.clone(); + wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || { + log.lock().unwrap().push(thread::current().id()); + panic!("a waker that panics"); + }))); + } + wait_async(&mut page); + let notified = notify(); + assert_eq!(notified.woke, 1); + assert!(notified.waker_calls >= 1, "the panicking waker did not run"); + assert!( + checkpoint_until_woken(&mut page), + "the waitAsync promise did not resolve after the waker panicked" + ); + + // A waker that removes itself and then panics, holding a capture that + // panics when dropped. It runs with no lock held, so it may call the + // setter. Once removed, the trampoline holds the last reference to it, + // and drops it after the call: if that drop came while the waker's panic + // unwound, the capture's panic would abort the process. Whichever thread + // calls it first removes it, so its calls are counted on every thread. + let before = calls(&log); + { + let capture = PanicsOnDrop(log.clone()); + wasmer_napi::set_foreground_task_waker(Some(Arc::new(move || { + capture.record(); + wasmer_napi::set_foreground_task_waker(None); + panic!("a waker that removed itself"); + }))); + } + wait_async(&mut page); + assert_eq!(notify().woke, 1); + assert!( + calls(&log) > before, + "the waker that removes itself did not run" + ); + assert!( + checkpoint_until_woken(&mut page), + "the waitAsync promise did not resolve after the waker removed itself" + ); + let before = calls(&log); + wait_async(&mut page); + assert_eq!(notify().woke, 1); + assert_eq!(calls(&log), before, "the waker did not remove itself"); + assert!( + checkpoint_until_woken(&mut page), + "the waitAsync promise did not resolve once the waker removed itself" + ); + + drop(notify_tx); + notifier.join().expect("the notifying guest panicked"); + assert_eq!(page.release(), 0); + drop(ManuallyDrop::into_inner(page)); +} diff --git a/tests/src/v8_foreground_task_waker_test.cc b/tests/src/v8_foreground_task_waker_test.cc new file mode 100644 index 000000000..0322bbd16 --- /dev/null +++ b/tests/src/v8_foreground_task_waker_test.cc @@ -0,0 +1,291 @@ +#include "../../v8/src/edge_v8_platform.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +namespace { + +// Every wait is bounded so a regression fails the test instead of hanging it. +constexpr auto kWaitTimeout = std::chrono::seconds(10); +// More than V8 posts while an idle isolate settles. +constexpr int kMaxPumps = 64; + +std::atomic g_passed{true}; + +#if defined(__GNUC__) +__attribute__((format(printf, 1, 2))) +#endif +void Fail(const char* format, ...) { + va_list args; + va_start(args, format); + std::vfprintf(stderr, format, args); + va_end(args); + std::fprintf(stderr, "\n"); + g_passed = false; +} + +// What the waker saw. It runs on whichever thread posted the task. +struct Wakes { + std::mutex mutex; + std::condition_variable cv; + // Calls made on a thread that set t_poster. + size_t from_poster = 0; + // Set by the main thread once it has pumped after a poster's wake. + bool pumped = false; +}; +Wakes g_wakes; + +// The test's posting threads mark themselves, so that a task V8 posts on its +// own, from another thread, doesn't count. +thread_local bool t_poster = false; +// A poster that sets this waits inside the waker until the main thread has +// pumped, so the pump sees exactly what was queued before the waker ran. +thread_local bool t_hold = false; +thread_local size_t t_calls = 0; + +void RecordingWaker() { + ++t_calls; + if (!t_poster) return; + std::unique_lock lock(g_wakes.mutex); + ++g_wakes.from_poster; + g_wakes.cv.notify_all(); + if (t_hold && + !g_wakes.cv.wait_for(lock, kWaitTimeout, [] { return g_wakes.pumped; })) { + Fail("the main thread did not pump after the wake"); + } +} + +void ResetWakes() { + std::lock_guard lock(g_wakes.mutex); + g_wakes.from_poster = 0; + g_wakes.pumped = false; +} + +// How many times a FlagTask ran. Shared with the task, so that a task a +// failing case leaves queued can still run safely at the final pump. +using RunCount = std::shared_ptr>; + +RunCount NewRunCount() { return std::make_shared>(0); } + +class FlagTask final : public v8::Task { + public: + explicit FlagTask(RunCount ran) : ran_(std::move(ran)) {} + void Run() override { ran_->fetch_add(1, std::memory_order_acq_rel); } + + private: + RunCount ran_; +}; + +// Posts from a thread of its own, as V8 does when another thread resolves an +// Atomics.waitAsync or finishes a WebAssembly.compile. Returns how many times +// the waker ran on that thread while it posted. +template +size_t PostFromAnotherThread(Post post) { + size_t calls = 0; + std::thread poster([&] { + t_poster = true; + post(); + calls = t_calls; + }); + poster.join(); + return calls; +} + +// Pumps like a guest's checkpoint does, one task at a time, until `ran` is +// set or nothing is left to run. +void PumpUntil(EdgeV8Platform* platform, v8::Isolate* isolate, const std::atomic& ran) { + for (int i = 0; i < kMaxPumps && ran.load(std::memory_order_acquire) == 0; ++i) { + if (!platform->PumpPendingForegroundTasks(isolate)) break; + } +} + +void PumpAll(EdgeV8Platform* platform, v8::Isolate* isolate) { + for (int i = 0; i < kMaxPumps && platform->PumpPendingForegroundTasks(isolate); ++i) { + } +} + +// Stands in for a guest's enqueue hook: keeps the records it is handed. +struct BoundTarget { + struct Entry { + unofficial_napi_foreground_task_callback callback; + void* data; + unofficial_napi_foreground_task_cleanup cleanup; + }; + std::mutex mutex; + std::vector entries; +}; + +napi_status EnqueueOnTarget(void* target, + unofficial_napi_foreground_task_callback callback, + void* data, + unofficial_napi_foreground_task_cleanup cleanup, + uint64_t /*delay_millis*/) { + auto* bound = static_cast(target); + std::lock_guard lock(bound->mutex); + bound->entries.push_back({callback, data, cleanup}); + return napi_ok; +} + +// An immediate task posted from another thread calls the waker on that +// thread, once, after the task is queued: the main thread, woken by it, finds +// the task at its next pump. +void ImmediateTaskWakes(EdgeV8Platform* platform, + v8::Isolate* isolate, + const std::shared_ptr& runner) { + EdgeV8Platform::SetForegroundTaskWaker(RecordingWaker); + ResetWakes(); + const RunCount ran = NewRunCount(); + size_t calls = 0; + std::thread poster([&] { + t_poster = true; + t_hold = true; + runner->PostTask(std::make_unique(ran)); + calls = t_calls; + }); + // This thread waits like a guest blocked on its event source. + { + std::unique_lock lock(g_wakes.mutex); + if (!g_wakes.cv.wait_for(lock, kWaitTimeout, [] { return g_wakes.from_poster > 0; })) { + Fail("immediate task: the waker was not called"); + } + } + // The poster waits in the waker meanwhile, so what is queued now was + // queued before the waker ran. + const bool ran_before_pump = ran->load(std::memory_order_acquire) != 0; + PumpUntil(platform, isolate, *ran); + const bool ran_at_pump = ran->load(std::memory_order_acquire) != 0; + { + std::lock_guard lock(g_wakes.mutex); + g_wakes.pumped = true; + g_wakes.cv.notify_all(); + } + poster.join(); + + if (calls != 1) { + Fail("immediate task: the posting thread called the waker %zu times, expected 1", calls); + } + if (ran_before_pump) Fail("immediate task: ran before any pump"); + if (!ran_at_pump) Fail("immediate task: not queued when the waker ran"); + PumpUntil(platform, isolate, *ran); + if (ran->load() != 1) Fail("immediate task: ran %d times, expected 1", ran->load()); +} + +// A delayed task doesn't call the waker. It runs at a pump after its delay. +void DelayedTaskDoesNotWake(EdgeV8Platform* platform, + v8::Isolate* isolate, + const std::shared_ptr& runner) { + EdgeV8Platform::SetForegroundTaskWaker(RecordingWaker); + const RunCount ran = NewRunCount(); + const size_t calls = PostFromAnotherThread([&] { + runner->PostDelayedTask(std::make_unique(ran), 0.05); + }); + if (calls != 0) { + Fail("delayed task: the posting thread called the waker %zu times, expected 0", calls); + } + + const auto deadline = std::chrono::steady_clock::now() + kWaitTimeout; + while (ran->load(std::memory_order_acquire) == 0 && + std::chrono::steady_clock::now() < deadline) { + PumpUntil(platform, isolate, *ran); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); + } + if (ran->load() != 1) Fail("delayed task: ran %d times after its delay, expected 1", ran->load()); +} + +// A cleared waker isn't called. The task still runs at the next pump. +void ClearedWakerIsNotCalled(EdgeV8Platform* platform, + v8::Isolate* isolate, + const std::shared_ptr& runner) { + EdgeV8Platform::SetForegroundTaskWaker(RecordingWaker); + EdgeV8Platform::SetForegroundTaskWaker(nullptr); + const RunCount ran = NewRunCount(); + const size_t calls = PostFromAnotherThread([&] { + runner->PostTask(std::make_unique(ran)); + }); + if (calls != 0) Fail("cleared waker: called %zu times, expected 0", calls); + PumpUntil(platform, isolate, *ran); + if (ran->load() != 1) { + Fail("cleared waker: the task ran %d times at the next pump, expected 1", ran->load()); + } +} + +// A task for an isolate with a target bound goes to that target, which runs it +// on its own: the waker is only for the queue a checkpoint runs. +void BoundTargetDoesNotWake(EdgeV8Platform* platform, + v8::Isolate* isolate, + const std::shared_ptr& runner) { + EdgeV8Platform::SetForegroundTaskWaker(RecordingWaker); + BoundTarget bound; + int env_tag = 0; + auto* env = reinterpret_cast(&env_tag); + if (!platform->BindForegroundTaskTarget(isolate, env, EnqueueOnTarget, &bound)) { + Fail("bound target: BindForegroundTaskTarget failed"); + return; + } + const RunCount ran = NewRunCount(); + const size_t calls = PostFromAnotherThread([&] { + runner->PostTask(std::make_unique(ran)); + }); + platform->ClearForegroundTaskTarget(isolate, env); + if (calls != 0) Fail("bound target: called the waker %zu times, expected 0", calls); + + std::vector entries; + { + std::lock_guard lock(bound.mutex); + entries.swap(bound.entries); + } + if (entries.size() != 1) Fail("bound target: got %zu tasks, expected 1", entries.size()); + for (const BoundTarget::Entry& entry : entries) { + entry.callback(env, entry.data); + if (entry.cleanup != nullptr) entry.cleanup(env, entry.data); + } + if (ran->load() != 1) Fail("bound target: the task ran %d times, expected 1", ran->load()); +} + +} // namespace + +// Own V8 initialization and disposal in a separate process: the waker is +// process-wide. +int main() { + EdgeV8Platform::SetWorkerThreadCount(2); + auto platform = EdgeV8Platform::Create(); + if (!platform) return 1; + v8::V8::InitializePlatform(platform.get()); + if (!v8::V8::Initialize()) return 1; + auto allocator = std::unique_ptr( + v8::ArrayBuffer::Allocator::NewDefaultAllocator()); + v8::Isolate::CreateParams params; + params.array_buffer_allocator = allocator.get(); + v8::Isolate* isolate = v8::Isolate::New(params); + if (!isolate) return 1; + { + v8::Isolate::Scope isolate_scope(isolate); + v8::HandleScope handle_scope(isolate); + // No target is bound, so the isolate's foreground tasks wait in the + // default platform's queue, which only PumpPendingForegroundTasks runs. + auto runner = platform->GetForegroundTaskRunner(isolate, v8::TaskPriority::kUserVisible); + PumpAll(platform.get(), isolate); + + ImmediateTaskWakes(platform.get(), isolate, runner); + DelayedTaskDoesNotWake(platform.get(), isolate, runner); + ClearedWakerIsNotCalled(platform.get(), isolate, runner); + BoundTargetDoesNotWake(platform.get(), isolate, runner); + EdgeV8Platform::SetForegroundTaskWaker(nullptr); + PumpAll(platform.get(), isolate); + } + platform->DisposeIsolate(isolate); + v8::V8::Dispose(); + v8::V8::DisposePlatform(); + if (g_passed) std::printf("foreground task waker: ok\n"); + return g_passed ? 0 : 1; +} diff --git a/v8/src/edge_v8_platform.cc b/v8/src/edge_v8_platform.cc index bbc3bb8ac..1dbce227c 100644 --- a/v8/src/edge_v8_platform.cc +++ b/v8/src/edge_v8_platform.cc @@ -16,6 +16,13 @@ namespace { +// EdgeV8Platform::SetForegroundTaskWaker. +std::atomic g_foreground_task_waker{nullptr}; + +void WakeForForegroundTask() { + if (void (*waker)() = g_foreground_task_waker.load(std::memory_order_acquire)) waker(); +} + struct WorkerWarmupState { std::mutex mutex; std::condition_variable cv; @@ -254,6 +261,10 @@ class EdgeV8Platform::ForegroundTaskRunner final : public v8::TaskRunner { } if (delay_ms == 0) { runner->PostTask(std::move(counted_task), location); + // Only a checkpoint runs it (PumpPendingForegroundTasks): wake the + // embedder so that one comes now. A delayed task waits for whatever + // checkpoint follows its delay, as before. + WakeForForegroundTask(); } else { runner->PostDelayedTask(std::move(counted_task), delay_ms / 1000.0, location); } @@ -318,6 +329,10 @@ bool EdgeV8Platform::SetWorkerThreadCount(int count) { return true; } +void EdgeV8Platform::SetForegroundTaskWaker(void (*waker)()) { + g_foreground_task_waker.store(waker, std::memory_order_release); +} + std::unique_ptr EdgeV8Platform::Create() { g_platform_created.store(true, std::memory_order_release); std::unique_ptr fallback = v8::platform::NewDefaultPlatform( diff --git a/v8/src/edge_v8_platform.h b/v8/src/edge_v8_platform.h index c0d947675..d8b25e366 100644 --- a/v8/src/edge_v8_platform.h +++ b/v8/src/edge_v8_platform.h @@ -31,6 +31,27 @@ class EdgeV8Platform final : public v8::Platform { // this has no effect afterwards; it returns false in that case. static bool SetWorkerThreadCount(int count); + // Sets a function to call when an immediate foreground task is queued + // where only an event-loop checkpoint runs it. For an isolate with no + // foreground task target bound (see BindForegroundTaskTarget), a task waits + // in the default platform's queue until PumpPendingForegroundTasks runs it. + // V8 posts such tasks from any thread, for example to resolve an + // Atomics.waitAsync that another thread's Atomics.notify woke, or to finish + // a WebAssembly.compile. An embedder whose thread sleeps until its next + // event can wake that thread from this function. + // + // The function is process-wide: it is called for every isolate's tasks, on + // the thread that posted the task, after the task is queued. That thread + // can be the isolate's own, in the middle of running JS or a garbage + // collection, and V8 may hold its own locks then. So the function must be + // quick, must not call into V8, and must not take a lock that the + // isolate's thread may hold. A delayed task doesn't call it, and runs at + // the first checkpoint after its delay. nullptr clears it. A thread that + // read the previous function just before a change may still call it once + // afterwards. It takes no data pointer, so that one atomic store publishes + // it. + static void SetForegroundTaskWaker(void (*waker)()); + ~EdgeV8Platform() override; bool RegisterIsolate(v8::Isolate* isolate);