| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -3,6 +3,7 @@ | |||
| 3 | 3 | #include <set> | |
| 4 | 4 | #include <vector> | |
| 5 | 5 | #include <map> | |
| 6 | + #include <algorithm> | ||
| 6 | 7 | ||
| 7 | 8 | #include "../include/lock_master.h" | |
| 8 | 9 | ||
@@ -78,7 +79,7 @@ void LockMasterImpl::Initialize() { | |||
| 78 | 79 | void LockMasterImpl::CleanupMutexes(uv_async_t *async) { | |
| 79 | 80 | uv_mutex_lock(&mapMutex); | |
| 80 | 81 | ||
| 81 | - for(std::map<const void *, std::pair<uv_mutex_t *, unsigned> >::iterator it=mutexes.begin(); it != mutexes.end(); ) | ||
| 82 | + for (auto it = mutexes.begin(); it != mutexes.end(); ) | ||
| 82 | 83 | { | |
| 83 | 84 | uv_mutex_t *mutex = it->second.first; | |
| 84 | 85 | unsigned use_count = it->second.second; | |
@@ -88,7 +89,7 @@ void LockMasterImpl::CleanupMutexes(uv_async_t *async) { | |||
| 88 | 89 | if (!use_count) { | |
| 89 | 90 | uv_mutex_destroy(mutex); | |
| 90 | 91 | free(mutex); | |
| 91 | - std::map<const void *, std::pair<uv_mutex_t *, unsigned> >::iterator to_erase = it; | ||
| 92 | + auto to_erase = it; | ||
| 92 | 93 | it++; | |
| 93 | 94 | mutexes.erase(to_erase); | |
| 94 | 95 | } else { | |
@@ -108,11 +109,10 @@ std::vector<uv_mutex_t *> LockMasterImpl::GetMutexes(int useCountDelta) { | |||
| 108 | 109 | ||
| 109 | 110 | uv_mutex_lock(&mapMutex); | |
| 110 | 111 | ||
| 111 | - for (std::set<const void *>::const_iterator it = objectsToLock.begin(); it != objectsToLock.end(); it++) { | ||
| 112 | - const void *object = *it; | ||
| 112 | + for (auto object : objectsToLock) { | ||
| 113 | 113 | if(object) { | |
| 114 | 114 | // ensure we have an initialized mutex for each object | |
| 115 | - std::map<const void *, std::pair<uv_mutex_t *, unsigned> >::iterator mutexIt = mutexes.find(object); | ||
| 115 | + auto mutexIt = mutexes.find(object); | ||
| 116 | 116 | if(mutexIt == mutexes.end()) { | |
| 117 | 117 | mutexIt = mutexes.insert( | |
| 118 | 118 | std::make_pair( | |
@@ -144,7 +144,7 @@ void LockMasterImpl::Unregister() { | |||
| 144 | 144 | void LockMasterImpl::Lock(bool acquireMutexes) { | |
| 145 | 145 | std::vector<uv_mutex_t *> objectMutexes = GetMutexes(acquireMutexes * 1); | |
| 146 | 146 | ||
| 147 | - std::vector<uv_mutex_t *>::iterator already_locked = objectMutexes.end(); | ||
| 147 | + auto alreadyLocked = objectMutexes.end(); | ||
| 148 | 148 | ||
| 149 | 149 | // we will attempt to lock all the mutexes at the same time to avoid deadlocks | |
| 150 | 150 | // note in most cases we are locking 0 or 1 mutexes. more than 1 implies | |
@@ -155,24 +155,22 @@ void LockMasterImpl::Lock(bool acquireMutexes) { | |||
| 155 | 155 | for(it = objectMutexes.begin(); it != objectMutexes.end(); it++) { | |
| 156 | 156 | // if we already locked this mutex in a previous pass via uv_mutex_lock, | |
| 157 | 157 | // we don't need to lock it again | |
| 158 | - if(it == already_locked) { | ||
| 158 | + if (it == alreadyLocked) { | ||
| 159 | 159 | continue; | |
| 160 | 160 | } | |
| 161 | 161 | // first, try to lock (non-blocking) | |
| 162 | 162 | bool failure = uv_mutex_trylock(*it); | |
| 163 | 163 | if(failure) { | |
| 164 | 164 | // we have failed to lock a mutex... unlock everything we have locked | |
| 165 | - for(std::vector<uv_mutex_t *>::iterator unlockIt = objectMutexes.begin(); unlockIt != it; unlockIt++) { | ||
| 166 | - uv_mutex_unlock(*unlockIt); | ||
| 167 | - } | ||
| 168 | - if(already_locked > it && already_locked != objectMutexes.end()) { | ||
| 169 | - uv_mutex_unlock(*already_locked); | ||
| 165 | + std::for_each(objectMutexes.begin(), it, uv_mutex_unlock); | ||
| 166 | + if (alreadyLocked > it && alreadyLocked != objectMutexes.end()) { | ||
| 167 | + uv_mutex_unlock(*alreadyLocked); | ||
| 170 | 168 | } | |
| 171 | 169 | // now do a blocking lock on what we couldn't lock | |
| 172 | 170 | uv_mutex_lock(*it); | |
| 173 | 171 | // mark that we have already locked this one | |
| 174 | 172 | // if there are more mutexes than this one, we will go back to locking everything | |
| 175 | - already_locked = it; | ||
| 173 | + alreadyLocked = it; | ||
| 176 | 174 | break; | |
| 177 | 175 | } | |
| 178 | 176 | } | |
@@ -182,9 +180,7 @@ void LockMasterImpl::Lock(bool acquireMutexes) { | |||
| 182 | 180 | void LockMasterImpl::Unlock(bool releaseMutexes) { | |
| 183 | 181 | std::vector<uv_mutex_t *> objectMutexes = GetMutexes(releaseMutexes * -1); | |
| 184 | 182 | ||
| 185 | - for(std::vector<uv_mutex_t *>::iterator it=objectMutexes.begin(); it != objectMutexes.end(); it++) { | ||
| 186 | - uv_mutex_unlock(*it); | ||
| 187 | - } | ||
| 183 | + std::for_each(objectMutexes.begin(), objectMutexes.end(), uv_mutex_unlock); | ||
| 188 | 184 | } | |
| 189 | 185 | ||
| 190 | 186 | void LockMasterImpl::CleanupMutexes() { | |
| Back | FazBrowse Home | New Git URL |
0 commit comments