Skip to content

Commit 7fb8674

Browse files
committed
Improve with Atomics.exchange, thanks to @belochub
1 parent f0f0747 commit 7fb8674

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

JavaScript/5-no-race.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ class Mutex {
1414
}
1515

1616
async enter() {
17-
Atomics.wait(this.lock, 0, LOCKED);
18-
Atomics.store(this.lock, 0, LOCKED);
17+
let prev = Atomics.exchange(this.lock, 0, LOCKED);
18+
while (prev !== UNLOCKED) {
19+
Atomics.wait(this.lock, 0, LOCKED);
20+
prev = Atomics.exchange(this.lock, 0, LOCKED);
21+
}
1922
this.owner = true;
2023
return true;
2124
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy