00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef AMINO_UTIL_CONDITION
00022 #define AMINO_UTIL_CONDITION
00023
00024 #include <time.h>
00025 #include <pthread.h>
00026 #include <amino/mutex.h>
00027 #include <amino/lock.h>
00028
00029 namespace amino{
00035 class condition_variable{
00036 private:
00037 pthread_cond_t fCond;
00038 public:
00039 typedef pthread_cond_t native_handle_type;
00040
00041 condition_variable(){
00042 pthread_cond_init(&fCond, NULL);
00043 }
00044
00045 ~condition_variable(){
00046 pthread_cond_destroy(&fCond);
00047 }
00048
00049 native_handle_type native_handle(){
00050 return fCond;
00051 }
00052
00064 template<typename mutexT>
00065 void wait(unique_lock<mutexT>& lock){
00066 pthread_cond_wait(&fCond, lock.mutex()->native_handle());
00067 }
00068
00069 void notify_one(){
00070 pthread_cond_signal(&fCond);
00071 }
00072
00073 void notify_all(){
00074 pthread_cond_broadcast(&fCond);
00075 }
00076
00093 template<typename mutexT>
00094 bool timed_wait(unique_lock<mutexT>&lock, long milli){
00095 struct timespec timeout;
00096 clock_gettime(CLOCK_REALTIME, &timeout);
00097 timeout.tv_sec += milli/1000;
00098 timeout.tv_nsec += (milli%1000)*1000000;
00099 return 0==pthread_cond_timedwait(&fCond,
00100 lock.mutex()->native_handle(), &timeout);
00101 }
00102 };
00103 }
00104
00105 #endif