1 | /* $Id: timer-r0drv-os2.cpp 52618 2014-09-05 12:07:29Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <[email protected]>
|
---|
8 | *
|
---|
9 | * Permission is hereby granted, free of charge, to any person
|
---|
10 | * obtaining a copy of this software and associated documentation
|
---|
11 | * files (the "Software"), to deal in the Software without
|
---|
12 | * restriction, including without limitation the rights to use,
|
---|
13 | * copy, modify, merge, publish, distribute, sublicense, and/or sell
|
---|
14 | * copies of the Software, and to permit persons to whom the
|
---|
15 | * Software is furnished to do so, subject to the following
|
---|
16 | * conditions:
|
---|
17 | *
|
---|
18 | * The above copyright notice and this permission notice shall be
|
---|
19 | * included in all copies or substantial portions of the Software.
|
---|
20 | *
|
---|
21 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
---|
22 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
---|
23 | * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
---|
24 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
---|
25 | * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
---|
26 | * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
---|
27 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
---|
28 | * OTHER DEALINGS IN THE SOFTWARE.
|
---|
29 | */
|
---|
30 |
|
---|
31 |
|
---|
32 | /*******************************************************************************
|
---|
33 | * Header Files *
|
---|
34 | *******************************************************************************/
|
---|
35 | #include "the-os2-kernel.h"
|
---|
36 |
|
---|
37 | #include <iprt/timer.h>
|
---|
38 | #include <iprt/time.h>
|
---|
39 | #include <iprt/spinlock.h>
|
---|
40 | #include <iprt/err.h>
|
---|
41 | #include <iprt/asm.h>
|
---|
42 | #include <iprt/assert.h>
|
---|
43 | #include <iprt/alloc.h>
|
---|
44 |
|
---|
45 | #include "internal/magics.h"
|
---|
46 |
|
---|
47 |
|
---|
48 | /*******************************************************************************
|
---|
49 | * Structures and Typedefs *
|
---|
50 | *******************************************************************************/
|
---|
51 | /**
|
---|
52 | * The internal representation of an OS/2 timer handle.
|
---|
53 | */
|
---|
54 | typedef struct RTTIMER
|
---|
55 | {
|
---|
56 | /** Magic.
|
---|
57 | * This is RTTIMER_MAGIC, but changes to something else before the timer
|
---|
58 | * is destroyed to indicate clearly that thread should exit. */
|
---|
59 | uint32_t volatile u32Magic;
|
---|
60 | /** The next timer in the timer list. */
|
---|
61 | PRTTIMER pNext;
|
---|
62 | /** Flag indicating the timer is suspended. */
|
---|
63 | uint8_t volatile fSuspended;
|
---|
64 | /** Cleared at the start of timer processing, set when calling pfnTimer.
|
---|
65 | * If any timer changes occurs while doing the callback this will be used to resume the cycle. */
|
---|
66 | bool fDone;
|
---|
67 | /** Callback. */
|
---|
68 | PFNRTTIMER pfnTimer;
|
---|
69 | /** User argument. */
|
---|
70 | void *pvUser;
|
---|
71 | /** The timer interval. 0 if one-shot. */
|
---|
72 | uint64_t u64NanoInterval;
|
---|
73 | /** The start of the current run.
|
---|
74 | * This is used to calculate when the timer ought to fire the next time. */
|
---|
75 | uint64_t volatile u64StartTS;
|
---|
76 | /** The start of the current run.
|
---|
77 | * This is used to calculate when the timer ought to fire the next time. */
|
---|
78 | uint64_t volatile u64NextTS;
|
---|
79 | /** The current tick number (since u64StartTS). */
|
---|
80 | uint64_t volatile iTick;
|
---|
81 | } RTTIMER;
|
---|
82 |
|
---|
83 |
|
---|
84 | /*******************************************************************************
|
---|
85 | * Global Variables *
|
---|
86 | *******************************************************************************/
|
---|
87 | /** Spinlock protecting the timers. */
|
---|
88 | static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
|
---|
89 | /** The timer head. */
|
---|
90 | static PRTTIMER volatile g_pTimerHead = NULL;
|
---|
91 | /** The number of active timers. */
|
---|
92 | static uint32_t volatile g_cActiveTimers = 0;
|
---|
93 | /** The number of active timers. */
|
---|
94 | static uint32_t volatile g_cTimers = 0;
|
---|
95 | /** The change number.
|
---|
96 | * This is used to detect list changes during the timer callback loop. */
|
---|
97 | static uint32_t volatile g_u32ChangeNo;
|
---|
98 |
|
---|
99 |
|
---|
100 | /*******************************************************************************
|
---|
101 | * Internal Functions *
|
---|
102 | *******************************************************************************/
|
---|
103 | RT_C_DECLS_BEGIN
|
---|
104 | DECLASM(void) rtTimerOs2Tick(void);
|
---|
105 | DECLASM(int) rtTimerOs2Arm(void);
|
---|
106 | DECLASM(int) rtTimerOs2Dearm(void);
|
---|
107 | RT_C_DECLS_END
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
|
---|
112 | {
|
---|
113 | *ppTimer = NULL;
|
---|
114 |
|
---|
115 | /*
|
---|
116 | * We don't support the fancy MP features.
|
---|
117 | */
|
---|
118 | if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
|
---|
119 | return VERR_NOT_SUPPORTED;
|
---|
120 |
|
---|
121 | /*
|
---|
122 | * Lazy initialize the spinlock.
|
---|
123 | */
|
---|
124 | if (g_Spinlock == NIL_RTSPINLOCK)
|
---|
125 | {
|
---|
126 | RTSPINLOCK Spinlock;
|
---|
127 | int rc = RTSpinlockCreate(&Spinlock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "RTTimerOS2");
|
---|
128 | AssertRCReturn(rc, rc);
|
---|
129 | //bool fRc;
|
---|
130 | //ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
|
---|
131 | //if (!fRc)
|
---|
132 | if (!ASMAtomicCmpXchgPtr((void * volatile *)&g_Spinlock, Spinlock, NIL_RTSPINLOCK))
|
---|
133 | RTSpinlockDestroy(Spinlock);
|
---|
134 | }
|
---|
135 |
|
---|
136 | /*
|
---|
137 | * Allocate and initialize the timer handle.
|
---|
138 | */
|
---|
139 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
140 | if (!pTimer)
|
---|
141 | return VERR_NO_MEMORY;
|
---|
142 |
|
---|
143 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
144 | pTimer->pNext = NULL;
|
---|
145 | pTimer->fSuspended = true;
|
---|
146 | pTimer->pfnTimer = pfnTimer;
|
---|
147 | pTimer->pvUser = pvUser;
|
---|
148 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
149 | pTimer->u64StartTS = 0;
|
---|
150 |
|
---|
151 | /*
|
---|
152 | * Insert the timer into the list (LIFO atm).
|
---|
153 | */
|
---|
154 | RTSpinlockAcquire(g_Spinlock);
|
---|
155 | g_u32ChangeNo++;
|
---|
156 | pTimer->pNext = g_pTimerHead;
|
---|
157 | g_pTimerHead = pTimer;
|
---|
158 | g_cTimers++;
|
---|
159 | RTSpinlockRelease(g_Spinlock);
|
---|
160 |
|
---|
161 | *ppTimer = pTimer;
|
---|
162 | return VINF_SUCCESS;
|
---|
163 | }
|
---|
164 |
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Validates the timer handle.
|
---|
168 | *
|
---|
169 | * @returns true if valid, false if invalid.
|
---|
170 | * @param pTimer The handle.
|
---|
171 | */
|
---|
172 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
173 | {
|
---|
174 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
175 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
176 | return true;
|
---|
177 | }
|
---|
178 |
|
---|
179 |
|
---|
180 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
181 | {
|
---|
182 | /* It's ok to pass NULL pointer. */
|
---|
183 | if (pTimer == /*NIL_RTTIMER*/ NULL)
|
---|
184 | return VINF_SUCCESS;
|
---|
185 | if (!rtTimerIsValid(pTimer))
|
---|
186 | return VERR_INVALID_HANDLE;
|
---|
187 |
|
---|
188 | /*
|
---|
189 | * Remove it from the list.
|
---|
190 | */
|
---|
191 | RTSpinlockAcquire(g_Spinlock);
|
---|
192 | g_u32ChangeNo++;
|
---|
193 | if (g_pTimerHead == pTimer)
|
---|
194 | g_pTimerHead = pTimer->pNext;
|
---|
195 | else
|
---|
196 | {
|
---|
197 | PRTTIMER pPrev = g_pTimerHead;
|
---|
198 | while (pPrev->pNext != pTimer)
|
---|
199 | {
|
---|
200 | pPrev = pPrev->pNext;
|
---|
201 | if (RT_UNLIKELY(!pPrev))
|
---|
202 | {
|
---|
203 | RTSpinlockRelease(g_Spinlock);
|
---|
204 | return VERR_INVALID_HANDLE;
|
---|
205 | }
|
---|
206 | }
|
---|
207 | pPrev->pNext = pTimer->pNext;
|
---|
208 | }
|
---|
209 | Assert(g_cTimers > 0);
|
---|
210 | g_cTimers--;
|
---|
211 | if (!pTimer->fSuspended)
|
---|
212 | {
|
---|
213 | Assert(g_cActiveTimers > 0);
|
---|
214 | g_cActiveTimers--;
|
---|
215 | if (!g_cActiveTimers)
|
---|
216 | rtTimerOs2Dearm();
|
---|
217 | }
|
---|
218 | RTSpinlockRelease(g_Spinlock);
|
---|
219 |
|
---|
220 | /*
|
---|
221 | * Free the associated resources.
|
---|
222 | */
|
---|
223 | pTimer->u32Magic++;
|
---|
224 | RTMemFree(pTimer);
|
---|
225 | return VINF_SUCCESS;
|
---|
226 | }
|
---|
227 |
|
---|
228 |
|
---|
229 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
230 | {
|
---|
231 | if (!rtTimerIsValid(pTimer))
|
---|
232 | return VERR_INVALID_HANDLE;
|
---|
233 | if (!pTimer->fSuspended)
|
---|
234 | return VERR_TIMER_ACTIVE;
|
---|
235 |
|
---|
236 | /*
|
---|
237 | * Calc when it should start firing and give the thread a kick so it get going.
|
---|
238 | */
|
---|
239 | u64First += RTTimeNanoTS();
|
---|
240 |
|
---|
241 | RTSpinlockAcquire(g_Spinlock);
|
---|
242 | g_u32ChangeNo++;
|
---|
243 | if (!g_cActiveTimers)
|
---|
244 | {
|
---|
245 | int rc = rtTimerOs2Arm();
|
---|
246 | if (RT_FAILURE(rc))
|
---|
247 | {
|
---|
248 | RTSpinlockRelease(g_Spinlock);
|
---|
249 | return rc;
|
---|
250 | }
|
---|
251 | }
|
---|
252 | g_cActiveTimers++;
|
---|
253 | pTimer->fSuspended = false;
|
---|
254 | pTimer->fDone = true; /* next tick, not current! */
|
---|
255 | pTimer->iTick = 0;
|
---|
256 | pTimer->u64StartTS = u64First;
|
---|
257 | pTimer->u64NextTS = u64First;
|
---|
258 | RTSpinlockRelease(g_Spinlock);
|
---|
259 |
|
---|
260 | return VINF_SUCCESS;
|
---|
261 | }
|
---|
262 |
|
---|
263 |
|
---|
264 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
265 | {
|
---|
266 | if (!rtTimerIsValid(pTimer))
|
---|
267 | return VERR_INVALID_HANDLE;
|
---|
268 | if (pTimer->fSuspended)
|
---|
269 | return VERR_TIMER_SUSPENDED;
|
---|
270 |
|
---|
271 | /*
|
---|
272 | * Suspend the timer.
|
---|
273 | */
|
---|
274 | RTSpinlockAcquire(g_Spinlock);
|
---|
275 | g_u32ChangeNo++;
|
---|
276 | pTimer->fSuspended = true;
|
---|
277 | Assert(g_cActiveTimers > 0);
|
---|
278 | g_cActiveTimers--;
|
---|
279 | if (!g_cActiveTimers)
|
---|
280 | rtTimerOs2Dearm();
|
---|
281 | RTSpinlockRelease(g_Spinlock);
|
---|
282 |
|
---|
283 | return VINF_SUCCESS;
|
---|
284 | }
|
---|
285 |
|
---|
286 |
|
---|
287 | RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
|
---|
288 | {
|
---|
289 | if (!rtTimerIsValid(pTimer))
|
---|
290 | return VERR_INVALID_HANDLE;
|
---|
291 |
|
---|
292 | return VERR_NOT_SUPPORTED;
|
---|
293 | }
|
---|
294 |
|
---|
295 |
|
---|
296 | DECLASM(void) rtTimerOs2Tick(void)
|
---|
297 | {
|
---|
298 | /*
|
---|
299 | * Query the current time and then take the lock.
|
---|
300 | */
|
---|
301 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
302 |
|
---|
303 | RTSpinlockAcquire(g_Spinlock);
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Clear the fDone flag.
|
---|
307 | */
|
---|
308 | PRTTIMER pTimer;
|
---|
309 | for (pTimer = g_pTimerHead; pTimer; pTimer = pTimer->pNext)
|
---|
310 | pTimer->fDone = false;
|
---|
311 |
|
---|
312 | /*
|
---|
313 | * Walk the timer list and do the callbacks for any active timer.
|
---|
314 | */
|
---|
315 | uint32_t u32CurChangeNo = g_u32ChangeNo;
|
---|
316 | pTimer = g_pTimerHead;
|
---|
317 | while (pTimer)
|
---|
318 | {
|
---|
319 | PRTTIMER pNext = pTimer->pNext;
|
---|
320 | if ( !pTimer->fSuspended
|
---|
321 | && !pTimer->fDone
|
---|
322 | && pTimer->u64NextTS <= u64NanoTS)
|
---|
323 | {
|
---|
324 | pTimer->fDone = true;
|
---|
325 | pTimer->iTick++;
|
---|
326 |
|
---|
327 | /* calculate the next timeout */
|
---|
328 | if (!pTimer->u64NanoInterval)
|
---|
329 | pTimer->fSuspended = true;
|
---|
330 | else
|
---|
331 | {
|
---|
332 | pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
|
---|
333 | if (pTimer->u64NextTS < u64NanoTS)
|
---|
334 | pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
|
---|
335 | }
|
---|
336 |
|
---|
337 | /* do the callout */
|
---|
338 | PFNRTTIMER pfnTimer = pTimer->pfnTimer;
|
---|
339 | void *pvUser = pTimer->pvUser;
|
---|
340 | RTSpinlockRelease(g_Spinlock);
|
---|
341 | pfnTimer(pTimer, pvUser, pTimer->iTick);
|
---|
342 |
|
---|
343 | RTSpinlockAcquire(g_Spinlock);
|
---|
344 |
|
---|
345 | /* check if anything changed. */
|
---|
346 | if (u32CurChangeNo != g_u32ChangeNo)
|
---|
347 | {
|
---|
348 | u32CurChangeNo = g_u32ChangeNo;
|
---|
349 | pNext = g_pTimerHead;
|
---|
350 | }
|
---|
351 | }
|
---|
352 |
|
---|
353 | /* next */
|
---|
354 | pTimer = pNext;
|
---|
355 | }
|
---|
356 |
|
---|
357 | RTSpinlockRelease(g_Spinlock);
|
---|
358 | }
|
---|
359 |
|
---|
360 |
|
---|
361 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
362 | {
|
---|
363 | return 32000000; /* 32ms */
|
---|
364 | }
|
---|
365 |
|
---|
366 |
|
---|
367 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
368 | {
|
---|
369 | return VERR_NOT_SUPPORTED;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
374 | {
|
---|
375 | return VERR_NOT_SUPPORTED;
|
---|
376 | }
|
---|
377 |
|
---|
378 |
|
---|
379 | RTDECL(bool) RTTimerCanDoHighResolution(void)
|
---|
380 | {
|
---|
381 | return false;
|
---|
382 | }
|
---|
383 |
|
---|