-
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #50 from matepek/development
Development
- Loading branch information
Showing
12 changed files
with
218 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,57 @@ | ||
export class TaskPool { | ||
/** | ||
* | ||
* @param availableSlot The available slot number. If -1 (negative) means no limit, acquire will always return true. | ||
* @param maxTaskCount Has to be bigger than 0 or `undefined`. | ||
*/ | ||
constructor(private availableSlot: number) {} | ||
constructor(private _maxTaskCount: number | undefined) { | ||
if (_maxTaskCount != undefined && _maxTaskCount < 1) throw Error('invalid argument'); | ||
} | ||
|
||
get maxTaskCount(): number | undefined { | ||
return this._maxTaskCount; | ||
} | ||
|
||
set maxTaskCount(maxTaskCount: number | undefined) { | ||
if (maxTaskCount != undefined && maxTaskCount < 1) throw Error('invalid argument'); | ||
this._maxTaskCount = maxTaskCount; | ||
|
||
while (this._waitingTasks.length > 0 && this._acquire()) | ||
this._waitingTasks.pop()!(); | ||
} | ||
|
||
acquire(): boolean { | ||
if (this.availableSlot < 0) return true; | ||
if (this.availableSlot == 0) return false; | ||
this.availableSlot -= 1; | ||
return true; | ||
scheduleTask<TResult>(task: () => TResult | PromiseLike<TResult>): Promise<TResult> { | ||
return new Promise<void>(resolve => { | ||
if (this._acquire()) | ||
resolve(); | ||
else | ||
this._waitingTasks.unshift(resolve); | ||
}).then(task) | ||
.then( | ||
(v: TResult) => { | ||
this._release(); | ||
return v; | ||
}, | ||
(reason?: any) => { | ||
this._release(); | ||
throw reason; | ||
}); | ||
} | ||
|
||
release(): void { | ||
if (this.availableSlot < 0) return; | ||
this.availableSlot += 1; | ||
private _runningTaskCount: number = 0; | ||
private readonly _waitingTasks: (() => void)[] = []; | ||
|
||
private _acquire(): boolean { | ||
if (this._maxTaskCount === undefined || this._runningTaskCount < this._maxTaskCount) { | ||
this._runningTaskCount += 1; | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
private _release(): void { | ||
this._runningTaskCount -= 1; | ||
|
||
while (this._waitingTasks.length > 0 && this._acquire()) | ||
this._waitingTasks.pop()!(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//----------------------------------------------------------------------------- | ||
// vscode-catch2-test-adapter was written by Mate Pek, and is placed in the | ||
// public domain. The author hereby disclaims copyright to this source code. | ||
|
||
export class TaskQueue { | ||
constructor(depends: Iterable<TaskQueue> = [], | ||
public readonly name?: string) { | ||
this._depends = [...depends]; | ||
// TODO check circular dependency | ||
} | ||
|
||
empty(): boolean { | ||
return this._count == 0; | ||
} | ||
|
||
get size(): number { | ||
return this._count; | ||
} | ||
|
||
get length(): number { | ||
return this._count; | ||
} | ||
|
||
then<TResult1>( | ||
task: (() => TResult1 | PromiseLike<TResult1>)): Promise<TResult1> { | ||
this._count++; | ||
|
||
const previous = this._queue; | ||
|
||
const current = Promise.all(this._depends.map(v => v._queue)) | ||
.then(() => { | ||
return previous.then(task); | ||
}); | ||
|
||
const decr = () => { this._count--; }; | ||
|
||
this._queue = current.then(decr, decr); | ||
|
||
return current; | ||
} | ||
|
||
dependsOn(depends: Iterable<TaskQueue>): void { | ||
for (const dep of depends) { | ||
this._depends.push(dep); | ||
} | ||
// TODO check circular dependency | ||
} | ||
|
||
private _count: number = 0; | ||
private _queue: Promise<void> = Promise.resolve(); | ||
private readonly _depends: Array<TaskQueue>; | ||
} |
Oops, something went wrong.