VirtualBox

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

Last change on this file since 39627 was 39083, checked in by vboxsync, 13 years ago

IPRT: -Wunused-parameter.

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