00001 /* 00002 * Copyright (C) 2009, Edmundo Albuquerque de Souza e Silva. 00003 * 00004 * This file may be distributed under the terms of the Q Public License 00005 * as defined by Trolltech AS of Norway and appearing in the file 00006 * LICENSE.QPL included in the packaging of this file. 00007 * 00008 * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING 00009 * THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR 00010 * PURPOSE. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, 00011 * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING 00012 * FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, 00013 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION 00014 * WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 00015 * 00016 * Thanks: Jose Renato Santos 00017 * 00018 */ 00019 00020 /////////////////////////////////////////////////////////////////////////////// 00021 // timer.h : Defines class CTimer, which 00022 // implements timing functions 00023 /////////////////////////////////////////////////////////////////////////////// 00024 00025 #ifndef __CTIMER_H_ 00026 #define __CTIMER_H_ 00027 00028 #include "typesfixed.h" 00029 #include "vsimutex.h" 00030 #include "vsithread.h" 00031 00032 #include <sys/time.h> 00033 00034 typedef enum 00035 { 00036 TimerTypeSingle, 00037 TimerTypePeriodic 00038 }TimerType; 00039 00040 class CTimer 00041 { 00042 public: 00043 typedef void (*callback_t)(void *callbackparm); 00044 00045 private: 00046 typedef enum 00047 { 00048 TimerStatusFree = 0, 00049 TimerStatusInactive = 1, 00050 TimerStatusActive = 2 00051 } TimerStatus; 00052 00053 struct TimerEntry 00054 { 00055 TimerStatus Status; 00056 struct timeval Time; 00057 struct timeval Interval; 00058 TimerType Type; 00059 callback_t Callback; 00060 void* Param; 00061 int Next; 00062 int Previous; 00063 }; 00064 TimerEntry* m_Table; 00065 int m_Free; 00066 int m_First; 00067 int m_nTimers; 00068 00069 struct timeval m_StartTime; 00070 00071 bool m_Initialized; 00072 bool m_StopThread; 00073 00074 CvsiMutex m_Mutex; 00075 CvsiThread m_Thread; 00076 00077 static void* Thread( void* param ); 00078 void TimerThread(); 00079 00080 void InitTimerSignalAction( void ); 00081 void ProgramTimer( struct timeval Time ); 00082 void Remove( int TimerId ); 00083 void Insert( int TimerId ); 00084 00085 void ProcessExpiredTimers(); 00086 void ReprogramTimer(); 00087 00088 public: 00089 void Handler( void ); 00090 CTimer(); 00091 ~CTimer(); 00092 int Initialize ( const int nTimers ); 00093 int Stop (); 00094 int GetTimer( callback_t callback ); 00095 int FreeTimer( const int TimerId ); 00096 int StartTimer( const int TimerId, struct timeval Interval, 00097 TimerType Type, void* Param ); 00098 int StopTimer( const int TimerId ); 00099 struct timeval CurrentTime(); 00100 }; 00101 00102 #endif // __CTIMER_H_