1 | /* $Id: os2timer.cpp,v 1.5 1999-08-19 18:46:05 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 "os2timer.h"
|
---|
25 | #include "misc.h"
|
---|
26 |
|
---|
27 |
|
---|
28 | static void _Optlink TimerHlpHandler(void *);
|
---|
29 |
|
---|
30 |
|
---|
31 | /******************************************************************************/
|
---|
32 | /******************************************************************************/
|
---|
33 | OS2Timer::OS2Timer() : TimerSem(0), TimerHandle(0), TimerThreadID(0),
|
---|
34 | clientCallback(NULL), TimerStatus(Stopped), fFatal(FALSE)
|
---|
35 | {
|
---|
36 | OS2Timer *timer = OS2Timer::timers;
|
---|
37 |
|
---|
38 | if(timer != NULL) {
|
---|
39 | while(timer->next != NULL) {
|
---|
40 | timer = timer->next;
|
---|
41 | }
|
---|
42 | timer->next = this;
|
---|
43 | }
|
---|
44 | else timers = this;
|
---|
45 |
|
---|
46 | TimerThreadID = _beginthread(TimerHlpHandler, NULL, 0x4000, (void *)this);
|
---|
47 | DosSleep(100);
|
---|
48 | }
|
---|
49 | /******************************************************************************/
|
---|
50 | /******************************************************************************/
|
---|
51 | OS2Timer::~OS2Timer()
|
---|
52 | {
|
---|
53 | OS2Timer *timer = OS2Timer::timers;
|
---|
54 |
|
---|
55 | KillTimer();
|
---|
56 |
|
---|
57 | if(timer != this) {
|
---|
58 | while(timer->next != this) {
|
---|
59 | timer = timer->next;
|
---|
60 | }
|
---|
61 | timer->next = this->next;
|
---|
62 | }
|
---|
63 | else timers = timer->next;
|
---|
64 | }
|
---|
65 | /******************************************************************************/
|
---|
66 | /******************************************************************************/
|
---|
67 | BOOL OS2Timer::StartTimer(int period, int resolution, LPTIMECALLBACK lptc,
|
---|
68 | int dwUser, int fuEvent)
|
---|
69 | {
|
---|
70 | APIRET rc;
|
---|
71 |
|
---|
72 | if(TimerThreadID == -1) {
|
---|
73 | return(FALSE);
|
---|
74 | }
|
---|
75 | if(TimerStatus == Stopped) {
|
---|
76 | clientCallback = lptc;
|
---|
77 | userData = dwUser;
|
---|
78 | if(fuEvent == TIME_PERIODIC)
|
---|
79 | rc = DosStartTimer(period, (HSEM)TimerSem, &TimerHandle);
|
---|
80 | else rc = DosAsyncTimer(period, (HSEM)TimerSem, &TimerHandle);
|
---|
81 | if(rc) {
|
---|
82 | #ifdef DEBUG
|
---|
83 | if(fuEvent == TIME_PERIODIC)
|
---|
84 | WriteLog("DosStartTimer failed %d\n", rc);
|
---|
85 | else WriteLog("DosAsyncTimer failed %d\n", rc);
|
---|
86 | #endif
|
---|
87 | return(FALSE);
|
---|
88 | }
|
---|
89 | TimerStatus = Running;
|
---|
90 | }
|
---|
91 | else return(FALSE); //already running (must use timeKillEvent first)
|
---|
92 | return(TRUE);
|
---|
93 | }
|
---|
94 | /******************************************************************************/
|
---|
95 | /******************************************************************************/
|
---|
96 | void OS2Timer::StopTimer()
|
---|
97 | {
|
---|
98 | if(TimerStatus == Running) {
|
---|
99 | DosStopTimer(TimerHandle);
|
---|
100 | TimerStatus = Stopped;
|
---|
101 | }
|
---|
102 | }
|
---|
103 | /******************************************************************************/
|
---|
104 | /******************************************************************************/
|
---|
105 | void OS2Timer::KillTimer()
|
---|
106 | {
|
---|
107 | fFatal = TRUE;
|
---|
108 | DosStopTimer(TimerHandle);
|
---|
109 | if(DosPostEventSem(TimerSem)) {//something went wrong
|
---|
110 | DosKillThread(TimerThreadID);
|
---|
111 | DosCloseEventSem(TimerSem);
|
---|
112 | }
|
---|
113 | TimerStatus = InActive;
|
---|
114 | }
|
---|
115 | /******************************************************************************/
|
---|
116 | //******************************************************************************
|
---|
117 | void OS2Timer::TimerHandler()
|
---|
118 | {
|
---|
119 | ULONG Count = 0;
|
---|
120 | APIRET rc = 0; /* Return code */
|
---|
121 |
|
---|
122 | #ifdef DEBUG
|
---|
123 | WriteLog("TimerHandler thread created\n");
|
---|
124 | #endif
|
---|
125 | rc = DosSetPriority (PRTYS_THREAD, /* Change a single thread */
|
---|
126 | PRTYC_TIMECRITICAL, /* Time critical class */
|
---|
127 | 0L, /* Increase by 15 */
|
---|
128 | 0L); /* Assume current thread */
|
---|
129 |
|
---|
130 | rc = DosCreateEventSem(NULL, &TimerSem, DC_SEM_SHARED, 0);
|
---|
131 |
|
---|
132 | if(rc != 0)
|
---|
133 | _endthread();
|
---|
134 |
|
---|
135 | #ifdef DEBUG
|
---|
136 | WriteLog("Semaphore created\n");
|
---|
137 | #endif
|
---|
138 | TimerStatus = Stopped;
|
---|
139 |
|
---|
140 | while(!fFatal) {
|
---|
141 | DosWaitEventSem(TimerSem, SEM_INDEFINITE_WAIT);
|
---|
142 | DosResetEventSem(TimerSem, &Count);
|
---|
143 | if(!fFatal) {
|
---|
144 | #ifdef DEBUG
|
---|
145 | //// WriteLog("T");
|
---|
146 | #endif
|
---|
147 | clientCallback((UINT)this, 0, userData, 0, 0);
|
---|
148 | }
|
---|
149 | }
|
---|
150 | DosCloseEventSem(TimerSem);
|
---|
151 | }
|
---|
152 | //******************************************************************************
|
---|
153 | //******************************************************************************
|
---|
154 | static void _Optlink TimerHlpHandler(void *timer)
|
---|
155 | {
|
---|
156 | ((OS2Timer *)timer)->TimerHandler();
|
---|
157 |
|
---|
158 | _endthread();
|
---|
159 | }
|
---|
160 | //******************************************************************************
|
---|
161 | //******************************************************************************
|
---|
162 | OS2Timer *OS2Timer::timers = NULL;
|
---|
163 | int OS2Timer::timerPeriod = 0;
|
---|
164 |
|
---|