[locks] Fixed typo in timedLock function and added some missed methods (#42373)

* Fixed typo in timedLock function and added some missed methods

* Added some tests for create helpers and timedLock method

* Fixed lint issues
This commit is contained in:
Alexey Ponomarev 2020-02-18 01:33:52 +01:00 committed by GitHub
parent c74c276c95
commit 670dc42a30
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 1 deletions

View File

@ -1,6 +1,7 @@
// Type definitions for Locks 0.2
// Project: https://github.com/Wizcorp/locks
// Definitions by: Joshua Graham <https://github.com/flippynips>
// Alexey Ponomarev <https://github.com/alexey-detr>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
@ -13,7 +14,7 @@ export class Mutex {
/** 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;
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. */
@ -67,3 +68,11 @@ export class CondVariable {
/** Set the conditional variable value. */
set(value: any): void;
}
export function createCondVariable(initialValue: any): CondVariable;
export function createSemaphore(initialValue: number): Semaphore;
export function createMutex(): Mutex;
export function createReadWriteLock(): ReadWriteLock;

View File

@ -3,14 +3,17 @@ import locks = require("locks");
// Mutex
const mutex: locks.Mutex = new locks.Mutex();
mutex.lock(() => {});
mutex.timedLock(1000, () => {});
if (mutex.tryLock()) { console.log('Should not happen'); }
mutex.unlock();
const createMutex: locks.Mutex = locks.createMutex();
// Semaphore
const semaphore: locks.Semaphore = new locks.Semaphore(1);
semaphore.wait(() => {});
semaphore.wait(() => {});
semaphore.signal();
const createSemaphore: locks.Semaphore = locks.createSemaphore(1);
// Read Write Lock
const readWriteLock: locks.ReadWriteLock = new locks.ReadWriteLock();
@ -20,6 +23,7 @@ readWriteLock.unlock();
readWriteLock.writeLock(() => {});
if (readWriteLock.tryWriteLock()) { console.log('Should not happen'); }
readWriteLock.unlock();
const createReadWriteLock: locks.ReadWriteLock = locks.createReadWriteLock();
// Conditional Variable
const condVariable: locks.CondVariable = new locks.CondVariable('ho');
@ -27,3 +31,4 @@ if (condVariable.get() !== 'ho') { console.log('Should not happen'); }
condVariable.wait('hi', () => {});
condVariable.set('hi');
if (condVariable.get() !== 'hi') { console.log('Should not happen'); }
const createCondVariable: locks.CondVariable = locks.createCondVariable('ho');