Skip to content

Commit d3b10f6

Browse files
author
Gabriel Schulhof
committed
src: use thread_local to declare modpending
The pointer used to hold an incoming dynamically loaded module's `node::node_module` structure needs to be thread-local. So far this was done with `uv_key_set()` and `uv_key_get()`. The language now supports the `thread_local` keyword which makes implementing this a lot cleaner. PR-URL: #28456 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent 3ab457e commit d3b10f6

File tree

1 file changed

+5
-12
lines changed

1 file changed

+5
-12
lines changed

src/node_binding.cc

+5-12
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,7 @@ using v8::Value;
243243
// Globals per process
244244
static node_module* modlist_internal;
245245
static node_module* modlist_linked;
246-
static uv_once_t init_modpending_once = UV_ONCE_INIT;
247-
static uv_key_t thread_local_modpending;
246+
static thread_local node_module* thread_local_modpending;
248247

249248
// This is set by node::Init() which is used by embedders
250249
bool node_is_initialized = false;
@@ -262,7 +261,7 @@ extern "C" void node_module_register(void* m) {
262261
mp->nm_link = modlist_linked;
263262
modlist_linked = mp;
264263
} else {
265-
uv_key_set(&thread_local_modpending, mp);
264+
thread_local_modpending = mp;
266265
}
267266
}
268267

@@ -406,10 +405,6 @@ inline napi_addon_register_func GetNapiInitializerCallback(DLib* dlib) {
406405
dlib->GetSymbolAddress(name));
407406
}
408407

409-
void InitModpendingOnce() {
410-
CHECK_EQ(0, uv_key_create(&thread_local_modpending));
411-
}
412-
413408
// DLOpen is process.dlopen(module, filename, flags).
414409
// Used to load 'module.node' dynamically shared objects.
415410
//
@@ -420,8 +415,7 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
420415
Environment* env = Environment::GetCurrent(args);
421416
auto context = env->context();
422417

423-
uv_once(&init_modpending_once, InitModpendingOnce);
424-
CHECK_NULL(uv_key_get(&thread_local_modpending));
418+
CHECK_NULL(thread_local_modpending);
425419

426420
if (args.Length() < 2) {
427421
env->ThrowError("process.dlopen needs at least 2 arguments.");
@@ -452,9 +446,8 @@ void DLOpen(const FunctionCallbackInfo<Value>& args) {
452446
// Objects containing v14 or later modules will have registered themselves
453447
// on the pending list. Activate all of them now. At present, only one
454448
// module per object is supported.
455-
node_module* mp =
456-
static_cast<node_module*>(uv_key_get(&thread_local_modpending));
457-
uv_key_set(&thread_local_modpending, nullptr);
449+
node_module* mp = thread_local_modpending;
450+
thread_local_modpending = nullptr;
458451

459452
if (!is_opened) {
460453
Local<String> errmsg =

0 commit comments

Comments
 (0)