| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 9f1b790 commit a5b6c2c
6 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -169,6 +169,7 @@ | |||
| 169 | 169 | 'src/node_http_parser.h', | |
| 170 | 170 | 'src/node_internals.h', | |
| 171 | 171 | 'src/node_javascript.h', | |
| 172 | + 'src/node_mutex.h', | ||
| 172 | 173 | 'src/node_root_certs.h', | |
| 173 | 174 | 'src/node_version.h', | |
| 174 | 175 | 'src/node_watchdog.h', | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -55,21 +55,14 @@ Agent::Agent(Environment* env) : state_(kNone), | |||
| 55 | 55 | parent_env_(env), | |
| 56 | 56 | child_env_(nullptr), | |
| 57 | 57 | dispatch_handler_(nullptr) { | |
| 58 | - int err; | ||
| 59 | - | ||
| 60 | - err = uv_sem_init(&start_sem_, 0); | ||
| 61 | - CHECK_EQ(err, 0); | ||
| 62 | - | ||
| 63 | - err = uv_mutex_init(&message_mutex_); | ||
| 64 | - CHECK_EQ(err, 0); | ||
| 58 | + CHECK_EQ(0, uv_sem_init(&start_sem_, 0)); | ||
| 65 | 59 | } | |
| 66 | 60 | ||
| 67 | 61 | ||
| 68 | 62 | Agent::~Agent() { | |
| 69 | 63 | Stop(); | |
| 70 | 64 | ||
| 71 | 65 | uv_sem_destroy(&start_sem_); | |
| 72 | - uv_mutex_destroy(&message_mutex_); | ||
| 73 | 66 | ||
| 74 | 67 | while (AgentMessage* msg = messages_.PopFront()) | |
| 75 | 68 | delete msg; | |
@@ -274,7 +267,7 @@ void Agent::ChildSignalCb(uv_async_t* signal) { | |||
| 274 | 267 | HandleScope scope(isolate); | |
| 275 | 268 | Local<Object> api = PersistentToLocal(isolate, a->api_); | |
| 276 | 269 | ||
| 277 | - uv_mutex_lock(&a->message_mutex_); | ||
| 270 | + Mutex::ScopedLock scoped_lock(a->message_mutex_); | ||
| 278 | 271 | while (AgentMessage* msg = a->messages_.PopFront()) { | |
| 279 | 272 | // Time to close everything | |
| 280 | 273 | if (msg->data() == nullptr) { | |
@@ -305,14 +298,12 @@ void Agent::ChildSignalCb(uv_async_t* signal) { | |||
| 305 | 298 | argv); | |
| 306 | 299 | delete msg; | |
| 307 | 300 | } | |
| 308 | - uv_mutex_unlock(&a->message_mutex_); | ||
| 309 | 301 | } | |
| 310 | 302 | ||
| 311 | 303 | ||
| 312 | 304 | void Agent::EnqueueMessage(AgentMessage* message) { | |
| 313 | - uv_mutex_lock(&message_mutex_); | ||
| 305 | + Mutex::ScopedLock scoped_lock(message_mutex_); | ||
| 314 | 306 | messages_.PushBack(message); | |
| 315 | - uv_mutex_unlock(&message_mutex_); | ||
| 316 | 307 | uv_async_send(&child_signal_); | |
| 317 | 308 | } | |
| 318 | 309 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -22,6 +22,7 @@ | |||
| 22 | 22 | #ifndef SRC_DEBUG_AGENT_H_ | |
| 23 | 23 | #define SRC_DEBUG_AGENT_H_ | |
| 24 | 24 | ||
| 25 | + #include "node_mutex.h" | ||
| 25 | 26 | #include "util.h" | |
| 26 | 27 | #include "util-inl.h" | |
| 27 | 28 | #include "uv.h" | |
@@ -115,7 +116,7 @@ class Agent { | |||
| 115 | 116 | bool wait_; | |
| 116 | 117 | ||
| 117 | 118 | uv_sem_t start_sem_; | |
| 118 | - uv_mutex_t message_mutex_; | ||
| 119 | + node::Mutex message_mutex_; | ||
| 119 | 120 | uv_async_t child_signal_; | |
| 120 | 121 | ||
| 121 | 122 | uv_thread_t thread_; | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -159,7 +159,7 @@ static double prog_start_time; | |||
| 159 | 159 | static bool debugger_running; | |
| 160 | 160 | static uv_async_t dispatch_debug_messages_async; | |
| 161 | 161 | ||
| 162 | - static uv_mutex_t node_isolate_mutex; | ||
| 162 | + static Mutex node_isolate_mutex; | ||
| 163 | 163 | static v8::Isolate* node_isolate; | |
| 164 | 164 | static v8::Platform* default_platform; | |
| 165 | 165 | ||
@@ -3535,18 +3535,17 @@ static void EnableDebug(Environment* env) { | |||
| 3535 | 3535 | ||
| 3536 | 3536 | // Called from an arbitrary thread. | |
| 3537 | 3537 | static void TryStartDebugger() { | |
| 3538 | - uv_mutex_lock(&node_isolate_mutex); | ||
| 3538 | + Mutex::ScopedLock scoped_lock(node_isolate_mutex); | ||
| 3539 | 3539 | if (auto isolate = node_isolate) { | |
| 3540 | 3540 | v8::Debug::DebugBreak(isolate); | |
| 3541 | 3541 | uv_async_send(&dispatch_debug_messages_async); | |
| 3542 | 3542 | } | |
| 3543 | - uv_mutex_unlock(&node_isolate_mutex); | ||
| 3544 | 3543 | } | |
| 3545 | 3544 | ||
| 3546 | 3545 | ||
| 3547 | 3546 | // Called from the main thread. | |
| 3548 | 3547 | static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle) { | |
| 3549 | - uv_mutex_lock(&node_isolate_mutex); | ||
| 3548 | + Mutex::ScopedLock scoped_lock(node_isolate_mutex); | ||
| 3550 | 3549 | if (auto isolate = node_isolate) { | |
| 3551 | 3550 | if (debugger_running == false) { | |
| 3552 | 3551 | fprintf(stderr, "Starting debugger agent.\n"); | |
@@ -3562,7 +3561,6 @@ static void DispatchDebugMessagesAsyncCallback(uv_async_t* handle) { | |||
| 3562 | 3561 | Isolate::Scope isolate_scope(isolate); | |
| 3563 | 3562 | v8::Debug::ProcessDebugMessages(); | |
| 3564 | 3563 | } | |
| 3565 | - uv_mutex_unlock(&node_isolate_mutex); | ||
| 3566 | 3564 | } | |
| 3567 | 3565 | ||
| 3568 | 3566 | ||
@@ -3888,8 +3886,6 @@ void Init(int* argc, | |||
| 3888 | 3886 | // Make inherited handles noninheritable. | |
| 3889 | 3887 | uv_disable_stdio_inheritance(); | |
| 3890 | 3888 | ||
| 3891 | - CHECK_EQ(0, uv_mutex_init(&node_isolate_mutex)); | ||
| 3892 | - | ||
| 3893 | 3889 | // init async debug messages dispatching | |
| 3894 | 3890 | // Main thread uses uv_default_loop | |
| 3895 | 3891 | CHECK_EQ(0, uv_async_init(uv_default_loop(), | |
@@ -4177,12 +4173,13 @@ static void StartNodeInstance(void* arg) { | |||
| 4177 | 4173 | #endif | |
| 4178 | 4174 | Isolate* isolate = Isolate::New(params); | |
| 4179 | 4175 | ||
| 4180 | - uv_mutex_lock(&node_isolate_mutex); | ||
| 4181 | - if (instance_data->is_main()) { | ||
| 4182 | - CHECK_EQ(node_isolate, nullptr); | ||
| 4183 | - node_isolate = isolate; | ||
| 4176 | + { | ||
| 4177 | + Mutex::ScopedLock scoped_lock(node_isolate_mutex); | ||
| 4178 | + if (instance_data->is_main()) { | ||
| 4179 | + CHECK_EQ(node_isolate, nullptr); | ||
| 4180 | + node_isolate = isolate; | ||
| 4181 | + } | ||
| 4184 | 4182 | } | |
| 4185 | - uv_mutex_unlock(&node_isolate_mutex); | ||
| 4186 | 4183 | ||
| 4187 | 4184 | if (track_heap_objects) { | |
| 4188 | 4185 | isolate->GetHeapProfiler()->StartTrackingHeapObjects(true); | |
@@ -4251,10 +4248,11 @@ static void StartNodeInstance(void* arg) { | |||
| 4251 | 4248 | env = nullptr; | |
| 4252 | 4249 | } | |
| 4253 | 4250 | ||
| 4254 | - uv_mutex_lock(&node_isolate_mutex); | ||
| 4255 | - if (node_isolate == isolate) | ||
| 4256 | - node_isolate = nullptr; | ||
| 4257 | - uv_mutex_unlock(&node_isolate_mutex); | ||
| 4251 | + { | ||
| 4252 | + Mutex::ScopedLock scoped_lock(node_isolate_mutex); | ||
| 4253 | + if (node_isolate == isolate) | ||
| 4254 | + node_isolate = nullptr; | ||
| 4255 | + } | ||
| 4258 | 4256 | ||
| 4259 | 4257 | CHECK_NE(isolate, nullptr); | |
| 4260 | 4258 | isolate->Dispose(); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -114,7 +114,7 @@ static X509_NAME *cnnic_ev_name = | |||
| 114 | 114 | d2i_X509_NAME(nullptr, &cnnic_ev_p, | |
| 115 | 115 | sizeof(CNNIC_EV_ROOT_CA_SUBJECT_DATA)-1); | |
| 116 | 116 | ||
| 117 | - static uv_mutex_t* locks; | ||
| 117 | + static Mutex* mutexes; | ||
| 118 | 118 | ||
| 119 | 119 | const char* const root_certs[] = { | |
| 120 | 120 | #include "node_root_certs.h" // NOLINT(build/include_order) | |
@@ -172,25 +172,19 @@ static void crypto_threadid_cb(CRYPTO_THREADID* tid) { | |||
| 172 | 172 | ||
| 173 | 173 | ||
| 174 | 174 | static void crypto_lock_init(void) { | |
| 175 | - int i, n; | ||
| 176 | - | ||
| 177 | - n = CRYPTO_num_locks(); | ||
| 178 | - locks = new uv_mutex_t[n]; | ||
| 179 | - | ||
| 180 | - for (i = 0; i < n; i++) | ||
| 181 | - if (uv_mutex_init(locks + i)) | ||
| 182 | - ABORT(); | ||
| 175 | + mutexes = new Mutex[CRYPTO_num_locks()]; | ||
| 183 | 176 | } | |
| 184 | 177 | ||
| 185 | 178 | ||
| 186 | 179 | static void crypto_lock_cb(int mode, int n, const char* file, int line) { | |
| 187 | 180 | CHECK(!(mode & CRYPTO_LOCK) ^ !(mode & CRYPTO_UNLOCK)); | |
| 188 | 181 | CHECK(!(mode & CRYPTO_READ) ^ !(mode & CRYPTO_WRITE)); | |
| 189 | 182 | ||
| 183 | + auto mutex = &mutexes[n]; | ||
| 190 | 184 | if (mode & CRYPTO_LOCK) | |
| 191 | - uv_mutex_lock(locks + n); | ||
| 185 | + mutex->Lock(); | ||
| 192 | 186 | else | |
| 193 | - uv_mutex_unlock(locks + n); | ||
| 187 | + mutex->Unlock(); | ||
| 194 | 188 | } | |
| 195 | 189 | ||
| 196 | 190 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,187 @@ | |||
| 1 | + #ifndef SRC_NODE_MUTEX_H_ | ||
| 2 | + #define SRC_NODE_MUTEX_H_ | ||
| 3 | + | ||
| 4 | + #include "util.h" | ||
| 5 | + #include "uv.h" | ||
| 6 | + | ||
| 7 | + namespace node { | ||
| 8 | + | ||
| 9 | + template <typename Traits> class ConditionVariableBase; | ||
| 10 | + template <typename Traits> class MutexBase; | ||
| 11 | + struct LibuvMutexTraits; | ||
| 12 | + | ||
| 13 | + using ConditionVariable = ConditionVariableBase<LibuvMutexTraits>; | ||
| 14 | + using Mutex = MutexBase<LibuvMutexTraits>; | ||
| 15 | + | ||
| 16 | + template <typename Traits> | ||
| 17 | + class MutexBase { | ||
| 18 | + public: | ||
| 19 | + inline MutexBase(); | ||
| 20 | + inline ~MutexBase(); | ||
| 21 | + inline void Lock(); | ||
| 22 | + inline void Unlock(); | ||
| 23 | + | ||
| 24 | + class ScopedLock; | ||
| 25 | + class ScopedUnlock; | ||
| 26 | + | ||
| 27 | + class ScopedLock { | ||
| 28 | + public: | ||
| 29 | + inline explicit ScopedLock(const MutexBase& mutex); | ||
| 30 | + inline explicit ScopedLock(const ScopedUnlock& scoped_unlock); | ||
| 31 | + inline ~ScopedLock(); | ||
| 32 | + | ||
| 33 | + private: | ||
| 34 | + template <typename> friend class ConditionVariableBase; | ||
| 35 | + friend class ScopedUnlock; | ||
| 36 | + const MutexBase& mutex_; | ||
| 37 | + DISALLOW_COPY_AND_ASSIGN(ScopedLock); | ||
| 38 | + }; | ||
| 39 | + | ||
| 40 | + class ScopedUnlock { | ||
| 41 | + public: | ||
| 42 | + inline explicit ScopedUnlock(const ScopedLock& scoped_lock); | ||
| 43 | + inline ~ScopedUnlock(); | ||
| 44 | + | ||
| 45 | + private: | ||
| 46 | + friend class ScopedLock; | ||
| 47 | + const MutexBase& mutex_; | ||
| 48 | + DISALLOW_COPY_AND_ASSIGN(ScopedUnlock); | ||
| 49 | + }; | ||
| 50 | + | ||
| 51 | + private: | ||
| 52 | + template <typename> friend class ConditionVariableBase; | ||
| 53 | + mutable typename Traits::MutexT mutex_; | ||
| 54 | + DISALLOW_COPY_AND_ASSIGN(MutexBase); | ||
| 55 | + }; | ||
| 56 | + | ||
| 57 | + template <typename Traits> | ||
| 58 | + class ConditionVariableBase { | ||
| 59 | + public: | ||
| 60 | + using ScopedLock = typename MutexBase<Traits>::ScopedLock; | ||
| 61 | + | ||
| 62 | + inline ConditionVariableBase(); | ||
| 63 | + inline ~ConditionVariableBase(); | ||
| 64 | + inline void Broadcast(const ScopedLock&); | ||
| 65 | + inline void Signal(const ScopedLock&); | ||
| 66 | + inline void Wait(const ScopedLock& scoped_lock); | ||
| 67 | + | ||
| 68 | + private: | ||
| 69 | + typename Traits::CondT cond_; | ||
| 70 | + DISALLOW_COPY_AND_ASSIGN(ConditionVariableBase); | ||
| 71 | + }; | ||
| 72 | + | ||
| 73 | + struct LibuvMutexTraits { | ||
| 74 | + using CondT = uv_cond_t; | ||
| 75 | + using MutexT = uv_mutex_t; | ||
| 76 | + | ||
| 77 | + static inline int cond_init(CondT* cond) { | ||
| 78 | + return uv_cond_init(cond); | ||
| 79 | + } | ||
| 80 | + | ||
| 81 | + static inline int mutex_init(MutexT* mutex) { | ||
| 82 | + return uv_mutex_init(mutex); | ||
| 83 | + } | ||
| 84 | + | ||
| 85 | + static inline void cond_broadcast(CondT* cond) { | ||
| 86 | + uv_cond_broadcast(cond); | ||
| 87 | + } | ||
| 88 | + | ||
| 89 | + static inline void cond_destroy(CondT* cond) { | ||
| 90 | + uv_cond_destroy(cond); | ||
| 91 | + } | ||
| 92 | + | ||
| 93 | + static inline void cond_signal(CondT* cond) { | ||
| 94 | + uv_cond_signal(cond); | ||
| 95 | + } | ||
| 96 | + | ||
| 97 | + static inline void cond_wait(CondT* cond, MutexT* mutex) { | ||
| 98 | + uv_cond_wait(cond, mutex); | ||
| 99 | + } | ||
| 100 | + | ||
| 101 | + static inline void mutex_destroy(MutexT* mutex) { | ||
| 102 | + uv_mutex_destroy(mutex); | ||
| 103 | + } | ||
| 104 | + | ||
| 105 | + static inline void mutex_lock(MutexT* mutex) { | ||
| 106 | + uv_mutex_lock(mutex); | ||
| 107 | + } | ||
| 108 | + | ||
| 109 | + static inline void mutex_unlock(MutexT* mutex) { | ||
| 110 | + uv_mutex_unlock(mutex); | ||
| 111 | + } | ||
| 112 | + }; | ||
| 113 | + | ||
| 114 | + template <typename Traits> | ||
| 115 | + ConditionVariableBase<Traits>::ConditionVariableBase() { | ||
| 116 | + CHECK_EQ(0, Traits::cond_init(&cond_)); | ||
| 117 | + } | ||
| 118 | + | ||
| 119 | + template <typename Traits> | ||
| 120 | + ConditionVariableBase<Traits>::~ConditionVariableBase() { | ||
| 121 | + Traits::cond_destroy(&cond_); | ||
| 122 | + } | ||
| 123 | + | ||
| 124 | + template <typename Traits> | ||
| 125 | + void ConditionVariableBase<Traits>::Broadcast(const ScopedLock&) { | ||
| 126 | + Traits::cond_broadcast(&cond_); | ||
| 127 | + } | ||
| 128 | + | ||
| 129 | + template <typename Traits> | ||
| 130 | + void ConditionVariableBase<Traits>::Signal(const ScopedLock&) { | ||
| 131 | + Traits::cond_signal(&cond_); | ||
| 132 | + } | ||
| 133 | + | ||
| 134 | + template <typename Traits> | ||
| 135 | + void ConditionVariableBase<Traits>::Wait(const ScopedLock& scoped_lock) { | ||
| 136 | + Traits::cond_wait(&cond_, &scoped_lock.mutex_.mutex_); | ||
| 137 | + } | ||
| 138 | + | ||
| 139 | + template <typename Traits> | ||
| 140 | + MutexBase<Traits>::MutexBase() { | ||
| 141 | + CHECK_EQ(0, Traits::mutex_init(&mutex_)); | ||
| 142 | + } | ||
| 143 | + | ||
| 144 | + template <typename Traits> | ||
| 145 | + MutexBase<Traits>::~MutexBase() { | ||
| 146 | + Traits::mutex_destroy(&mutex_); | ||
| 147 | + } | ||
| 148 | + | ||
| 149 | + template <typename Traits> | ||
| 150 | + void MutexBase<Traits>::Lock() { | ||
| 151 | + Traits::mutex_lock(&mutex_); | ||
| 152 | + } | ||
| 153 | + | ||
| 154 | + template <typename Traits> | ||
| 155 | + void MutexBase<Traits>::Unlock() { | ||
| 156 | + Traits::mutex_unlock(&mutex_); | ||
| 157 | + } | ||
| 158 | + | ||
| 159 | + template <typename Traits> | ||
| 160 | + MutexBase<Traits>::ScopedLock::ScopedLock(const MutexBase& mutex) | ||
| 161 | + : mutex_(mutex) { | ||
| 162 | + Traits::mutex_lock(&mutex_.mutex_); | ||
| 163 | + } | ||
| 164 | + | ||
| 165 | + template <typename Traits> | ||
| 166 | + MutexBase<Traits>::ScopedLock::ScopedLock(const ScopedUnlock& scoped_unlock) | ||
| 167 | + : MutexBase(scoped_unlock.mutex_) {} | ||
| 168 | + | ||
| 169 | + template <typename Traits> | ||
| 170 | + MutexBase<Traits>::ScopedLock::~ScopedLock() { | ||
| 171 | + Traits::mutex_unlock(&mutex_.mutex_); | ||
| 172 | + } | ||
| 173 | + | ||
| 174 | + template <typename Traits> | ||
| 175 | + MutexBase<Traits>::ScopedUnlock::ScopedUnlock(const ScopedLock& scoped_lock) | ||
| 176 | + : mutex_(scoped_lock.mutex_) { | ||
| 177 | + Traits::mutex_unlock(&mutex_.mutex_); | ||
| 178 | + } | ||
| 179 | + | ||
| 180 | + template <typename Traits> | ||
| 181 | + MutexBase<Traits>::ScopedUnlock::~ScopedUnlock() { | ||
| 182 | + Traits::mutex_lock(&mutex_.mutex_); | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + } // namespace node | ||
| 186 | + | ||
| 187 | + #endif // SRC_NODE_MUTEX_H_ | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments