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