From 0e3d4fcbd47b12eabc573cd4be28007c27df3548 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Tue, 19 Oct 2021 18:42:29 +0000 Subject: [PATCH] dawn_node: Prevent setImmediate() being queued multiple times The AsyncRunner will enqueue a call to `Device::Tick()` when the runner count moves from 0 async tasks to 1. It has been observed that some 'async' tasks are actually synchronious, which results in multiple tick callbacks being enqueued before the first has a chance to run. Fix this by using another boolean to track whether the function has been queued. Bug: dawn:1127 Change-Id: I7dd81d33d601bf1d3cefb5c4dad6c237883e51ee Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/66820 Auto-Submit: Ben Clayton Reviewed-by: Antonio Maiorano Commit-Queue: Ben Clayton --- src/dawn_node/binding/AsyncRunner.cpp | 5 +++++ src/dawn_node/binding/AsyncRunner.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/dawn_node/binding/AsyncRunner.cpp b/src/dawn_node/binding/AsyncRunner.cpp index 7162b2e754..788abc2410 100644 --- a/src/dawn_node/binding/AsyncRunner.cpp +++ b/src/dawn_node/binding/AsyncRunner.cpp @@ -37,6 +37,10 @@ namespace wgpu { namespace binding { void AsyncRunner::QueueTick() { // TODO(crbug.com/dawn/1127): We probably want to reduce the frequency at which this gets // called. + if (tick_queued_) { + return; + } + tick_queued_ = true; env_.Global() .Get("setImmediate") .As() @@ -44,6 +48,7 @@ namespace wgpu { namespace binding { // TODO(crbug.com/dawn/1127): Create once, reuse. Napi::Function::New(env_, [this](const Napi::CallbackInfo&) { + tick_queued_ = false; if (count_ > 0) { device_.Tick(); QueueTick(); diff --git a/src/dawn_node/binding/AsyncRunner.h b/src/dawn_node/binding/AsyncRunner.h index 83644c09af..a86ee3a0b6 100644 --- a/src/dawn_node/binding/AsyncRunner.h +++ b/src/dawn_node/binding/AsyncRunner.h @@ -45,6 +45,7 @@ namespace wgpu { namespace binding { Napi::Env env_; wgpu::Device const device_; uint64_t count_ = 0; + bool tick_queued_ = false; }; // AsyncTask is a RAII helper for calling AsyncRunner::Begin() on construction, and