From 403c10041606d0def68f7a077c4ffeea5d406423 Mon Sep 17 00:00:00 2001 From: webreflection Date: Wed, 7 May 2025 13:04:37 +0200 Subject: [PATCH] Allow maxSpinWait option --- src/worker.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/worker.js b/src/worker.js index f42a20c..6990eaf 100644 --- a/src/worker.js +++ b/src/worker.js @@ -44,8 +44,12 @@ export default async options => { ({ pause, wait } = Atomics); // prefer the fast path when possible if (pause && !WORKAROUND && !(sab instanceof ArrayBuffer)) { + const MAX_SPIN_LOCK = options?.maxSpinLock || Infinity; + const $wait = wait; wait = (view, index) => { - while (view[index] < 1) pause(); + let spin = 0; + while (view[index] < 1 && spin++ < MAX_SPIN_LOCK) pause(); + if (view[index] < 1) $wait(view, index); }; } }