FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Use auto, range-based for loops, and for_each · PB-Tech/nodegit@90d8073 · GitHub

Repository navigation

Commit 90d8073

Browse files
committed
Use auto, range-based for loops, and for_each
1 parent fb4603d commit 90d8073

1 file changed

Lines changed: 12 additions & 16 deletions

File tree

‎generate/templates/manual/src/lock_master.cc‎

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <set>
44
#include <vector>
55
#include <map>
6+
#include <algorithm>
67

78
#include "../include/lock_master.h"
89

@@ -78,7 +79,7 @@ void LockMasterImpl::Initialize() {
7879
void LockMasterImpl::CleanupMutexes(uv_async_t *async) {
7980
uv_mutex_lock(&mapMutex);
8081

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(); )
8283
{
8384
uv_mutex_t *mutex = it->second.first;
8485
unsigned use_count = it->second.second;
@@ -88,7 +89,7 @@ void LockMasterImpl::CleanupMutexes(uv_async_t *async) {
8889
if (!use_count) {
8990
uv_mutex_destroy(mutex);
9091
free(mutex);
91-
std::map<const void *, std::pair<uv_mutex_t *, unsigned> >::iterator to_erase = it;
92+
auto to_erase = it;
9293
it++;
9394
mutexes.erase(to_erase);
9495
} else {
@@ -108,11 +109,10 @@ std::vector<uv_mutex_t *> LockMasterImpl::GetMutexes(int useCountDelta) {
108109

109110
uv_mutex_lock(&mapMutex);
110111

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) {
113113
if(object) {
114114
// 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);
116116
if(mutexIt == mutexes.end()) {
117117
mutexIt = mutexes.insert(
118118
std::make_pair(
@@ -144,7 +144,7 @@ void LockMasterImpl::Unregister() {
144144
void LockMasterImpl::Lock(bool acquireMutexes) {
145145
std::vector<uv_mutex_t *> objectMutexes = GetMutexes(acquireMutexes * 1);
146146

147-
std::vector<uv_mutex_t *>::iterator already_locked = objectMutexes.end();
147+
auto alreadyLocked = objectMutexes.end();
148148

149149
// we will attempt to lock all the mutexes at the same time to avoid deadlocks
150150
// note in most cases we are locking 0 or 1 mutexes. more than 1 implies
@@ -155,24 +155,22 @@ void LockMasterImpl::Lock(bool acquireMutexes) {
155155
for(it = objectMutexes.begin(); it != objectMutexes.end(); it++) {
156156
// if we already locked this mutex in a previous pass via uv_mutex_lock,
157157
// we don't need to lock it again
158-
if(it == already_locked) {
158+
if (it == alreadyLocked) {
159159
continue;
160160
}
161161
// first, try to lock (non-blocking)
162162
bool failure = uv_mutex_trylock(*it);
163163
if(failure) {
164164
// 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);
170168
}
171169
// now do a blocking lock on what we couldn't lock
172170
uv_mutex_lock(*it);
173171
// mark that we have already locked this one
174172
// if there are more mutexes than this one, we will go back to locking everything
175-
already_locked = it;
173+
alreadyLocked = it;
176174
break;
177175
}
178176
}
@@ -182,9 +180,7 @@ void LockMasterImpl::Lock(bool acquireMutexes) {
182180
void LockMasterImpl::Unlock(bool releaseMutexes) {
183181
std::vector<uv_mutex_t *> objectMutexes = GetMutexes(releaseMutexes * -1);
184182

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);
188184
}
189185

190186
void LockMasterImpl::CleanupMutexes() {

0 commit comments

Comments
 (0)

Back | FazBrowse Home | New Git URL