UNPKG

nanolith

Version:

Multi-threading in no time with seamless TypeScript support.

1 lines 412 B
export const createMutex=()=>{const t=new Int32Array(new SharedArrayBuffer(4));return t.set([0]),t};export const lockMutexSync=t=>{0!==Atomics.compareExchange(t,0,0,1)&&(Atomics.wait(t,0,1,1/0),lockMutexSync(t))};export const lockMutex=t=>new Promise((e=>{lockMutexSync(t),e()}));export const unlockMutex=t=>{if(1!==Atomics.compareExchange(t,0,1,0))throw new Error("Mutex inconsistency.");Atomics.notify(t,0,1)};