00001 /*! \file timing.c 00002 This file contains functions for time management. 00003 */ 00004 00005 #include "timing.h" 00006 00007 /*! \brief Static variable used by start_chrono() to save the starting timestamp. 00008 */ 00009 static struct timeval start; 00010 00011 /*! \brief Static variable used by stop_chrono() to save the stoping timestamp. 00012 */ 00013 static struct timeval stop; 00014 00015 /*! \brief Start the stopwatch. 00016 \return Upon successful completion, the function returns 0. Otherwise the function returns -1; 00017 */ 00018 int start_chrono() { return (gettimeofday(&start, NULL)); } 00019 00020 /*! \example test_stopwatch.c 00021 This example shows how to use the function start_chrono() 00022 */ 00023 00024 /*! \brief Stop the stopwatch. 00025 \return Upon successful completion, the function returns 0. Otherwise the function returns -1; 00026 */ 00027 int stop_chrono() { return (gettimeofday(&stop, NULL)); } 00028 00029 /*! \example test_stopwatch.c 00030 This example shows how to use the function stop_chrono() 00031 */ 00032 00033 /*! \brief Get the measured time in ms. 00034 \return The function returns the number of mili seceond. 00035 */ 00036 unsigned long int get_ms() 00037 { 00038 long t; 00039 00040 t = (stop.tv_sec*1000 + stop.tv_usec/1000) - (start.tv_sec*1000 + start.tv_usec/1000); 00041 00042 return (unsigned long int)t; 00043 } 00044 00045 /*! \example test_stopwatch.c 00046 This example shows how to use the function get_ms() 00047 */ 00048 00049 /*! \brief This structure is used to specify the timer's delay (for a one shot timer). 00050 */ 00051 static struct itimerval one_shot_timer; 00052 00053 /*! \brief This variable is used by the system call setitimer(). This system call requires a structure itimerval to save 00054 the current timer configuration (it is not used here). 00055 */ 00056 static struct itimerval old_one_shot_timer; 00057 00058 /*! \brief This variable is used by the functions pause_one_shot_timer() and restart_one_shot_timer(). It is used to save the current 00059 timer's state (i.e: the remaining time). 00060 */ 00061 static struct itimerval current_one_shot_timer; 00062 00063 00064 /*! \brief Create a one shot timer which precision is the micro second. The timer raises the signal SIGALRM when the timout 00065 expired. 00066 \param sec Number of second 00067 \param micro_second Number of micro seconds. 00068 \return The function returns one of the following values: 00069 <UL> 00070 <li>SET_ONE_SHOT_TIMER_ERROR: An error occured while creating the timer. 00071 <li>SET_ONE_SHOT_TIMER_OK: The timer is successfuly created. 00072 </UL> 00073 */ 00074 00075 int set_one_shot_timer (long sec, long micro_second) 00076 { 00077 one_shot_timer.it_interval.tv_sec = 0; 00078 one_shot_timer.it_interval.tv_usec = 0; 00079 one_shot_timer.it_value.tv_sec = sec; 00080 one_shot_timer.it_value.tv_usec = micro_second; 00081 if (setitimer (ITIMER_REAL, &one_shot_timer, &old_one_shot_timer) == -1) 00082 { return SET_ONE_SHOT_TIMER_ERROR; } 00083 00084 return SET_ONE_SHOT_TIMER_OK; 00085 } 00086 00087 /*! \example test_one_shot_timer.c 00088 This file shows how to use the function set_one_shot_timer(). 00089 */ 00090 00091 /*! \brief Cancel the previously activated one shot timer. 00092 \return The function returns one of the following values: 00093 <UL> 00094 <li>CANCEL_ONE_SHOT_TIMER_ERROR: An error occured while canceling the timer. 00095 <li>CANCEL_ONE_SHOT_TIMER_OK: The timer is successfuly canceled. 00096 </UL> 00097 */ 00098 00099 int cancel_one_shot_timer () 00100 { 00101 one_shot_timer.it_interval.tv_sec = 0; 00102 one_shot_timer.it_interval.tv_usec = 0; 00103 one_shot_timer.it_value.tv_sec = 0; 00104 one_shot_timer.it_value.tv_usec = 0; 00105 if (setitimer (ITIMER_REAL, &one_shot_timer, &old_one_shot_timer) == -1) 00106 { return CANCEL_ONE_SHOT_TIMER_ERROR; } 00107 00108 return CANCEL_ONE_SHOT_TIMER_OK; 00109 } 00110 00111 /*! \example test_one_shot_timer.c 00112 This file shows how to use the function cancel_one_shot_timer(). 00113 */ 00114 00115 /*! \brief Pause the one shot timer. 00116 \return The function return one of the following values: 00117 <UL> 00118 <li>PAUSE_ONE_SHOT_TIMER_ERROR: An error occured with the high precision timer. 00119 <li>PAUSE_ONE_SHOT_TIMER_OK: The operation was successful. 00120 </UL> 00121 \warning You can restart the timer with restart_one_shot_timer(). 00122 */ 00123 00124 int pause_one_shot_timer () 00125 { 00126 if (getitimer (ITIMER_REAL, ¤t_one_shot_timer) == -1) 00127 { return PAUSE_ONE_SHOT_TIMER_ERROR; } 00128 00129 if (cancel_one_shot_timer() != CANCEL_ONE_SHOT_TIMER_OK) 00130 { return PAUSE_ONE_SHOT_TIMER_ERROR; } 00131 00132 return PAUSE_ONE_SHOT_TIMER_OK; 00133 } 00134 00135 /*! \example test_one_shot_timer.c 00136 This file shows how to use the function pause_one_shot_timer(). 00137 */ 00138 00139 /*! \brief Restart the one shot timer. 00140 \return The function return one of the following values: 00141 <UL> 00142 <li>RESTART_ONE_SHOT_TIMER_ERROR: An error occured with the high precision timer. 00143 <li>RESTART_ONE_SHOT_TIMER_OK: The operation was successful. 00144 </UL> 00145 \warning The timer should have been paused with the function pause_one_shot_timer(). 00146 */ 00147 00148 int restart_one_shot_timer () 00149 { 00150 if (setitimer (ITIMER_REAL, ¤t_one_shot_timer, &old_one_shot_timer) == -1) 00151 { return RESTART_ONE_SHOT_TIMER_ERROR; } 00152 00153 return RESTART_ONE_SHOT_TIMER_OK; 00154 } 00155 00156 /*! \example test_one_shot_timer.c 00157 This file shows how to use the function restart_one_shot_timer(). 00158 */ 00159 00160 00161