diff options
author | Anna Henningsen <anna@addaleax.net> | 2017-09-04 23:31:20 +0300 |
---|---|---|
committer | Anna Henningsen <anna@addaleax.net> | 2018-05-09 19:19:02 +0300 |
commit | ccdee34072eab0f7ea385571aa43b00cb8dba4fd (patch) | |
tree | cc2901f53f2f894abb25aeb66ada30527aec87fe /src/cares_wrap.cc | |
parent | 987387534aba3f23346e5b076058fcb567651389 (diff) |
src: use lock for c-ares library init/cleanup
This helps embedders wishing to use Node.js in a multi-threaded fashion
and helps pave the way for thread-based worker support.
Thanks to Stephen Belanger for reviewing this commit in its original PR.
Refs: https://github.com/ayojs/ayo/pull/82
PR-URL: https://github.com/nodejs/node/pull/20539
Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Daniel Bevenius <daniel.bevenius@gmail.com>
Diffstat (limited to 'src/cares_wrap.cc')
-rw-r--r-- | src/cares_wrap.cc | 5 |
1 files changed, 5 insertions, 0 deletions
diff --git a/src/cares_wrap.cc b/src/cares_wrap.cc index 4df47d75d43..4208c02d4fe 100644 --- a/src/cares_wrap.cc +++ b/src/cares_wrap.cc @@ -70,6 +70,8 @@ using v8::Value; namespace { +Mutex ares_library_mutex; + inline uint16_t cares_get_16bit(const unsigned char* p) { return static_cast<uint32_t>(p[0] << 8U) | (static_cast<uint32_t>(p[1])); } @@ -470,6 +472,7 @@ void ChannelWrap::Setup() { int r; if (!library_inited_) { + Mutex::ScopedLock lock(ares_library_mutex); // Multiple calls to ares_library_init() increase a reference counter, // so this is a no-op except for the first call to it. r = ares_library_init(ARES_LIB_INIT_ALL); @@ -483,6 +486,7 @@ void ChannelWrap::Setup() { ARES_OPT_FLAGS | ARES_OPT_SOCK_STATE_CB); if (r != ARES_SUCCESS) { + Mutex::ScopedLock lock(ares_library_mutex); ares_library_cleanup(); return env()->ThrowError(ToErrorCodeString(r)); } @@ -500,6 +504,7 @@ void ChannelWrap::Setup() { ChannelWrap::~ChannelWrap() { if (library_inited_) { + Mutex::ScopedLock lock(ares_library_mutex); // This decreases the reference counter increased by ares_library_init(). ares_library_cleanup(); } |