timerutil.h

Go to the documentation of this file.
00001 
00024 #ifndef _TIMERUTIL_H
00025 #define _TIMERUTIL_H
00026 
00027 #include <sys/time.h>
00028 
00033 typedef struct TimerUtilObjStruct {
00034     struct timeval original;
00035     struct timeval previous;
00036 } TimerUtilObj;
00037 
00041 typedef TimerUtilObj *TimerUtilHandle;
00042 
00046 enum TimerUtilStatusEnum {
00047     TIMERUTIL_FAILURE = -1,
00048     TIMERUTIL_SUCCESS = 0
00049 };
00050 
00054 typedef enum TimerUtilStatusEnum TimerUtilStatus;
00055 
00062 static inline TimerUtilStatus TimerUtil_reset(TimerUtilHandle hTimer)
00063 {
00064     struct timeval tv;
00065 
00066     if (gettimeofday(&tv, NULL) == -1) {
00067         return TIMERUTIL_FAILURE;
00068     }
00069 
00070     hTimer->original = tv;
00071     hTimer->previous = tv;
00072 
00073     return TIMERUTIL_SUCCESS;
00074 }
00075 
00084 static inline TimerUtilStatus TimerUtil_delta(TimerUtilHandle hTimer,
00085                                               unsigned long *deltaPtr)
00086 {
00087     struct timeval tv;
00088     time_t         s;
00089     suseconds_t    us;
00090 
00091     if (gettimeofday(&tv, NULL) == -1) {
00092         return TIMERUTIL_FAILURE;
00093     }
00094 
00095     s = tv.tv_sec - hTimer->previous.tv_sec;
00096     us = tv.tv_usec - hTimer->previous.tv_usec;
00097 
00098     *deltaPtr = s * 1000000 + us;
00099 
00100     hTimer->previous = tv;
00101 
00102     return TIMERUTIL_SUCCESS;
00103 }
00104 
00113 static inline TimerUtilStatus TimerUtil_total(TimerUtilHandle hTimer,
00114                                               unsigned long *totalPtr)
00115 {
00116     struct timeval tv;
00117     time_t         s;
00118     suseconds_t    us;
00119 
00120     if (gettimeofday(&tv, NULL) == -1) {
00121         return TIMERUTIL_FAILURE;
00122     }
00123 
00124     s = tv.tv_sec - hTimer->original.tv_sec;
00125     us = tv.tv_usec - hTimer->original.tv_usec;
00126 
00127     *totalPtr = s * 1000000 + us;
00128 
00129     return TIMERUTIL_SUCCESS;
00130 }
00131 
00132 #endif // _TIMERUTIL_H

Copyright 2006, Texas Instruments Incorporated