VirtualBox

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

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

Adopted the posix timer code to support the three new timer APIs

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