VirtualBox

source: vbox/trunk/src/VBox/Runtime/generic/timerlr-generic.cpp@ 39875

最後變更 在這個檔案從39875是 39083,由 vboxsync 提交於 13 年 前

IPRT: -Wunused-parameter.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Id
檔案大小: 10.8 KB
 
1/* $Id: timerlr-generic.cpp 39083 2011-10-22 00:28:46Z vboxsync $ */
2/** @file
3 * IPRT - Low Resolution Timers, Generic.
4 *
5 * This code is more or less identical to timer-generic.cpp, so
6 * bugfixes goes into both files.
7 */
8
9/*
10 * Copyright (C) 2006-2008 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.alldomusa.eu.org. This file is free software;
14 * you can redistribute it and/or modify it under the terms of the GNU
15 * General Public License (GPL) as published by the Free Software
16 * Foundation, in version 2 as it comes in the "COPYING" file of the
17 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
18 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
19 *
20 * The contents of this file may alternatively be used under the terms
21 * of the Common Development and Distribution License Version 1.0
22 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
23 * VirtualBox OSE distribution, in which case the provisions of the
24 * CDDL are applicable instead of those of the GPL.
25 *
26 * You may elect to license modified versions of this file under the
27 * terms and conditions of either the GPL or the CDDL or both.
28 */
29
30
31/*******************************************************************************
32* Header Files *
33*******************************************************************************/
34#include <iprt/timer.h>
35#include "internal/iprt.h"
36
37#include <iprt/thread.h>
38#include <iprt/err.h>
39#include <iprt/assert.h>
40#include <iprt/alloc.h>
41#include <iprt/asm.h>
42#include <iprt/semaphore.h>
43#include <iprt/time.h>
44#include <iprt/log.h>
45#include "internal/magics.h"
46
47
48/*******************************************************************************
49* Structures and Typedefs *
50*******************************************************************************/
51/**
52 * The internal representation of a timer handle.
53 */
54typedef struct RTTIMERLRINT
55{
56 /** Magic.
57 * This is RTTIMERRT_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 timer is suspended. */
61 bool volatile fSuspended;
62 /** Flag indicating that the timer has been destroyed. */
63 bool volatile fDestroyed;
64 /** Callback. */
65 PFNRTTIMERLR pfnTimer;
66 /** User argument. */
67 void *pvUser;
68 /** The timer thread. */
69 RTTHREAD hThread;
70 /** Event semaphore on which the thread is blocked. */
71 RTSEMEVENT hEvent;
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} RTTIMERLRINT;
83typedef RTTIMERLRINT *PRTTIMERLRINT;
84
85
86/*******************************************************************************
87* Internal Functions *
88*******************************************************************************/
89static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThread, void *pvUser);
90
91
92RTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
93{
94 AssertPtr(phTimerLR);
95 *phTimerLR = NIL_RTTIMERLR;
96
97 /*
98 * We don't support the fancy MP features, nor intervals lower than 100 ms.
99 */
100 if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
101 return VERR_NOT_SUPPORTED;
102 if (u64NanoInterval && u64NanoInterval < 100*1000*1000)
103 return VERR_INVALID_PARAMETER;
104
105 /*
106 * Allocate and initialize the timer handle.
107 */
108 PRTTIMERLRINT pThis = (PRTTIMERLRINT)RTMemAlloc(sizeof(*pThis));
109 if (!pThis)
110 return VERR_NO_MEMORY;
111
112 pThis->u32Magic = RTTIMERLR_MAGIC;
113 pThis->fSuspended = true;
114 pThis->fDestroyed = false;
115 pThis->pfnTimer = pfnTimer;
116 pThis->pvUser = pvUser;
117 pThis->hThread = NIL_RTTHREAD;
118 pThis->hEvent = NIL_RTSEMEVENT;
119 pThis->u64NanoInterval = u64NanoInterval;
120 pThis->u64StartTS = 0;
121
122 int rc = RTSemEventCreate(&pThis->hEvent);
123 if (RT_SUCCESS(rc))
124 {
125 rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TIMER");
126 if (RT_SUCCESS(rc))
127 {
128 *phTimerLR = pThis;
129 return VINF_SUCCESS;
130 }
131
132 pThis->u32Magic = 0;
133 RTSemEventDestroy(pThis->hEvent);
134 pThis->hEvent = NIL_RTSEMEVENT;
135 }
136 RTMemFree(pThis);
137
138 return rc;
139}
140RT_EXPORT_SYMBOL(RTTimerLRCreateEx);
141
142
143RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR)
144{
145 /*
146 * Validate input, NIL is fine though.
147 */
148 if (hTimerLR == NIL_RTTIMERLR)
149 return VINF_SUCCESS;
150 PRTTIMERLRINT pThis = hTimerLR;
151 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
152 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
153 AssertReturn(!pThis->fDestroyed, 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 hThread = pThis->hThread;
160 if (!pThis->fSuspended)
161 {
162 ASMAtomicWriteBool(&pThis->fSuspended, true);
163 ASMAtomicWriteBool(&pThis->fDestroyed, true);
164 }
165 else
166 {
167 ASMAtomicWriteBool(&pThis->fDestroyed, true);
168 int rc = RTSemEventSignal(pThis->hEvent);
169 if (rc == VERR_ALREADY_POSTED)
170 rc = VINF_SUCCESS;
171 AssertRC(rc);
172 }
173
174 RTThreadWait(hThread, 250, NULL);
175 return VINF_SUCCESS;
176}
177RT_EXPORT_SYMBOL(RTTimerLRDestroy);
178
179
180RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First)
181{
182 /*
183 * Validate input.
184 */
185 PRTTIMERLRINT pThis = hTimerLR;
186 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
187 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
188 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
189
190 if (u64First && u64First < 100*1000*1000)
191 return VERR_INVALID_PARAMETER;
192
193 if (!pThis->fSuspended)
194 return VERR_TIMER_ACTIVE;
195
196 /*
197 * Calc when it should start firing and give the thread a kick so it get going.
198 */
199 u64First += RTTimeNanoTS();
200 ASMAtomicWriteU64(&pThis->iTick, 0);
201 ASMAtomicWriteU64(&pThis->u64StartTS, u64First);
202 ASMAtomicWriteU64(&pThis->u64NextTS, u64First);
203 ASMAtomicWriteBool(&pThis->fSuspended, false);
204 int rc = RTSemEventSignal(pThis->hEvent);
205 if (rc == VERR_ALREADY_POSTED)
206 rc = VINF_SUCCESS;
207 AssertRC(rc);
208 return rc;
209}
210RT_EXPORT_SYMBOL(RTTimerLRStart);
211
212
213RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR)
214{
215 /*
216 * Validate input.
217 */
218 PRTTIMERLRINT pThis = hTimerLR;
219 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
220 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
221 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
222
223 if (pThis->fSuspended)
224 return VERR_TIMER_SUSPENDED;
225
226 /*
227 * Mark it as suspended and kick the thread.
228 */
229 ASMAtomicWriteBool(&pThis->fSuspended, true);
230 int rc = RTSemEventSignal(pThis->hEvent);
231 if (rc == VERR_ALREADY_POSTED)
232 rc = VINF_SUCCESS;
233 AssertRC(rc);
234 return rc;
235}
236RT_EXPORT_SYMBOL(RTTimerLRStop);
237
238
239static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThreadSelf, void *pvUser)
240{
241 PRTTIMERLRINT pThis = (PRTTIMERLRINT)pvUser;
242 NOREF(hThreadSelf);
243
244 /*
245 * The loop.
246 */
247 while (!ASMAtomicUoReadBool(&pThis->fDestroyed))
248 {
249 if (ASMAtomicUoReadBool(&pThis->fSuspended))
250 {
251 int rc = RTSemEventWait(pThis->hEvent, RT_INDEFINITE_WAIT);
252 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
253 {
254 AssertRC(rc);
255 RTThreadSleep(1000); /* Don't cause trouble! */
256 }
257 }
258 else
259 {
260 uint64_t cNanoSeconds;
261 const uint64_t u64NanoTS = RTTimeNanoTS();
262 if (u64NanoTS >= pThis->u64NextTS)
263 {
264 pThis->iTick++;
265 pThis->pfnTimer(pThis, pThis->pvUser, pThis->iTick);
266
267 /* status changed? */
268 if ( ASMAtomicUoReadBool(&pThis->fSuspended)
269 || ASMAtomicUoReadBool(&pThis->fDestroyed))
270 continue;
271
272 /* one shot? */
273 if (!pThis->u64NanoInterval)
274 {
275 ASMAtomicWriteBool(&pThis->fSuspended, true);
276 continue;
277 }
278
279 /*
280 * Calc the next time we should fire.
281 *
282 * If we're more than 60 intervals behind, just skip ahead. We
283 * don't want the timer thread running wild just because the
284 * clock changed in an unexpected way. As seen in #3611 this
285 * does happen during suspend/resume, but it may also happen
286 * if we're using a non-monotonic clock as time source.
287 */
288 pThis->u64NextTS = pThis->u64StartTS + pThis->iTick * pThis->u64NanoInterval;
289 if (RT_LIKELY(pThis->u64NextTS > u64NanoTS))
290 cNanoSeconds = pThis->u64NextTS - u64NanoTS;
291 else
292 {
293 uint64_t iActualTick = (u64NanoTS - pThis->u64StartTS) / pThis->u64NanoInterval;
294 if (iActualTick - pThis->iTick > 60)
295 pThis->iTick = iActualTick - 1;
296#ifdef IN_RING0
297 cNanoSeconds = RTTimerGetSystemGranularity() / 2;
298#else
299 cNanoSeconds = 1000000; /* 1ms */
300#endif
301 pThis->u64NextTS = u64NanoTS + cNanoSeconds;
302 }
303 }
304 else
305 cNanoSeconds = pThis->u64NextTS - u64NanoTS;
306
307 /* block. */
308 int rc = RTSemEventWait(pThis->hEvent,
309 (RTMSINTERVAL)(cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000));
310 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
311 {
312 AssertRC(rc);
313 RTThreadSleep(1000); /* Don't cause trouble! */
314 }
315 }
316 }
317
318 /*
319 * Release the timer resources.
320 */
321 ASMAtomicWriteU32(&pThis->u32Magic, ~RTTIMERLR_MAGIC); /* make the handle invalid. */
322 int rc = RTSemEventDestroy(pThis->hEvent); AssertRC(rc);
323 pThis->hEvent = NIL_RTSEMEVENT;
324 pThis->hThread = NIL_RTTHREAD;
325 RTMemFree(pThis);
326
327 return VINF_SUCCESS;
328}
329
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette