00001 #ifndef _sys_posix_Condition_h
00002 #define _sys_posix_Condition_h
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "PrivatePosix.h"
00026
00027 #include "qpid/sys/Mutex.h"
00028 #include "qpid/sys/Time.h"
00029
00030 #include <time.h>
00031 #include <sys/errno.h>
00032 #include <boost/noncopyable.hpp>
00033
00034 namespace qpid {
00035 namespace sys {
00036
00040 class Condition
00041 {
00042 public:
00043 inline Condition();
00044 inline ~Condition();
00045 inline void wait(Mutex&);
00046 inline bool wait(Mutex&, const AbsTime& absoluteTime);
00047 inline void notify();
00048 inline void notifyAll();
00049
00050 private:
00051 pthread_cond_t condition;
00052 };
00053
00054 Condition::Condition() {
00055 QPID_POSIX_ASSERT_THROW_IF(pthread_cond_init(&condition, 0));
00056 }
00057
00058 Condition::~Condition() {
00059 QPID_POSIX_ASSERT_THROW_IF(pthread_cond_destroy(&condition));
00060 }
00061
00062 void Condition::wait(Mutex& mutex) {
00063 QPID_POSIX_ASSERT_THROW_IF(pthread_cond_wait(&condition, &mutex.mutex));
00064 }
00065
00066 bool Condition::wait(Mutex& mutex, const AbsTime& absoluteTime){
00067 struct timespec ts;
00068 toTimespec(ts, Duration(absoluteTime));
00069 int status = pthread_cond_timedwait(&condition, &mutex.mutex, &ts);
00070 if (status != 0) {
00071 if (status == ETIMEDOUT) return false;
00072 throw QPID_POSIX_ERROR(status);
00073 }
00074 return true;
00075 }
00076
00077 void Condition::notify(){
00078 QPID_POSIX_ASSERT_THROW_IF(pthread_cond_signal(&condition));
00079 }
00080
00081 void Condition::notifyAll(){
00082 QPID_POSIX_ASSERT_THROW_IF(pthread_cond_broadcast(&condition));
00083 }
00084
00085 }}
00086 #endif