Skip to content

Instantly share code, notes, and snippets.

@sumanssaurabh
Forked from gregkorossy/semaphore.js
Created September 13, 2020 13:31
Show Gist options
  • Save sumanssaurabh/cdaa0761b8fcc4ffe8464e7335fdb258 to your computer and use it in GitHub Desktop.
Save sumanssaurabh/cdaa0761b8fcc4ffe8464e7335fdb258 to your computer and use it in GitHub Desktop.

Revisions

  1. @gregkorossy gregkorossy revised this gist Mar 14, 2018. 1 changed file with 24 additions and 10 deletions.
    34 changes: 24 additions & 10 deletions semaphore.js
    Original file line number Diff line number Diff line change
    @@ -6,17 +6,19 @@ function Semaphore(max) {
    if (waiting.length > 0 && counter < max){
    counter++;
    let promise = waiting.shift();
    promise();
    promise.resolve();
    }
    }

    this.acquire = function() {
    if(counter < max) {
    counter++
    return;
    } else {
    return new Promise(resolve => {
    waiting.push(resolve);
    resolve();
    });
    } else {
    return new Promise((resolve, err) => {
    waiting.push({resolve: resolve, err: err});
    });
    }
    }
    @@ -28,7 +30,12 @@ function Semaphore(max) {

    this.purge = function() {
    let unresolved = waiting.length;
    counter = 0;

    for (let i = 0; i < unresolved; i++) {
    waiting[i].err('Task has been purged.');
    }

    counter = 0;
    waiting = [];

    return unresolved;
    @@ -41,11 +48,15 @@ let sema = new Semaphore(2);

    async function test(id) {
    console.log('queueing task', id);
    await sema.acquire();
    console.log('running task', id);
    setTimeout(() => {
    try {
    await sema.acquire();
    console.log('running task', id);
    setTimeout(() => {
    sema.release();
    }, 2000);
    }, 2000);
    } catch (e) {
    console.error(id, e);
    }
    }

    test(1);
    @@ -64,4 +75,7 @@ setTimeout(() => {
    test(20);
    test(21);
    test(22);
    }, 2700);
    }, 2700);

    // PURGE TEST
    // setTimeout(() => {sema.purge();}, 2200);
  2. @gregkorossy gregkorossy revised this gist Mar 14, 2018. 1 changed file with 19 additions and 19 deletions.
    38 changes: 19 additions & 19 deletions semaphore.js
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,17 @@ function Semaphore(max) {
    var waiting = [];

    var take = function() {
    if (waiting.length > 0 && counter < max){
    counter++;
    if (waiting.length > 0 && counter < max){
    counter++;
    let promise = waiting.shift();
    promise();
    promise();
    }
    }

    this.acquire = function() {
    if(counter < max) {
    counter++
    return;
    counter++
    return;
    } else {
    return new Promise(resolve => {
    waiting.push(resolve);
    @@ -27,8 +27,8 @@ function Semaphore(max) {
    }

    this.purge = function() {
    let unresolved = waiting.length;
    counter = 0;
    let unresolved = waiting.length;
    counter = 0;
    waiting = [];

    return unresolved;
    @@ -40,12 +40,12 @@ function Semaphore(max) {
    let sema = new Semaphore(2);

    async function test(id) {
    console.log('queueing task', id);
    await sema.acquire();
    console.log('running task', id);
    setTimeout(() => {
    sema.release();
    }, 2000);
    console.log('queueing task', id);
    await sema.acquire();
    console.log('running task', id);
    setTimeout(() => {
    sema.release();
    }, 2000);
    }

    test(1);
    @@ -55,13 +55,13 @@ test(4);
    test(5);

    setTimeout(() => {
    test(10);
    test(11);
    test(12);
    test(10);
    test(11);
    test(12);
    }, 1500);

    setTimeout(() => {
    test(20);
    test(21);
    test(22);
    test(20);
    test(21);
    test(22);
    }, 2700);
  3. @gregkorossy gregkorossy created this gist Mar 14, 2018.
    67 changes: 67 additions & 0 deletions semaphore.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,67 @@
    function Semaphore(max) {
    var counter = 0;
    var waiting = [];

    var take = function() {
    if (waiting.length > 0 && counter < max){
    counter++;
    let promise = waiting.shift();
    promise();
    }
    }

    this.acquire = function() {
    if(counter < max) {
    counter++
    return;
    } else {
    return new Promise(resolve => {
    waiting.push(resolve);
    });
    }
    }

    this.release = function() {
    counter--;
    take();
    }

    this.purge = function() {
    let unresolved = waiting.length;
    counter = 0;
    waiting = [];

    return unresolved;
    }
    }

    // testing the semaphore

    let sema = new Semaphore(2);

    async function test(id) {
    console.log('queueing task', id);
    await sema.acquire();
    console.log('running task', id);
    setTimeout(() => {
    sema.release();
    }, 2000);
    }

    test(1);
    test(2);
    test(3);
    test(4);
    test(5);

    setTimeout(() => {
    test(10);
    test(11);
    test(12);
    }, 1500);

    setTimeout(() => {
    test(20);
    test(21);
    test(22);
    }, 2700);