| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 3e6b3b2 commit a2da9e2
3 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1331,7 +1331,7 @@ Maybe<bool> SiblingGroup::Dispatch( | |||
| 1331 | 1331 | std::shared_ptr<Message> message, | |
| 1332 | 1332 | std::string* error) { | |
| 1333 | 1333 | ||
| 1334 | - Mutex::ScopedLock lock(group_mutex_); | ||
| 1334 | + RwLock::ScopedReadLock lock(group_mutex_); | ||
| 1335 | 1335 | ||
| 1336 | 1336 | // The source MessagePortData is not part of this group. | |
| 1337 | 1337 | if (ports_.find(source) == ports_.end()) { | |
@@ -1376,7 +1376,7 @@ void SiblingGroup::Entangle(MessagePortData* port) { | |||
| 1376 | 1376 | } | |
| 1377 | 1377 | ||
| 1378 | 1378 | void SiblingGroup::Entangle(std::initializer_list<MessagePortData*> ports) { | |
| 1379 | - Mutex::ScopedLock lock(group_mutex_); | ||
| 1379 | + RwLock::ScopedWriteLock lock(group_mutex_); | ||
| 1380 | 1380 | for (MessagePortData* data : ports) { | |
| 1381 | 1381 | ports_.insert(data); | |
| 1382 | 1382 | CHECK(!data->group_); | |
@@ -1386,7 +1386,7 @@ void SiblingGroup::Entangle(std::initializer_list<MessagePortData*> ports) { | |||
| 1386 | 1386 | ||
| 1387 | 1387 | void SiblingGroup::Disentangle(MessagePortData* data) { | |
| 1388 | 1388 | auto self = shared_from_this(); // Keep alive until end of function. | |
| 1389 | - Mutex::ScopedLock lock(group_mutex_); | ||
| 1389 | + RwLock::ScopedWriteLock lock(group_mutex_); | ||
| 1390 | 1390 | ports_.erase(data); | |
| 1391 | 1391 | data->group_.reset(); | |
| 1392 | 1392 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -150,9 +150,9 @@ class SiblingGroup final : public std::enable_shared_from_this<SiblingGroup> { | |||
| 150 | 150 | size_t size() const { return ports_.size(); } | |
| 151 | 151 | ||
| 152 | 152 | private: | |
| 153 | - std::string name_; | ||
| 153 | + const std::string name_; | ||
| 154 | + RwLock group_mutex_; // Protects ports_. | ||
| 154 | 155 | std::set<MessagePortData*> ports_; | |
| 155 | - Mutex group_mutex_; | ||
| 156 | 156 | ||
| 157 | 157 | static void CheckSiblingGroup(const std::string& name); | |
| 158 | 158 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -14,9 +14,11 @@ namespace node { | |||
| 14 | 14 | template <typename Traits> class ConditionVariableBase; | |
| 15 | 15 | template <typename Traits> class MutexBase; | |
| 16 | 16 | struct LibuvMutexTraits; | |
| 17 | + struct LibuvRwlockTraits; | ||
| 17 | 18 | ||
| 18 | 19 | using ConditionVariable = ConditionVariableBase<LibuvMutexTraits>; | |
| 19 | 20 | using Mutex = MutexBase<LibuvMutexTraits>; | |
| 21 | + using RwLock = MutexBase<LibuvRwlockTraits>; | ||
| 20 | 22 | ||
| 21 | 23 | template <typename T, typename MutexT = Mutex> | |
| 22 | 24 | class ExclusiveAccess { | |
@@ -70,6 +72,8 @@ class MutexBase { | |||
| 70 | 72 | inline ~MutexBase(); | |
| 71 | 73 | inline void Lock(); | |
| 72 | 74 | inline void Unlock(); | |
| 75 | + inline void RdLock(); | ||
| 76 | + inline void RdUnlock(); | ||
| 73 | 77 | ||
| 74 | 78 | MutexBase(const MutexBase&) = delete; | |
| 75 | 79 | MutexBase& operator=(const MutexBase&) = delete; | |
@@ -92,6 +96,21 @@ class MutexBase { | |||
| 92 | 96 | const MutexBase& mutex_; | |
| 93 | 97 | }; | |
| 94 | 98 | ||
| 99 | + class ScopedReadLock { | ||
| 100 | + public: | ||
| 101 | + inline explicit ScopedReadLock(const MutexBase& mutex); | ||
| 102 | + inline ~ScopedReadLock(); | ||
| 103 | + | ||
| 104 | + ScopedReadLock(const ScopedReadLock&) = delete; | ||
| 105 | + ScopedReadLock& operator=(const ScopedReadLock&) = delete; | ||
| 106 | + | ||
| 107 | + private: | ||
| 108 | + template <typename> friend class ConditionVariableBase; | ||
| 109 | + const MutexBase& mutex_; | ||
| 110 | + }; | ||
| 111 | + | ||
| 112 | + using ScopedWriteLock = ScopedLock; | ||
| 113 | + | ||
| 95 | 114 | class ScopedUnlock { | |
| 96 | 115 | public: | |
| 97 | 116 | inline explicit ScopedUnlock(const ScopedLock& scoped_lock); | |
@@ -167,6 +186,42 @@ struct LibuvMutexTraits { | |||
| 167 | 186 | static inline void mutex_unlock(MutexT* mutex) { | |
| 168 | 187 | uv_mutex_unlock(mutex); | |
| 169 | 188 | } | |
| 189 | + | ||
| 190 | + static inline void mutex_rdlock(MutexT* mutex) { | ||
| 191 | + uv_mutex_lock(mutex); | ||
| 192 | + } | ||
| 193 | + | ||
| 194 | + static inline void mutex_rdunlock(MutexT* mutex) { | ||
| 195 | + uv_mutex_unlock(mutex); | ||
| 196 | + } | ||
| 197 | + }; | ||
| 198 | + | ||
| 199 | + struct LibuvRwlockTraits { | ||
| 200 | + using MutexT = uv_rwlock_t; | ||
| 201 | + | ||
| 202 | + static inline int mutex_init(MutexT* mutex) { | ||
| 203 | + return uv_rwlock_init(mutex); | ||
| 204 | + } | ||
| 205 | + | ||
| 206 | + static inline void mutex_destroy(MutexT* mutex) { | ||
| 207 | + uv_rwlock_destroy(mutex); | ||
| 208 | + } | ||
| 209 | + | ||
| 210 | + static inline void mutex_lock(MutexT* mutex) { | ||
| 211 | + uv_rwlock_wrlock(mutex); | ||
| 212 | + } | ||
| 213 | + | ||
| 214 | + static inline void mutex_unlock(MutexT* mutex) { | ||
| 215 | + uv_rwlock_wrunlock(mutex); | ||
| 216 | + } | ||
| 217 | + | ||
| 218 | + static inline void mutex_rdlock(MutexT* mutex) { | ||
| 219 | + uv_rwlock_rdlock(mutex); | ||
| 220 | + } | ||
| 221 | + | ||
| 222 | + static inline void mutex_rdunlock(MutexT* mutex) { | ||
| 223 | + uv_rwlock_rdunlock(mutex); | ||
| 224 | + } | ||
| 170 | 225 | }; | |
| 171 | 226 | ||
| 172 | 227 | template <typename Traits> | |
@@ -214,6 +269,16 @@ void MutexBase<Traits>::Unlock() { | |||
| 214 | 269 | Traits::mutex_unlock(&mutex_); | |
| 215 | 270 | } | |
| 216 | 271 | ||
| 272 | + template <typename Traits> | ||
| 273 | + void MutexBase<Traits>::RdLock() { | ||
| 274 | + Traits::mutex_rdlock(&mutex_); | ||
| 275 | + } | ||
| 276 | + | ||
| 277 | + template <typename Traits> | ||
| 278 | + void MutexBase<Traits>::RdUnlock() { | ||
| 279 | + Traits::mutex_rdunlock(&mutex_); | ||
| 280 | + } | ||
| 281 | + | ||
| 217 | 282 | template <typename Traits> | |
| 218 | 283 | MutexBase<Traits>::ScopedLock::ScopedLock(const MutexBase& mutex) | |
| 219 | 284 | : mutex_(mutex) { | |
@@ -229,6 +294,17 @@ MutexBase<Traits>::ScopedLock::~ScopedLock() { | |||
| 229 | 294 | Traits::mutex_unlock(&mutex_.mutex_); | |
| 230 | 295 | } | |
| 231 | 296 | ||
| 297 | + template <typename Traits> | ||
| 298 | + MutexBase<Traits>::ScopedReadLock::ScopedReadLock(const MutexBase& mutex) | ||
| 299 | + : mutex_(mutex) { | ||
| 300 | + Traits::mutex_rdlock(&mutex_.mutex_); | ||
| 301 | + } | ||
| 302 | + | ||
| 303 | + template <typename Traits> | ||
| 304 | + MutexBase<Traits>::ScopedReadLock::~ScopedReadLock() { | ||
| 305 | + Traits::mutex_rdunlock(&mutex_.mutex_); | ||
| 306 | + } | ||
| 307 | + | ||
| 232 | 308 | template <typename Traits> | |
| 233 | 309 | MutexBase<Traits>::ScopedUnlock::ScopedUnlock(const ScopedLock& scoped_lock) | |
| 234 | 310 | : mutex_(scoped_lock.mutex_) { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments