Repository navigation
Expand file tree
/
Copy pathSDL_systimer.c
More file actions
180 lines (148 loc) · 4.77 KB
/
Copy pathSDL_systimer.c
File metadata and controls
180 lines (148 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/*
Simple DirectMedia Layer
Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "../../SDL_internal.h"
#ifdef SDL_TIMER_OS2
#include "SDL_timer.h"
#include "../../core/os2/SDL_os2.h"
#define INCL_DOSERRORS
#define INCL_DOSMISC
#define INCL_DOSPROFILE
#define INCL_DOSSEMAPHORES
#define INCL_DOSDATETIME
#define INCL_DOSPROCESS
#define INCL_DOSEXCEPTIONS
#include <os2.h>
/* No need to switch priorities in SDL_Delay() for OS/2 versions > Warp3 fp 42, */
/*#define _SWITCH_PRIORITY*/
typedef unsigned long long ULLONG;
static SDL_bool ticks_started = SDL_FALSE;
static ULONG ulTmrFreq = 0;
static ULLONG ullTmrStart = 0;
void SDL_TicksInit(void)
{
ULONG ulTmrStart; /* for 32-bit fallback. */
ULONG ulRC;
if (ticks_started) {
return;
}
ticks_started = SDL_TRUE;
ulRC = DosTmrQueryFreq(&ulTmrFreq);
if (ulRC != NO_ERROR) {
debug_os2("DosTmrQueryFreq() failed, rc = %u", ulRC);
} else {
ulRC = DosTmrQueryTime((PQWORD)&ullTmrStart);
if (ulRC == NO_ERROR) {
return;
}
debug_os2("DosTmrQueryTime() failed, rc = %u", ulRC);
}
ulTmrFreq = 0; /* Error - use DosQuerySysInfo() for timer. */
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulTmrStart, sizeof(ULONG));
ullTmrStart = (ULLONG) ulTmrStart;
}
void SDL_TicksQuit(void)
{
ticks_started = SDL_FALSE;
}
Uint64 SDL_GetTicks64(void)
{
Uint64 ui64Result;
ULLONG ullTmrNow;
if (!ticks_started) {
SDL_TicksInit();
}
if (ulTmrFreq != 0) {
DosTmrQueryTime((PQWORD)&ullTmrNow);
ui64Result = (ullTmrNow - ullTmrStart) * 1000 / ulTmrFreq;
} else {
/* note that this counter rolls over to 0 every ~49 days. Fix your system so DosTmrQueryTime works if you need to avoid this. */
ULONG ulTmrNow;
DosQuerySysInfo(QSV_MS_COUNT, QSV_MS_COUNT, &ulTmrNow, sizeof(ULONG));
ui64Result = (((Uint64) ulTmrNow) - ullTmrStart);
}
return ui64Result;
}
Uint64 SDL_GetPerformanceCounter(void)
{
QWORD qwTmrNow;
if (ulTmrFreq == 0 || (DosTmrQueryTime(&qwTmrNow) != NO_ERROR)) {
return SDL_GetTicks64();
}
return *((Uint64 *)&qwTmrNow);
}
Uint64 SDL_GetPerformanceFrequency(void)
{
return (ulTmrFreq == 0)? 1000 : (Uint64)ulTmrFreq;
}
void SDL_Delay(Uint32 ms)
{
HTIMER hTimer = NULLHANDLE;
ULONG ulRC;
#ifdef _SWITCH_PRIORITY
PPIB pib;
PTIB tib;
BOOL fSetPriority = ms < 50;
ULONG ulSavePriority;
ULONG ulNesting;
#endif
HEV hevTimer;
if (ms == 0) {
DosSleep(0);
return;
}
ulRC = DosCreateEventSem(NULL, &hevTimer, DC_SEM_SHARED, FALSE);
if (ulRC != NO_ERROR) {
debug_os2("DosAsyncTimer() failed, rc = %u", ulRC);
DosSleep(ms);
return;
}
#ifdef _SWITCH_PRIORITY
if (fSetPriority) {
if (DosGetInfoBlocks(&tib, &pib) != NO_ERROR)
fSetPriority = FALSE;
else {
ulSavePriority = tib->tib_ptib2->tib2_ulpri;
if (((ulSavePriority & 0xFF00) == 0x0300) || /* already have high pr. */
(DosEnterMustComplete( &ulNesting) != NO_ERROR))
fSetPriority = FALSE;
else {
DosSetPriority(PRTYS_THREAD, PRTYC_TIMECRITICAL, 0, 0);
}
}
}
#endif
DosResetEventSem(hevTimer, &ulRC);
ulRC = DosAsyncTimer(ms, (HSEM)hevTimer, &hTimer);
#ifdef _SWITCH_PRIORITY
if (fSetPriority) {
if (DosSetPriority(PRTYS_THREAD, (ulSavePriority >> 8) & 0xFF, 0, 0) == NO_ERROR)
DosSetPriority(PRTYS_THREAD, 0, ulSavePriority & 0xFF, 0);
DosExitMustComplete(&ulNesting);
}
#endif
if (ulRC != NO_ERROR) {
debug_os2("DosAsyncTimer() failed, rc = %u", ulRC);
} else {
DosWaitEventSem(hevTimer, SEM_INDEFINITE_WAIT);
}
if (ulRC != NO_ERROR)
DosSleep(ms);
DosCloseEventSem(hevTimer);
}
#endif /* SDL_TIMER_OS2 */
/* vi: set ts=4 sw=4 expandtab: */