forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_locks.cc
More file actions
310 lines (259 loc) · 9.68 KB
/
Copy pathnode_locks.cc
File metadata and controls
310 lines (259 loc) · 9.68 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "node_locks.h"
#include <algorithm>
#include <memory>
#include <utility>
#include "env-inl.h"
#include "node.h"
#include "node_internals.h"
#include "v8.h"
namespace node {
namespace worker {
using v8::Array;
using v8::Boolean;
using v8::Context;
using v8::External;
using v8::Function;
using v8::FunctionCallbackInfo;
using v8::Isolate;
using v8::Local;
using v8::Null;
using v8::Number;
using v8::Object;
using v8::Promise;
using v8::String;
using v8::TryCatch;
using v8::Undefined;
using v8::Value;
LockManager LockManager::current_;
void LockManager::ReleaseLock(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
LockManager* manager = LockManager::GetCurrent();
Lock* lock = reinterpret_cast<Lock*>(args.Data().As<External>()->Value());
Mutex::ScopedLock(manager->work_mutex_);
for (auto const& maybe : manager->held_locks_) {
if (maybe.get() == lock) {
manager->held_locks_.erase(
std::remove(
manager->held_locks_.begin(), manager->held_locks_.end(), maybe),
manager->held_locks_.end());
break;
}
}
manager->ProcessQueue(env);
}
void LockManager::ProcessQueue(Environment* env) {
// For each request of queue:
for (auto const& request : pending_requests_) {
// If request is grantable, then run these steps:
if (IsGrantable(request.get())) {
// Let waiting be a new Promise.
Local<Promise::Resolver> waiting =
Promise::Resolver::New(env->context()).ToLocalChecked();
Lock* lock = new Lock(
env, request->mode(), request->name(), request->promise(), waiting);
// Let r be the result of invoking callback with a
// new Lock object associated with lock as the only argument.
Local<Value> args[] = {
String::NewFromUtf8(env->isolate(), lock->name().c_str()),
Number::New(env->isolate(), lock->mode()),
waiting->GetPromise(),
Function::New(env->context(),
LockManager::ReleaseLock,
External::New(env->isolate(), lock))
.ToLocalChecked(),
};
TryCatch try_catch(env->isolate());
Local<Value> r;
if (request->callback()
->Call(env->context(), Undefined(env->isolate()), 4, args)
.ToLocal(&r)) {
waiting->Resolve(env->context(), r).ToChecked();
} else {
waiting->Reject(env->context(), try_catch.Exception()).ToChecked();
}
// Remove request from queue.
pending_requests_.erase(
std::remove(
pending_requests_.begin(), pending_requests_.end(), request),
pending_requests_.end());
// Append lock to origin’s held lock set.
held_locks_.emplace_back(lock);
}
}
}
bool LockManager::IsGrantable(const LockRequest* request) {
// If mode is "exclusive", then return true if all of the
// following conditions are true, and false otherwise:
if (request->mode() == Lock::Mode::kExclusive) {
// No lock in held has a name that equals name
for (auto const& lock : held_locks_) {
if (lock->name() == request->name()) {
return false;
}
}
// No lock request in queue earlier than request has a name that equals
// name.
for (auto const& req : pending_requests_) {
if (request == req.get()) {
break;
}
if (req->name() == request->name()) {
return false;
}
}
return true;
} else {
// Otherwise, mode is "shared"; return true if all of the
// following conditions are true, and false otherwise:
// No lock in held has mode "exclusive" and has a name that equals name.
for (auto const& lock : held_locks_) {
if (lock->name() == request->name() &&
lock->mode() == Lock::Mode::kExclusive) {
return false;
}
}
// No lock request in queue earlier than request has a
// mode "exclusive" and name that equals name
for (auto const& req : pending_requests_) {
if (request == req.get()) {
break;
}
if (req->name() == request->name() &&
req->mode() == Lock::Mode::kExclusive) {
return false;
}
}
return true;
}
}
bool LockManager::HasPending() {
Mutex::ScopedLock lock(work_mutex_);
return pending_requests_.size() > 0 || held_locks_.size() > 0;
}
// https://wicg.github.io/web-locks/#algorithm-request-lock
// promise, callback, name, mode, ifAvailable, steal
void LockManager::Request(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
LockManager* manager = LockManager::GetCurrent();
CHECK_EQ(args.Length(), 6);
CHECK(args[0]->IsPromise());
CHECK(args[1]->IsFunction());
CHECK(args[2]->IsString());
CHECK(args[3]->IsNumber());
CHECK(args[4]->IsBoolean());
CHECK(args[5]->IsBoolean());
Local<Promise::Resolver> promise = args[0].As<Promise::Resolver>();
Local<Function> callback = args[1].As<Function>();
String::Utf8Value name_utf8(env->isolate(), args[2]);
std::string name(*name_utf8);
Lock::Mode mode = static_cast<Lock::Mode>(args[3].As<Number>()->Int32Value());
bool if_available = args[4]->IsTrue();
bool steal = args[5]->IsTrue();
Mutex::ScopedLock(manager->work_mutex_);
LockRequest* request = new LockRequest(env, promise, callback, name, mode);
if (steal) {
for (auto const& lock : manager->held_locks_) {
if (lock->name() == name) {
// Reject lock’s released promise with an "AbortError" DOMException.
Local<Function> domexception_ctor = env->domexception_function();
CHECK(!domexception_ctor.IsEmpty());
Local<Value> argv[] = {
FIXED_ONE_BYTE_STRING(env->isolate(),
"Lock request could not be fulfilled"),
FIXED_ONE_BYTE_STRING(env->isolate(), "AbortError"),
};
Local<Value> exception =
domexception_ctor->NewInstance(env->context(), 2, argv)
.ToLocalChecked();
lock->released_promise()->Reject(env->context(), exception).ToChecked();
// Remove lock from held lock set
manager->held_locks_.erase(
std::remove(
manager->held_locks_.begin(), manager->held_locks_.end(), lock),
manager->held_locks_.end());
}
}
// Prepend request in origin’s lock request queue.
manager->pending_requests_.emplace_front(request);
} else {
// Otherwise, run these steps:
// If ifAvailable is true and request is not
// grantable, then run these steps:
if (if_available && !manager->IsGrantable(request)) {
// Let r be the result of invoking callback with null as the only
// argument. Note that r could be a regular completion, an abrupt
// completion, or an unresolved Promise.
TryCatch try_catch(env->isolate());
Local<Value> r;
Local<Value> null = Null(env->isolate());
if (callback->Call(env->context(), Undefined(env->isolate()), 1, &null)
.ToLocal(&r)) {
// Resolve promise with r and abort these steps.
promise->Resolve(env->context(), r).ToChecked();
return;
} else {
promise->Reject(env->context(), try_catch.Exception()).ToChecked();
}
}
// Enqueue request in origin's lock request queue;
manager->pending_requests_.emplace_back(request);
}
manager->ProcessQueue(env);
}
// https://wicg.github.io/web-locks/#snapshot-the-lock-state
void LockManager::Snapshot(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
Isolate* isolate = env->isolate();
Local<Context> context = env->context();
LockManager* manager = LockManager::GetCurrent();
Mutex::ScopedLock(manager->work_mutex_);
Local<Object> obj = Object::New(isolate);
{
Local<Array> pending =
Array::New(isolate, manager->pending_requests_.size());
for (uint32_t i = 0; i < manager->pending_requests_.size(); i += 1) {
LockRequest* request = manager->pending_requests_[i].get();
Local<Object> obj = Object::New(isolate);
obj->Set(context,
env->name_string(),
String::NewFromUtf8(isolate, request->name().c_str()))
.ToChecked();
obj->Set(context,
env->mode_string(),
request->mode() == Lock::Mode::kShared ? env->shared_string()
: env->exclusive_string())
.ToChecked();
pending->Set(context, i, obj).ToChecked();
}
obj->Set(context, env->pending_string(), pending).ToChecked();
}
{
Local<Array> held = Array::New(isolate, manager->held_locks_.size());
for (uint32_t i = 0; i < manager->held_locks_.size(); i += 1) {
Lock* lock = manager->held_locks_[i].get();
Local<Object> obj = Object::New(isolate);
obj->Set(context,
env->name_string(),
String::NewFromUtf8(isolate, lock->name().c_str()))
.ToChecked();
obj->Set(context,
env->mode_string(),
lock->mode() == Lock::Mode::kShared ? env->shared_string()
: env->exclusive_string())
.ToChecked();
held->Set(context, i, obj).ToChecked();
}
obj->Set(context, env->held_string(), held).ToChecked();
}
args.GetReturnValue().Set(obj);
}
void InitializeLocks(Local<Object> target,
Local<Value> unused,
Local<Context> context) {
Environment* env = Environment::GetCurrent(context);
env->SetMethod(target, "snapshot", LockManager::Snapshot);
env->SetMethod(target, "request", LockManager::Request);
}
} // namespace worker
} // namespace node
NODE_MODULE_CONTEXT_AWARE_INTERNAL(locks, node::worker::InitializeLocks)