Merge pull request #29215 from flippynips/master

Addition of 'locks' definitions.
This commit is contained in:
Andrew Casey
2018-09-27 17:46:56 -07:00
committed by GitHub
4 changed files with 123 additions and 0 deletions

69
types/locks/index.d.ts vendored Normal file
View File

@@ -0,0 +1,69 @@
// Type definitions for Locks 0.2
// Project: https://github.com/Wizcorp/locks
// Definitions by: Joshua Graham <https://github.com/flippynips>
// 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;
}

View File

@@ -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'); }

24
types/locks/tsconfig.json Normal file
View File

@@ -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"
]
}

1
types/locks/tslint.json Normal file
View File

@@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }