v8/test/mjsunit/shared-memory/mutex.js
Shu-yu Guo 6c4b2e0ef2 [shared-struct] Fix unlocking in JSAtomicsMutex
Errors in the callback were not correctly unlocking the mutex, oops.

Bug: v8:12547
Change-Id: If44ebc023b8192605c9f29bfd4099a197110f5c4
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/3760986
Commit-Queue: Shu-yu Guo <syg@chromium.org>
Reviewed-by: Adam Klein <adamk@chromium.org>
Cr-Commit-Position: refs/heads/main@{#81708}
2022-07-14 01:03:57 +00:00

47 lines
1.3 KiB
JavaScript

// Copyright 2022 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// Flags: --harmony-struct
let mutex = new Atomics.Mutex;
let locked_count = 0;
assertEquals(42, Atomics.Mutex.lock(mutex, () => {
locked_count++; return 42;
}));
assertEquals(locked_count, 1);
// tryLock returns true when successful.
assertTrue(Atomics.Mutex.tryLock(mutex, () => { locked_count++; }));
assertEquals(locked_count, 2);
// Recursively locking throws.
Atomics.Mutex.lock(mutex, () => {
locked_count++;
assertThrows(() => {
Atomics.Mutex.lock(mutex, () => { throw "unreachable"; });
}, Error);
});
assertEquals(locked_count, 3);
// Recursive tryLock'ing returns false.
Atomics.Mutex.lock(mutex, () => {
locked_count++;
assertFalse(Atomics.Mutex.tryLock(mutex, () => { throw "unreachable"; }));
});
assertEquals(locked_count, 4);
// Throwing in the callback should unlock the mutex.
assertThrowsEquals(() => {
Atomics.Mutex.lock(mutex, () => { throw 42; });
}, 42);
Atomics.Mutex.lock(mutex, () => { locked_count++; });
assertEquals(locked_count, 5);
assertThrowsEquals(() => {
Atomics.Mutex.tryLock(mutex, () => { throw 42; });
}, 42);
Atomics.Mutex.tryLock(mutex, () => { locked_count++; });
assertEquals(locked_count, 6);