1 | /* $Id: timer-generic.cpp 33540 2010-10-28 09:27:05Z 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 | */
|
---|
52 | typedef 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 | *******************************************************************************/
|
---|
86 | static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser);
|
---|
87 |
|
---|
88 |
|
---|
89 | RTDECL(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 | }
|
---|
134 | RT_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 | */
|
---|
143 | DECLINLINE(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 |
|
---|
152 | RTDECL(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 | }
|
---|
182 | RT_EXPORT_SYMBOL(RTTimerDestroy);
|
---|
183 |
|
---|
184 |
|
---|
185 | RTDECL(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 | }
|
---|
206 | RT_EXPORT_SYMBOL(RTTimerStart);
|
---|
207 |
|
---|
208 |
|
---|
209 | RTDECL(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 | }
|
---|
226 | RT_EXPORT_SYMBOL(RTTimerStop);
|
---|
227 |
|
---|
228 |
|
---|
229 | RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
|
---|
230 | {
|
---|
231 | if (!rtTimerIsValid(pTimer))
|
---|
232 | return VERR_INVALID_HANDLE;
|
---|
233 | return VERR_NOT_SUPPORTED;
|
---|
234 | }
|
---|
235 | RT_EXPORT_SYMBOL(RTTimerChangeInterval);
|
---|
236 |
|
---|
237 |
|
---|
238 | static DECLCALLBACK(int) rtTimerThread(RTTHREAD Thread, void *pvUser)
|
---|
239 | {
|
---|
240 | PRTTIMER pTimer = (PRTTIMER)pvUser;
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * The loop.
|
---|
244 | */
|
---|
245 | while (!pTimer->fDestroyed)
|
---|
246 | {
|
---|
247 | if (pTimer->fSuspended)
|
---|
248 | {
|
---|
249 | int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
|
---|
250 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
|
---|
251 | {
|
---|
252 | AssertRC(rc);
|
---|
253 | RTThreadSleep(1000); /* Don't cause trouble! */
|
---|
254 | }
|
---|
255 | }
|
---|
256 | else
|
---|
257 | {
|
---|
258 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
259 | if (u64NanoTS >= pTimer->u64NextTS)
|
---|
260 | {
|
---|
261 | pTimer->iTick++;
|
---|
262 | pTimer->pfnTimer(pTimer, pTimer->pvUser, pTimer->iTick);
|
---|
263 |
|
---|
264 | /* status changed? */
|
---|
265 | if (pTimer->fSuspended || pTimer->fDestroyed)
|
---|
266 | continue;
|
---|
267 |
|
---|
268 | /* one shot? */
|
---|
269 | if (!pTimer->u64NanoInterval)
|
---|
270 | {
|
---|
271 | ASMAtomicXchgU8(&pTimer->fSuspended, true);
|
---|
272 | continue;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* calc the next time we should fire. */
|
---|
276 | pTimer->u64NextTS = pTimer->u64StartTS + pTimer->iTick * pTimer->u64NanoInterval;
|
---|
277 | if (pTimer->u64NextTS < u64NanoTS)
|
---|
278 | #ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
|
---|
279 | pTimer->u64NextTS = u64NanoTS + 1;
|
---|
280 | #else
|
---|
281 | pTimer->u64NextTS = u64NanoTS + RTTimerGetSystemGranularity() / 2;
|
---|
282 | #endif
|
---|
283 | }
|
---|
284 |
|
---|
285 | /* block. */
|
---|
286 | uint64_t cNanoSeconds = pTimer->u64NextTS - u64NanoTS;
|
---|
287 | #ifdef IN_RING3 /* In ring-3 we'll catch up lost ticks immediately. */
|
---|
288 | if (cNanoSeconds > 10)
|
---|
289 | #endif
|
---|
290 | {
|
---|
291 | int rc = RTSemEventWait(pTimer->Event, cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000);
|
---|
292 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
|
---|
293 | {
|
---|
294 | AssertRC(rc);
|
---|
295 | RTThreadSleep(1000); /* Don't cause trouble! */
|
---|
296 | }
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|
300 |
|
---|
301 | /*
|
---|
302 | * Release the timer resources.
|
---|
303 | */
|
---|
304 | ASMAtomicIncU32(&pTimer->u32Magic); /* make the handle invalid. */
|
---|
305 | int rc = RTSemEventDestroy(pTimer->Event); AssertRC(rc);
|
---|
306 | pTimer->Event = NIL_RTSEMEVENT;
|
---|
307 | pTimer->Thread = NIL_RTTHREAD;
|
---|
308 | RTMemFree(pTimer);
|
---|
309 |
|
---|
310 | return VINF_SUCCESS;
|
---|
311 | }
|
---|
312 |
|
---|
313 |
|
---|
314 |
|
---|
315 |
|
---|
316 | RTDECL(uint32_t) RTTimerGetSystemGranularity(void)
|
---|
317 | {
|
---|
318 | return 10000000; /* 10ms */
|
---|
319 | }
|
---|
320 | RT_EXPORT_SYMBOL(RTTimerGetSystemGranularity);
|
---|
321 |
|
---|
322 |
|
---|
323 | RTDECL(int) RTTimerRequestSystemGranularity(uint32_t u32Request, uint32_t *pu32Granted)
|
---|
324 | {
|
---|
325 | return VERR_NOT_SUPPORTED;
|
---|
326 | }
|
---|
327 | RT_EXPORT_SYMBOL(RTTimerRequestSystemGranularity);
|
---|
328 |
|
---|
329 |
|
---|
330 | RTDECL(int) RTTimerReleaseSystemGranularity(uint32_t u32Granted)
|
---|
331 | {
|
---|
332 | return VERR_NOT_SUPPORTED;
|
---|
333 | }
|
---|
334 | RT_EXPORT_SYMBOL(RTTimerReleaseSystemGranularity);
|
---|
335 |
|
---|
336 |
|
---|
337 | RTDECL(bool) RTTimerCanDoHighResolution(void)
|
---|
338 | {
|
---|
339 | return false;
|
---|
340 | }
|
---|
341 | RT_EXPORT_SYMBOL(RTTimerCanDoHighResolution);
|
---|