Menu

[r1584]: / framework / trunk / booster / lib / thread / src / thread.cpp  Maximize  Restore  History

Download this file

282 lines (250 with data), 6.5 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
//
// Copyright (c) 2010 Artyom Beilis (Tonkikh)
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#define BOOSTER_SOURCE
#include <pthread.h>
#include <booster/thread.h>
#include <booster/system_error.h>
#include <errno.h>
#include <string.h>
#ifdef BOOSTER_POSIX
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#endif
namespace booster {
struct thread::data {
pthread_t p;
function<void()> cb;
};
extern "C" void *booster_thread_func(void *p)
{
thread::data *d=reinterpret_cast<thread::data *>(p);
try {
d->cb();
}
catch(std::exception const &/*e*/) {
/// TODO
}
catch(...) {
/// TODO
}
return 0;
}
thread::thread(function<void()> const &cb) :
d(new thread::data)
{
d->cb=cb;
::pthread_create(&d->p,0,booster_thread_func,d.get());
}
thread::~thread()
{
}
void thread::join()
{
pthread_join(d->p,0);
}
struct mutex::data { pthread_mutex_t m; };
mutex::mutex() : d(new data)
{
pthread_mutex_init(&d->m,0);
}
mutex::~mutex()
{
pthread_mutex_destroy(&d->m);
}
void mutex::lock() { pthread_mutex_lock(&d->m); }
void mutex::unlock() { pthread_mutex_unlock(&d->m); }
struct recursive_mutex::data { pthread_mutex_t m; };
recursive_mutex::recursive_mutex() : d(new data)
{
pthread_mutexattr_t a;
pthread_mutexattr_init(&a);
pthread_mutexattr_settype(&a,PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&d->m,&a);
}
recursive_mutex::~recursive_mutex()
{
pthread_mutex_destroy(&d->m);
}
void recursive_mutex::lock() { pthread_mutex_lock(&d->m); }
void recursive_mutex::unlock() { pthread_mutex_unlock(&d->m); }
struct shared_mutex::data { pthread_rwlock_t m; };
shared_mutex::shared_mutex() : d(new data)
{
pthread_rwlock_init(&d->m,0);
}
shared_mutex::~shared_mutex()
{
pthread_rwlock_destroy(&d->m);
}
void shared_mutex::shared_lock() { pthread_rwlock_rdlock(&d->m); }
void shared_mutex::unique_lock() { pthread_rwlock_wrlock(&d->m); }
void shared_mutex::unlock() { pthread_rwlock_unlock(&d->m); }
struct condition_variable::data { pthread_cond_t c; };
condition_variable::condition_variable() : d(new data)
{
pthread_cond_init(&d->c,0);
}
condition_variable::~condition_variable()
{
pthread_cond_destroy(&d->c);
}
void condition_variable::notify_one()
{
pthread_cond_signal(&d->c);
}
void condition_variable::notify_all()
{
pthread_cond_broadcast(&d->c);
}
void condition_variable::wait(unique_lock<mutex> &m)
{
pthread_cond_wait(&d->c,&(m.mutex()->d->m));
}
namespace details {
struct thread_specific_impl::data {
std::auto_ptr<object> base;
pthread_key_t key;
};
extern "C" {
void booster_details_thread_specific_impl_deleter(void *pobject)
{
if(pobject == 0)
return;
delete reinterpret_cast<thread_specific_impl::object *>(pobject);
}
}
thread_specific_impl::thread_specific_impl(object *bptr) :
d(new data())
{
d->base.reset(bptr);
if(pthread_key_create(&d->key,booster_details_thread_specific_impl_deleter) < 0)
throw system::system_error(system::error_code(errno,system::system_category));
}
thread_specific_impl::~thread_specific_impl()
{
pthread_key_delete(d->key);
}
thread_specific_impl::object *thread_specific_impl::get_object() const
{
void *pobj = pthread_getspecific(d->key);
if(!pobj) {
pobj = reinterpret_cast<void *>(d->base->clone());
if(pthread_setspecific(d->key,pobj) < 0) {
int err=errno;
delete reinterpret_cast<object *>(pobj);
throw system::system_error(system::error_code(err,system::system_category));
}
}
return reinterpret_cast<object *>(pobj);
}
} // details
#ifdef BOOSTER_POSIX
struct fork_shared_mutex::data {
pthread_rwlock_t lock;
FILE *lock_file;
};
fork_shared_mutex::fork_shared_mutex() : d(new data)
{
pthread_rwlock_init(&d->lock,0);
d->lock_file = tmpfile();
if(!d->lock_file) {
int err=errno;
pthread_rwlock_destroy(&d->lock);
throw system::system_error(system::error_code(err,system::system_category));
}
}
fork_shared_mutex::~fork_shared_mutex()
{
// only threads see the lock - it is safe
fclose(d->lock_file);
pthread_rwlock_destroy(&d->lock);
}
bool fork_shared_mutex::try_unique_lock()
{
if(pthread_rwlock_trywrlock(&d->lock)!=0)
return false;
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_WRLCK;
lock.l_whence=SEEK_SET;
int res = 0;
while((res = ::fcntl(fileno(d->lock_file),F_SETLK,&lock))!=0 && errno==EINTR)
;
if(res == 0)
return true;
int err = errno;
pthread_rwlock_unlock(&d->lock);
if(err == EACCES || err==EAGAIN)
return false;
throw system::system_error(system::error_code(err,system::system_category));
}
void fork_shared_mutex::unique_lock()
{
pthread_rwlock_wrlock(&d->lock);
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_WRLCK;
lock.l_whence=SEEK_SET;
int res = 0;
while((res = ::fcntl(fileno(d->lock_file),F_SETLKW,&lock))!=0 && errno==EINTR)
;
if(res == 0)
return;
int err = errno;
pthread_rwlock_unlock(&d->lock);
throw system::system_error(system::error_code(err,system::system_category));
}
bool fork_shared_mutex::try_shared_lock()
{
if(pthread_rwlock_tryrdlock(&d->lock)!=0)
return false;
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_RDLCK;
lock.l_whence=SEEK_SET;
int res = 0;
while((res = ::fcntl(fileno(d->lock_file),F_SETLK,&lock))!=0 && errno==EINTR)
;
if(res == 0)
return true;
int err = errno;
pthread_rwlock_unlock(&d->lock);
if(err == EACCES || err==EAGAIN)
return false;
throw system::system_error(system::error_code(err,system::system_category));
}
void fork_shared_mutex::shared_lock()
{
pthread_rwlock_rdlock(&d->lock);
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_RDLCK;
lock.l_whence=SEEK_SET;
int res = 0;
while((res = ::fcntl(fileno(d->lock_file),F_SETLKW,&lock))!=0 && errno==EINTR)
;
if(res == 0)
return;
int err = errno;
pthread_rwlock_unlock(&d->lock);
throw system::system_error(system::error_code(err,system::system_category));
}
void fork_shared_mutex::unlock()
{
struct flock lock;
memset(&lock,0,sizeof(lock));
lock.l_type=F_UNLCK;
lock.l_whence=SEEK_SET;
int res = 0;
while((res = ::fcntl(fileno(d->lock_file),F_SETLKW,&lock))!=0 && errno==EINTR)
;
pthread_rwlock_unlock(&d->lock);
}
#endif
} // booster
MongoDB Logo MongoDB