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