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
|
/*
Copyright (C) 2005-2009 Michel de Boer <michel@twinklephone.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <string>
#include <iostream>
#include "mutex.h"
#include "thread.h"
#include <stdexcept>
using namespace std;
///////////////////////////
// t_mutex
///////////////////////////
t_mutex::t_mutex() {
pthread_mutex_init(&mutex, NULL);
}
t_mutex::t_mutex(bool recursive) {
pthread_mutexattr_t attr;
pthread_mutexattr_init(&attr);
int ret = pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE_NP);
if (ret != 0) throw string(
"t_mutex::t_mutex failed to create a recursive mutex.");
pthread_mutex_init(&mutex, &attr);
pthread_mutexattr_destroy(&attr);
}
t_mutex::~t_mutex() {
pthread_mutex_destroy(&mutex);
}
void t_mutex::lock(void) {
int ret = pthread_mutex_lock(&mutex);
if (ret != 0) throw string("t_mutex::lock failed.");
}
int t_mutex::trylock(void) {
int ret = pthread_mutex_trylock(&mutex);
switch (ret) {
case 0:
case EBUSY:
return ret;
default:
throw string("t_mutex::trylock failed.");
}
}
void t_mutex::unlock(void) {
int ret = pthread_mutex_unlock(&mutex);
if (ret != 0) throw ("t_mutex::unlock failed.");
}
///////////////////////////
// t_recursive_mutex
///////////////////////////
t_recursive_mutex::t_recursive_mutex() : t_mutex(true) {}
t_recursive_mutex::~t_recursive_mutex() {}
///////////////////////////
// t_guard_mutex
///////////////////////////
t_mutex_guard::t_mutex_guard(t_mutex &mutex) : mutex_(mutex) {
mutex_.lock();
}
t_mutex_guard::~t_mutex_guard() {
mutex_.unlock();
}
///////////////////////////
// t_rwmutex
///////////////////////////
t_rwmutex::t_rwmutex()
{
int ret = pthread_rwlock_init(&_lock, nullptr);
if (ret != 0) throw string(
"t_rwmutex::t_rwmutex failed to create a r/w mutex.");
}
t_rwmutex::~t_rwmutex()
{
pthread_rwlock_destroy(&_lock);
}
void t_rwmutex::lockRead()
{
int err = pthread_rwlock_rdlock(&_lock);
if (err != 0)
throw std::logic_error("Mutex lock failed");
}
void t_rwmutex::lockWrite()
{
int err = pthread_rwlock_wrlock(&_lock);
if (err != 0)
throw std::logic_error("Mutex lock failed");
}
void t_rwmutex::unlock()
{
pthread_rwlock_unlock(&_lock);
}
|