00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include "condition.h"
00019
00020 #if USE_PRIVATE__PTHREADS
00021 #if TIME_WITH_SYS_TIME
00022 # include <sys/time.h>
00023 # include <time.h>
00024 #else
00025 # if HAVE_SYS_TIME_H
00026 # include <sys/time.h>
00027 # else
00028 # include <time.h>
00029 # endif
00030 #endif
00031 #else
00032 # include <glib/gmain.h>
00033 # include <glib/gtimer.h>
00034 #endif
00035
00036
00037 inline int WaitCondition::wait(ConditionMutex &m,long msec)
00038 {
00039 if(msec<0) msec=0;
00040
00041 #if USE_PRIVATE__PTHREADS
00042
00043 timeval tv;
00044 gettimeofday(&tv,NULL);
00045 tv.tv_sec+=msec/1000;
00046 tv.tv_usec+=(msec%1000)*1000;
00047 if(tv.tv_usec>1000000)
00048 { tv.tv_usec-=1000000; ++tv.tv_sec; }
00049
00050 timespec tspec;
00051 tspec.tv_sec=tv.tv_sec;
00052 tspec.tv_nsec=1000*tv.tv_usec;
00053
00054
00055 switch(pthread_cond_timedwait(&c,&m.m,&tspec))
00056 {
00057 case ETIMEDOUT: return(1);
00058 case EINTR: return(2);
00059 }
00060 return(0);
00061 #else
00062 GTimeVal tv;
00063 g_get_current_time(&tv);
00064 g_time_val_add(&tv,1000*msec);
00065 if(g_cond_timed_wait(c,m.m,&tv))
00066 { return(0); }
00067 return(1);
00068 #endif
00069 }
00070
00071
00072 WaitCondition::WaitCondition()
00073 {
00074 #if USE_PRIVATE__PTHREADS
00075
00076
00077 pthread_cond_init(&c,NULL);
00078
00079 #else
00080
00081 c=g_cond_new();
00082 #endif
00083 }
00084
00085 WaitCondition::~WaitCondition()
00086 {
00087 #if USE_PRIVATE__PTHREADS
00088 pthread_cond_destroy(&c);
00089 #else
00090
00091 g_cond_free(c);
00092 #endif
00093 }