-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
181 lines (171 loc) · 3.69 KB
/
index.js
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function createLock({maxActiveReader = Infinity} = {}) {
let firstInactiveTask;
let lastTask;
let activeReader = 0;
const self = {
read: fn => que(fn, false),
write: fn => que(fn, true),
length: 0
};
return self;
function que(fn, block) {
const task = createTask({fn, block});
if (!lastTask) {
firstInactiveTask = lastTask = task;
} else {
lastTask.next = task;
task.prev = lastTask;
lastTask = task;
if (!firstInactiveTask) {
firstInactiveTask = task;
}
}
self.length++;
deque();
return task.q.promise;
}
function defer() {
const o = {};
o.promise = new Promise((resolve, reject) => {
o.resolve = resolve;
o.reject = reject;
});
return o;
}
function createTask({
fn,
block = false,
prev,
next,
q = defer(),
q2 = fn.length ? defer() : null
}) {
return {fn, block, prev, next, q, q2};
}
function deque() {
const task = firstInactiveTask;
if (
!task ||
task.block && task.prev ||
task.prev && task.prev.block ||
activeReader >= maxActiveReader
) {
return;
}
if (!task.block) {
activeReader++;
}
firstInactiveTask = task.next;
let result;
try {
result = task.fn(task.q2 && task.q2.resolve);
} catch (err) {
task.q.reject(err);
// auto release with sync error
// q2 is useless in this case
onDone();
return;
}
if (task.q2) {
task.q2.promise.then(_onDone);
}
if (result && result.then) {
const pending = result.then(task.q.resolve, task.q.reject);
if (!task.q2) {
pending.then(onDone);
}
} else {
task.q.resolve(result);
if (!task.q2) {
// it's a sync function and you don't want to release it manually, why
// do you need a lock?
onDone();
return;
}
}
deque();
function onDone() {
_onDone();
}
function _onDone(afterDone) {
if (task.prev) {
task.prev.next = task.next;
}
if (task.next) {
task.next.prev = task.prev;
}
if (lastTask === task) {
lastTask = task.prev;
}
if (!task.block) {
activeReader--;
}
self.length--;
if (afterDone) {
afterDone();
}
deque();
}
}
}
function createLockPool(options) {
const locks = new Map; // scope -> lock
return {
read: (scope, fn) => op(scope, fn, "read"),
write: (scope, fn) => op(scope, fn, "write"),
locks
};
async function op(scopeIter, fn, opType) {
// FIXME: dead lock if there are duplicated scopes?
const scopes = [];
const acquiring = [];
for (const scope of scopeIter) {
let lock = locks.get(scope);
if (!lock) {
lock = createLock(options);
locks.set(scope, lock);
}
const o = {
lock,
scope,
relase: null
};
acquiring.push(lock[opType](release => {
o.release = release;
}));
scopes.push(o);
}
await Promise.all(acquiring);
let result;
try {
result = fn(fn.length && onDone);
} catch (err) {
onDone();
throw err;
}
if (result && result.then) {
if (!fn.length) {
result.then(onDone, onDone);
}
return await result;
}
if (!fn.length) {
onDone();
}
return result;
function onDone() {
for (const scope of scopes) {
scope.release(() => {
if (!scope.lock.length) {
locks.delete(scope.scope);
}
});
}
scopes.length = 0;
}
}
}
module.exports = {
createLock,
createLockPool
};