Skip to content

Instantly share code, notes, and snippets.

@scorredoira
Created April 27, 2017 10:07
Show Gist options
  • Save scorredoira/1d925a58f4bcd9189fc74e2ac5414be5 to your computer and use it in GitHub Desktop.
Save scorredoira/1d925a58f4bcd9189fc74e2ac5414be5 to your computer and use it in GitHub Desktop.
namespace web {
let global = sync.newMutex();
let locks: Map<sync.Mutex> = <any>{}
export function getLockedMutex(key: string): sync.Mutex {
global.lock()
try {
let m = locks[key]
if (!m) {
m = sync.newMutex()
locks[key] = m;
}
// lock the mutex inside the global lock
m.lock();
return m;
}
finally {
global.unlock()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment