VirtualBox

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

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

Automated rebranding to Oracle copyright/license strings via filemuncher

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.7 KB
Line 
1/* $Id: timer-generic.cpp 28800 2010-04-27 08:22:32Z 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, unsigned 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 fireing 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
229static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
230{
231 PRTTIMER pTimer = (PRTTIMER)pvUser;
232
233 /*
234 * The loop.
235 */
236 while (!pTimer->fDestroyed)
237 {
238 if (pTimer->fSuspended)
239 {
240 int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
241 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
242 {
243 AssertRC(rc);
244 RTThreadSleep(1000); /* Don't cause trouble! */
245 }
246 }
247 else
248 {
249 const uint64_t u64NanoTS = RTTimeNanoTS();
250 if (u64NanoTS >= pTimer->u64NextTS)
251 {
252 pTimer->iTick++;
253 pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
254
255 /* status changed? */
256 if (pTimer->fSuspended || pTimer->fDestroyed)
257 continue;
258
259 /* one shot? */
260 if (!pTimer->u64NanoInterval)
261 {
262 ASMAtomicXchgU8(&pTimer->fSuspended, true);
263 continue;
264 }
265
266 /* calc the next time we should fire. */
267 pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
268 if (pTimer->u64NextTS < u64NanoTS)
269#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
270 pTimer->u64NextTS = u64NanoTS + 1;
271#else
272 pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
273#endif
274 }
275
276 /* block. */
277 uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
278#ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
279 if (cNanoSeconds > 10)
280#endif
281 {
282 int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
283 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
284 {
285 AssertRC(rc);
286 RTThreadSleep(1000); /* Don't cause trouble! */
287 }
288 }
289 }
290 }
291
292 /*
293 * Release the timer resources.
294 */
295 ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
296 int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
297 pTimer->Event = NIL_RTSEMEVENT;
298 pTimer->Thread = NIL_RTTHREAD;
299 RTMemFree(pTimer);
300
301 return VINF_SUCCESS;
302}
303
304
305
306
307RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
308{
309 return 10000000; /* 10ms */
310}
311RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
312
313
314RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
315{
316 return VERR_NOT_SUPPORTED;
317}
318RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
319
320
321RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
322{
323 return VERR_NOT_SUPPORTED;
324}
325RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
326
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