diff --git a/types/locks/index.d.ts b/types/locks/index.d.ts new file mode 100644 index 0000000000..8c112d65d1 --- /dev/null +++ b/types/locks/index.d.ts @@ -0,0 +1,69 @@ +// Type definitions for Locks 0.2 +// Project: https://github.com/Wizcorp/locks +// Definitions by: Joshua Graham +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/** Solitary access lock */ +export class Mutex { + /** Construct a new mutex lock. */ + constructor(); + /** Flag indicating whether the lock is currently taken. */ + isLocked: boolean; + /** Take the lock. */ + lock(callback: () => void): void; + /** Wait the specified number of milliseconds to take the lock. If error is set, the lock wasn't taken. */ + timedlock(ttl: number, callback: (error: Error) => void): void; + /** Try taking the lock. If false, the lock wasn't taken. */ + tryLock(): boolean; + /** Release the current lock. */ + unlock(): void; + /** Clear any waiting lock callbacks. */ + resetQueue(): void; +} + +/** Reader writer lock */ +export class ReadWriteLock { + /** Construct a new reader writer lock */ + constructor(); + /** Flag indicating whether the reader writer lock is holding the read lock */ + isReadLocked: boolean; + /** Flag indicating whether the reader writer lock is holding the write lock */ + isWriteLocked: boolean; + /** Get the read lock */ + readLock(callback: () => void): void; + /** Get the write lock */ + writeLock(callback: () => void): void; + /** Wait the specified number of milliseconds to take the read lock. If error is set, the lock wasn't taken. */ + timedReadLock(ttl: number, callback: (error: Error) => void): void; + /** Wait the specified number of milliseconds to take the write lock. If error is set, the lock wasn't taken. */ + timedWriteLock(ttl: number, callback: (error: Error) => void): void; + /** Try taking the read lock. If false, the lock wasn't taken. */ + tryReadLock(): boolean; + /** Try taking the write lock. If false, the lock wasn't taken. */ + tryWriteLock(): boolean; + /** Release the write lock if taken, or one of the taken read locks. */ + unlock(): void; +} + +/** Semaphore signaller. */ +export class Semaphore { + /** Construct a new semaphore. */ + constructor(initialCount: number); + /** Queue a callback. Callbacks are called in sequence on signals. */ + wait(callback: () => void): void; + /** Signal a callback. */ + signal(): void; +} + +/** Conditional variable instance */ +export class CondVariable { + /** Construct a new conditional variable with the specified initial value. */ + constructor(initialValue: any); + /** Get the current conditional variable value. */ + get(): any; + /** Add a callback when the specified conditional variable value matches the specified value. */ + wait(value: any, callback: () => void): void; + /** Set the conditional variable value. */ + set(value: any): void; +} diff --git a/types/locks/locks-tests.ts b/types/locks/locks-tests.ts new file mode 100644 index 0000000000..1e7d768a0c --- /dev/null +++ b/types/locks/locks-tests.ts @@ -0,0 +1,29 @@ +import locks = require("locks"); + +// Mutex +const mutex: locks.Mutex = new locks.Mutex(); +mutex.lock(() => {}); +if (mutex.tryLock()) { console.log('Should not happen'); } +mutex.unlock(); + +// Semaphore +const semaphore: locks.Semaphore = new locks.Semaphore(1); +semaphore.wait(() => {}); +semaphore.wait(() => {}); +semaphore.signal(); + +// Read Write Lock +const readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock(); +readWriteLock.readLock(() => {}); +if (readWriteLock.tryReadLock()) { console.log('Should not happen'); } +readWriteLock.unlock(); +readWriteLock.writeLock(() => {}); +if (readWriteLock.tryWriteLock()) { console.log('Should not happen'); } +readWriteLock.unlock(); + +// Conditional Variable +const condVariable: locks.CondVariable = new locks.CondVariable('ho'); +if (condVariable.get() !== 'ho') { console.log('Should not happen'); } +condVariable.wait('hi', () => {}); +condVariable.set('hi'); +if (condVariable.get() !== 'hi') { console.log('Should not happen'); } diff --git a/types/locks/tsconfig.json b/types/locks/tsconfig.json new file mode 100644 index 0000000000..123bb4c13f --- /dev/null +++ b/types/locks/tsconfig.json @@ -0,0 +1,24 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6", + "dom" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "locks-tests.ts" + ] +} \ No newline at end of file diff --git a/types/locks/tslint.json b/types/locks/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/locks/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }