1 #ifndef LIBCTB_LINUX_TIMER_H_INCLUDED_ 2 #define LIBCTB_LINUX_TIMER_H_INCLUDED_ 3 4 ///////////////////////////////////////////////////////////////////////////// 5 // Name: linux/timer.h 6 // Purpose: 7 // Author: Joachim Buermann 8 // Id: $Id: timer.h,v 1.1.1.1 2004/11/24 10:30:11 jb Exp $ 9 // Copyright: (c) 2001 Joachim Buermann 10 // Licence: wxWindows licence 11 ///////////////////////////////////////////////////////////////////////////// 12 13 /** 14 \file timer.h 15 */ 16 17 #include <pthread.h> 18 19 namespace ctb { 20 21 /*! 22 \brief A data struct, using from class timer. 23 */ 24 struct timer_control 25 { 26 /*! 27 under linux, we used usec internally 28 */ 29 unsigned int usecs; 30 /*! 31 covers the adress of the exitflag 32 */ 33 int *exitflag; 34 /*! 35 covers the adress of the exit function. NULL, if 36 there was no exit function. 37 */ 38 void* (*exitfnc)(void*); 39 }; 40 41 /*! 42 \class Timer 43 \brief A thread based timer class for handling timeouts in 44 an easier way. 45 46 On starting every timer instance will create it's own thread. 47 The thread makes simply nothing, until it's given time is over. 48 After that, he set a variable, refer by it's adress to one 49 and exit. 50 51 There are a lot of situations, which the timer class must handle. 52 The timer instance leaves his valid range (for example, the 53 timer instance is local inside a function, and the function fished) 54 BEFORE the thread was ending. In this case, the destructor must 55 terminate the thread in a correct way. (This is very different 56 between the OS. threads are a system resource like file descriptors 57 and must be deallocated after using it). 58 59 The thread should be asynchronously stopped. Means, under all 60 circumstance, it must be possible, to finish the timer and start 61 it again. 62 63 Several timer instance can be used simultanously. 64 */ 65 class Timer 66 { 67 protected: 68 /*! 69 control covers the time interval, the adress 70 of the exitflag, and if not null, a function, which will 71 be called on the end. 72 */ 73 timer_control control; 74 /*! 75 stopped will be set by calling the stop() method. 76 Internaly the timer thread steadily tests the state of 77 this variable. If stopped not zero, the thread will be 78 finished. 79 */ 80 int stopped; 81 /*! 82 under linux we use the pthread library. tid covers the 83 identifier for a separate threads. 84 */ 85 pthread_t tid; 86 /*! 87 here we store the time interval, whilst the timer run. 88 This is waste!!! 89 */ 90 unsigned int timer_secs; 91 92 public: 93 /*! 94 The constructor creates an timer object with the given 95 properties. The timer at this moment is not started. This 96 will be done with the start() member function. 97 \param msec time interval after that the the variable 98 pointed by exitflag is setting to one. 99 \param exitflag the adress of an integer, which was set 100 to one after the given time interval. 101 \warning The integer variable shouldn't leave it's valid 102 range, before the timer was finished. So never take a 103 local variable. 104 \param exitfnc A function, which was called after msec. 105 If you don't want this, refer a NULL pointer. 106 */ 107 Timer(unsigned int msec,int* exitflag,void*(*exitfnc)(void*)); 108 /*! 109 the destructor. If his was called (for example by leaving the 110 valid range of the timer object), the timer thread automaticaly 111 will finished. The exitflag wouldn't be set, also the exitfnc 112 wouldn't be called. 113 */ 114 ~Timer(); 115 /*! 116 starts the timer. But now a thread will created and started. 117 After this, the timer thread will be running until he was stopped 118 by calling stop() or reached his given time interval. 119 */ 120 int start(); 121 /*! 122 stops the timer and canceled the timer thread. After timer::stop() a new 123 start() will started the timer from beginning. 124 */ 125 int stop(); 126 }; 127 128 /*! 129 \brief sleepms 130 A plattform independent function, to go to sleep for the given 131 time interval. 132 \param ms time interval in milli seconds 133 */ 134 void sleepms(unsigned int ms); 135 136 } // namespace ctb 137 138 #endif 139 140