| 1 | /* $Id: os2timer.cpp,v 1.3 1999-06-10 16:24:34 phaller Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * OS/2 Timer class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * | 
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 9 | * | 
|---|
| 10 | */ | 
|---|
| 11 | #define INCL_DOSPROCESS | 
|---|
| 12 | #define INCL_DOSDATETIME | 
|---|
| 13 | #define INCL_DOSSEMAPHORES | 
|---|
| 14 | #include <os2.h> | 
|---|
| 15 | #include <process.h> | 
|---|
| 16 | #include "win32type.h" | 
|---|
| 17 | #include "wintimer.h" | 
|---|
| 18 | #include "os2timer.h" | 
|---|
| 19 | #include "misc.h" | 
|---|
| 20 |  | 
|---|
| 21 | static void _Optlink TimerHlpHandler(void *); | 
|---|
| 22 |  | 
|---|
| 23 | /******************************************************************************/ | 
|---|
| 24 | /******************************************************************************/ | 
|---|
| 25 | OS2Timer::OS2Timer() : TimerSem(0), TimerHandle(0), TimerThreadID(0), | 
|---|
| 26 | clientCallback(NULL), TimerStatus(Stopped), fFatal(FALSE) | 
|---|
| 27 | { | 
|---|
| 28 | OS2Timer *timer = OS2Timer::timers; | 
|---|
| 29 |  | 
|---|
| 30 | if(timer != NULL) { | 
|---|
| 31 | while(timer->next != NULL) { | 
|---|
| 32 | timer = timer->next; | 
|---|
| 33 | } | 
|---|
| 34 | timer->next = this; | 
|---|
| 35 | } | 
|---|
| 36 | else  timers      = this; | 
|---|
| 37 |  | 
|---|
| 38 | TimerThreadID = _beginthread(TimerHlpHandler, NULL, 0x4000, (void *)this); | 
|---|
| 39 | DosSleep(100); | 
|---|
| 40 | } | 
|---|
| 41 | /******************************************************************************/ | 
|---|
| 42 | /******************************************************************************/ | 
|---|
| 43 | OS2Timer::~OS2Timer() | 
|---|
| 44 | { | 
|---|
| 45 | OS2Timer *timer = OS2Timer::timers; | 
|---|
| 46 |  | 
|---|
| 47 | KillTimer(); | 
|---|
| 48 |  | 
|---|
| 49 | if(timer != this) { | 
|---|
| 50 | while(timer->next != this) { | 
|---|
| 51 | timer = timer->next; | 
|---|
| 52 | } | 
|---|
| 53 | timer->next = this->next; | 
|---|
| 54 | } | 
|---|
| 55 | else  timers = timer->next; | 
|---|
| 56 | } | 
|---|
| 57 | /******************************************************************************/ | 
|---|
| 58 | /******************************************************************************/ | 
|---|
| 59 | BOOL OS2Timer::StartTimer(int period, int resolution, LPTIMECALLBACK lptc, | 
|---|
| 60 | int dwUser, int fuEvent) | 
|---|
| 61 | { | 
|---|
| 62 | APIRET rc; | 
|---|
| 63 |  | 
|---|
| 64 | if(TimerThreadID == -1) { | 
|---|
| 65 | return(FALSE); | 
|---|
| 66 | } | 
|---|
| 67 | if(TimerStatus == Stopped) { | 
|---|
| 68 | clientCallback = lptc; | 
|---|
| 69 | userData       = dwUser; | 
|---|
| 70 | if(fuEvent == TIME_PERIODIC) | 
|---|
| 71 | rc = DosStartTimer(period, (HSEM)TimerSem, &TimerHandle); | 
|---|
| 72 | else    rc = DosAsyncTimer(period, (HSEM)TimerSem, &TimerHandle); | 
|---|
| 73 | if(rc) { | 
|---|
| 74 | #ifdef DEBUG | 
|---|
| 75 | if(fuEvent == TIME_PERIODIC) | 
|---|
| 76 | WriteLog("DosStartTimer failed %d\n", rc); | 
|---|
| 77 | else    WriteLog("DosAsyncTimer failed %d\n", rc); | 
|---|
| 78 | #endif | 
|---|
| 79 | return(FALSE); | 
|---|
| 80 | } | 
|---|
| 81 | TimerStatus = Running; | 
|---|
| 82 | } | 
|---|
| 83 | else  return(FALSE);  //already running (must use timeKillEvent first) | 
|---|
| 84 | return(TRUE); | 
|---|
| 85 | } | 
|---|
| 86 | /******************************************************************************/ | 
|---|
| 87 | /******************************************************************************/ | 
|---|
| 88 | void OS2Timer::StopTimer() | 
|---|
| 89 | { | 
|---|
| 90 | if(TimerStatus == Running) { | 
|---|
| 91 | DosStopTimer(TimerHandle); | 
|---|
| 92 | TimerStatus = Stopped; | 
|---|
| 93 | } | 
|---|
| 94 | } | 
|---|
| 95 | /******************************************************************************/ | 
|---|
| 96 | /******************************************************************************/ | 
|---|
| 97 | void OS2Timer::KillTimer() | 
|---|
| 98 | { | 
|---|
| 99 | fFatal = TRUE; | 
|---|
| 100 | DosStopTimer(TimerHandle); | 
|---|
| 101 | if(DosPostEventSem(TimerSem)) {//something went wrong | 
|---|
| 102 | DosKillThread(TimerThreadID); | 
|---|
| 103 | DosCloseEventSem(TimerSem); | 
|---|
| 104 | } | 
|---|
| 105 | TimerStatus = InActive; | 
|---|
| 106 | } | 
|---|
| 107 | /******************************************************************************/ | 
|---|
| 108 | //****************************************************************************** | 
|---|
| 109 | void OS2Timer::TimerHandler() | 
|---|
| 110 | { | 
|---|
| 111 | ULONG   Count = 0; | 
|---|
| 112 | APIRET  rc = 0;       /* Return code  */ | 
|---|
| 113 |  | 
|---|
| 114 | #ifdef DEBUG | 
|---|
| 115 | WriteLog("TimerHandler thread created\n"); | 
|---|
| 116 | #endif | 
|---|
| 117 | rc = DosSetPriority (PRTYS_THREAD,        /* Change a single thread */ | 
|---|
| 118 | PRTYC_TIMECRITICAL,  /* Time critical class    */ | 
|---|
| 119 | 0L,                  /* Increase by 15         */ | 
|---|
| 120 | 0L);                 /* Assume current thread  */ | 
|---|
| 121 |  | 
|---|
| 122 | rc = DosCreateEventSem(NULL, &TimerSem, DC_SEM_SHARED, 0); | 
|---|
| 123 |  | 
|---|
| 124 | if(rc != 0) | 
|---|
| 125 | _endthread(); | 
|---|
| 126 |  | 
|---|
| 127 | #ifdef DEBUG | 
|---|
| 128 | WriteLog("Semaphore created\n"); | 
|---|
| 129 | #endif | 
|---|
| 130 | TimerStatus = Stopped; | 
|---|
| 131 |  | 
|---|
| 132 | while(!fFatal) { | 
|---|
| 133 | DosWaitEventSem(TimerSem, SEM_INDEFINITE_WAIT); | 
|---|
| 134 | DosResetEventSem(TimerSem, &Count); | 
|---|
| 135 | if(!fFatal) { | 
|---|
| 136 | #ifdef DEBUG | 
|---|
| 137 | ////        WriteLog("T"); | 
|---|
| 138 | #endif | 
|---|
| 139 | clientCallback((UINT)this, 0, userData, 0, 0); | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 | DosCloseEventSem(TimerSem); | 
|---|
| 143 | } | 
|---|
| 144 | //****************************************************************************** | 
|---|
| 145 | //****************************************************************************** | 
|---|
| 146 | static void _Optlink TimerHlpHandler(void *timer) | 
|---|
| 147 | { | 
|---|
| 148 | ((OS2Timer *)timer)->TimerHandler(); | 
|---|
| 149 |  | 
|---|
| 150 | _endthread(); | 
|---|
| 151 | } | 
|---|
| 152 | //****************************************************************************** | 
|---|
| 153 | //****************************************************************************** | 
|---|
| 154 | OS2Timer *OS2Timer::timers      = NULL; | 
|---|
| 155 | int       OS2Timer::timerPeriod = 0; | 
|---|