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