1 | /* $Id: timer-r0drv-os2.cpp 9444 2008-06-05 18:08:17Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * IPRT - Memory Allocation, Ring-0 Driver, OS/2.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
---|
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 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 occures 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 | __BEGIN_DECLS
|
---|
104 | DECLASM(void) rtTimerOs2Tick(void);
|
---|
105 | DECLASM(int) rtTimerOs2Arm(void);
|
---|
106 | DECLASM(int) rtTimerOs2Dearm(void);
|
---|
107 | __END_DECLS
|
---|
108 |
|
---|
109 |
|
---|
110 |
|
---|
111 | RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned 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);
|
---|
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 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
155 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
156 | g_u32ChangeNo++;
|
---|
157 | pTimer->pNext = g_pTimerHead;
|
---|
158 | g_pTimerHead = pTimer;
|
---|
159 | g_cTimers++;
|
---|
160 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
161 |
|
---|
162 | *ppTimer = pTimer;
|
---|
163 | return VINF_SUCCESS;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Validates the timer handle.
|
---|
169 | *
|
---|
170 | * @returns true if valid, false if invalid.
|
---|
171 | * @param pTimer The handle.
|
---|
172 | */
|
---|
173 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
174 | {
|
---|
175 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
176 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
177 | return true;
|
---|
178 | }
|
---|
179 |
|
---|
180 |
|
---|
181 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
182 | {
|
---|
183 | /* It's ok to pass NULL pointer. */
|
---|
184 | if (pTimer == /*NIL_RTTIMER*/ NULL)
|
---|
185 | return VINF_SUCCESS;
|
---|
186 | if (!rtTimerIsValid(pTimer))
|
---|
187 | return VERR_INVALID_HANDLE;
|
---|
188 |
|
---|
189 | /*
|
---|
190 | * Remove it from the list.
|
---|
191 | */
|
---|
192 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
193 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
194 | g_u32ChangeNo++;
|
---|
195 | if (g_pTimerHead == pTimer)
|
---|
196 | g_pTimerHead = pTimer->pNext;
|
---|
197 | else
|
---|
198 | {
|
---|
199 | PRTTIMER pPrev = g_pTimerHead;
|
---|
200 | while (pPrev->pNext != pTimer)
|
---|
201 | {
|
---|
202 | pPrev = pPrev->pNext;
|
---|
203 | if (RT_UNLIKELY(!pPrev))
|
---|
204 | {
|
---|
205 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
206 | return VERR_INVALID_HANDLE;
|
---|
207 | }
|
---|
208 | }
|
---|
209 | pPrev->pNext = pTimer->pNext;
|
---|
210 | }
|
---|
211 | Assert(g_cTimers > 0);
|
---|
212 | g_cTimers--;
|
---|
213 | if (!pTimer->fSuspended)
|
---|
214 | {
|
---|
215 | Assert(g_cActiveTimers > 0);
|
---|
216 | g_cActiveTimers--;
|
---|
217 | if (!g_cActiveTimers)
|
---|
218 | rtTimerOs2Dearm();
|
---|
219 | }
|
---|
220 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
221 |
|
---|
222 | /*
|
---|
223 | * Free the associated resources.
|
---|
224 | */
|
---|
225 | pTimer->u32Magic++;
|
---|
226 | RTMemFree(pTimer);
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 |
|
---|
230 |
|
---|
231 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
232 | {
|
---|
233 | if (!rtTimerIsValid(pTimer))
|
---|
234 | return VERR_INVALID_HANDLE;
|
---|
235 | if (!pTimer->fSuspended)
|
---|
236 | return VERR_TIMER_ACTIVE;
|
---|
237 |
|
---|
238 | /*
|
---|
239 | * Calc when it should start fireing and give the thread a kick so it get going.
|
---|
240 | */
|
---|
241 | u64First += RTTimeNanoTS();
|
---|
242 |
|
---|
243 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
244 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
245 | g_u32ChangeNo++;
|
---|
246 | if (!g_cActiveTimers)
|
---|
247 | {
|
---|
248 | int rc = rtTimerOs2Arm();
|
---|
249 | if (RT_FAILURE(rc))
|
---|
250 | {
|
---|
251 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
252 | return rc;
|
---|
253 | }
|
---|
254 | }
|
---|
255 | g_cActiveTimers++;
|
---|
256 | pTimer->fSuspended = false;
|
---|
257 | pTimer->fDone = true; /* next tick, not current! */
|
---|
258 | pTimer->iTick = 0;
|
---|
259 | pTimer->u64StartTS = u64First;
|
---|
260 | pTimer->u64NextTS = u64First;
|
---|
261 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
262 |
|
---|
263 | return VINF_SUCCESS;
|
---|
264 | }
|
---|
265 |
|
---|
266 |
|
---|
267 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
268 | {
|
---|
269 | if (!rtTimerIsValid(pTimer))
|
---|
270 | return VERR_INVALID_HANDLE;
|
---|
271 | if (pTimer->fSuspended)
|
---|
272 | return VERR_TIMER_SUSPENDED;
|
---|
273 |
|
---|
274 | /*
|
---|
275 | * Suspend the timer.
|
---|
276 | */
|
---|
277 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
278 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
279 | g_u32ChangeNo++;
|
---|
280 | pTimer->fSuspended = true;
|
---|
281 | Assert(g_cActiveTimers > 0);
|
---|
282 | g_cActiveTimers--;
|
---|
283 | if (!g_cActiveTimers)
|
---|
284 | rtTimerOs2Dearm();
|
---|
285 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
286 |
|
---|
287 | return VINF_SUCCESS;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | DECLASM(void) rtTimerOs2Tick(void)
|
---|
292 | {
|
---|
293 | /*
|
---|
294 | * Query the current time and then take the lock.
|
---|
295 | */
|
---|
296 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
297 |
|
---|
298 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
299 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Clear the fDone flag.
|
---|
303 | */
|
---|
304 | PRTTIMER pTimer;
|
---|
305 | for (pTimer = g_pTimerHead; pTimer; pTimer = pTimer->pNext)
|
---|
306 | pTimer->fDone = false;
|
---|
307 |
|
---|
308 | /*
|
---|
309 | * Walk the timer list and do the callbacks for any active timer.
|
---|
310 | */
|
---|
311 | uint32_t u32CurChangeNo = g_u32ChangeNo;
|
---|
312 | pTimer = g_pTimerHead;
|
---|
313 | while (pTimer)
|
---|
314 | {
|
---|
315 | PRTTIMER pNext = pTimer->pNext;
|
---|
316 | if ( !pTimer->fSuspended
|
---|
317 | && !pTimer->fDone
|
---|
318 | && pTimer->u64NextTS <= u64NanoTS)
|
---|
319 | {
|
---|
320 | pTimer->fDone = true;
|
---|
321 | pTimer->iTick++;
|
---|
322 |
|
---|
323 | /* calculate the next timeout */
|
---|
324 | if (!pTimer->u64NanoInterval)
|
---|
325 | pTimer->fSuspended = true;
|
---|
326 | else
|
---|
327 | {
|
---|
328 | pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
|
---|
329 | if (pTimer->u64NextTS < u64NanoTS)
|
---|
330 | pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
|
---|
331 | }
|
---|
332 |
|
---|
333 | /* do the callout */
|
---|
334 | PFNRTTIMER pfnTimer = pTimer->pfnTimer;
|
---|
335 | void *pvUser = pTimer->pvUser;
|
---|
336 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
337 | pfnTimer(pTimer, pvUser, pTimer->iTick);
|
---|
338 |
|
---|
339 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
340 |
|
---|
341 | /* check if anything changed. */
|
---|
342 | if (u32CurChangeNo != g_u32ChangeNo)
|
---|
343 | {
|
---|
344 | u32CurChangeNo = g_u32ChangeNo;
|
---|
345 | pNext = g_pTimerHead;
|
---|
346 | }
|
---|
347 | }
|
---|
348 |
|
---|
349 | /* next */
|
---|
350 | pTimer = pNext;
|
---|
351 | }
|
---|
352 |
|
---|
353 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
358 | {
|
---|
359 | return 32000000; /* 32ms */
|
---|
360 | }
|
---|
361 |
|
---|
362 |
|
---|
363 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
364 | {
|
---|
365 | return VERR_NOT_SUPPORTED;
|
---|
366 | }
|
---|
367 |
|
---|
368 |
|
---|
369 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
370 | {
|
---|
371 | return VERR_NOT_SUPPORTED;
|
---|
372 | }
|
---|
373 |
|
---|