Project

General

Profile

« Previous | Next » 

Revision 261548ff

Added by koszko about 2 years ago

emply an sh-based build system; make some changes to blocking

View differences:

common/lock.js
19 19
 * in a promise.
20 20
 */
21 21

  
22
"use strict";
23

  
24
(() => {
25
    function make_lock() {
26
	return {free: true, queue: []};
22
function make_lock() {
23
    return {free: true, queue: []};
24
}
25

  
26
function _lock(lock, cb) {
27
    if (lock.free) {
28
	lock.free = false;
29
	setTimeout(cb);
30
    } else {
31
	lock.queue.push(cb);
27 32
    }
28

  
29
    function _lock(lock, cb) {
30
	if (lock.free) {
31
	    lock.free = false;
32
	    setTimeout(cb);
33
	} else {
34
	    lock.queue.push(cb);
35
	}
33
}
34

  
35
function lock(lock) {
36
    return new Promise((resolve, reject) => _lock(lock, resolve));
37
}
38

  
39
function unlock(lock) {
40
    if (lock.free)
41
	throw new Exception("Attempting to release a free lock");
42

  
43
    if (lock.queue.length === 0) {
44
	lock.free = true;
45
    } else {
46
	let cb = lock.queue[0];
47
	lock.queue.splice(0, 1);
48
	setTimeout(cb);
36 49
    }
50
}
37 51

  
38
    function lock(lock) {
39
	return new Promise((resolve, reject) => _lock(lock, resolve));
40
    }
41

  
42
    function unlock(lock) {
43
	if (lock.free)
44
	    throw new Exception("Attempting to release a free lock");
45

  
46
	if (lock.queue.length === 0) {
47
	    lock.free = true;
48
	} else {
49
	    let cb = lock.queue[0];
50
	    lock.queue.splice(0, 1);
51
	    setTimeout(cb);
52
	}
53
    }
54

  
55
    window.make_lock = make_lock;
56
    window.lock = lock;
57
    window.unlock = unlock;
58
})();
52
/*
53
 * EXPORTS_START
54
 * EXPORT make_lock
55
 * EXPORT lock
56
 * EXPORT unlock
57
 * EXPORTS_END
58
 */

Also available in: Unified diff