-
Notifications
You must be signed in to change notification settings - Fork 1
/
lock_many.tcc
53 lines (49 loc) · 1.17 KB
/
lock_many.tcc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#define lock_many_tcc
#include <mutex>
#include <vector>
#include <algorithm>
#include "lock_many.h"
namespace kaiu {
template <typename It>
lock_many::lock_many(It begin, It end)
{
/* Map mutexes to unique_lock<mutex> */
for ( ; begin != end; ++begin) {
locks.emplace_back(*begin, std::defer_lock);
}
const size_t count = locks.size();
if (count == 0) {
return;
}
size_t start = 0;
/* Repeatedly try to lock all mutexes */
do {
bool failed = false;
/* Iterate over all mutexes, start from last which failed to lock */
for (size_t counter = 0; counter < count; counter++) {
const size_t i = (start + counter) % count;
auto& lock = locks[i];
/* First mutex of the loop: Block to lock */
if (counter == 0) {
lock.lock();
/* Subsequent mutexes: Try to lock, fail if we can't */
} else if (!lock.try_lock()) {
failed = true;
/* Next loop will begin by blocking until we can lock this mutex */
start = i;
break;
}
}
/* Locked all mutexes? Break out of loop */
if (!failed) {
break;
}
/* Unlock all if we failed to lock all */
for (auto& lock : locks) {
if (lock.owns_lock()) {
lock.unlock();
}
}
} while (true);
}
}