//================================================================ /** * @packageDocumentation * @module std.internal */ //================================================================ export namespace SafeLock { export async function lock( locker: () => Promise, unlocker: () => Promise, lambda: () => void | Promise, ): Promise { await locker(); await _Process(unlocker, lambda); } export async function try_lock( locker: () => Promise, unlocker: () => Promise, lambda: () => void | Promise, ): Promise { if ((await locker()) === false) return false; await _Process(unlocker, lambda); return true; } async function _Process( unlocker: () => Promise, lambda: () => void | Promise, ): Promise { // PROCESS THE CRITICAL SECTION try { await lambda(); } catch (error) { await unlocker(); throw error; } // TERMINATE THE CRITICAL SECTION await unlocker(); } }