| 1 | /* $Id: os2timer.cpp,v 1.7 1999-08-24 21:21:11 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 |
|
|---|
| 12 |
|
|---|
| 13 | /****************************************************************************
|
|---|
| 14 | * Includes *
|
|---|
| 15 | ****************************************************************************/
|
|---|
| 16 |
|
|---|
| 17 | #define INCL_DOSPROCESS
|
|---|
| 18 | #define INCL_DOSDATETIME
|
|---|
| 19 | #define INCL_DOSSEMAPHORES
|
|---|
| 20 | #include <os2wrap.h> //Odin32 OS/2 api wrappers
|
|---|
| 21 | #include <process.h>
|
|---|
| 22 | #include "win32type.h"
|
|---|
| 23 | #include "wintimer.h"
|
|---|
| 24 | #include <wprocess.h>
|
|---|
| 25 | #include "os2timer.h"
|
|---|
| 26 | #include "misc.h"
|
|---|
| 27 |
|
|---|
| 28 |
|
|---|
| 29 |
|
|---|
| 30 | /****************************************************************************
|
|---|
| 31 | * Structures *
|
|---|
| 32 | ****************************************************************************/
|
|---|
| 33 |
|
|---|
| 34 | #if 0
|
|---|
| 35 | //@@@PH started new implementation
|
|---|
| 36 | typedef struct _MMTIMEREVENT
|
|---|
| 37 | {
|
|---|
| 38 | struct _MMTIMEREVENT* prev;
|
|---|
| 39 | struct _MMTIMEREVENT* next;
|
|---|
| 40 |
|
|---|
| 41 | DWORD id; // event id
|
|---|
| 42 | DWORD timeScheduled; // system time to fire event
|
|---|
| 43 | DWORD timePeriod; // period if periodic event
|
|---|
| 44 | TID tidCaller; // thread ID of caller thread
|
|---|
| 45 | DWORD dwUser; // user supplied value
|
|---|
| 46 | LPTIMERCALLBACK lpCallback; // address to call
|
|---|
| 47 | DWORD dwFlags; // event flags
|
|---|
| 48 | } MMTIMEREVENT, *PMMTIMEREVENT, *LPTIMEREVENT;
|
|---|
| 49 |
|
|---|
| 50 | typedef struct _MMTIMERRESOLUTION
|
|---|
| 51 | {
|
|---|
| 52 | struct _MMTIMERRESOLUTION* prev;
|
|---|
| 53 | struct _MMTIMERRESOLUTION* next;
|
|---|
| 54 |
|
|---|
| 55 | DWORD dwResolution; // requested resolution for block
|
|---|
| 56 | } MMTIMERRESOLUTION, *PMMTIMERRESOLUTION, *LPMMTIMERRESOLUTION;
|
|---|
| 57 |
|
|---|
| 58 | /*
|
|---|
| 59 | enterResolutionScope
|
|---|
| 60 | leaveResolutionScope
|
|---|
| 61 |
|
|---|
| 62 | addEvent
|
|---|
| 63 | removeEvent
|
|---|
| 64 | rescheduleEvent
|
|---|
| 65 | callbackCaller
|
|---|
| 66 | */
|
|---|
| 67 | #endif
|
|---|
| 68 |
|
|---|
| 69 | /****************************************************************************
|
|---|
| 70 | * Local Prototypes *
|
|---|
| 71 | ****************************************************************************/
|
|---|
| 72 |
|
|---|
| 73 | static void _Optlink TimerHlpHandler(void *);
|
|---|
| 74 |
|
|---|
| 75 |
|
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 | /******************************************************************************/
|
|---|
| 79 | /******************************************************************************/
|
|---|
| 80 | OS2Timer::OS2Timer() : TimerSem(0), TimerHandle(0), TimerThreadID(0),
|
|---|
| 81 | clientCallback(NULL), TimerStatus(Stopped), fFatal(FALSE)
|
|---|
| 82 | {
|
|---|
| 83 | OS2Timer *timer = OS2Timer::timers;
|
|---|
| 84 |
|
|---|
| 85 | if(timer != NULL) {
|
|---|
| 86 | while(timer->next != NULL) {
|
|---|
| 87 | timer = timer->next;
|
|---|
| 88 | }
|
|---|
| 89 | timer->next = this;
|
|---|
| 90 | }
|
|---|
| 91 | else timers = this;
|
|---|
| 92 |
|
|---|
| 93 | TimerThreadID = _beginthread(TimerHlpHandler, NULL, 0x4000, (void *)this);
|
|---|
| 94 | DosSleep(100);
|
|---|
| 95 | }
|
|---|
| 96 | /******************************************************************************/
|
|---|
| 97 | /******************************************************************************/
|
|---|
| 98 | OS2Timer::~OS2Timer()
|
|---|
| 99 | {
|
|---|
| 100 | OS2Timer *timer = OS2Timer::timers;
|
|---|
| 101 |
|
|---|
| 102 | KillTimer();
|
|---|
| 103 |
|
|---|
| 104 | if(timer != this) {
|
|---|
| 105 | while(timer->next != this) {
|
|---|
| 106 | timer = timer->next;
|
|---|
| 107 | }
|
|---|
| 108 | timer->next = this->next;
|
|---|
| 109 | }
|
|---|
| 110 | else timers = timer->next;
|
|---|
| 111 | }
|
|---|
| 112 | /******************************************************************************/
|
|---|
| 113 | /******************************************************************************/
|
|---|
| 114 | BOOL OS2Timer::StartTimer(int period, int resolution, LPTIMECALLBACK lptc,
|
|---|
| 115 | int dwUser, int fuEvent)
|
|---|
| 116 | {
|
|---|
| 117 | APIRET rc;
|
|---|
| 118 |
|
|---|
| 119 | if(TimerThreadID == -1) {
|
|---|
| 120 | return(FALSE);
|
|---|
| 121 | }
|
|---|
| 122 | if(TimerStatus == Stopped) {
|
|---|
| 123 | clientCallback = lptc;
|
|---|
| 124 | userData = dwUser;
|
|---|
| 125 | if(fuEvent == TIME_PERIODIC)
|
|---|
| 126 | rc = DosStartTimer(period, (HSEM)TimerSem, &TimerHandle);
|
|---|
| 127 | else rc = DosAsyncTimer(period, (HSEM)TimerSem, &TimerHandle);
|
|---|
| 128 | if(rc) {
|
|---|
| 129 | #ifdef DEBUG
|
|---|
| 130 | if(fuEvent == TIME_PERIODIC)
|
|---|
| 131 | WriteLog("DosStartTimer failed %d\n", rc);
|
|---|
| 132 | else WriteLog("DosAsyncTimer failed %d\n", rc);
|
|---|
| 133 | #endif
|
|---|
| 134 | return(FALSE);
|
|---|
| 135 | }
|
|---|
| 136 | TimerStatus = Running;
|
|---|
| 137 | }
|
|---|
| 138 | else return(FALSE); //already running (must use timeKillEvent first)
|
|---|
| 139 | return(TRUE);
|
|---|
| 140 | }
|
|---|
| 141 | /******************************************************************************/
|
|---|
| 142 | /******************************************************************************/
|
|---|
| 143 | void OS2Timer::StopTimer()
|
|---|
| 144 | {
|
|---|
| 145 | if(TimerStatus == Running) {
|
|---|
| 146 | DosStopTimer(TimerHandle);
|
|---|
| 147 | TimerStatus = Stopped;
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | /******************************************************************************/
|
|---|
| 151 | /******************************************************************************/
|
|---|
| 152 | void OS2Timer::KillTimer()
|
|---|
| 153 | {
|
|---|
| 154 | fFatal = TRUE;
|
|---|
| 155 | DosStopTimer(TimerHandle);
|
|---|
| 156 | if(DosPostEventSem(TimerSem)) {//something went wrong
|
|---|
| 157 | DosKillThread(TimerThreadID);
|
|---|
| 158 | DosCloseEventSem(TimerSem);
|
|---|
| 159 | }
|
|---|
| 160 | TimerStatus = InActive;
|
|---|
| 161 | }
|
|---|
| 162 | /******************************************************************************/
|
|---|
| 163 | //******************************************************************************
|
|---|
| 164 | void OS2Timer::TimerHandler()
|
|---|
| 165 | {
|
|---|
| 166 | ULONG Count = 0;
|
|---|
| 167 | APIRET rc = 0; /* Return code */
|
|---|
| 168 | USHORT selTIB;
|
|---|
| 169 |
|
|---|
| 170 | dprintf(("WINMM: TimerHandler thread created\n"));
|
|---|
| 171 |
|
|---|
| 172 | rc = DosSetPriority (PRTYS_THREAD, /* Change a single thread */
|
|---|
| 173 | PRTYC_TIMECRITICAL, /* Time critical class */
|
|---|
| 174 | 0L, /* Increase by 15 */
|
|---|
| 175 | 0L); /* Assume current thread */
|
|---|
| 176 |
|
|---|
| 177 | rc = DosCreateEventSem(NULL, &TimerSem, DC_SEM_SHARED, 0);
|
|---|
| 178 |
|
|---|
| 179 | if(rc != 0)
|
|---|
| 180 | _endthread();
|
|---|
| 181 |
|
|---|
| 182 | dprintf(("WINMM: OS2Timer:Semaphore created\n"));
|
|---|
| 183 |
|
|---|
| 184 | TimerStatus = Stopped;
|
|---|
| 185 |
|
|---|
| 186 | while(!fFatal)
|
|---|
| 187 | {
|
|---|
| 188 | DosWaitEventSem(TimerSem, SEM_INDEFINITE_WAIT);
|
|---|
| 189 | DosResetEventSem(TimerSem, &Count);
|
|---|
| 190 | if(!fFatal)
|
|---|
| 191 | {
|
|---|
| 192 | // @@@PH: we're calling the client with PRTYC_TIMECRITICAL !!!
|
|---|
| 193 | // It'd be much nicer to call with original priority!
|
|---|
| 194 | // @@@PH: plus the original thread is supposed to stop while the
|
|---|
| 195 | // time event is scheduled (DosSuspendThread()) ? It's
|
|---|
| 196 | // much like raising a signal (SIGALARM)
|
|---|
| 197 |
|
|---|
| 198 | selTIB = SetWin32TIB();
|
|---|
| 199 | clientCallback((UINT)this, 0, userData, 0, 0);
|
|---|
| 200 | SetFS(selTIB);
|
|---|
| 201 | }
|
|---|
| 202 | }
|
|---|
| 203 | DosCloseEventSem(TimerSem);
|
|---|
| 204 | }
|
|---|
| 205 | //******************************************************************************
|
|---|
| 206 | //******************************************************************************
|
|---|
| 207 | static void _Optlink TimerHlpHandler(void *timer)
|
|---|
| 208 | {
|
|---|
| 209 | ((OS2Timer *)timer)->TimerHandler();
|
|---|
| 210 |
|
|---|
| 211 | _endthread();
|
|---|
| 212 | }
|
|---|
| 213 | //******************************************************************************
|
|---|
| 214 | //******************************************************************************
|
|---|
| 215 | OS2Timer *OS2Timer::timers = NULL;
|
|---|
| 216 | int OS2Timer::timerPeriod = 0;
|
|---|
| 217 |
|
|---|