1 #ifndef DOZERG_MUTEX_H_20070924 2 #define DOZERG_MUTEX_H_20070924 26 #include "scoped_ptr.hh" 28 #include "tools/time.hh" 29 #include "tools/system.hh" 41 THROW_RT_IF_FAIL(::pthread_mutexattr_init(&attr_));
44 ::pthread_mutexattr_destroy(&attr_);
46 void pshared(
bool on)
throw(std::runtime_error){
47 THROW_RT_IF_FAIL(::pthread_mutexattr_setpshared(&attr_, (on ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)));
49 bool pshared()
const throw(std::runtime_error){
51 THROW_RT_IF_FAIL(::pthread_mutexattr_getpshared(&attr_, &on));
52 return (PTHREAD_PROCESS_SHARED == on);
54 void type(
int t)
throw(std::runtime_error){
55 THROW_RT_IF_FAIL(::pthread_mutexattr_settype(&attr_, t));
57 int type()
const throw(std::runtime_error){
59 THROW_RT_IF_FAIL(::pthread_mutexattr_gettype(&attr_, &t));
63 const pthread_mutexattr_t * a()
const{
return &attr_;}
65 __Myt & operator =(
const __Myt &);
67 pthread_mutexattr_t attr_;
78 CMutex()
throw(std::runtime_error){
80 THROW_RT_IF_FAIL(::pthread_mutex_init(&mutex_, NULL));
81 #else //check dead lock 83 attr.type(PTHREAD_MUTEX_ERRORCHECK);
84 THROW_RT_IF_FAIL(::pthread_mutex_init(&mutex_, attr.a()));
88 THROW_RT_IF_FAIL(::pthread_mutex_init(&mutex_, attr.a()));
91 ::pthread_mutex_destroy(&mutex_);
93 void lock()
volatile throw(std::runtime_error){
94 THROW_RT_IF_FAIL(::pthread_mutex_lock(m()));
96 bool tryLock()
volatile{
97 return (0 == ::pthread_mutex_trylock(m()));
100 bool timeLock(uint32_t timeMs)
volatile{
102 if(!tools::GetAbsTimespec(timeMs, &ts))
104 return (0 == ::pthread_mutex_timedlock(m(), &ts));
106 void unlock()
volatile const throw(std::runtime_error){
107 THROW_RT_IF_FAIL(::pthread_mutex_unlock(m()));
110 pthread_mutex_t * m()
const volatile{
111 return const_cast<pthread_mutex_t *
>(&mutex_);
114 __Myt & operator =(
const __Myt &);
116 mutable pthread_mutex_t mutex_;
125 THROW_RT_IF_FAIL(::pthread_condattr_init(&attr_));
128 ::pthread_condattr_destroy(&attr_);
130 void pshared(
bool on)
throw(std::runtime_error){
131 THROW_RT_IF_FAIL(::pthread_condattr_setpshared(&attr_, (on ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)));
133 bool pshared()
const throw(std::runtime_error){
135 THROW_RT_IF_FAIL(::pthread_condattr_getpshared(&attr_, &on));
136 return (PTHREAD_PROCESS_SHARED == on);
139 const pthread_condattr_t * a()
const{
return &attr_;}
141 __Myt operator =(
const __Myt &);
143 pthread_condattr_t attr_;
151 THROW_RT_IF_FAIL(::pthread_cond_init(&cond_, NULL));
154 THROW_RT_IF_FAIL(::pthread_cond_init(&cond_, attr.a()));
157 ::pthread_cond_destroy(&cond_);
159 void signal()
throw(std::runtime_error){
160 THROW_RT_IF_FAIL(::pthread_cond_signal(&cond_));
162 void broadcast()
throw(std::runtime_error){
163 THROW_RT_IF_FAIL(::pthread_cond_broadcast(&cond_));
165 void wait(
volatile CMutex & m)
throw(std::runtime_error){
166 THROW_RT_IF_FAIL(::pthread_cond_wait(&cond_, m.m()));
169 bool timeWait(
volatile CMutex & m, uint32_t timeMs){
171 if(!tools::GetAbsTimespec(timeMs, &ts))
173 return (0 == ::pthread_cond_timedwait(&cond_, m.m(), &ts));
177 __Myt & operator =(
const __Myt &);
179 pthread_cond_t cond_;
190 THROW_RT_IF_FAIL(::pthread_rwlockattr_init(&attr_));
193 ::pthread_rwlockattr_destroy(&attr_);
195 void pshared(
bool on)
throw(std::runtime_error){
196 THROW_RT_IF_FAIL(::pthread_rwlockattr_setpshared(&attr_, (on ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)));
198 bool pshared()
const throw(std::runtime_error){
200 THROW_RT_IF_FAIL(::pthread_rwlockattr_getpshared(&attr_, &on));
201 return (PTHREAD_PROCESS_SHARED == on);
204 const pthread_rwlockattr_t * a()
const{
return &attr_;}
206 __Myt operator =(
const __Myt &);
208 pthread_rwlockattr_t attr_;
215 CRWLock()
throw(std::runtime_error){
216 THROW_RT_IF_FAIL(::pthread_rwlock_init(&lock_, NULL));
219 THROW_RT_IF_FAIL(::pthread_rwlock_init(&lock_, attr.a()));
222 ::pthread_rwlock_destroy(&lock_);
224 void readLock()
const volatile throw(std::runtime_error){
225 THROW_RT_IF_FAIL(::pthread_rwlock_rdlock(l()));
227 bool tryReadLock()
const volatile{
228 return (0 == ::pthread_rwlock_tryrdlock(l()));
231 bool timeReadLock(uint32_t timeMs)
const volatile{
233 if(!tools::GetAbsTimespec(timeMs, &ts))
235 return (0 == ::pthread_rwlock_timedrdlock(l(), &ts));
237 void writeLock()
volatile throw(std::runtime_error){
238 THROW_RT_IF_FAIL(::pthread_rwlock_wrlock(l()));
240 bool tryWriteLock()
volatile{
241 return (0 == ::pthread_rwlock_trywrlock(l()));
244 bool timeWriteLock(uint32_t timeMs)
volatile{
246 if(!tools::GetAbsTimespec(timeMs, &ts))
248 return (0 == ::pthread_rwlock_timedwrlock(l(), &ts));
250 void unlock()
const volatile throw(std::runtime_error){
251 THROW_RT_IF_FAIL(::pthread_rwlock_unlock(l()));
254 pthread_rwlock_t * l()
const volatile{
255 return const_cast<pthread_rwlock_t *
>(&lock_);
258 __Myt & operator =(
const __Myt &);
260 mutable pthread_rwlock_t lock_;
267 explicit CSpinLock(
int pshared =
false)
throw(std::runtime_error){
268 THROW_RT_IF_FAIL(::pthread_spin_init(&lock_, (pshared ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)));
271 ::pthread_spin_destroy(&lock_);
273 void lock()
volatile throw(std::runtime_error){
274 THROW_RT_IF_FAIL(::pthread_spin_lock(l()));
276 bool tryLock()
volatile{
277 return (0 == ::pthread_spin_trylock(l()));
279 void unlock()
const volatile throw(std::runtime_error){
280 THROW_RT_IF_FAIL(::pthread_spin_unlock(l()));
283 pthread_spinlock_t * l()
const volatile{
284 return const_cast<pthread_spinlock_t *
>(&lock_);
287 __Myt & operator =(
const __Myt &);
289 mutable pthread_spinlock_t lock_;
296 explicit CFileLock(
const char * filename)
299 THROW_RT_IF_FAIL(file_.valid() ? 0 : errno);
301 explicit CFileLock(
const std::string & filename)
304 THROW_RT_IF_FAIL(file_.valid() ? 0 : errno);
306 void readLock()
const volatile throw(std::runtime_error){
307 THROW_RT_IF_FAIL((0 == ::flock(fd(), LOCK_SH)) ? 0 : errno);
309 bool tryReadLock()
const volatile{
310 return (0 == ::flock(fd(), LOCK_SH | LOCK_NB));
312 void writeLock()
volatile throw(std::runtime_error){
313 THROW_RT_IF_FAIL((0 == ::flock(fd(), LOCK_EX)) ? 0 : errno);
315 bool tryWriteLock()
volatile{
316 return (0 == ::flock(fd(), LOCK_EX | LOCK_NB));
318 void unlock()
const volatile throw(std::runtime_error){
319 THROW_RT_IF_FAIL((0 == ::flock(fd(), LOCK_UN)) ? 0 : errno);
322 int fd()
const volatile{
323 return const_cast<const CFile &
>(file_).fd();
325 CFileLock(
const __Myt &);
326 __Myt & operator =(
const __Myt &);
338 void unlock(
const volatile lock_type & m)
const{m.unlock();}
339 void readLock(
const volatile lock_type & m)
const{
340 const_cast<volatile lock_type &
>(m).lock();
342 bool tryReadLock(
const volatile lock_type & m)
const{
343 return const_cast<volatile lock_type &
>(m).tryLock();
345 bool timeReadLock(
const volatile lock_type & m, uint32_t timeMs)
const{
346 return const_cast<volatile lock_type &
>(m).timeLock(timeMs);
348 void writeLock(
volatile lock_type & m)
const{m.lock();}
349 bool tryWriteLock(
volatile lock_type & m)
const{
return m.tryLock();}
350 bool timeWriteLock(
volatile lock_type & m, uint32_t timeMs)
const{
351 return m.timeLock(timeMs);
359 void unlock(
const volatile lock_type & m)
const{m.unlock();}
360 void readLock(
const volatile lock_type & m)
const{m.readLock();}
361 bool tryReadLock(
const volatile lock_type & m)
const{
return m.tryReadLock();}
362 bool timeReadLock(
const volatile lock_type & m, uint32_t timeMs)
const{
363 return m.timeReadLock(timeMs);
365 void writeLock(
volatile lock_type & m)
const{m.writeLock();}
366 bool tryWriteLock(
volatile lock_type & m)
const{
return m.tryWriteLock();}
367 bool timeWriteLock(
volatile lock_type & m, uint32_t timeMs)
const{
368 return m.timeWriteLock(timeMs);
376 void unlock(
const volatile lock_type & m)
const{m.unlock();}
377 void readLock(
const volatile lock_type & m)
const{
const_cast<volatile lock_type &
>(m).lock();}
378 bool tryReadLock(
const volatile lock_type & m)
const{
379 return const_cast<volatile lock_type &
>(m).tryLock();
381 bool timeReadLock(
const volatile lock_type & m, uint32_t timeMs)
const{
382 const int64_t
end = tools::MonoTimeUs(NULL,
true) + timeMs * 1000;
386 }
while(end > tools::MonoTimeUs(NULL,
true));
389 void writeLock(
volatile lock_type & m)
const{m.lock();}
390 bool tryWriteLock(
volatile lock_type & m)
const{
return m.tryLock();}
391 bool timeWriteLock(
volatile lock_type & m, uint32_t timeMs)
const{
392 const int64_t
end = tools::MonoTimeUs(NULL,
true) + timeMs * 1000;
396 }
while(end > tools::MonoTimeUs(NULL,
true));
405 void unlock(
const volatile lock_type & m)
const{m.unlock();}
406 void readLock(
const volatile lock_type & m)
const{m.readLock();}
407 bool tryReadLock(
const volatile lock_type & m)
const{
return m.tryReadLock();}
408 bool timeReadLock(
const volatile lock_type & m, uint32_t timeMs)
const{
409 const int64_t
end = tools::MonoTimeUs(NULL,
true) + timeMs * 1000;
413 }
while(end > tools::MonoTimeUs(NULL,
true));
416 void writeLock(
volatile lock_type & m)
const{m.writeLock();}
417 bool tryWriteLock(
volatile lock_type & m)
const{
return m.tryWriteLock();}
418 bool timeWriteLock(
volatile lock_type & m, uint32_t timeMs)
const{
419 const int64_t
end = tools::MonoTimeUs(NULL,
true) + timeMs * 1000;
423 }
while(end > tools::MonoTimeUs(NULL,
true));
429 template<
class LockT>
435 typedef typename COmitCV<LockT>::result_type lock_type;
438 explicit CGuard(
volatile lock_type & r,
bool bWrite =
true)
441 bWrite ? adapter_type().writeLock(r) : adapter_type().readLock(r);
443 explicit CGuard(
const volatile lock_type & r)
446 adapter_type().readLock(r);
448 explicit CGuard(
volatile lock_type * p,
bool bWrite =
true)
452 bWrite ? adapter_type().writeLock(*p) : adapter_type().readLock(*p);
454 explicit CGuard(
const volatile lock_type * p)
457 adapter_type().readLock(*p);
461 adapter_type().unlock(*lock_);
465 __Myt & operator =(
const __Myt &);
467 const volatile lock_type *
const lock_;
518 typedef void (*__Function)(void);
521 , once_(PTHREAD_ONCE_INIT)
523 void runOnce(__Function func = NULL){
524 ::pthread_once(&once_, (func ? func : func_));
528 pthread_once_t once_;
536 typedef T value_type;
537 typedef value_type & reference;
538 typedef const value_type & const_reference;
539 typedef value_type * pointer;
540 typedef const value_type * const_pointer;
543 static void Dtor(
void * p){__Ptr sp(static_cast<pointer>(p));}
546 : key_(PTHREAD_KEYS_MAX)
548 ::pthread_key_create(&key_, &Dtor);
550 bool valid()
const{
return (key_ != PTHREAD_KEYS_MAX);}
551 pointer ptr(){
return static_cast<pointer
>(getValue());}
552 const_pointer ptr()
const{
return static_cast<const_pointer
>(getValue());}
553 reference ref(){
return *ptr();}
554 const_reference ref()
const{
return *ptr();}
555 std::string toString()
const{
557 oss<<
"{key_="<<key_<<
'}';
561 void * getValue()
const{
564 void * p = ::pthread_getspecific(key_);
566 __Ptr sp(tools::New<value_type>());
567 if(0 == ::pthread_setspecific(key_, sp.get()))
584 THROW_RT_IF_FAIL(::pthread_barrierattr_init(&attr_));
587 ::pthread_barrierattr_destroy(&attr_);
589 void pshared(
bool on)
throw(std::runtime_error){
590 THROW_RT_IF_FAIL(::pthread_barrierattr_setpshared(&attr_, (on ? PTHREAD_PROCESS_SHARED : PTHREAD_PROCESS_PRIVATE)));
592 bool pshared()
const throw(std::runtime_error){
594 THROW_RT_IF_FAIL(::pthread_barrierattr_getpshared(&attr_, &on));
595 return (PTHREAD_PROCESS_SHARED == on);
598 const pthread_barrierattr_t * a()
const{
return &attr_;}
600 __Myt & operator =(
const __Myt &);
601 pthread_barrierattr_t attr_;
608 explicit CBarrier(
size_t count)
throw(std::runtime_error){
609 THROW_RT_IF_FAIL(::pthread_barrier_init(&barrier_, NULL, count));
612 THROW_RT_IF_FAIL(::pthread_barrier_init(&barrier_, attr.a(), count));
615 ::pthread_barrier_destroy(&barrier_);
617 void wait()
throw(std::runtime_error){
618 int err = ::pthread_barrier_wait(&barrier_);
619 if(0 != err && PTHREAD_BARRIER_SERIAL_THREAD != err)
620 throw std::runtime_error(tools::ErrorMsg(err).c_str());
624 __Myt & operator =(
const __Myt &);
626 pthread_barrier_t barrier_;
Definition: to_string.hh:43
Definition: scoped_ptr.hh:10
Encapsulation for fd (file descriptor) and regular files.
Regular file operations.
Definition: file.hh:181
NS_IMPL::CManipulatorEnd< void, void > end()
End operations for CInByteStream, COutByteStreamStrRef, COutByteStreamVecRef.
Definition: data_stream.hh:929