Skip to content

Commit

Permalink
[locks] Fixed typo in timedLock function and added some missed methods (
Browse files Browse the repository at this point in the history
#42373)

* Fixed typo in timedLock function and added some missed methods

* Added some tests for create helpers and timedLock method

* Fixed lint issues
  • Loading branch information
alexey-detr committed Feb 18, 2020
1 parent c74c276 commit 670dc42
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
11 changes: 10 additions & 1 deletion types/locks/index.d.ts
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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. */
Expand Down Expand Up @@ -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;
5 changes: 5 additions & 0 deletions types/locks/locks-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -20,10 +23,12 @@ 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');
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');

0 comments on commit 670dc42

Please sign in to comment.