VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/os2/timer-r0drv-os2.cpp@ 2981

Last change on this file since 2981 was 2981, checked in by vboxsync, 18 years ago

InnoTek -> innotek: all the headers and comments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.0 KB
Line 
1/* $Id: timer-r0drv-os2.cpp 2981 2007-06-01 16:01:28Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - 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 */
54typedef 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. */
88static RTSPINLOCK g_Spinlock = NIL_RTSPINLOCK;
89/** The timer head. */
90static PRTTIMER volatile g_pTimerHead = NULL;
91/** The number of active timers. */
92static uint32_t volatile g_cActiveTimers = 0;
93/** The number of active timers. */
94static uint32_t volatile g_cTimers = 0;
95/** The change number.
96 * This is used to detect list changes during the timer callback loop. */
97static uint32_t volatile g_u32ChangeNo;
98
99
100/*******************************************************************************
101* Internal Functions *
102*******************************************************************************/
103__BEGIN_DECLS
104DECLASM(void) rtTimerOs2Tick(void);
105DECLASM(int) rtTimerOs2Arm(void);
106DECLASM(int) rtTimerOs2Dearm(void);
107__END_DECLS
108
109
110
111RTDECL(int) RTTimerCreate(PRTTIMER *ppTimer, unsigned uMilliesInterval, PFNRTTIMER pfnTimer, void *pvUser)
112{
113 int rc = RTTimerCreateEx(ppTimer, uMilliesInterval * UINT64_C(1000000), 0, pfnTimer, pvUser);
114 if (RT_SUCCESS(rc))
115 {
116 rc = RTTimerStart(*ppTimer, 0);
117 if (RT_SUCCESS(rc))
118 return rc;
119 int rc2 = RTTimerDestroy(*ppTimer); AssertRC(rc2);
120 *ppTimer = NULL;
121 }
122 return rc;
123}
124
125
126RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
127{
128 *ppTimer = NULL;
129
130 /*
131 * Lazy initialize the spinlock.
132 */
133 if (g_Spinlock == NIL_RTSPINLOCK)
134 {
135 RTSPINLOCK Spinlock;
136 int rc = RTSpinlockCreate(&Spinlock);
137 AssertRCReturn(rc, rc);
138 //bool fRc;
139 //ASMAtomicCmpXchgSize(&g_Spinlock, Spinlock, NIL_RTSPINLOCK, fRc);
140 //if (!fRc)
141 if (!ASMAtomicCmpXchgPtr((void * volatile *)&g_Spinlock, Spinlock, NIL_RTSPINLOCK))
142 RTSpinlockDestroy(Spinlock);
143 }
144
145 /*
146 * Allocate and initialize the timer handle.
147 */
148 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
149 if (!pTimer)
150 return VERR_NO_MEMORY;
151
152 pTimer->u32Magic = RTTIMER_MAGIC;
153 pTimer->pNext = NULL;
154 pTimer->fSuspended = true;
155 pTimer->pfnTimer = pfnTimer;
156 pTimer->pvUser = pvUser;
157 pTimer->u64NanoInterval = u64NanoInterval;
158 pTimer->u64StartTS = 0;
159
160 /*
161 * Insert the timer into the list (LIFO atm).
162 */
163 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
164 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
165 g_u32ChangeNo++;
166 pTimer->pNext = g_pTimerHead;
167 g_pTimerHead = pTimer;
168 g_cTimers++;
169 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
170
171 *ppTimer = pTimer;
172 return VINF_SUCCESS;
173}
174
175
176/**
177 * Validates the timer handle.
178 *
179 * @returns true if valid, false if invalid.
180 * @param pTimer The handle.
181 */
182DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
183{
184 AssertReturn(VALID_PTR(pTimer), false);
185 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
186 return true;
187}
188
189
190RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
191{
192 /* It's ok to pass NULL pointer. */
193 if (pTimer == /*NIL_RTTIMER*/ NULL)
194 return VINF_SUCCESS;
195 if (!rtTimerIsValid(pTimer))
196 return VERR_INVALID_HANDLE;
197
198 /*
199 * Remove it from the list.
200 */
201 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
202 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
203 g_u32ChangeNo++;
204 if (g_pTimerHead == pTimer)
205 g_pTimerHead = pTimer->pNext;
206 else
207 {
208 PRTTIMER pPrev = g_pTimerHead;
209 while (pPrev->pNext != pTimer)
210 {
211 pPrev = pPrev->pNext;
212 if (RT_UNLIKELY(!pPrev))
213 {
214 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
215 return VERR_INVALID_HANDLE;
216 }
217 }
218 pPrev->pNext = pTimer->pNext;
219 }
220 Assert(g_cTimers > 0);
221 g_cTimers--;
222 if (!pTimer->fSuspended)
223 {
224 Assert(g_cActiveTimers > 0);
225 g_cActiveTimers--;
226 if (!g_cActiveTimers)
227 rtTimerOs2Dearm();
228 }
229 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
230
231 /*
232 * Free the associated resources.
233 */
234 pTimer->u32Magic++;
235 RTMemFree(pTimer);
236 return VINF_SUCCESS;
237}
238
239
240RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
241{
242 if (!rtTimerIsValid(pTimer))
243 return VERR_INVALID_HANDLE;
244 if (!pTimer->fSuspended)
245 return VERR_TIMER_ACTIVE;
246
247 /*
248 * Calc when it should start fireing and give the thread a kick so it get going.
249 */
250 u64First += RTTimeNanoTS();
251
252 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
253 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
254 g_u32ChangeNo++;
255 if (!g_cActiveTimers)
256 {
257 int rc = rtTimerOs2Arm();
258 if (RT_FAILURE(rc))
259 {
260 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
261 return rc;
262 }
263 }
264 g_cActiveTimers++;
265 pTimer->fSuspended = false;
266 pTimer->fDone = true; /* next tick, not current! */
267 pTimer->iTick = 0;
268 pTimer->u64StartTS = u64First;
269 pTimer->u64NextTS = u64First;
270 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
271
272 return VINF_SUCCESS;
273}
274
275
276RTDECL(int) RTTimerStop(PRTTIMER pTimer)
277{
278 if (!rtTimerIsValid(pTimer))
279 return VERR_INVALID_HANDLE;
280 if (pTimer->fSuspended)
281 return VERR_TIMER_SUSPENDED;
282
283 /*
284 * Suspend the timer.
285 */
286 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
287 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
288 g_u32ChangeNo++;
289 pTimer->fSuspended = true;
290 Assert(g_cActiveTimers > 0);
291 g_cActiveTimers--;
292 if (!g_cActiveTimers)
293 rtTimerOs2Dearm();
294 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
295
296 return VINF_SUCCESS;
297}
298
299
300DECLASM(void) rtTimerOs2Tick(void)
301{
302 /*
303 * Query the current time and then take the lock.
304 */
305 const uint64_t u64NanoTS = RTTimeNanoTS();
306
307 RTSPINLOCKTMP Tmp = RTSPINLOCKTMP_INITIALIZER;
308 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
309
310 /*
311 * Clear the fDone flag.
312 */
313 PRTTIMER pTimer;
314 for (pTimer = g_pTimerHead; pTimer; pTimer = pTimer->pNext)
315 pTimer->fDone = false;
316
317 /*
318 * Walk the timer list and do the callbacks for any active timer.
319 */
320 uint32_t u32CurChangeNo = g_u32ChangeNo;
321 pTimer = g_pTimerHead;
322 while (pTimer)
323 {
324 PRTTIMER pNext = pTimer->pNext;
325 if ( !pTimer->fSuspended
326 && !pTimer->fDone
327 && pTimer->u64NextTS <= u64NanoTS)
328 {
329 pTimer->fDone = true;
330
331 /* calculate the next timeout */
332 if (!pTimer->u64NanoInterval)
333 pTimer->fSuspended = true;
334 else
335 {
336 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
337 if (pTimer->u64NextTS < u64NanoTS)
338 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
339 }
340
341 /* do the callout */
342 PFNRTTIMER pfnTimer = pTimer->pfnTimer;
343 void *pvUser = pTimer->pvUser;
344 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
345 pfnTimer(pTimer, pvUser);
346
347 RTSpinlockAcquireNoInts(g_Spinlock, &Tmp);
348
349 /* check if anything changed. */
350 if (u32CurChangeNo != g_u32ChangeNo)
351 {
352 u32CurChangeNo = g_u32ChangeNo;
353 pNext = g_pTimerHead;
354 }
355 }
356
357 /* next */
358 pTimer = pNext;
359 }
360
361 RTSpinlockReleaseNoInts(g_Spinlock, &Tmp);
362}
363
364
365RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
366{
367 return 32000000; /* 32ms */
368}
369
370
371RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
372{
373 return VERR_NOT_SUPPORTED;
374}
375
376
377RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
378{
379 return VERR_NOT_SUPPORTED;
380}
381
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette