1 | /* $Id: timerlr-generic.cpp 98103 2023-01-17 14:15: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-2023 Oracle and/or its affiliates.
|
---|
11 | *
|
---|
12 | * This file is part of VirtualBox base platform packages, as
|
---|
13 | * available from https://www.virtualbox.org.
|
---|
14 | *
|
---|
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 | *
|
---|
28 | * The contents of this file may alternatively be used under the terms
|
---|
29 | * of the Common Development and Distribution License Version 1.0
|
---|
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
|
---|
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.
|
---|
36 | *
|
---|
37 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
38 | */
|
---|
39 |
|
---|
40 |
|
---|
41 | /*********************************************************************************************************************************
|
---|
42 | * Header Files *
|
---|
43 | *********************************************************************************************************************************/
|
---|
44 | #include <iprt/timer.h>
|
---|
45 | #include "internal/iprt.h"
|
---|
46 |
|
---|
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 |
|
---|
58 | /*********************************************************************************************************************************
|
---|
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 | /*********************************************************************************************************************************
|
---|
66 | * Structures and Typedefs *
|
---|
67 | *********************************************************************************************************************************/
|
---|
68 | /**
|
---|
69 | * The internal representation of a timer handle.
|
---|
70 | */
|
---|
71 | typedef struct RTTIMERLRINT
|
---|
72 | {
|
---|
73 | /** Magic.
|
---|
74 | * This is RTTIMERRT_MAGIC, but changes to something else before the timer
|
---|
75 | * is destroyed to indicate clearly that thread should exit. */
|
---|
76 | uint32_t volatile u32Magic;
|
---|
77 | /** Flag indicating the timer is suspended. */
|
---|
78 | bool volatile fSuspended;
|
---|
79 | /** Flag indicating that the timer has been destroyed. */
|
---|
80 | bool volatile fDestroyed;
|
---|
81 | /** Set when the thread is blocked. */
|
---|
82 | bool volatile fBlocked;
|
---|
83 | bool fPadding;
|
---|
84 | /** The timer interval. 0 if one-shot. */
|
---|
85 | uint64_t volatile u64NanoInterval;
|
---|
86 | /** The start of the current run (ns).
|
---|
87 | * This is used to calculate when the timer ought to fire the next time. */
|
---|
88 | uint64_t volatile u64StartTS;
|
---|
89 | /** The start of the current run (ns).
|
---|
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;
|
---|
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;
|
---|
103 | } RTTIMERLRINT;
|
---|
104 | typedef RTTIMERLRINT *PRTTIMERLRINT;
|
---|
105 |
|
---|
106 |
|
---|
107 | /*********************************************************************************************************************************
|
---|
108 | * Internal Functions *
|
---|
109 | *********************************************************************************************************************************/
|
---|
110 | static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThread, void *pvUser);
|
---|
111 |
|
---|
112 |
|
---|
113 | RTDECL(int) RTTimerLRCreateEx(RTTIMERLR *phTimerLR, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMERLR pfnTimer, void *pvUser)
|
---|
114 | {
|
---|
115 | AssertPtr(phTimerLR);
|
---|
116 | *phTimerLR = NIL_RTTIMERLR;
|
---|
117 |
|
---|
118 | /*
|
---|
119 | * We don't support the fancy MP features, nor intervals lower than 100 ms.
|
---|
120 | */
|
---|
121 | AssertReturn(!(fFlags & RTTIMER_FLAGS_CPU_SPECIFIC), VERR_NOT_SUPPORTED);
|
---|
122 | AssertReturn(!u64NanoInterval || u64NanoInterval >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
|
---|
123 |
|
---|
124 | /*
|
---|
125 | * Allocate and initialize the timer handle.
|
---|
126 | */
|
---|
127 | PRTTIMERLRINT pThis = (PRTTIMERLRINT)RTMemAlloc(sizeof(*pThis));
|
---|
128 | if (!pThis)
|
---|
129 | return VERR_NO_MEMORY;
|
---|
130 |
|
---|
131 | pThis->u32Magic = RTTIMERLR_MAGIC;
|
---|
132 | pThis->fSuspended = true;
|
---|
133 | pThis->fDestroyed = false;
|
---|
134 | pThis->fBlocked = false;
|
---|
135 | pThis->fPadding = false;
|
---|
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;
|
---|
142 |
|
---|
143 | int rc = RTSemEventCreate(&pThis->hEvent);
|
---|
144 | if (RT_SUCCESS(rc))
|
---|
145 | {
|
---|
146 | rc = RTThreadCreate(&pThis->hThread, rtTimerLRThread, pThis, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "TimerLR");
|
---|
147 | if (RT_SUCCESS(rc))
|
---|
148 | {
|
---|
149 | *phTimerLR = pThis;
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | }
|
---|
152 |
|
---|
153 | pThis->u32Magic = 0;
|
---|
154 | RTSemEventDestroy(pThis->hEvent);
|
---|
155 | pThis->hEvent = NIL_RTSEMEVENT;
|
---|
156 | }
|
---|
157 | RTMemFree(pThis);
|
---|
158 |
|
---|
159 | return rc;
|
---|
160 | }
|
---|
161 | RT_EXPORT_SYMBOL(RTTimerLRCreateEx);
|
---|
162 |
|
---|
163 |
|
---|
164 | RTDECL(int) RTTimerLRDestroy(RTTIMERLR hTimerLR)
|
---|
165 | {
|
---|
166 | /*
|
---|
167 | * Validate input, NIL is fine though.
|
---|
168 | */
|
---|
169 | if (hTimerLR == NIL_RTTIMERLR)
|
---|
170 | return VINF_SUCCESS;
|
---|
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);
|
---|
175 |
|
---|
176 | /*
|
---|
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.
|
---|
180 | */
|
---|
181 | RTTHREAD hThread = pThis->hThread;
|
---|
182 | if (!pThis->fSuspended)
|
---|
183 | ASMAtomicWriteBool(&pThis->fSuspended, true);
|
---|
184 | ASMAtomicWriteBool(&pThis->fDestroyed, true);
|
---|
185 | int rc = RTSemEventSignal(pThis->hEvent);
|
---|
186 | if (rc == VERR_ALREADY_POSTED)
|
---|
187 | rc = VINF_SUCCESS;
|
---|
188 | AssertRC(rc);
|
---|
189 |
|
---|
190 | RTThreadWait(hThread, 250, NULL);
|
---|
191 | return VINF_SUCCESS;
|
---|
192 | }
|
---|
193 | RT_EXPORT_SYMBOL(RTTimerLRDestroy);
|
---|
194 |
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Internal worker fro RTTimerLRStart and RTTiemrLRChangeInterval.
|
---|
198 | */
|
---|
199 | static int rtTimerLRStart(PRTTIMERLRINT pThis, uint64_t u64First)
|
---|
200 | {
|
---|
201 | if (!pThis->fSuspended)
|
---|
202 | return VERR_TIMER_ACTIVE;
|
---|
203 |
|
---|
204 | /*
|
---|
205 | * Calc when it should start firing and give the thread a kick so it get going.
|
---|
206 | */
|
---|
207 | u64First += RTTimeNanoTS();
|
---|
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);
|
---|
213 | if (rc == VERR_ALREADY_POSTED)
|
---|
214 | rc = VINF_SUCCESS;
|
---|
215 | AssertRC(rc);
|
---|
216 | return rc;
|
---|
217 | }
|
---|
218 |
|
---|
219 |
|
---|
220 | RTDECL(int) RTTimerLRStart(RTTIMERLR hTimerLR, uint64_t u64First)
|
---|
221 | {
|
---|
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);
|
---|
229 | AssertReturn(!u64First || u64First >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
|
---|
230 |
|
---|
231 | /*
|
---|
232 | * Do the job.
|
---|
233 | */
|
---|
234 | return rtTimerLRStart(pThis, u64First);
|
---|
235 | }
|
---|
236 | RT_EXPORT_SYMBOL(RTTimerLRStart);
|
---|
237 |
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Internal worker for RTTimerLRStop and RTTimerLRChangeInterval
|
---|
241 | */
|
---|
242 | static int rtTimerLRStop(PRTTIMERLRINT pThis, bool fSynchronous)
|
---|
243 | {
|
---|
244 | /*
|
---|
245 | * Fail if already suspended.
|
---|
246 | */
|
---|
247 | if (pThis->fSuspended)
|
---|
248 | return VERR_TIMER_SUSPENDED;
|
---|
249 |
|
---|
250 | /*
|
---|
251 | * Mark it as suspended and kick the thread.
|
---|
252 | * It's simpler to always reset the thread user semaphore, so we do that first.
|
---|
253 | */
|
---|
254 | int rc = RTThreadUserReset(pThis->hThread);
|
---|
255 | AssertRC(rc);
|
---|
256 |
|
---|
257 | ASMAtomicWriteBool(&pThis->fSuspended, true);
|
---|
258 | rc = RTSemEventSignal(pThis->hEvent);
|
---|
259 | if (rc == VERR_ALREADY_POSTED)
|
---|
260 | rc = VINF_SUCCESS;
|
---|
261 | AssertRC(rc);
|
---|
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 |
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | RTDECL(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 | }
|
---|
291 | RT_EXPORT_SYMBOL(RTTimerLRStop);
|
---|
292 |
|
---|
293 |
|
---|
294 | RTDECL(int) RTTimerLRChangeInterval(RTTIMERLR hTimerLR, uint64_t u64NanoInterval)
|
---|
295 | {
|
---|
296 | /*
|
---|
297 | * Validate input.
|
---|
298 | */
|
---|
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);
|
---|
303 | AssertReturn(!u64NanoInterval || u64NanoInterval >= RTTIMERLR_MIN_INTERVAL, VERR_OUT_OF_RANGE);
|
---|
304 |
|
---|
305 | /*
|
---|
306 | * Do the job accoring to state and caller.
|
---|
307 | */
|
---|
308 | int rc;
|
---|
309 | if (pThis->fSuspended)
|
---|
310 | {
|
---|
311 | /* Stopped: Just update the interval. */
|
---|
312 | ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
|
---|
313 | rc = VINF_SUCCESS;
|
---|
314 | }
|
---|
315 | else if (RTThreadSelf() == pThis->hThread)
|
---|
316 | {
|
---|
317 | /* Running: Updating interval from the callback. */
|
---|
318 | uint64_t u64Now = RTTimeNanoTS();
|
---|
319 | pThis->iTick = 0;
|
---|
320 | pThis->u64StartTS = u64Now;
|
---|
321 | pThis->u64NextTS = u64Now;
|
---|
322 | ASMAtomicWriteU64(&pThis->u64NanoInterval, u64NanoInterval);
|
---|
323 | rc = VINF_SUCCESS;
|
---|
324 | }
|
---|
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 | }
|
---|
335 |
|
---|
336 | return rc;
|
---|
337 | }
|
---|
338 | RT_EXPORT_SYMBOL(RTTimerLRChangeInterval);
|
---|
339 |
|
---|
340 |
|
---|
341 | static DECLCALLBACK(int) rtTimerLRThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
342 | {
|
---|
343 | PRTTIMERLRINT pThis = (PRTTIMERLRINT)pvUser;
|
---|
344 | NOREF(hThreadSelf);
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * The loop.
|
---|
348 | */
|
---|
349 | while (!ASMAtomicUoReadBool(&pThis->fDestroyed))
|
---|
350 | {
|
---|
351 | if (ASMAtomicUoReadBool(&pThis->fSuspended))
|
---|
352 | {
|
---|
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);
|
---|
359 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
|
---|
360 | {
|
---|
361 | AssertRC(rc);
|
---|
362 | RTThreadSleep(1000); /* Don't cause trouble! */
|
---|
363 | }
|
---|
364 | ASMAtomicWriteBool(&pThis->fBlocked, false);
|
---|
365 | }
|
---|
366 | else
|
---|
367 | {
|
---|
368 | uint64_t cNanoSeconds;
|
---|
369 | const uint64_t u64NanoTS = RTTimeNanoTS();
|
---|
370 | uint64_t u64NextTS = pThis->u64NextTS;
|
---|
371 | if (u64NanoTS >= u64NextTS)
|
---|
372 | {
|
---|
373 | uint64_t iTick = ++pThis->iTick;
|
---|
374 | pThis->pfnTimer(pThis, pThis->pvUser, iTick);
|
---|
375 |
|
---|
376 | /* status changed? */
|
---|
377 | if ( ASMAtomicUoReadBool(&pThis->fSuspended)
|
---|
378 | || ASMAtomicUoReadBool(&pThis->fDestroyed))
|
---|
379 | continue;
|
---|
380 |
|
---|
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)
|
---|
393 | {
|
---|
394 | ASMAtomicWriteBool(&pThis->fSuspended, true);
|
---|
395 | continue;
|
---|
396 | }
|
---|
397 |
|
---|
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
|
---|
403 | * clock changed in an unexpected way. As seen in @bugref{3611} this
|
---|
404 | * does happen during suspend/resume, but it may also happen
|
---|
405 | * if we're using a non-monotonic clock as time source.
|
---|
406 | */
|
---|
407 | u64NextTS = u64StartTS + iTick * u64NanoInterval;
|
---|
408 | if (RT_LIKELY(u64NextTS > u64NanoTS))
|
---|
409 | cNanoSeconds = u64NextTS - u64NanoTS;
|
---|
410 | else
|
---|
411 | {
|
---|
412 | uint64_t iActualTick = (u64NanoTS - u64StartTS) / u64NanoInterval;
|
---|
413 | if (iActualTick - iTick > 60)
|
---|
414 | pThis->iTick = iActualTick - 1;
|
---|
415 | #ifdef IN_RING0
|
---|
416 | cNanoSeconds = RTTimerGetSystemGranularity() / 2;
|
---|
417 | #else
|
---|
418 | cNanoSeconds = RT_NS_1MS;
|
---|
419 | #endif
|
---|
420 | u64NextTS = u64NanoTS + cNanoSeconds;
|
---|
421 | }
|
---|
422 |
|
---|
423 | pThis->u64NextTS = u64NextTS;
|
---|
424 | }
|
---|
425 | else
|
---|
426 | cNanoSeconds = u64NextTS - u64NanoTS;
|
---|
427 |
|
---|
428 | /* block. */
|
---|
429 | ASMAtomicWriteBool(&pThis->fBlocked, true);
|
---|
430 | int rc = RTSemEventWait(pThis->hEvent,
|
---|
431 | (RTMSINTERVAL)(cNanoSeconds < 1000000 ? 1 : cNanoSeconds / 1000000));
|
---|
432 | if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED && rc != VERR_TIMEOUT)
|
---|
433 | {
|
---|
434 | AssertRC(rc);
|
---|
435 | RTThreadSleep(1000); /* Don't cause trouble! */
|
---|
436 | }
|
---|
437 | ASMAtomicWriteBool(&pThis->fBlocked, false);
|
---|
438 | }
|
---|
439 | }
|
---|
440 |
|
---|
441 | /*
|
---|
442 | * Release the timer resources.
|
---|
443 | */
|
---|
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);
|
---|
449 |
|
---|
450 | return VINF_SUCCESS;
|
---|
451 | }
|
---|
452 |
|
---|