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