Mercurial > sdl-ios-xcode
annotate src/timer/os2/SDL_systimer.c @ 1358:c71e05b4dc2e
More header massaging... works great on Windows. ;-)
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Fri, 10 Feb 2006 06:48:43 +0000 |
parents | c9b51268668f |
children | 19418e4422cb |
rev | line source |
---|---|
1190 | 1 /* |
2 SDL - Simple DirectMedia Layer | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
3 Copyright (C) 1997-2006 Sam Lantinga |
1190 | 4 |
5 This library is free software; you can redistribute it and/or | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
6 modify it under the terms of the GNU Lesser General Public |
1190 | 7 License as published by the Free Software Foundation; either |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
8 version 2.1 of the License, or (at your option) any later version. |
1190 | 9 |
10 This library is distributed in the hope that it will be useful, | |
11 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
13 Lesser General Public License for more details. |
1190 | 14 |
1312
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
15 You should have received a copy of the GNU Lesser General Public |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
16 License along with this library; if not, write to the Free Software |
c9b51268668f
Updated copyright information and removed rcs id lines (problematic in branch merges)
Sam Lantinga <slouken@libsdl.org>
parents:
1190
diff
changeset
|
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
1190 | 18 |
19 Sam Lantinga | |
20 slouken@libsdl.org | |
21 */ | |
22 | |
23 #define INCL_DOSMISC | |
24 #define INCL_DOSERRORS | |
25 #define INCL_DOSSEMAPHORES | |
26 #define INCL_DOSDATETIME | |
27 #define INCL_DOSPROCESS | |
28 #define INCL_DOSPROFILE | |
29 #define INCL_DOSEXCEPTIONS | |
30 #include <os2.h> | |
31 | |
32 #include "SDL_thread.h" | |
33 #include "SDL_timer.h" | |
34 #include "SDL_timer_c.h" | |
35 | |
36 | |
37 #define TIME_WRAP_VALUE (~(DWORD)0) | |
38 | |
39 /* The first high-resolution ticks value of the application */ | |
40 static long long hires_start_ticks; | |
41 /* The number of ticks per second of the high-resolution performance counter */ | |
42 static ULONG hires_ticks_per_second; | |
43 | |
44 void SDL_StartTicks(void) | |
45 { | |
46 DosTmrQueryFreq(&hires_ticks_per_second); | |
47 DosTmrQueryTime((PQWORD)&hires_start_ticks); | |
48 } | |
49 | |
50 DECLSPEC Uint32 SDLCALL SDL_GetTicks(void) | |
51 { | |
52 long long hires_now; | |
53 ULONG ticks = ticks; | |
54 | |
55 DosTmrQueryTime((PQWORD)&hires_now); | |
56 /* | |
57 hires_now -= hires_start_ticks; | |
58 hires_now *= 1000; | |
59 hires_now /= hires_ticks_per_second; | |
60 */ | |
61 /* inline asm to avoid runtime inclusion */ | |
62 _asm { | |
63 push edx | |
64 push eax | |
65 mov eax, dword ptr hires_now | |
66 mov edx, dword ptr hires_now+4 | |
67 sub eax, dword ptr hires_start_ticks | |
68 sbb edx, dword ptr hires_start_ticks+4 | |
69 mov ebx,1000 | |
70 mov ecx,edx | |
71 mul ebx | |
72 push eax | |
73 push edx | |
74 mov eax,ecx | |
75 mul ebx | |
76 pop eax | |
77 add edx,eax | |
78 pop eax | |
79 mov ebx, dword ptr hires_ticks_per_second | |
80 div ebx | |
81 mov dword ptr ticks, eax | |
82 pop edx | |
83 pop eax | |
84 } | |
85 | |
86 return ticks; | |
87 | |
88 } | |
89 | |
90 /* High resolution sleep, originally made by Ilya Zakharevich */ | |
91 DECLSPEC void SDLCALL SDL_Delay(Uint32 ms) | |
92 { | |
93 /* This is similar to DosSleep(), but has 8ms granularity in time-critical | |
94 threads even on Warp3. */ | |
95 HEV hevEvent1 = 0; /* Event semaphore handle */ | |
96 HTIMER htimerEvent1 = 0; /* Timer handle */ | |
97 APIRET rc = NO_ERROR; /* Return code */ | |
98 int ret = 1; | |
99 ULONG priority = 0, nesting; /* Shut down the warnings */ | |
100 PPIB pib; | |
101 PTIB tib; | |
102 char *e = NULL; | |
103 APIRET badrc; | |
104 int switch_priority = 50; | |
105 | |
106 DosCreateEventSem(NULL, /* Unnamed */ | |
107 &hevEvent1, /* Handle of semaphore returned */ | |
108 DC_SEM_SHARED, /* Shared needed for DosAsyncTimer */ | |
109 FALSE); /* Semaphore is in RESET state */ | |
110 | |
111 if (ms >= switch_priority) | |
112 switch_priority = 0; | |
113 if (switch_priority) | |
114 { | |
115 if (DosGetInfoBlocks(&tib, &pib)!=NO_ERROR) | |
116 switch_priority = 0; | |
117 else | |
118 { | |
119 /* In Warp3, to switch scheduling to 8ms step, one needs to do | |
120 DosAsyncTimer() in time-critical thread. On laters versions, | |
121 more and more cases of wait-for-something are covered. | |
122 | |
123 It turns out that on Warp3fp42 it is the priority at the time | |
124 of DosAsyncTimer() which matters. Let's hope that this works | |
125 with later versions too... XXXX | |
126 */ | |
127 priority = (tib->tib_ptib2->tib2_ulpri); | |
128 if ((priority & 0xFF00) == 0x0300) /* already time-critical */ | |
129 switch_priority = 0; | |
130 /* Make us time-critical. Just modifying TIB is not enough... */ | |
131 /* tib->tib_ptib2->tib2_ulpri = 0x0300;*/ | |
132 /* We do not want to run at high priority if a signal causes us | |
133 to longjmp() out of this section... */ | |
134 if (DosEnterMustComplete(&nesting)) | |
135 switch_priority = 0; | |
136 else | |
137 DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0); | |
138 } | |
139 } | |
140 | |
141 if ((badrc = DosAsyncTimer(ms, | |
142 (HSEM) hevEvent1, /* Semaphore to post */ | |
143 &htimerEvent1))) /* Timer handler (returned) */ | |
144 e = "DosAsyncTimer"; | |
145 | |
146 if (switch_priority && tib->tib_ptib2->tib2_ulpri == 0x0300) | |
147 { | |
148 /* Nobody switched priority while we slept... Ignore errors... */ | |
149 /* tib->tib_ptib2->tib2_ulpri = priority; */ /* Get back... */ | |
150 if (!(rc = DosSetPriority(PRTYS_THREAD, (priority>>8) & 0xFF, 0, 0))) | |
151 rc = DosSetPriority(PRTYS_THREAD, 0, priority & 0xFF, 0); | |
152 } | |
153 if (switch_priority) | |
154 rc = DosExitMustComplete(&nesting); /* Ignore errors */ | |
155 | |
156 /* The actual blocking call is made with "normal" priority. This way we | |
157 should not bother with DosSleep(0) etc. to compensate for us interrupting | |
158 higher-priority threads. The goal is to prohibit the system spending too | |
159 much time halt()ing, not to run us "no matter what". */ | |
160 if (!e) /* Wait for AsyncTimer event */ | |
161 badrc = DosWaitEventSem(hevEvent1, SEM_INDEFINITE_WAIT); | |
162 | |
163 if (e) ; /* Do nothing */ | |
164 else if (badrc == ERROR_INTERRUPT) | |
165 ret = 0; | |
166 else if (badrc) | |
167 e = "DosWaitEventSem"; | |
168 if ((rc = DosCloseEventSem(hevEvent1)) && !e) { /* Get rid of semaphore */ | |
169 e = "DosCloseEventSem"; | |
170 badrc = rc; | |
171 } | |
172 if (e) | |
173 { | |
174 SDL_SetError("[SDL_Delay] : Had error in %s(), rc is 0x%x\n", e, badrc); | |
175 } | |
176 } | |
177 | |
178 /* Data to handle a single periodic alarm */ | |
179 static int timer_alive = 0; | |
180 static SDL_Thread *timer = NULL; | |
181 | |
182 static int RunTimer(void *unused) | |
183 { | |
184 DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0); | |
185 while ( timer_alive ) { | |
186 if ( SDL_timer_running ) { | |
187 SDL_ThreadedTimerCheck(); | |
188 } | |
189 SDL_Delay(10); | |
190 } | |
191 return(0); | |
192 } | |
193 | |
194 /* This is only called if the event thread is not running */ | |
195 int SDL_SYS_TimerInit(void) | |
196 { | |
197 timer_alive = 1; | |
198 timer = SDL_CreateThread(RunTimer, NULL); | |
199 if ( timer == NULL ) | |
200 return(-1); | |
201 return(SDL_SetTimerThreaded(1)); | |
202 } | |
203 | |
204 void SDL_SYS_TimerQuit(void) | |
205 { | |
206 timer_alive = 0; | |
207 if ( timer ) { | |
208 SDL_WaitThread(timer, NULL); | |
209 timer = NULL; | |
210 } | |
211 } | |
212 | |
213 int SDL_SYS_StartTimer(void) | |
214 { | |
215 SDL_SetError("Internal logic error: OS/2 uses threaded timer"); | |
216 return(-1); | |
217 } | |
218 | |
219 void SDL_SYS_StopTimer(void) | |
220 { | |
221 return; | |
222 } | |
223 | |
224 |