aboutsummaryrefslogtreecommitdiff
path: root/common/lock.js
blob: 6cf0835dc50e02f5420d0ef21cbd5a8b97db127f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
 * This file is part of Haketilo.
 *
 * Function: Implement a lock (aka binary semaphore aka mutex).
 *
 * Copyright (C) 2021 Wojtek Kosior
 * Redistribution terms are gathered in the `copyright' file.
 */

/*
 * Javascript runs single-threaded, with an event loop. Because of that,
 * explicit synchronization is often not needed. An exception is when we use
 * an API function that must wait. Ajax is an example. Callback passed to ajax
 * call doesn't get called immediately, but after some time. In the meantime
 * some other piece of code might get to execute and modify some variables.
 * Access to WebExtension local storage is another situation where this problem
 * can occur.
 *
 * This is a solution. A lock object, that can be used to delay execution of
 * some code until other code finishes its critical work. Locking is wrapped
 * in a promise.
 */

function make_lock() {
    return {free: true, queue: []};
}

function _lock(lock, cb) {
    if (lock.free) {
	lock.free = false;
	setTimeout(cb);
    } else {
	lock.queue.push(cb);
    }
}

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

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

    if (lock.queue.length === 0) {
	lock.free = true;
    } else {
	let cb = lock.queue[0];
	lock.queue.splice(0, 1);
	setTimeout(cb);
    }
}

/*
 * EXPORTS_START
 * EXPORT make_lock
 * EXPORT lock
 * EXPORT unlock
 * EXPORTS_END
 */