/
mutex.hh
99 lines (84 loc) · 2.35 KB
/
mutex.hh
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef MUTEX_HH
#define MUTEX_HH 1
#include <stdexcept>
#include <iostream>
#include <sstream>
#include <pthread.h>
#include <cerrno>
#include <cstring>
#include <cassert>
#include "common.hh"
/**
* Abstraction built on top of pthread mutexes
*/
class Mutex {
public:
Mutex()
#ifndef WIN32
: holder(0)
#endif
{
pthread_mutexattr_t *attr = NULL;
int e=0;
#ifdef HAVE_PTHREAD_MUTEX_ERRORCHECK
pthread_mutexattr_t the_attr;
attr = &the_attr;
if (pthread_mutexattr_init(attr) != 0 ||
(e = pthread_mutexattr_settype(attr, PTHREAD_MUTEX_ERRORCHECK)) != 0) {
std::string message = "MUTEX ERROR: Failed to initialize mutex: ";
message.append(std::strerror(e));
throw std::runtime_error(message);
}
#endif
if ((e = pthread_mutex_init(&mutex, attr)) != 0) {
std::string message = "MUTEX ERROR: Failed to initialize mutex: ";
message.append(std::strerror(e));
throw std::runtime_error(message);
}
}
virtual ~Mutex() {
int e;
if ((e = pthread_mutex_destroy(&mutex)) != 0) {
std::string message = "MUTEX ERROR: Failed to destroy mutex: ";
message.append(std::strerror(e));
throw std::runtime_error(message);
}
}
protected:
// The holders of locks twiddle these flags.
friend class LockHolder;
friend class MultiLockHolder;
void acquire() {
int e;
if ((e = pthread_mutex_lock(&mutex)) != 0) {
std::string message = "MUTEX ERROR: Failed to acquire lock: ";
message.append(std::strerror(e));
throw std::runtime_error(message);
}
setHolder();
}
void release() {
#ifndef WIN32
assert(holder == pthread_self());
holder = 0;
#endif
int e;
if ((e = pthread_mutex_unlock(&mutex)) != 0) {
std::string message = "MUTEX_ERROR: Failed to release lock: ";
message.append(std::strerror(e));
throw std::runtime_error(message);
}
}
void setHolder() {
#ifndef WIN32
holder = pthread_self();
#endif
}
pthread_mutex_t mutex;
#ifndef WIN32
pthread_t holder;
#endif
DISALLOW_COPY_AND_ASSIGN(Mutex);
};
#endif