mirror of
https://github.com/TheBinaryNinja/tvapp2.git
synced 2026-06-04 08:35:41 -04:00
48 lines
803 B
JavaScript
48 lines
803 B
JavaScript
/*
|
|
Semaphore > Declare
|
|
|
|
allows multiple threads to work with the same shared resources
|
|
*/
|
|
|
|
class Semaphore
|
|
{
|
|
constructor( max )
|
|
{
|
|
this.max = max;
|
|
this.queue = [];
|
|
this.active = 0;
|
|
}
|
|
|
|
async acquire()
|
|
{
|
|
if ( this.active < this.max )
|
|
{
|
|
this.active++;
|
|
return;
|
|
}
|
|
|
|
return new Promise( ( resolve ) => this.queue.push( resolve ) );
|
|
}
|
|
|
|
release()
|
|
{
|
|
this.active--;
|
|
if ( this.queue.length > 0 )
|
|
{
|
|
const resolve = this.queue.shift();
|
|
this.active++;
|
|
resolve();
|
|
}
|
|
}
|
|
}
|
|
|
|
/*
|
|
export class
|
|
|
|
@usage import Log from './classes/Log.js';
|
|
*/
|
|
|
|
// eslint-disable-next-line no-restricted-syntax
|
|
export default Semaphore;
|
|
|