VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/timer-generic.cpp@ 25641

Last change on this file since 25641 was 25641, checked in by vboxsync, 15 years ago

iprt: file header fixes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.9 KB
Line 
1/* $Id: timer-generic.cpp 25641 2010-01-05 08:08:44Z vboxsync $ */
2/** @file
3 * IPRT - Timers, Generic.
4 */
5
6/*
7 * Copyright (C) 2006-2007 Sun Microsystems, Inc.
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 *
26 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
27 * Clara, CA 95054 USA or visit http://www.sun.com if you need
28 * additional information or have any questions.
29 */
30
31
32/*******************************************************************************
33* Header Files *
34*******************************************************************************/
35#include <iprt/timer.h>
36#include "internal/iprt.h"
37
38#include <iprt/thread.h>
39#include <iprt/err.h>
40#include <iprt/assert.h>
41#include <iprt/alloc.h>
42#include <iprt/asm.h>
43#include <iprt/semaphore.h>
44#include <iprt/time.h>
45#include <iprt/log.h>
46#include "internal/magics.h"
47
48
49
50/*******************************************************************************
51* Structures and Typedefs *
52*******************************************************************************/
53/**
54 * The internal representation of a timer handle.
55 */
56typedef struct RTTIMER
57{
58 /** Magic.
59 * This is RTTIMER_MAGIC, but changes to something else before the timer
60 * is destroyed to indicate clearly that thread should exit. */
61 uint32_t volatile u32Magic;
62 /** Flag indicating the timer is suspended. */
63 uint8_t volatile fSuspended;
64 /** Flag indicating that the timer has been destroyed. */
65 uint8_t volatile fDestroyed;
66 /** Callback. */
67 PFNRTTIMER pfnTimer;
68 /** User argument. */
69 void *pvUser;
70 /** The timer thread. */
71 RTTHREAD Thread;
72 /** Event semaphore on which the thread is blocked. */
73 RTSEMEVENT Event;
74 /** The timer interval. 0 if one-shot. */
75 uint64_t u64NanoInterval;
76 /** The start of the current run (ns).
77 * This is used to calculate when the timer ought to fire the next time. */
78 uint64_t volatile u64StartTS;
79 /** The start of the current run (ns).
80 * This is used to calculate when the timer ought to fire the next time. */
81 uint64_t volatile u64NextTS;
82 /** The current tick number (since u64StartTS). */
83 uint64_t volatile iTick;
84} RTTIMER;
85
86
87/*******************************************************************************
88* Internal Functions *
89*******************************************************************************/
90static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
91
92
93RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
94{
95 *ppTimer = NULL;
96
97 /*
98 * We don't support the fancy MP features.
99 */
100 if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
101 return VERR_NOT_SUPPORTED;
102
103 /*
104 * Allocate and initialize the timer handle.
105 */
106 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
107 if (!pTimer)
108 return VERR_NO_MEMORY;
109
110 pTimer->u32Magic = RTTIMER_MAGIC;
111 pTimer->fSuspended = true;
112 pTimer->fDestroyed = false;
113 pTimer->pfnTimer = pfnTimer;
114 pTimer->pvUser = pvUser;
115 pTimer->Thread = NIL_RTTHREAD;
116 pTimer->Event = NIL_RTSEMEVENT;
117 pTimer->u64NanoInterval = u64NanoInterval;
118 pTimer->u64StartTS = 0;
119
120 int rc = RTSemEventCreate(&pTimer->Event);
121 if (RT_SUCCESS(rc))
122 {
123 rc = RTThreadCreate(&pTimer->Thread, rtTimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
124 if (RT_SUCCESS(rc))
125 {
126 *ppTimer = pTimer;
127 return VINF_SUCCESS;
128 }
129
130 pTimer->u32Magic = 0;
131 RTSemEventDestroy(pTimer->Event);
132 pTimer->Event = NIL_RTSEMEVENT;
133 }
134 RTMemFree(pTimer);
135
136 return rc;
137}
138RT_EXPORT_SYMBOL(RTTimerCreateEx);
139
140
141/**
142 * Validates the timer handle.
143 *
144 * @returns true if valid, false if invalid.
145 * @param pTimer The handle.
146 */
147DECLINLINE(bool) rtTimerIsValid(PRTTIMER pTimer)
148{
149 AssertReturn(VALID_PTR(pTimer), false);
150 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, false);
151 AssertReturn(!pTimer->fDestroyed, false);
152 return true;
153}
154
155
156RTDECL(int) RTTimerDestroy(PRTTIMER pTimer)
157{
158 /* It's ok to pass NULL pointer. */
159 if (pTimer == /*NIL_RTTIMER*/ NULL)
160 return VINF_SUCCESS;
161 if (!rtTimerIsValid(pTimer))
162 return VERR_INVALID_HANDLE;
163
164 /*
165 * If the timer is active, we just flag it to self destruct on the next tick.
166 * If it's suspended we can safely set the destroy flag and signal it.
167 */
168 RTTHREAD Thread = pTimer->Thread;
169 if (!pTimer->fSuspended)
170 {
171 ASMAtomicXchgU8(&pTimer->fSuspended, true);
172 ASMAtomicXchgU8(&pTimer->fDestroyed, true);
173 }
174 else
175 {
176 ASMAtomicXchgU8(&pTimer->fDestroyed, true);
177 int rc = RTSemEventSignal(pTimer->Event);
178 if (rc == VERR_ALREADY_POSTED)
179 rc = VINF_SUCCESS;
180 AssertRC(rc);
181 }
182
183 RTThreadWait(Thread, 250, NULL);
184 return VINF_SUCCESS;
185}
186RT_EXPORT_SYMBOL(RTTimerDestroy);
187
188
189RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
190{
191 if (!rtTimerIsValid(pTimer))
192 return VERR_INVALID_HANDLE;
193 if (!pTimer->fSuspended)
194 return VERR_TIMER_ACTIVE;
195
196 /*
197 * Calc when it should start fireing and give the thread a kick so it get going.
198 */
199 u64First += RTTimeNanoTS();
200 ASMAtomicXchgU64(&pTimer->iTick, 0);
201 ASMAtomicXchgU64(&pTimer->u64StartTS, u64First);
202 ASMAtomicXchgU64(&pTimer->u64NextTS, u64First);
203 ASMAtomicXchgU8(&pTimer->fSuspended, false);
204 int rc = RTSemEventSignal(pTimer->Event);
205 if (rc == VERR_ALREADY_POSTED)
206 rc = VINF_SUCCESS;
207 AssertRC(rc);
208 return rc;
209}
210RT_EXPORT_SYMBOL(RTTimerStart);
211
212
213RTDECL(int) RTTimerStop(PRTTIMER pTimer)
214{
215 if (!rtTimerIsValid(pTimer))
216 return VERR_INVALID_HANDLE;
217 if (pTimer->fSuspended)
218 return VERR_TIMER_SUSPENDED;
219
220 /*
221 * Mark it as suspended and kick the thread.
222 */
223 ASMAtomicXchgU8(&pTimer->fSuspended, true);
224 int rc = RTSemEventSignal(pTimer->Event);
225 if (rc == VERR_ALREADY_POSTED)
226 rc = VINF_SUCCESS;
227 AssertRC(rc);
228 return rc;
229}
230RT_EXPORT_SYMBOL(RTTimerStop);
231
232
233static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
234{
235 PRTTIMER pTimer = (PRTTIMER)pvUser;
236
237 /*
238 * The loop.
239 */
240 while (!pTimer->fDestroyed)
241 {
242 if (pTimer->fSuspended)
243 {
244 int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
245 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
246 {
247 AssertRC(rc);
248 RTThreadSleep(1000); /* Don't cause trouble! */
249 }
250 }
251 else
252 {
253 const uint64_t u64NanoTS = RTTimeNanoTS();
254 if (u64NanoTS >= pTimer->u64NextTS)
255 {
256 pTimer->iTick++;
257 pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
258
259 /* status changed? */
260 if (pTimer->fSuspended || pTimer->fDestroyed)
261 continue;
262
263 /* one shot? */
264 if (!pTimer->u64NanoInterval)
265 {
266 ASMAtomicXchgU8(&pTimer->fSuspended, true);
267 continue;
268 }
269
270 /* calc the next time we should fire. */
271 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
272 if (pTimer->u64NextTS < u64NanoTS)
273#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
274 pTimer->u64NextTS = u64NanoTS + 1;
275#else
276 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
277#endif
278 }
279
280 /* block. */
281 uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
282#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
283 if (cNanoSeconds > 10)
284#endif
285 {
286 int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
287 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
288 {
289 AssertRC(rc);
290 RTThreadSleep(1000); /* Don't cause trouble! */
291 }
292 }
293 }
294 }
295
296 /*
297 * Release the timer resources.
298 */
299 ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
300 int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
301 pTimer->Event = NIL_RTSEMEVENT;
302 pTimer->Thread = NIL_RTTHREAD;
303 RTMemFree(pTimer);
304
305 return VINF_SUCCESS;
306}
307
308
309
310
311RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
312{
313 return 10000000; /* 10ms */
314}
315RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
316
317
318RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
319{
320 return VERR_NOT_SUPPORTED;
321}
322RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
323
324
325RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
326{
327 return VERR_NOT_SUPPORTED;
328}
329RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
330
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