27 #ifndef NIDAS_UTIL_THREADSUPPORT_H
28 #define NIDAS_UTIL_THREADSUPPORT_H
31 #include <semaphore.h>
37 namespace nidas {
namespace util {
77 #ifdef PTHREAD_PRIO_INHERIT
86 void setPriorityCeiling(
int val)
throw(
Exception);
87 int getPriorityCeiling()
const;
90 #ifdef PTHREAD_PRIO_INHERIT
96 void setProtocol(
int val)
throw(
Exception);
97 int getProtocol()
const;
109 pthread_mutexattr_t*
ptr()
143 pthread_rwlockattr_t*
ptr()
171 explicit Mutex(
int type=PTHREAD_MUTEX_DEFAULT)
throw();
209 if((res = ::pthread_mutex_lock(&
_p_mutex)))
210 throw Exception(
"Mutex::lock",res);
221 if ((res = ::pthread_mutex_unlock(&
_p_mutex)))
222 throw Exception(
"Mutex::unlock",res);
228 pthread_mutex_t*
ptr();
314 if ((res = ::pthread_cond_signal (&
_p_cond)))
315 throw Exception(
"Cond::signal",res);
326 if ((res = ::pthread_cond_broadcast (&
_p_cond)))
373 RWLock(
const RWLock& x)
throw();
388 if ((res = ::pthread_rwlock_rdlock(&_p_rwlock)))
389 throw Exception(
"RWLock::rdlock",res);
399 if ((res = ::pthread_rwlock_wrlock(&_p_rwlock)))
400 throw Exception(
"RWLock::wrlock",res);
410 if ((res = ::pthread_rwlock_unlock(&_p_rwlock)))
411 throw Exception(
"RWLock::unlock",res);
417 pthread_rwlock_t* ptr();
471 return sem_trywait(&_sem) == 0;
494 sem_getvalue(&_sem,&val);
501 sem_t*
ptr() {
return &_sem; }
524 void sync(std::string& msg);
568 if (condp) condp->unlock();
569 else if (mutexp) mutexp->unlock();
614 if (_condp) _condp->unlock();
615 else if (_mutexp) _mutexp->unlock();
void signal()
Unblock at least one thread waiting on the condition variable.
Definition: ThreadSupport.h:310
Synchronized is used a simple guard object for critical sections.
Definition: ThreadSupport.h:544
int _n
Definition: ThreadSupport.h:529
Autolock(Cond &cond)
Construct the guard object and lock() the lock.
Definition: ThreadSupport.h:597
Semaphore()
Constructor.
Definition: ThreadSupport.h:441
~Cond()
Destruct a Cond.
Definition: ThreadSupport.cc:281
~MutexAttributes()
Definition: ThreadSupport.cc:66
void wrlock()
Acquire a write lock.
Definition: ThreadSupport.h:396
Synchronized(Cond &cond_)
Construct the guard object and lock() the lock.
Definition: ThreadSupport.h:554
void lock()
Lock the mutex associated with the condition variable.
Definition: ThreadSupport.h:290
~Autolock()
On destruction, unlock the lock.
Definition: ThreadSupport.h:612
AutoRdLock(RWLock &rwlock)
Construct the guard object and lock() the lock.
Definition: ThreadSupport.h:638
pthread_mutex_t _p_mutex
Definition: ThreadSupport.h:236
sem_t _sem
Definition: ThreadSupport.h:515
void broadcast()
Restart all threads waiting on the condition variable.
Definition: ThreadSupport.h:322
void post()
Atomically increment the Semaphore.
Definition: ThreadSupport.h:477
~AutoWrLock()
On destruction, unlock the lock.
Definition: ThreadSupport.h:677
void unlock()
Unlock the RWLock.
Definition: ThreadSupport.h:407
Mutex * _mutexp
Definition: ThreadSupport.h:619
~Mutex()
Destruct a Mutex.
Definition: ThreadSupport.cc:240
RWLockAttributes _attrs
Definition: ThreadSupport.h:427
A C++ wrapper for a POSIX rwlock attributes.
Definition: ThreadSupport.h:122
Cond * _condp
Definition: ThreadSupport.h:620
void setPShared(int val)
Set the mutex pshared attribute, one of PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED.
Definition: ThreadSupport.cc:144
Synchronized(Mutex &mutex_)
Definition: ThreadSupport.h:558
pthread_rwlockattr_t _attrs
Definition: ThreadSupport.h:148
Autolock for acquiring/releasing a write lock on a RWLock.
Definition: ThreadSupport.h:662
int _count
Definition: ThreadSupport.h:530
int debug
Definition: ThreadSupport.h:531
Cond & operator=(const Cond &)
No assignment allowed.
RWLockAttributes()
Definition: ThreadSupport.cc:167
pthread_mutex_t * ptr()
Get the pointer to the pthread_mutex_t.
Definition: ThreadSupport.cc:258
Mutex & operator=(const Mutex &)
No assignment allowed.
void wait()
Suspend calling thread until the semaphore has a non-zero count.
Definition: ThreadSupport.h:459
Mutex * mutexp
Definition: ThreadSupport.h:573
Mutex _mutex
Definition: ThreadSupport.h:351
Mutex(int type=PTHREAD_MUTEX_DEFAULT)
Construct a POSIX mutex of the given type.
Definition: ThreadSupport.cc:212
Definition: Exception.h:35
A C++ wrapper for a POSIX rwlock.
Definition: ThreadSupport.h:358
pthread_cond_t _p_cond
Definition: ThreadSupport.h:349
~RWLockAttributes()
Definition: ThreadSupport.cc:184
RWLock & _rwlock
Definition: ThreadSupport.h:683
A C++ wrapper for a POSIX mutex attributes.
Definition: ThreadSupport.h:51
void rdlock()
Acquire a read lock.
Definition: ThreadSupport.h:385
void lock()
Lock the Mutex.
Definition: ThreadSupport.h:206
Definition: ThreadSupport.h:518
Cond()
Construct a POSIX condition variable, with default attributes.
Definition: ThreadSupport.cc:263
int getPShared() const
Definition: ThreadSupport.cc:205
A POSIX semaphore.
Definition: ThreadSupport.h:434
Autolock for acquiring/releasing a read lock on a RWLock.
Definition: ThreadSupport.h:631
~Synchronized()
On destruction, unlock the lock.
Definition: ThreadSupport.h:566
void setPShared(int val)
Set the mutex pshared attribute, one of PTHREAD_PROCESS_PRIVATE or PTHREAD_PROCESS_SHARED.
Definition: ThreadSupport.cc:189
~Semaphore()
Destructor.
Definition: ThreadSupport.h:450
MutexAttributes _attrs
Definition: ThreadSupport.h:238
Cond _co
Definition: ThreadSupport.h:528
void wait()
Wait on the condition variable.
Definition: ThreadSupport.cc:306
pthread_mutexattr_t _attrs
Definition: ThreadSupport.h:114
RWLock & _rwlock
Definition: ThreadSupport.h:652
Autolock(Mutex &mutex)
Construct the guard object and lock() the lock.
Definition: ThreadSupport.h:604
pthread_mutexattr_t * ptr()
Definition: ThreadSupport.h:109
AutoWrLock(RWLock &rwlock)
Construct the guard object and lock() the lock.
Definition: ThreadSupport.h:669
void setType(int val)
Set the mutex type attribute, one of PTHREAD_MUTEX_NORMAL,PTHREAD_MUTEX_ERRORCHECK,PTHREAD_MUTEX_RECURSIVE, or PTHREAD_MUTEX_DEFAULT.
Definition: ThreadSupport.cc:71
pthread_rwlock_t _p_rwlock
Definition: ThreadSupport.h:425
A wrapper class for a Posix condition variable.
Definition: ThreadSupport.h:245
int getValue()
Get the current value of the Semaphore.
Definition: ThreadSupport.h:491
int getPShared() const
Definition: ThreadSupport.cc:160
sem_t * ptr()
Get the pointer to the semaphore (for legacy C code).
Definition: ThreadSupport.h:501
~AutoRdLock()
On destruction, unlock the lock.
Definition: ThreadSupport.h:646
MutexAttributes()
Create instance with default values, type = PTHREAD_MUTEX_DEFAULT, priority protocol=PTHREAD_PRIO_NON...
Definition: ThreadSupport.cc:42
void unlock()
Unlock the mutex associated with the condition variable.
Definition: ThreadSupport.h:299
A C++ wrapper for a POSIX mutex.
Definition: ThreadSupport.h:154
bool check()
Do a non-blocking wait on the Semaphore.
Definition: ThreadSupport.h:469
pthread_rwlockattr_t * ptr()
Definition: ThreadSupport.h:143
int getType() const
Definition: ThreadSupport.cc:87
void unlock()
Unlock the Mutex.
Definition: ThreadSupport.h:218
Cond * condp
Definition: ThreadSupport.h:574
Autolock is used a simple guard object for critical sections.
Definition: ThreadSupport.h:590