-
Notifications
You must be signed in to change notification settings - Fork 223
Expand file tree
/
Copy pathMutex.cpp
More file actions
148 lines (122 loc) · 2.89 KB
/
Mutex.cpp
File metadata and controls
148 lines (122 loc) · 2.89 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Mutex.cpp
* Mutex and ConditionVariable
* Copyright (C) 2010 Simon Newton
*/
#include <pthread.h>
#include "ola/thread/Mutex.h"
namespace ola {
namespace thread {
/**
* Construct a new mutex object
*/
Mutex::Mutex() {
pthread_mutex_init(&m_mutex, NULL);
}
/**
* Clean up
*/
Mutex::~Mutex() {
pthread_mutex_destroy(&m_mutex);
}
/**
* Lock this mutex
*/
void Mutex::Lock() {
pthread_mutex_lock(&m_mutex);
}
/**
* Try and lock this mutex
* @return true if we got the lock, false otherwise
*/
bool Mutex::TryLock() {
int i = pthread_mutex_trylock(&m_mutex);
return i == 0;
}
/**
* Unlock this mutex
*/
void Mutex::Unlock() {
pthread_mutex_unlock(&m_mutex);
}
/**
* Create a new MutexLocker and lock the mutex.
*/
MutexLocker::MutexLocker(Mutex *mutex)
: m_mutex(mutex),
m_requires_unlock(true) {
m_mutex->Lock();
}
/**
* Destroy this MutexLocker and unlock the mutex
*/
MutexLocker::~MutexLocker() {
Release();
}
void MutexLocker::Release() {
if (m_requires_unlock) {
m_mutex->Unlock();
m_requires_unlock = false;
}
}
/**
* New ConditionVariable
*/
ConditionVariable::ConditionVariable() {
pthread_cond_init(&m_condition, NULL);
}
/**
* Clean up
*/
ConditionVariable::~ConditionVariable() {
pthread_cond_destroy(&m_condition);
}
/**
* Wait on a condition variable
* @param mutex the mutex that is locked
*/
void ConditionVariable::Wait(Mutex *mutex) {
pthread_cond_wait(&m_condition, &mutex->m_mutex);
}
/**
* Timed Wait
* @param mutex the mutex that is locked
* @param wake_up_time the time to wait up.
* @returns true if we received a signal, false if the timeout expired.
*/
bool ConditionVariable::TimedWait(Mutex *mutex, const TimeStamp &wake_up_time) {
struct timespec ts = {
wake_up_time.Seconds(),
wake_up_time.Microseconds() * ONE_THOUSAND
};
int i = pthread_cond_timedwait(&m_condition, &mutex->m_mutex, &ts);
return i == 0;
}
/**
* Wake up a single listener
*/
void ConditionVariable::Signal() {
pthread_cond_signal(&m_condition);
}
/**
* Wake up all listeners
*/
void ConditionVariable::Broadcast() {
pthread_cond_broadcast(&m_condition);
}
} // namespace thread
} // namespace ola