VirtualBox

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

Last change on this file since 60066 was 57358, checked in by vboxsync, 9 years ago

*: scm cleanup run.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 12.3 KB
Line 
1/* $Id: timerlr-generic.cpp 57358 2015-08-14 15:16:38Z 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-2015 Oracle Corporation
11 *
12 * This file is part of VirtualBox Open Source Edition (OSE), as
13 * available from http://www.virtualbox.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, "TimerLR");
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 stop and destruct it in one go, to avoid
157 * unnecessary waiting for the next tick. If it's suspended we can safely
158 * set the destroy flag and signal it.
159 */
160 RTTHREAD hThread = pThis->hThread;
161 if (!pThis->fSuspended)
162 ASMAtomicWriteBool(&pThis->fSuspended, true);
163 ASMAtomicWriteBool(&pThis->fDestroyed, true);
164 int rc = RTSemEventSignal(pThis->hEvent);
165 if (rc == VERR_ALREADY_POSTED)
166 rc = VINF_SUCCESS;
167 AssertRC(rc);
168
169 RTThreadWait(hThread, 250, NULL);
170 return VINF_SUCCESS;
171}
172RT_EXPORT_SYMBOL(RTTimerLRDestroy);
173
174
175RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First)
176{
177 /*
178 * Validate input.
179 */
180 PRTTIMERLRINT pThis = hTimerLR;
181 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
182 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
183 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
184
185 if (u64First && u64First < 100*1000*1000)
186 return VERR_INVALID_PARAMETER;
187
188 if (!pThis->fSuspended)
189 return VERR_TIMER_ACTIVE;
190
191 /*
192 * Calc when it should start firing and give the thread a kick so it get going.
193 */
194 u64First += RTTimeNanoTS();
195 ASMAtomicWriteU64(&pThis->iTick, 0);
196 ASMAtomicWriteU64(&pThis->u64StartTS, u64First);
197 ASMAtomicWriteU64(&pThis->u64NextTS, u64First);
198 ASMAtomicWriteBool(&pThis->fSuspended, false);
199 int rc = RTSemEventSignal(pThis->hEvent);
200 if (rc == VERR_ALREADY_POSTED)
201 rc = VINF_SUCCESS;
202 AssertRC(rc);
203 return rc;
204}
205RT_EXPORT_SYMBOL(RTTimerLRStart);
206
207
208RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR)
209{
210 /*
211 * Validate input.
212 */
213 PRTTIMERLRINT pThis = hTimerLR;
214 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
215 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
216 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
217
218 if (pThis->fSuspended)
219 return VERR_TIMER_SUSPENDED;
220
221 /*
222 * Mark it as suspended and kick the thread.
223 */
224 ASMAtomicWriteBool(&pThis->fSuspended, true);
225 int rc = RTSemEventSignal(pThis->hEvent);
226 if (rc == VERR_ALREADY_POSTED)
227 rc = VINF_SUCCESS;
228 AssertRC(rc);
229 return rc;
230}
231RT_EXPORT_SYMBOL(RTTimerLRStop);
232
233RTDECL(int) RTTimerLRChangeInterval(RTTIMERLR hTimerLR, uint64_t u64NanoInterval)
234{
235 PRTTIMERLRINT pThis = hTimerLR;
236 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
237 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
238 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
239
240 if (u64NanoInterval && u64NanoInterval < 100*1000*1000)
241 return VERR_INVALID_PARAMETER;
242
243#if 0
244 if (!pThis->fSuspended)
245 {
246 int rc = RTTimerLRStop(hTimerLR);
247 if (RT_FAILURE(rc))
248 return rc;
249
250 ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
251
252 rc = RTTimerLRStart(hTimerLR, 0);
253 if (RT_FAILURE(rc))
254 return rc;
255 }
256 else
257#endif
258 {
259 uint64_t u64Now = RTTimeNanoTS();
260 ASMAtomicWriteU64(&pThis->iTick, 0);
261 ASMAtomicWriteU64(&pThis->u64StartTS, u64Now);
262 ASMAtomicWriteU64(&pThis->u64NextTS, u64Now);
263 ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
264 int rc = RTSemEventSignal(pThis->hEvent);
265 }
266
267 return VINF_SUCCESS;
268}
269RT_EXPORT_SYMBOL(RTTimerLRChangeInterval);
270
271static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThreadSelf, void *pvUser)
272{
273 PRTTIMERLRINT pThis = (PRTTIMERLRINT)pvUser;
274 NOREF(hThreadSelf);
275
276 /*
277 * The loop.
278 */
279 while (!ASMAtomicUoReadBool(&pThis->fDestroyed))
280 {
281 if (ASMAtomicUoReadBool(&pThis->fSuspended))
282 {
283 int rc = RTSemEventWait(pThis->hEvent, RT_INDEFINITE_WAIT);
284 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
285 {
286 AssertRC(rc);
287 RTThreadSleep(1000); /* Don't cause trouble! */
288 }
289 }
290 else
291 {
292 uint64_t cNanoSeconds;
293 const uint64_t u64NanoTS = RTTimeNanoTS();
294 if (u64NanoTS >= pThis->u64NextTS)
295 {
296 pThis->iTick++;
297 pThis->pfnTimer(pThis, pThis->pvUser, pThis->iTick);
298
299 /* status changed? */
300 if ( ASMAtomicUoReadBool(&pThis->fSuspended)
301 || ASMAtomicUoReadBool(&pThis->fDestroyed))
302 continue;
303
304 /* one shot? */
305 if (!pThis->u64NanoInterval)
306 {
307 ASMAtomicWriteBool(&pThis->fSuspended, true);
308 continue;
309 }
310
311 /*
312 * Calc the next time we should fire.
313 *
314 * If we're more than 60 intervals behind, just skip ahead. We
315 * don't want the timer thread running wild just because the
316 * clock changed in an unexpected way. As seen in @bugref{3611} this
317 * does happen during suspend/resume, but it may also happen
318 * if we're using a non-monotonic clock as time source.
319 */
320 pThis->u64NextTS = pThis->u64StartTS + pThis->iTick * pThis->u64NanoInterval;
321 if (RT_LIKELY(pThis->u64NextTS > u64NanoTS))
322 cNanoSeconds = pThis->u64NextTS - u64NanoTS;
323 else
324 {
325 uint64_t iActualTick = (u64NanoTS - pThis->u64StartTS) / pThis->u64NanoInterval;
326 if (iActualTick - pThis->iTick > 60)
327 pThis->iTick = iActualTick - 1;
328#ifdef IN_RING0
329 cNanoSeconds = RTTimerGetSystemGranularity() / 2;
330#else
331 cNanoSeconds = 1000000; /* 1ms */
332#endif
333 pThis->u64NextTS = u64NanoTS + cNanoSeconds;
334 }
335 }
336 else
337 cNanoSeconds = pThis->u64NextTS - u64NanoTS;
338
339 /* block. */
340 int rc = RTSemEventWait(pThis->hEvent,
341 (RTMSINTERVAL)(cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000));
342 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
343 {
344 AssertRC(rc);
345 RTThreadSleep(1000); /* Don't cause trouble! */
346 }
347 }
348 }
349
350 /*
351 * Release the timer resources.
352 */
353 ASMAtomicWriteU32(&pThis->u32Magic, ~RTTIMERLR_MAGIC); /* make the handle invalid. */
354 int rc = RTSemEventDestroy(pThis->hEvent); AssertRC(rc);
355 pThis->hEvent = NIL_RTSEMEVENT;
356 pThis->hThread = NIL_RTTHREAD;
357 RTMemFree(pThis);
358
359 return VINF_SUCCESS;
360}
361
Note: See TracBrowser for help on using the repository browser.

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