forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_locks.h
More file actions
202 lines (167 loc) · 6.02 KB
/
node_locks.h
File metadata and controls
202 lines (167 loc) · 6.02 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
#ifndef SRC_NODE_LOCKS_H_
#define SRC_NODE_LOCKS_H_
#if defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS
#include <deque>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include "base_object.h"
#include "env.h"
#include "node_mutex.h"
#include "v8.h"
namespace node::worker::locks {
class Lock final : public MemoryRetainer {
public:
enum class Mode { Shared, Exclusive };
Lock(Environment* env,
const std::u16string& name,
Mode mode,
const std::string& client_id,
v8::Local<v8::Promise::Resolver> waiting,
v8::Local<v8::Promise::Resolver> released);
~Lock() = default;
Lock(const Lock&) = delete;
Lock& operator=(const Lock&) = delete;
// Resource name for this lock as DOMString
const std::u16string& name() const { return name_; }
// Lock mode (shared or exclusive).
Mode mode() const { return mode_; }
// Client identifier string.
const std::string& client_id() const { return client_id_; }
// Environment that owns this lock.
Environment* env() const { return env_; }
// Returns true if this lock was stolen by another request.
bool is_stolen() const { return stolen_; }
// Marks this lock as stolen.
void mark_stolen() { stolen_ = true; }
// Promise that resolves when the user callback completes.
v8::Local<v8::Promise::Resolver> waiting_promise() {
return waiting_promise_.Get(env_->isolate());
}
// Promise that resolves when the lock is finally released.
v8::Local<v8::Promise::Resolver> released_promise() {
return released_promise_.Get(env_->isolate());
}
void MemoryInfo(node::MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(Lock)
SET_SELF_SIZE(Lock)
private:
Environment* env_;
std::u16string name_;
Mode mode_;
std::string client_id_;
bool stolen_ = false;
v8::Global<v8::Promise::Resolver> waiting_promise_;
v8::Global<v8::Promise::Resolver> released_promise_;
};
class LockHolder final : public BaseObject {
public:
LockHolder(Environment* env,
v8::Local<v8::Object> obj,
std::shared_ptr<Lock> lock)
: BaseObject(env, obj), lock_(std::move(lock)) {
MakeWeak();
}
~LockHolder() = default;
LockHolder(const LockHolder&) = delete;
LockHolder& operator=(const LockHolder&) = delete;
std::shared_ptr<Lock> lock() const { return lock_; }
void MemoryInfo(node::MemoryTracker* tracker) const override;
SET_MEMORY_INFO_NAME(LockHolder)
SET_SELF_SIZE(LockHolder)
static BaseObjectPtr<LockHolder> Create(Environment* env,
std::shared_ptr<Lock> lock);
private:
static v8::Local<v8::FunctionTemplate> GetConstructorTemplate(
Environment* env);
std::shared_ptr<Lock> lock_;
};
class LockRequest final {
public:
LockRequest(Environment* env,
v8::Local<v8::Promise::Resolver> waiting,
v8::Local<v8::Promise::Resolver> released,
v8::Local<v8::Function> callback,
const std::u16string& name,
Lock::Mode mode,
std::string client_id,
bool steal,
bool if_available);
~LockRequest() = default;
LockRequest(const LockRequest&) = delete;
LockRequest& operator=(const LockRequest&) = delete;
const std::u16string& name() const { return name_; }
Lock::Mode mode() const { return mode_; }
const std::string& client_id() const { return client_id_; }
bool steal() const { return steal_; }
// Returns true if this is an ifAvailable request.
bool if_available() const { return if_available_; }
Environment* env() const { return env_; }
v8::Local<v8::Promise::Resolver> waiting_promise() {
return waiting_promise_.Get(env_->isolate());
}
v8::Local<v8::Promise::Resolver> released_promise() {
return released_promise_.Get(env_->isolate());
}
v8::Local<v8::Function> callback() { return callback_.Get(env_->isolate()); }
private:
Environment* env_;
std::u16string name_;
Lock::Mode mode_;
std::string client_id_;
bool steal_;
bool if_available_;
v8::Global<v8::Promise::Resolver> waiting_promise_;
v8::Global<v8::Promise::Resolver> released_promise_;
v8::Global<v8::Function> callback_;
};
class LockManager final {
public:
static void Request(const v8::FunctionCallbackInfo<v8::Value>& args);
static void Query(const v8::FunctionCallbackInfo<v8::Value>& args);
void ProcessQueue(Environment* env);
void CleanupEnvironment(Environment* env);
static void OnEnvironmentCleanup(void* arg);
static LockManager* GetCurrent() { return ¤t_; }
void ReleaseLockAndProcessQueue(Environment* env,
std::shared_ptr<Lock> lock,
v8::Local<v8::Value> result,
bool was_rejected = false);
struct LocksCountersSnapshot {
uint64_t total_steals = 0;
uint64_t total_aborts = 0;
uint64_t total_exclusive_acquired = 0;
uint64_t total_shared_acquired = 0;
size_t holders_exclusive = 0;
size_t holders_shared = 0;
size_t pending_exclusive = 0;
size_t pending_shared = 0;
};
LocksCountersSnapshot GetCountersSnapshot() const;
private:
LockManager() = default;
~LockManager() = default;
LockManager(const LockManager&) = delete;
LockManager& operator=(const LockManager&) = delete;
bool IsGrantable(const LockRequest* req) const;
void CleanupStolenLocks(Environment* env);
void ReleaseLock(Lock* lock);
void WakeEnvironment(Environment* env);
static LockManager current_;
mutable Mutex mutex_;
struct Counters {
uint64_t total_steals = 0;
uint64_t total_aborts = 0;
uint64_t total_exclusive_acquired = 0;
uint64_t total_shared_acquired = 0;
};
Counters counters;
// All entries for a given Environment* are purged in CleanupEnvironment().
std::unordered_map<std::u16string, std::deque<std::shared_ptr<Lock>>>
held_locks_;
std::deque<std::unique_ptr<LockRequest>> pending_queue_;
std::unordered_set<Environment*> registered_envs_;
};
} // namespace node::worker::locks
#endif // NODE_WANT_INTERNALS
#endif // SRC_NODE_LOCKS_H_