00001 #ifndef __J2K__TimerThread_CPP__
00002 #define __J2K__TimerThread_CPP__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include <j2k/Fred/QNX/TimerThread.hpp>
00017
00018 const int TimerThread::defaultMsg = -1;
00019
00020 TimerThread::TimerThread()
00021 : MsgThread(),
00022 Timer_ID( -1 ),
00023 Proxy_ID( -1 )
00024 { }
00025
00026 TimerThread::~TimerThread() {
00027 stop();
00028 }
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 pid_t TimerThread::start( timespec& period,
00042 int algorithm,
00043 int priority,
00044 size_t stack_size )
00045 {
00046
00047 if ( MsgThread::start( algorithm, priority, stack_size ) < 0 ) {
00048 return -1;
00049 }
00050
00051
00052 Proxy_ID = qnx_proxy_attach( Thread::pid,
00053 (char*)&defaultMsg,
00054 sizeof(&defaultMsg),
00055 priority );
00056
00057 if ( Proxy_ID < 0 ) {
00058 stop();
00059 return -1;
00060 }
00061
00062
00063 struct sigevent evt;
00064 evt.sigev_signo = -Proxy_ID;
00065 Timer_ID = timer_create( CLOCK_REALTIME, &evt );
00066
00067 if ( Timer_ID < 0 ) {
00068 stop();
00069 return -1;
00070 }
00071
00072
00073 itimerspec timer;
00074 timer.it_value = period;
00075 timer.it_interval = period;
00076
00077 if ( timer_settime( timer_ID, 0, &timer, NULL ) < 0 ) {
00078 stop();
00079 return -1;
00080 }
00081
00082 return Thread::pid;
00083 }
00084
00085
00086
00087
00088 int TimerThread::stop()
00089 {
00090
00091 if ( Timer_ID >= 0 ) {
00092 timer_delete( Timer_ID );
00093 timer_ID = -1;
00094 }
00095
00096
00097 if ( Proxy_ID >= 0 ) {
00098 qnx_proxy_detach( Proxy_ID );
00099 Proxy_ID = -1;
00100 }
00101
00102
00103 return MsgThread::stop();
00104 }
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117
00118 int TimerThread::message_filter( int msg, char* data, pid_t pid )
00119 {
00120 if ( pid == Proxy_ID && msg == defaultMsg ) {
00121 tick();
00122 return 1;
00123 }
00124 return 0;
00125 }
00126
00127 int MsgSort( void* msg, void* ptrThis, pid_t rPid, void* p )
00128 {
00129 if ( rPid == Proxy_ID && (*((int*)msg)) == MSG_TIMER_TICK ) {
00130 p = NULL;
00131 Tick();
00132 return MSG_TIMER_TICK;
00133 }
00134 return 0;
00135 }
00136
00137 #endif