1 | /* $Id: timer-r0drv-os2.cpp 8245 2008-04-21 17:24:28Z 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 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 | * Lazy initialize the spinlock.
|
---|
117 | */
|
---|
118 | if (g_Spinlock == NIL_RTSPINLOCK)
|
---|
119 | {
|
---|
120 | RTSPINLOCK Spinlock;
|
---|
121 | int rc = RTSpinlockCreate(&Spinlock);
|
---|
122 | AssertRCReturn(rc, rc);
|
---|
123 | //bool fRc;
|
---|
124 | //ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
|
---|
125 | //if (!fRc)
|
---|
126 | if (!ASMAtomicCmpXchgPtr((void * volatile *)&g_Spinlock, Spinlock, NIL_RTSPINLOCK))
|
---|
127 | RTSpinlockDestroy(Spinlock);
|
---|
128 | }
|
---|
129 |
|
---|
130 | /*
|
---|
131 | * Allocate and initialize the timer handle.
|
---|
132 | */
|
---|
133 | PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
|
---|
134 | if (!pTimer)
|
---|
135 | return VERR_NO_MEMORY;
|
---|
136 |
|
---|
137 | pTimer->u32Magic = RTTIMER_MAGIC;
|
---|
138 | pTimer->pNext = NULL;
|
---|
139 | pTimer->fSuspended = true;
|
---|
140 | pTimer->pfnTimer = pfnTimer;
|
---|
141 | pTimer->pvUser = pvUser;
|
---|
142 | pTimer->u64NanoInterval = u64NanoInterval;
|
---|
143 | pTimer->u64StartTS = 0;
|
---|
144 |
|
---|
145 | /*
|
---|
146 | * Insert the timer into the list (LIFO atm).
|
---|
147 | */
|
---|
148 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
149 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
150 | g_u32ChangeNo++;
|
---|
151 | pTimer->pNext = g_pTimerHead;
|
---|
152 | g_pTimerHead = pTimer;
|
---|
153 | g_cTimers++;
|
---|
154 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
155 |
|
---|
156 | *ppTimer = pTimer;
|
---|
157 | return VINF_SUCCESS;
|
---|
158 | }
|
---|
159 |
|
---|
160 |
|
---|
161 | /**
|
---|
162 | * Validates the timer handle.
|
---|
163 | *
|
---|
164 | * @returns true if valid, false if invalid.
|
---|
165 | * @param pTimer The handle.
|
---|
166 | */
|
---|
167 | DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
|
---|
168 | {
|
---|
169 | AssertReturn(VALID_PTR(pTimer), false);
|
---|
170 | AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
|
---|
171 | return true;
|
---|
172 | }
|
---|
173 |
|
---|
174 |
|
---|
175 | RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
|
---|
176 | {
|
---|
177 | /* It's ok to pass NULL pointer. */
|
---|
178 | if (pTimer == /*NIL_RTTIMER*/ NULL)
|
---|
179 | return VINF_SUCCESS;
|
---|
180 | if (!rtTimerIsValid(pTimer))
|
---|
181 | return VERR_INVALID_HANDLE;
|
---|
182 |
|
---|
183 | /*
|
---|
184 | * Remove it from the list.
|
---|
185 | */
|
---|
186 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
187 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
188 | g_u32ChangeNo++;
|
---|
189 | if (g_pTimerHead == pTimer)
|
---|
190 | g_pTimerHead = pTimer->pNext;
|
---|
191 | else
|
---|
192 | {
|
---|
193 | PRTTIMER pPrev = g_pTimerHead;
|
---|
194 | while (pPrev->pNext != pTimer)
|
---|
195 | {
|
---|
196 | pPrev = pPrev->pNext;
|
---|
197 | if (RT_UNLIKELY(!pPrev))
|
---|
198 | {
|
---|
199 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
200 | return VERR_INVALID_HANDLE;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | pPrev->pNext = pTimer->pNext;
|
---|
204 | }
|
---|
205 | Assert(g_cTimers > 0);
|
---|
206 | g_cTimers--;
|
---|
207 | if (!pTimer->fSuspended)
|
---|
208 | {
|
---|
209 | Assert(g_cActiveTimers > 0);
|
---|
210 | g_cActiveTimers--;
|
---|
211 | if (!g_cActiveTimers)
|
---|
212 | rtTimerOs2Dearm();
|
---|
213 | }
|
---|
214 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
215 |
|
---|
216 | /*
|
---|
217 | * Free the associated resources.
|
---|
218 | */
|
---|
219 | pTimer->u32Magic++;
|
---|
220 | RTMemFree(pTimer);
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | }
|
---|
223 |
|
---|
224 |
|
---|
225 | RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
|
---|
226 | {
|
---|
227 | if (!rtTimerIsValid(pTimer))
|
---|
228 | return VERR_INVALID_HANDLE;
|
---|
229 | if (!pTimer->fSuspended)
|
---|
230 | return VERR_TIMER_ACTIVE;
|
---|
231 |
|
---|
232 | /*
|
---|
233 | * Calc when it should start fireing and give the thread a kick so it get going.
|
---|
234 | */
|
---|
235 | u64First += RTTimeNanoTS();
|
---|
236 |
|
---|
237 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
238 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
239 | g_u32ChangeNo++;
|
---|
240 | if (!g_cActiveTimers)
|
---|
241 | {
|
---|
242 | int rc = rtTimerOs2Arm();
|
---|
243 | if (RT_FAILURE(rc))
|
---|
244 | {
|
---|
245 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
246 | return rc;
|
---|
247 | }
|
---|
248 | }
|
---|
249 | g_cActiveTimers++;
|
---|
250 | pTimer->fSuspended = false;
|
---|
251 | pTimer->fDone = true; /* next tick, not current! */
|
---|
252 | pTimer->iTick = 0;
|
---|
253 | pTimer->u64StartTS = u64First;
|
---|
254 | pTimer->u64NextTS = u64First;
|
---|
255 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
256 |
|
---|
257 | return VINF_SUCCESS;
|
---|
258 | }
|
---|
259 |
|
---|
260 |
|
---|
261 | RTDECL(int) RTTimerStop(PRTTIMER pTimer)
|
---|
262 | {
|
---|
263 | if (!rtTimerIsValid(pTimer))
|
---|
264 | return VERR_INVALID_HANDLE;
|
---|
265 | if (pTimer->fSuspended)
|
---|
266 | return VERR_TIMER_SUSPENDED;
|
---|
267 |
|
---|
268 | /*
|
---|
269 | * Suspend the timer.
|
---|
270 | */
|
---|
271 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
272 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
273 | g_u32ChangeNo++;
|
---|
274 | pTimer->fSuspended = true;
|
---|
275 | Assert(g_cActiveTimers > 0);
|
---|
276 | g_cActiveTimers--;
|
---|
277 | if (!g_cActiveTimers)
|
---|
278 | rtTimerOs2Dearm();
|
---|
279 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
280 |
|
---|
281 | return VINF_SUCCESS;
|
---|
282 | }
|
---|
283 |
|
---|
284 |
|
---|
285 | DECLASM(void) rtTimerOs2Tick(void)
|
---|
286 | {
|
---|
287 | /*
|
---|
288 | * Query the current time and then take the lock.
|
---|
289 | */
|
---|
290 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
291 |
|
---|
292 | RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
|
---|
293 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
294 |
|
---|
295 | /*
|
---|
296 | * Clear the fDone flag.
|
---|
297 | */
|
---|
298 | PRTTIMER pTimer;
|
---|
299 | for (pTimer = g_pTimerHead; pTimer; pTimer = pTimer->pNext)
|
---|
300 | pTimer->fDone = false;
|
---|
301 |
|
---|
302 | /*
|
---|
303 | * Walk the timer list and do the callbacks for any active timer.
|
---|
304 | */
|
---|
305 | uint32_t u32CurChangeNo = g_u32ChangeNo;
|
---|
306 | pTimer = g_pTimerHead;
|
---|
307 | while (pTimer)
|
---|
308 | {
|
---|
309 | PRTTIMER pNext = pTimer->pNext;
|
---|
310 | if ( !pTimer->fSuspended
|
---|
311 | && !pTimer->fDone
|
---|
312 | && pTimer->u64NextTS <= u64NanoTS)
|
---|
313 | {
|
---|
314 | pTimer->fDone = true;
|
---|
315 |
|
---|
316 | /* calculate the next timeout */
|
---|
317 | if (!pTimer->u64NanoInterval)
|
---|
318 | pTimer->fSuspended = true;
|
---|
319 | else
|
---|
320 | {
|
---|
321 | pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
|
---|
322 | if (pTimer->u64NextTS < u64NanoTS)
|
---|
323 | pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
|
---|
324 | }
|
---|
325 |
|
---|
326 | /* do the callout */
|
---|
327 | PFNRTTIMER pfnTimer = pTimer->pfnTimer;
|
---|
328 | void *pvUser = pTimer->pvUser;
|
---|
329 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
330 | pfnTimer(pTimer, pvUser);
|
---|
331 |
|
---|
332 | RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
|
---|
333 |
|
---|
334 | /* check if anything changed. */
|
---|
335 | if (u32CurChangeNo != g_u32ChangeNo)
|
---|
336 | {
|
---|
337 | u32CurChangeNo = g_u32ChangeNo;
|
---|
338 | pNext = g_pTimerHead;
|
---|
339 | }
|
---|
340 | }
|
---|
341 |
|
---|
342 | /* next */
|
---|
343 | pTimer = pNext;
|
---|
344 | }
|
---|
345 |
|
---|
346 | RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
|
---|
347 | }
|
---|
348 |
|
---|
349 |
|
---|
350 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
351 | {
|
---|
352 | return 32000000; /* 32ms */
|
---|
353 | }
|
---|
354 |
|
---|
355 |
|
---|
356 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
357 | {
|
---|
358 | return VERR_NOT_SUPPORTED;
|
---|
359 | }
|
---|
360 |
|
---|
361 |
|
---|
362 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
363 | {
|
---|
364 | return VERR_NOT_SUPPORTED;
|
---|
365 | }
|
---|
366 |
|
---|