ARPDAS_QNX6 1.0
|
00001 #include <string.h> 00002 #include <errno.h> 00003 #include "Timeout.h" 00004 #include "nortlib.h" 00005 00006 Timeout::Timeout() { 00007 Clear(); 00008 } 00009 00010 /** 00011 * Specifies a desired timeout relative to the current time. The event loop 00012 * will subtract the then-current time to determine the correct relative 00013 * timeout value. 00014 * @param secs Seconds 00015 * @param msecs Milleseconds 00016 */ 00017 void Timeout::Set( time_t secs, long msecs ) { 00018 int whole_secs; 00019 int rv = clock_gettime(CLOCK_REALTIME, &when); 00020 if ( rv == -1 ) nl_error(3, "Error from clock_gettime(); '%s'", strerror(errno) ); 00021 when.tv_nsec += msecs*1000000L; 00022 when.tv_sec += secs; 00023 whole_secs = when.tv_nsec/1000000000L; 00024 when.tv_sec += whole_secs; 00025 when.tv_nsec -= whole_secs*1000000000L; 00026 } 00027 00028 /** 00029 * Clears the timeout, indicating no timeout required. 00030 */ 00031 void Timeout::Clear() { 00032 when.tv_sec = 0L; 00033 when.tv_nsec = 0L; 00034 } 00035 00036 /** 00037 * @return true if a timeout value is set. 00038 */ 00039 bool Timeout::Set() { 00040 return (when.tv_sec != 0 || when.tv_nsec != 0); 00041 } 00042 00043 /** 00044 * @return true if a timeout value has been set and has expired. 00045 */ 00046 bool Timeout::Expired() { 00047 struct timespec now; 00048 int rv; 00049 if (!Set()) return false; 00050 rv = clock_gettime(CLOCK_REALTIME, &now); 00051 return (now.tv_sec > when.tv_sec || 00052 ((now.tv_sec == when.tv_sec) && 00053 (now.tv_nsec > when.tv_nsec))); 00054 } 00055 00056 TimeoutAccumulator::TimeoutAccumulator() { 00057 when.tv_sec = 0L; 00058 when.tv_nsec = 0L; 00059 how_soon.tv_sec = 0L; 00060 how_soon.tv_usec = 0L; 00061 } 00062 00063 void TimeoutAccumulator::Set( Timeout *to ) { 00064 if ( to == NULL ) { 00065 when.tv_sec = 0L; 00066 when.tv_nsec = 0L; 00067 how_soon.tv_sec = 0L; 00068 how_soon.tv_usec = 0L; 00069 } else { 00070 when = to->when; 00071 } 00072 } 00073 00074 void TimeoutAccumulator::Set_Min( Timeout *to ) { 00075 if ( to != NULL ) { 00076 if ( ( when.tv_sec == 0 && when.tv_nsec == 0 ) || 00077 ( (to->when.tv_sec != 0 || to->when.tv_nsec != 0) && 00078 ( ( to->when.tv_sec < when.tv_sec ) || 00079 ( to->when.tv_sec == when.tv_sec && to->when.tv_nsec < when.tv_nsec ) ) ) ) { 00080 Set(to); 00081 } 00082 } 00083 } 00084 00085 /** 00086 * \returns struct timeval pointer appropriate for select(). 00087 * If no timeout is specified, returns NULL. If the timeout has passed, 00088 * timeout is set at zero. 00089 */ 00090 struct timeval *TimeoutAccumulator::timeout_val() { 00091 struct timespec now; 00092 clock_gettime(CLOCK_REALTIME, &now); 00093 if ( when.tv_sec == 0 && when.tv_nsec == 0 ) { 00094 return NULL; 00095 } else if ( ( now.tv_sec > when.tv_sec ) || 00096 ( now.tv_sec == when.tv_sec && now.tv_nsec > when.tv_nsec ) ) { 00097 how_soon.tv_sec = 0L; 00098 how_soon.tv_usec = 0L; 00099 } else { 00100 how_soon.tv_usec = (when.tv_nsec - now.tv_nsec)/1000; 00101 how_soon.tv_sec = (when.tv_sec - now.tv_sec); 00102 if (how_soon.tv_usec < 0) { 00103 --how_soon.tv_sec; 00104 how_soon.tv_usec += 1000000L; 00105 } 00106 } 00107 return &how_soon; 00108 }