VirtualBox

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

Last change on this file was 98103, checked in by vboxsync, 17 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 15.1 KB
RevLine 
[25641]1/* $Id: timerlr-generic.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
[4750]2/** @file
[10944]3 * IPRT - Low Resolution Timers, Generic.
4 *
[33540]5 * This code is more or less identical to timer-generic.cpp, so
[10944]6 * bugfixes goes into both files.
[4750]7 */
8
9/*
[98103]10 * Copyright (C) 2006-2023 Oracle and/or its affiliates.
[4071]11 *
[96407]12 * This file is part of VirtualBox base platform packages, as
13 * available from https://www.virtualbox.org.
[5999]14 *
[96407]15 * This program is free software; you can redistribute it and/or
16 * modify it under the terms of the GNU General Public License
17 * as published by the Free Software Foundation, in version 3 of the
18 * License.
19 *
20 * This program is distributed in the hope that it will be useful, but
21 * WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23 * General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, see <https://www.gnu.org/licenses>.
27 *
[5999]28 * The contents of this file may alternatively be used under the terms
29 * of the Common Development and Distribution License Version 1.0
[96407]30 * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
31 * in the VirtualBox distribution, in which case the provisions of the
[5999]32 * CDDL are applicable instead of those of the GPL.
33 *
34 * You may elect to license modified versions of this file under the
35 * terms and conditions of either the GPL or the CDDL or both.
[96407]36 *
37 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
[4750]38 */
39
40
[57358]41/*********************************************************************************************************************************
42* Header Files *
43*********************************************************************************************************************************/
[4750]44#include <iprt/timer.h>
[21337]45#include "internal/iprt.h"
46
[4750]47#include <iprt/thread.h>
48#include <iprt/err.h>
49#include <iprt/assert.h>
50#include <iprt/alloc.h>
51#include <iprt/asm.h>
52#include <iprt/semaphore.h>
53#include <iprt/time.h>
54#include <iprt/log.h>
55#include "internal/magics.h"
56
57
[57358]58/*********************************************************************************************************************************
[80665]59* Defined Constants And Macros *
60*********************************************************************************************************************************/
61/** The smallest interval for low resolution timers. */
62#define RTTIMERLR_MIN_INTERVAL RT_NS_100MS
63
64
65/*********************************************************************************************************************************
[57358]66* Structures and Typedefs *
67*********************************************************************************************************************************/
[4750]68/**
69 * The internal representation of a timer handle.
70 */
[10944]71typedef struct RTTIMERLRINT
[4750]72{
73 /** Magic.
[10944]74 * This is RTTIMERRT_MAGIC, but changes to something else before the timer
[4750]75 * is destroyed to indicate clearly that thread should exit. */
76 uint32_t volatile u32Magic;
[14298]77 /** Flag indicating the timer is suspended. */
[10944]78 bool volatile fSuspended;
[4750]79 /** Flag indicating that the timer has been destroyed. */
[10944]80 bool volatile fDestroyed;
[80665]81 /** Set when the thread is blocked. */
82 bool volatile fBlocked;
83 bool fPadding;
[4750]84 /** The timer interval. 0 if one-shot. */
[80665]85 uint64_t volatile u64NanoInterval;
[9308]86 /** The start of the current run (ns).
[4750]87 * This is used to calculate when the timer ought to fire the next time. */
88 uint64_t volatile u64StartTS;
[9308]89 /** The start of the current run (ns).
[4750]90 * This is used to calculate when the timer ought to fire the next time. */
91 uint64_t volatile u64NextTS;
92 /** The current tick number (since u64StartTS). */
93 uint64_t volatile iTick;
[80665]94
95 /** Callback. */
96 PFNRTTIMERLR pfnTimer;
97 /** User argument. */
98 void *pvUser;
99 /** The timer thread. */
100 RTTHREAD hThread;
101 /** Event semaphore on which the thread is blocked. */
102 RTSEMEVENT hEvent;
[10944]103} RTTIMERLRINT;
104typedef RTTIMERLRINT *PRTTIMERLRINT;
[4750]105
106
[57358]107/*********************************************************************************************************************************
108* Internal Functions *
109*********************************************************************************************************************************/
[10944]110static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThread, void *pvUser);
[4750]111
112
[10944]113RTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
[4750]114{
[10944]115 AssertPtr(phTimerLR);
116 *phTimerLR = NIL_RTTIMERLR;
[4750]117
118 /*
[10944]119 * We don't support the fancy MP features, nor intervals lower than 100 ms.
[9416]120 */
[80665]121 AssertReturn(!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC), VERR_NOT_SUPPORTED);
122 AssertReturn(!u64NanoInterval || u64NanoInterval >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
[9416]123
124 /*
[4750]125 * Allocate and initialize the timer handle.
126 */
[10944]127 PRTTIMERLRINT pThis = (PRTTIMERLRINT)RTMemAlloc(sizeof(*pThis));
128 if (!pThis)
[4750]129 return VERR_NO_MEMORY;
130
[10944]131 pThis->u32Magic = RTTIMERLR_MAGIC;
132 pThis->fSuspended = true;
133 pThis->fDestroyed = false;
[80665]134 pThis->fBlocked = false;
135 pThis->fPadding = false;
[10944]136 pThis->pfnTimer = pfnTimer;
137 pThis->pvUser = pvUser;
138 pThis->hThread = NIL_RTTHREAD;
139 pThis->hEvent = NIL_RTSEMEVENT;
140 pThis->u64NanoInterval = u64NanoInterval;
141 pThis->u64StartTS = 0;
[4750]142
[10944]143 int rc = RTSemEventCreate(&pThis->hEvent);
[4750]144 if (RT_SUCCESS(rc))
145 {
[43533]146 rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TimerLR");
[4750]147 if (RT_SUCCESS(rc))
148 {
[10944]149 *phTimerLR = pThis;
[4750]150 return VINF_SUCCESS;
151 }
152
[10944]153 pThis->u32Magic = 0;
154 RTSemEventDestroy(pThis->hEvent);
155 pThis->hEvent = NIL_RTSEMEVENT;
[4750]156 }
[10944]157 RTMemFree(pThis);
[4750]158
159 return rc;
160}
[21337]161RT_EXPORT_SYMBOL(RTTimerLRCreateEx);
[4750]162
163
[10944]164RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR)
[4750]165{
[10944]166 /*
167 * Validate input, NIL is fine though.
168 */
169 if (hTimerLR == NIL_RTTIMERLR)
[4750]170 return VINF_SUCCESS;
[10944]171 PRTTIMERLRINT pThis = hTimerLR;
172 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
173 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
174 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
[4750]175
176 /*
[42235]177 * If the timer is active, we stop and destruct it in one go, to avoid
178 * unnecessary waiting for the next tick. If it's suspended we can safely
179 * set the destroy flag and signal it.
[4750]180 */
[10944]181 RTTHREAD hThread = pThis->hThread;
182 if (!pThis->fSuspended)
183 ASMAtomicWriteBool(&pThis->fSuspended, true);
[42235]184 ASMAtomicWriteBool(&pThis->fDestroyed, true);
185 int rc = RTSemEventSignal(pThis->hEvent);
186 if (rc == VERR_ALREADY_POSTED)
187 rc = VINF_SUCCESS;
188 AssertRC(rc);
[4750]189
[42235]190 RTThreadWait(hThread, 250, NULL);
[4750]191 return VINF_SUCCESS;
192}
[21337]193RT_EXPORT_SYMBOL(RTTimerLRDestroy);
[4750]194
195
[80665]196/**
197 * Internal worker fro RTTimerLRStart and RTTiemrLRChangeInterval.
198 */
199static int rtTimerLRStart(PRTTIMERLRINT pThis, uint64_t u64First)
[4750]200{
[10944]201 if (!pThis->fSuspended)
[4750]202 return VERR_TIMER_ACTIVE;
203
204 /*
[23619]205 * Calc when it should start firing and give the thread a kick so it get going.
[4750]206 */
207 u64First += RTTimeNanoTS();
[10944]208 ASMAtomicWriteU64(&pThis->iTick, 0);
209 ASMAtomicWriteU64(&pThis->u64StartTS, u64First);
210 ASMAtomicWriteU64(&pThis->u64NextTS, u64First);
211 ASMAtomicWriteBool(&pThis->fSuspended, false);
212 int rc = RTSemEventSignal(pThis->hEvent);
[4750]213 if (rc == VERR_ALREADY_POSTED)
214 rc = VINF_SUCCESS;
215 AssertRC(rc);
216 return rc;
217}
218
219
[80665]220RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First)
[4750]221{
[10944]222 /*
223 * Validate input.
224 */
225 PRTTIMERLRINT pThis = hTimerLR;
226 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
227 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
228 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
[80665]229 AssertReturn(!u64First || u64First >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
[10944]230
[80665]231 /*
232 * Do the job.
233 */
234 return rtTimerLRStart(pThis, u64First);
235}
236RT_EXPORT_SYMBOL(RTTimerLRStart);
237
238
239/**
240 * Internal worker for RTTimerLRStop and RTTimerLRChangeInterval
241 */
242static int rtTimerLRStop(PRTTIMERLRINT pThis, bool fSynchronous)
243{
244 /*
245 * Fail if already suspended.
246 */
[10944]247 if (pThis->fSuspended)
[4750]248 return VERR_TIMER_SUSPENDED;
249
250 /*
251 * Mark it as suspended and kick the thread.
[80665]252 * It's simpler to always reset the thread user semaphore, so we do that first.
[4750]253 */
[80665]254 int rc = RTThreadUserReset(pThis->hThread);
255 AssertRC(rc);
256
[10944]257 ASMAtomicWriteBool(&pThis->fSuspended, true);
[80665]258 rc = RTSemEventSignal(pThis->hEvent);
[4750]259 if (rc == VERR_ALREADY_POSTED)
260 rc = VINF_SUCCESS;
261 AssertRC(rc);
[80665]262
263 /*
264 * Wait for the thread to stop running if synchronous.
265 */
266 if (fSynchronous && RT_SUCCESS(rc))
267 {
268 rc = RTThreadUserWait(pThis->hThread, RT_MS_1MIN);
269 AssertRC(rc);
270 }
271
[4750]272 return rc;
273}
[80665]274
275
276RTDECL(int) RTTimerLRStop(RTTIMERLR hTimerLR)
277{
278 /*
279 * Validate input.
280 */
281 PRTTIMERLRINT pThis = hTimerLR;
282 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
283 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
284 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
285
286 /*
287 * Do the job.
288 */
289 return rtTimerLRStop(pThis, false);
290}
[21337]291RT_EXPORT_SYMBOL(RTTimerLRStop);
[4750]292
[80665]293
[39910]294RTDECL(int) RTTimerLRChangeInterval(RTTIMERLR hTimerLR, uint64_t u64NanoInterval)
295{
[80665]296 /*
297 * Validate input.
298 */
[39910]299 PRTTIMERLRINT pThis = hTimerLR;
300 AssertPtrReturn(pThis, VERR_INVALID_HANDLE);
301 AssertReturn(pThis->u32Magic == RTTIMERLR_MAGIC, VERR_INVALID_HANDLE);
302 AssertReturn(!pThis->fDestroyed, VERR_INVALID_HANDLE);
[80665]303 AssertReturn(!u64NanoInterval || u64NanoInterval >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
[4750]304
[80665]305 /*
306 * Do the job accoring to state and caller.
307 */
308 int rc;
309 if (pThis->fSuspended)
[39910]310 {
[80665]311 /* Stopped: Just update the interval. */
[39910]312 ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
[80665]313 rc = VINF_SUCCESS;
[39910]314 }
[80665]315 else if (RTThreadSelf() == pThis->hThread)
[39910]316 {
[80665]317 /* Running: Updating interval from the callback. */
[39910]318 uint64_t u64Now = RTTimeNanoTS();
[80665]319 pThis->iTick = 0;
320 pThis->u64StartTS = u64Now;
321 pThis->u64NextTS = u64Now;
[39910]322 ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
[80665]323 rc = VINF_SUCCESS;
[39910]324 }
[80665]325 else
326 {
327 /* Running: Stopping */
328 rc = rtTimerLRStop(pThis, true);
329 if (RT_SUCCESS(rc))
330 {
331 ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
332 rc = rtTimerLRStart(pThis, 0);
333 }
334 }
[39910]335
[80665]336 return rc;
[39910]337}
338RT_EXPORT_SYMBOL(RTTimerLRChangeInterval);
339
[80665]340
[39083]341static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThreadSelf, void *pvUser)
[4750]342{
[10944]343 PRTTIMERLRINT pThis = (PRTTIMERLRINT)pvUser;
[39083]344 NOREF(hThreadSelf);
[4750]345
346 /*
347 * The loop.
348 */
[10944]349 while (!ASMAtomicUoReadBool(&pThis->fDestroyed))
[4750]350 {
[10944]351 if (ASMAtomicUoReadBool(&pThis->fSuspended))
[4750]352 {
[80665]353 /* Signal rtTimerLRStop thread. */
354 int rc = RTThreadUserSignal(hThreadSelf);
355 AssertRC(rc);
356
357 ASMAtomicWriteBool(&pThis->fBlocked, true);
358 rc = RTSemEventWait(pThis->hEvent, RT_INDEFINITE_WAIT);
[4750]359 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
360 {
361 AssertRC(rc);
362 RTThreadSleep(1000); /* Don't cause trouble! */
363 }
[80665]364 ASMAtomicWriteBool(&pThis->fBlocked, false);
[4750]365 }
366 else
367 {
[21399]368 uint64_t cNanoSeconds;
369 const uint64_t u64NanoTS = RTTimeNanoTS();
[80665]370 uint64_t u64NextTS = pThis->u64NextTS;
371 if (u64NanoTS >= u64NextTS)
[4750]372 {
[80665]373 uint64_t iTick = ++pThis->iTick;
374 pThis->pfnTimer(pThis, pThis->pvUser, iTick);
[4750]375
376 /* status changed? */
[80665]377 if ( ASMAtomicUoReadBool(&pThis->fSuspended)
378 || ASMAtomicUoReadBool(&pThis->fDestroyed))
[4750]379 continue;
380
[80665]381 /*
382 * Read timer data (it's all volatile and better if we read it all at once):
383 */
384 iTick = pThis->iTick;
385 uint64_t const u64StartTS = pThis->u64StartTS;
386 uint64_t const u64NanoInterval = pThis->u64NanoInterval;
387 ASMCompilerBarrier();
388
389 /*
390 * Suspend if one shot.
391 */
392 if (!u64NanoInterval)
[4750]393 {
[10944]394 ASMAtomicWriteBool(&pThis->fSuspended, true);
[4750]395 continue;
396 }
397
[21399]398 /*
399 * Calc the next time we should fire.
400 *
401 * If we're more than 60 intervals behind, just skip ahead. We
402 * don't want the timer thread running wild just because the
[41774]403 * clock changed in an unexpected way. As seen in @bugref{3611} this
[21399]404 * does happen during suspend/resume, but it may also happen
405 * if we're using a non-monotonic clock as time source.
406 */
[80665]407 u64NextTS = u64StartTS + iTick * u64NanoInterval;
408 if (RT_LIKELY(u64NextTS > u64NanoTS))
409 cNanoSeconds = u64NextTS - u64NanoTS;
[21399]410 else
411 {
[80665]412 uint64_t iActualTick = (u64NanoTS - u64StartTS) / u64NanoInterval;
413 if (iActualTick - iTick > 60)
[21399]414 pThis->iTick = iActualTick - 1;
415#ifdef IN_RING0
416 cNanoSeconds = RTTimerGetSystemGranularity() / 2;
[4750]417#else
[80665]418 cNanoSeconds = RT_NS_1MS;
[9308]419#endif
[80665]420 u64NextTS = u64NanoTS + cNanoSeconds;
[21399]421 }
[80665]422
423 pThis->u64NextTS = u64NextTS;
[4750]424 }
[21399]425 else
[80665]426 cNanoSeconds = u64NextTS - u64NanoTS;
[4750]427
428 /* block. */
[80665]429 ASMAtomicWriteBool(&pThis->fBlocked, true);
[34507]430 int rc = RTSemEventWait(pThis->hEvent,
431 (RTMSINTERVAL)(cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000));
[21399]432 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
[4750]433 {
[21399]434 AssertRC(rc);
435 RTThreadSleep(1000); /* Don't cause trouble! */
[4750]436 }
[80665]437 ASMAtomicWriteBool(&pThis->fBlocked, false);
[4750]438 }
439 }
440
441 /*
442 * Release the timer resources.
443 */
[10944]444 ASMAtomicWriteU32(&pThis->u32Magic, ~RTTIMERLR_MAGIC); /* make the handle invalid. */
445 int rc = RTSemEventDestroy(pThis->hEvent); AssertRC(rc);
446 pThis->hEvent = NIL_RTSEMEVENT;
447 pThis->hThread = NIL_RTTHREAD;
448 RTMemFree(pThis);
[4750]449
450 return VINF_SUCCESS;
451}
452
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use