VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/posix/timer-posix.cpp@ 96108

Last change on this file since 96108 was 95190, checked in by vboxsync, 3 years ago

IPRT/r3/darwin: Use pthread_sigmask instead of sigprocmask on darwin, as xnu is applying sigprocmask input to all threads of a process which isn't at all what we wish for.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 27.5 KB
Line 
1/* $Id: timer-posix.cpp 95190 2022-06-03 15:11:16Z vboxsync $ */
2/** @file
3 * IPRT - Timer, POSIX.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
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 (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Defined Constants And Macros *
30*********************************************************************************************************************************/
31/** Enables the use of POSIX RT timers. */
32#ifndef RT_OS_SOLARIS /* Solaris 10 doesn't have SIGEV_THREAD */
33# define IPRT_WITH_POSIX_TIMERS
34#endif /* !RT_OS_SOLARIS */
35
36/** @def RT_TIMER_SIGNAL
37 * The signal number that the timers use.
38 * We currently use SIGALRM for both setitimer and posix real time timers
39 * out of simplicity, but we might want change this later for the posix ones. */
40#ifdef IPRT_WITH_POSIX_TIMERS
41# define RT_TIMER_SIGNAL SIGALRM
42#else
43# define RT_TIMER_SIGNAL SIGALRM
44#endif
45
46
47/*********************************************************************************************************************************
48* Header Files *
49*********************************************************************************************************************************/
50#define LOG_GROUP RTLOGGROUP_TIMER
51#include <iprt/timer.h>
52#include <iprt/alloc.h>
53#include <iprt/assert.h>
54#include <iprt/thread.h>
55#include <iprt/log.h>
56#include <iprt/asm.h>
57#include <iprt/semaphore.h>
58#include <iprt/string.h>
59#include <iprt/once.h>
60#include <iprt/err.h>
61#include <iprt/initterm.h>
62#include <iprt/critsect.h>
63#include "internal/magics.h"
64
65#include <unistd.h>
66#include <sys/fcntl.h>
67#include <sys/ioctl.h>
68#ifdef RT_OS_LINUX
69# include <linux/rtc.h>
70#endif
71#include <sys/time.h>
72#include <signal.h>
73#include <errno.h>
74#include <pthread.h>
75#if defined(RT_OS_DARWIN)
76# define sigprocmask pthread_sigmask /* On xnu sigprocmask works on the process, not the calling thread as elsewhere. */
77#endif
78
79
80/*********************************************************************************************************************************
81* Global Variables *
82*********************************************************************************************************************************/
83#ifdef IPRT_WITH_POSIX_TIMERS
84/** Init the critsect on first call. */
85static RTONCE g_TimerOnce = RTONCE_INITIALIZER;
86/** Global critsect that serializes timer creation and destruction.
87 * This is lazily created on the first RTTimerCreateEx call and will not be
88 * freed up (I'm afraid). */
89static RTCRITSECT g_TimerCritSect;
90/**
91 * Global counter of RTTimer instances. The signal thread is
92 * started when it changes from 0 to 1. The signal thread
93 * terminates when it becomes 0 again.
94 */
95static uint32_t volatile g_cTimerInstances;
96/** The signal handling thread. */
97static RTTHREAD g_TimerThread;
98#endif /* IPRT_WITH_POSIX_TIMERS */
99
100
101/*********************************************************************************************************************************
102* Structures and Typedefs *
103*********************************************************************************************************************************/
104/**
105 * The internal representation of a timer handle.
106 */
107typedef struct RTTIMER
108{
109 /** Magic.
110 * This is RTTIMER_MAGIC, but changes to something else before the timer
111 * is destroyed to indicate clearly that thread should exit. */
112 uint32_t volatile u32Magic;
113 /** Flag indicating the timer is suspended. */
114 uint8_t volatile fSuspended;
115 /** Flag indicating that the timer has been destroyed. */
116 uint8_t volatile fDestroyed;
117#ifndef IPRT_WITH_POSIX_TIMERS /** @todo We have to take the signals on a dedicated timer thread as
118 * we (might) have code assuming that signals doesn't screw around
119 * on existing threads. (It would be sufficient to have one thread
120 * per signal of course since the signal will be masked while it's
121 * running, however, it may just cause more complications than its
122 * worth - sigwait/sigwaitinfo work atomically anyway...)
123 * Also, must block the signal in the thread main procedure too. */
124 /** The timer thread. */
125 RTTHREAD Thread;
126 /** Event semaphore on which the thread is blocked. */
127 RTSEMEVENT Event;
128#endif /* !IPRT_WITH_POSIX_TIMERS */
129 /** User argument. */
130 void *pvUser;
131 /** Callback. */
132 PFNRTTIMER pfnTimer;
133 /** The timer interval. 0 if one-shot. */
134 uint64_t u64NanoInterval;
135#ifndef IPRT_WITH_POSIX_TIMERS
136 /** The first shot interval. 0 if ASAP. */
137 uint64_t volatile u64NanoFirst;
138#endif /* !IPRT_WITH_POSIX_TIMERS */
139 /** The current timer tick. */
140 uint64_t volatile iTick;
141#ifndef IPRT_WITH_POSIX_TIMERS
142 /** The error/status of the timer.
143 * Initially -1, set to 0 when the timer have been successfully started, and
144 * to errno on failure in starting the timer. */
145 int volatile iError;
146#else /* IPRT_WITH_POSIX_TIMERS */
147 timer_t NativeTimer;
148#endif /* IPRT_WITH_POSIX_TIMERS */
149
150} RTTIMER;
151
152
153
154#ifdef IPRT_WITH_POSIX_TIMERS
155
156/**
157 * RTOnce callback that initializes the critical section.
158 *
159 * @returns RTCritSectInit return code.
160 * @param pvUser NULL, ignored.
161 *
162 */
163static DECLCALLBACK(int) rtTimerOnce(void *pvUser)
164{
165 NOREF(pvUser);
166 return RTCritSectInit(&g_TimerCritSect);
167}
168#endif
169
170
171/**
172 * Signal handler which ignore everything it gets.
173 *
174 * @param iSignal The signal number.
175 */
176static void rttimerSignalIgnore(int iSignal)
177{
178 //AssertBreakpoint();
179 NOREF(iSignal);
180}
181
182
183/**
184 * RT_TIMER_SIGNAL wait thread.
185 */
186static DECLCALLBACK(int) rttimerThread(RTTHREAD hThreadSelf, void *pvArg)
187{
188 NOREF(hThreadSelf); NOREF(pvArg);
189#ifndef IPRT_WITH_POSIX_TIMERS
190 PRTTIMER pTimer = (PRTTIMER)pvArg;
191 RTTIMER Timer = *pTimer;
192 Assert(pTimer->u32Magic == RTTIMER_MAGIC);
193#endif /* !IPRT_WITH_POSIX_TIMERS */
194
195 /*
196 * Install signal handler.
197 */
198 struct sigaction SigAct;
199 memset(&SigAct, 0, sizeof(SigAct));
200 SigAct.sa_flags = SA_RESTART;
201 sigemptyset(&SigAct.sa_mask);
202 SigAct.sa_handler = rttimerSignalIgnore;
203 if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
204 {
205 SigAct.sa_flags &= ~SA_RESTART;
206 if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
207 AssertMsgFailed(("sigaction failed, errno=%d\n", errno));
208 }
209
210 /*
211 * Mask most signals except those which might be used by the pthread implementation (linux).
212 */
213 sigset_t SigSet;
214 sigfillset(&SigSet);
215 sigdelset(&SigSet, SIGTERM);
216 sigdelset(&SigSet, SIGHUP);
217 sigdelset(&SigSet, SIGINT);
218 sigdelset(&SigSet, SIGABRT);
219 sigdelset(&SigSet, SIGKILL);
220#ifdef SIGRTMIN
221 for (int iSig = SIGRTMIN; iSig < SIGRTMAX; iSig++)
222 sigdelset(&SigSet, iSig);
223#endif
224 if (sigprocmask(SIG_SETMASK, &SigSet, NULL))
225 {
226#ifdef IPRT_WITH_POSIX_TIMERS
227 int rc = RTErrConvertFromErrno(errno);
228#else
229 int rc = pTimer->iError = RTErrConvertFromErrno(errno);
230#endif
231 AssertMsgFailed(("sigprocmask -> errno=%d\n", errno));
232 return rc;
233 }
234
235 /*
236 * The work loop.
237 */
238 RTThreadUserSignal(hThreadSelf);
239
240#ifndef IPRT_WITH_POSIX_TIMERS
241 while ( !pTimer->fDestroyed
242 && pTimer->u32Magic == RTTIMER_MAGIC)
243 {
244 /*
245 * Wait for a start or destroy event.
246 */
247 if (pTimer->fSuspended)
248 {
249 int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
250 if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
251 {
252 AssertRC(rc);
253 if (pTimer->fDestroyed)
254 continue;
255 RTThreadSleep(1000); /* Don't cause trouble! */
256 }
257 if ( pTimer->fSuspended
258 || pTimer->fDestroyed)
259 continue;
260 }
261
262 /*
263 * Start the timer.
264 *
265 * For some SunOS (/SysV?) threading compatibility Linux will only
266 * deliver the RT_TIMER_SIGNAL to the thread calling setitimer(). Therefore
267 * we have to call it here.
268 *
269 * It turns out this might not always be the case, see RT_TIMER_SIGNAL killing
270 * processes on RH 2.4.21.
271 */
272 struct itimerval TimerVal;
273 if (pTimer->u64NanoFirst)
274 {
275 uint64_t u64 = RT_MAX(1000, pTimer->u64NanoFirst);
276 TimerVal.it_value.tv_sec = u64 / 1000000000;
277 TimerVal.it_value.tv_usec = (u64 % 1000000000) / 1000;
278 }
279 else
280 {
281 TimerVal.it_value.tv_sec = 0;
282 TimerVal.it_value.tv_usec = 10;
283 }
284 if (pTimer->u64NanoInterval)
285 {
286 uint64_t u64 = RT_MAX(1000, pTimer->u64NanoInterval);
287 TimerVal.it_interval.tv_sec = u64 / 1000000000;
288 TimerVal.it_interval.tv_usec = (u64 % 1000000000) / 1000;
289 }
290 else
291 {
292 TimerVal.it_interval.tv_sec = 0;
293 TimerVal.it_interval.tv_usec = 0;
294 }
295
296 if (setitimer(ITIMER_REAL, &TimerVal, NULL))
297 {
298 ASMAtomicXchgU8(&pTimer->fSuspended, true);
299 pTimer->iError = RTErrConvertFromErrno(errno);
300 RTThreadUserSignal(hThreadSelf);
301 continue; /* back to suspended mode. */
302 }
303 pTimer->iError = 0;
304 RTThreadUserSignal(hThreadSelf);
305
306 /*
307 * Timer Service Loop.
308 */
309 sigemptyset(&SigSet);
310 sigaddset(&SigSet, RT_TIMER_SIGNAL);
311 do
312 {
313 siginfo_t SigInfo;
314 RT_ZERO(SigInfo);
315#ifdef RT_OS_DARWIN
316 if (RT_LIKELY(sigwait(&SigSet, &SigInfo.si_signo) >= 0))
317 {
318#else
319 if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
320 {
321 if (RT_LIKELY(SigInfo.si_signo == RT_TIMER_SIGNAL))
322#endif
323 {
324 if (RT_UNLIKELY( pTimer->fSuspended
325 || pTimer->fDestroyed
326 || pTimer->u32Magic != RTTIMER_MAGIC))
327 break;
328
329 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
330
331 /* auto suspend one-shot timers. */
332 if (RT_UNLIKELY(!pTimer->u64NanoInterval))
333 {
334 ASMAtomicWriteU8(&pTimer->fSuspended, true);
335 break;
336 }
337 }
338 }
339 else if (errno != EINTR)
340 AssertMsgFailed(("sigwaitinfo -> errno=%d\n", errno));
341 } while (RT_LIKELY( !pTimer->fSuspended
342 && !pTimer->fDestroyed
343 && pTimer->u32Magic == RTTIMER_MAGIC));
344
345 /*
346 * Disable the timer.
347 */
348 struct itimerval TimerVal2 = {{0,0}, {0,0}};
349 if (setitimer(ITIMER_REAL, &TimerVal2, NULL))
350 AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
351
352 /*
353 * ACK any pending suspend request.
354 */
355 if (!pTimer->fDestroyed)
356 {
357 pTimer->iError = 0;
358 RTThreadUserSignal(hThreadSelf);
359 }
360 }
361
362 /*
363 * Exit.
364 */
365 pTimer->iError = 0;
366 RTThreadUserSignal(hThreadSelf);
367
368#else /* IPRT_WITH_POSIX_TIMERS */
369
370 sigemptyset(&SigSet);
371 sigaddset(&SigSet, RT_TIMER_SIGNAL);
372 while (g_cTimerInstances)
373 {
374 siginfo_t SigInfo;
375 RT_ZERO(SigInfo);
376 if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
377 {
378 LogFlow(("rttimerThread: signo=%d pTimer=%p\n", SigInfo.si_signo, SigInfo.si_value.sival_ptr));
379 if (RT_LIKELY( SigInfo.si_signo == RT_TIMER_SIGNAL
380 && SigInfo.si_code == SI_TIMER)) /* The SI_TIMER check is *essential* because of the pthread_kill. */
381 {
382 PRTTIMER pTimer = (PRTTIMER)SigInfo.si_value.sival_ptr;
383 AssertPtr(pTimer);
384 if (RT_UNLIKELY( !RT_VALID_PTR(pTimer)
385 || ASMAtomicUoReadU8(&pTimer->fSuspended)
386 || ASMAtomicUoReadU8(&pTimer->fDestroyed)
387 || pTimer->u32Magic != RTTIMER_MAGIC))
388 continue;
389
390 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
391
392 /* auto suspend one-shot timers. */
393 if (RT_UNLIKELY(!pTimer->u64NanoInterval))
394 ASMAtomicWriteU8(&pTimer->fSuspended, true);
395 }
396 }
397 }
398#endif /* IPRT_WITH_POSIX_TIMERS */
399
400 return VINF_SUCCESS;
401}
402
403
404RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t fFlags, PFNRTTIMER pfnTimer, void *pvUser)
405{
406 /*
407 * We don't support the fancy MP features.
408 */
409 if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
410 return VERR_NOT_SUPPORTED;
411
412 /*
413 * We need the signal masks to be set correctly, which they won't be in
414 * unobtrusive mode.
415 */
416 if (RTR3InitIsUnobtrusive())
417 return VERR_NOT_SUPPORTED;
418
419#ifndef IPRT_WITH_POSIX_TIMERS
420 /*
421 * Check if timer is busy.
422 */
423 struct itimerval TimerVal;
424 if (getitimer(ITIMER_REAL, &TimerVal))
425 {
426 AssertMsgFailed(("getitimer() -> errno=%d\n", errno));
427 return VERR_NOT_IMPLEMENTED;
428 }
429 if ( TimerVal.it_value.tv_usec
430 || TimerVal.it_value.tv_sec
431 || TimerVal.it_interval.tv_usec
432 || TimerVal.it_interval.tv_sec)
433 {
434 AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
435 return VERR_TIMER_BUSY;
436 }
437#endif /* !IPRT_WITH_POSIX_TIMERS */
438
439 /*
440 * Block RT_TIMER_SIGNAL from calling thread.
441 */
442 sigset_t SigSet;
443 sigemptyset(&SigSet);
444 sigaddset(&SigSet, RT_TIMER_SIGNAL);
445 sigprocmask(SIG_BLOCK, &SigSet, NULL);
446
447#ifndef IPRT_WITH_POSIX_TIMERS /** @todo combine more of the setitimer/timer_create code. setitimer could also use the global thread. */
448 /** @todo Move this RTC hack else where... */
449 static bool fDoneRTC;
450 if (!fDoneRTC)
451 {
452 fDoneRTC = true;
453 /* check resolution. */
454 TimerVal.it_interval.tv_sec = 0;
455 TimerVal.it_interval.tv_usec = 1000;
456 TimerVal.it_value = TimerVal.it_interval;
457 if ( setitimer(ITIMER_REAL, &TimerVal, NULL)
458 || getitimer(ITIMER_REAL, &TimerVal)
459 || TimerVal.it_interval.tv_usec > 1000)
460 {
461 /*
462 * Try open /dev/rtc to set the irq rate to 1024 and
463 * turn periodic
464 */
465 Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
466# ifdef RT_OS_LINUX
467 int fh = open("/dev/rtc", O_RDONLY);
468 if (fh >= 0)
469 {
470 if ( ioctl(fh, RTC_IRQP_SET, 1024) < 0
471 || ioctl(fh, RTC_PIE_ON, 0) < 0)
472 Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
473 ioctl(fh, F_SETFL, O_ASYNC);
474 ioctl(fh, F_SETOWN, getpid());
475 /* not so sure if closing it is a good idea... */
476 //close(fh);
477 }
478 else
479 Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
480# endif
481 }
482 /* disable it */
483 TimerVal.it_interval.tv_sec = 0;
484 TimerVal.it_interval.tv_usec = 0;
485 TimerVal.it_value = TimerVal.it_interval;
486 setitimer(ITIMER_REAL, &TimerVal, NULL);
487 }
488
489 /*
490 * Create a new timer.
491 */
492 int rc;
493 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
494 if (pTimer)
495 {
496 pTimer->u32Magic = RTTIMER_MAGIC;
497 pTimer->fSuspended = true;
498 pTimer->fDestroyed = false;
499 pTimer->Thread = NIL_RTTHREAD;
500 pTimer->Event = NIL_RTSEMEVENT;
501 pTimer->pfnTimer = pfnTimer;
502 pTimer->pvUser = pvUser;
503 pTimer->u64NanoInterval = u64NanoInterval;
504 pTimer->u64NanoFirst = 0;
505 pTimer->iTick = 0;
506 pTimer->iError = 0;
507 rc = RTSemEventCreate(&pTimer->Event);
508 AssertRC(rc);
509 if (RT_SUCCESS(rc))
510 {
511 rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
512 AssertRC(rc);
513 if (RT_SUCCESS(rc))
514 {
515 /*
516 * Wait for the timer thread to initialize it self.
517 * This might take a little while...
518 */
519 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
520 AssertRC(rc);
521 if (RT_SUCCESS(rc))
522 {
523 rc = RTThreadUserReset(pTimer->Thread); AssertRC(rc);
524 rc = pTimer->iError;
525 AssertRC(rc);
526 if (RT_SUCCESS(rc))
527 {
528 RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
529 *ppTimer = pTimer;
530 return VINF_SUCCESS;
531 }
532 }
533
534 /* bail out */
535 ASMAtomicXchgU8(&pTimer->fDestroyed, true);
536 ASMAtomicXchgU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
537 RTThreadWait(pTimer->Thread, 45*1000, NULL);
538 }
539 RTSemEventDestroy(pTimer->Event);
540 pTimer->Event = NIL_RTSEMEVENT;
541 }
542 RTMemFree(pTimer);
543 }
544 else
545 rc = VERR_NO_MEMORY;
546
547#else /* IPRT_WITH_POSIX_TIMERS */
548
549 /*
550 * Do the global init first.
551 */
552 int rc = RTOnce(&g_TimerOnce, rtTimerOnce, NULL);
553 if (RT_FAILURE(rc))
554 return rc;
555
556 /*
557 * Create a new timer structure.
558 */
559 LogFlow(("RTTimerCreateEx: u64NanoInterval=%llu fFlags=%lu\n", u64NanoInterval, fFlags));
560 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
561 if (pTimer)
562 {
563 /* Initialize timer structure. */
564 pTimer->u32Magic = RTTIMER_MAGIC;
565 pTimer->fSuspended = true;
566 pTimer->fDestroyed = false;
567 pTimer->pfnTimer = pfnTimer;
568 pTimer->pvUser = pvUser;
569 pTimer->u64NanoInterval = u64NanoInterval;
570 pTimer->iTick = 0;
571
572 /*
573 * Create a timer that deliver RT_TIMER_SIGNAL upon timer expiration.
574 */
575 struct sigevent SigEvt;
576 SigEvt.sigev_notify = SIGEV_SIGNAL;
577 SigEvt.sigev_signo = RT_TIMER_SIGNAL;
578 SigEvt.sigev_value.sival_ptr = pTimer; /* sigev_value gets copied to siginfo. */
579 int err = timer_create(CLOCK_REALTIME, &SigEvt, &pTimer->NativeTimer);
580 if (!err)
581 {
582 /*
583 * Increment the timer count, do this behind the critsect to avoid races.
584 */
585 RTCritSectEnter(&g_TimerCritSect);
586
587 if (ASMAtomicIncU32(&g_cTimerInstances) != 1)
588 {
589 Assert(g_cTimerInstances > 1);
590 RTCritSectLeave(&g_TimerCritSect);
591
592 LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
593 *ppTimer = pTimer;
594 return VINF_SUCCESS;
595 }
596
597 /*
598 * Create the signal handling thread. It will wait for the signal
599 * and execute the timer functions.
600 */
601 rc = RTThreadCreate(&g_TimerThread, rttimerThread, NULL, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
602 if (RT_SUCCESS(rc))
603 {
604 rc = RTThreadUserWait(g_TimerThread, 45*1000); /* this better not fail... */
605 if (RT_SUCCESS(rc))
606 {
607 RTCritSectLeave(&g_TimerCritSect);
608
609 LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
610 *ppTimer = pTimer;
611 return VINF_SUCCESS;
612 }
613 /* darn, what do we do here? */
614 }
615
616 /* bail out */
617 ASMAtomicDecU32(&g_cTimerInstances);
618 Assert(!g_cTimerInstances);
619
620 RTCritSectLeave(&g_TimerCritSect);
621
622 timer_delete(pTimer->NativeTimer);
623 }
624 else
625 {
626 rc = RTErrConvertFromErrno(err);
627 Log(("RTTimerCreateEx: err=%d (%Rrc)\n", err, rc));
628 }
629
630 RTMemFree(pTimer);
631 }
632 else
633 rc = VERR_NO_MEMORY;
634
635#endif /* IPRT_WITH_POSIX_TIMERS */
636 return rc;
637}
638
639
640RTR3DECL(int) RTTimerDestroy(PRTTIMER pTimer)
641{
642 LogFlow(("RTTimerDestroy: pTimer=%p\n", pTimer));
643
644 /*
645 * Validate input.
646 */
647 /* NULL is ok. */
648 if (!pTimer)
649 return VINF_SUCCESS;
650 int rc = VINF_SUCCESS;
651 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
652 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
653#ifdef IPRT_WITH_POSIX_TIMERS
654 AssertReturn(g_TimerThread != RTThreadSelf(), VERR_INTERNAL_ERROR);
655#else
656 AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
657#endif
658
659 /*
660 * Mark the semaphore as destroyed.
661 */
662 ASMAtomicWriteU8(&pTimer->fDestroyed, true);
663 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
664
665#ifdef IPRT_WITH_POSIX_TIMERS
666 /*
667 * Suspend the timer if it's running.
668 */
669 if (!pTimer->fSuspended)
670 {
671 struct itimerspec TimerSpec;
672 TimerSpec.it_value.tv_sec = 0;
673 TimerSpec.it_value.tv_nsec = 0;
674 TimerSpec.it_interval.tv_sec = 0;
675 TimerSpec.it_interval.tv_nsec = 0;
676 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL); NOREF(err);
677 AssertMsg(!err, ("%d / %d\n", err, errno));
678 }
679#endif
680
681 /*
682 * Poke the thread and wait for it to finish.
683 * This is only done for the last timer when using posix timers.
684 */
685#ifdef IPRT_WITH_POSIX_TIMERS
686 RTTHREAD Thread = NIL_RTTHREAD;
687 RTCritSectEnter(&g_TimerCritSect);
688 if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
689 {
690 Thread = g_TimerThread;
691 g_TimerThread = NIL_RTTHREAD;
692 }
693 RTCritSectLeave(&g_TimerCritSect);
694#else /* IPRT_WITH_POSIX_TIMERS */
695 RTTHREAD Thread = pTimer->Thread;
696 rc = RTSemEventSignal(pTimer->Event);
697 AssertRC(rc);
698#endif /* IPRT_WITH_POSIX_TIMERS */
699 if (Thread != NIL_RTTHREAD)
700 {
701 /* Signal it so it gets out of the sigwait if it's stuck there... */
702 pthread_kill((pthread_t)RTThreadGetNative(Thread), RT_TIMER_SIGNAL);
703
704 /*
705 * Wait for the thread to complete.
706 */
707 rc = RTThreadWait(Thread, 30 * 1000, NULL);
708 AssertRC(rc);
709 }
710
711
712 /*
713 * Free up the resources associated with the timer.
714 */
715#ifdef IPRT_WITH_POSIX_TIMERS
716 timer_delete(pTimer->NativeTimer);
717#else
718 RTSemEventDestroy(pTimer->Event);
719 pTimer->Event = NIL_RTSEMEVENT;
720#endif /* !IPRT_WITH_POSIX_TIMERS */
721 if (RT_SUCCESS(rc))
722 RTMemFree(pTimer);
723 return rc;
724}
725
726
727RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
728{
729 /*
730 * Validate input.
731 */
732 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
733 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
734#ifndef IPRT_WITH_POSIX_TIMERS
735 AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
736#endif
737
738 /*
739 * Already running?
740 */
741 if (!ASMAtomicXchgU8(&pTimer->fSuspended, false))
742 return VERR_TIMER_ACTIVE;
743 LogFlow(("RTTimerStart: pTimer=%p u64First=%llu u64NanoInterval=%llu\n", pTimer, u64First, pTimer->u64NanoInterval));
744
745#ifndef IPRT_WITH_POSIX_TIMERS
746 /*
747 * Tell the thread to start servicing the timer.
748 * Wait for it to ACK the request to avoid reset races.
749 */
750 RTThreadUserReset(pTimer->Thread);
751 ASMAtomicUoWriteU64(&pTimer->u64NanoFirst, u64First);
752 ASMAtomicUoWriteU64(&pTimer->iTick, 0);
753 ASMAtomicWriteU8(&pTimer->fSuspended, false);
754 int rc = RTSemEventSignal(pTimer->Event);
755 if (RT_SUCCESS(rc))
756 {
757 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
758 AssertRC(rc);
759 RTThreadUserReset(pTimer->Thread);
760 }
761 else
762 AssertRC(rc);
763
764#else /* IPRT_WITH_POSIX_TIMERS */
765 /*
766 * Start the timer.
767 */
768 struct itimerspec TimerSpec;
769 TimerSpec.it_value.tv_sec = u64First / 1000000000; /* nanosec => sec */
770 TimerSpec.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
771 TimerSpec.it_interval.tv_sec = pTimer->u64NanoInterval / 1000000000;
772 TimerSpec.it_interval.tv_nsec = pTimer->u64NanoInterval % 1000000000;
773 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
774 int rc = err == 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
775#endif /* IPRT_WITH_POSIX_TIMERS */
776
777 if (RT_FAILURE(rc))
778 ASMAtomicXchgU8(&pTimer->fSuspended, false);
779 return rc;
780}
781
782
783RTDECL(int) RTTimerStop(PRTTIMER pTimer)
784{
785 /*
786 * Validate input.
787 */
788 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
789 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
790
791 /*
792 * Already running?
793 */
794 if (ASMAtomicXchgU8(&pTimer->fSuspended, true))
795 return VERR_TIMER_SUSPENDED;
796 LogFlow(("RTTimerStop: pTimer=%p\n", pTimer));
797
798#ifndef IPRT_WITH_POSIX_TIMERS
799 /*
800 * Tell the thread to stop servicing the timer.
801 */
802 RTThreadUserReset(pTimer->Thread);
803 ASMAtomicXchgU8(&pTimer->fSuspended, true);
804 int rc = VINF_SUCCESS;
805 if (RTThreadSelf() != pTimer->Thread)
806 {
807 pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), RT_TIMER_SIGNAL);
808 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
809 AssertRC(rc);
810 RTThreadUserReset(pTimer->Thread);
811 }
812
813#else /* IPRT_WITH_POSIX_TIMERS */
814 /*
815 * Stop the timer.
816 */
817 struct itimerspec TimerSpec;
818 TimerSpec.it_value.tv_sec = 0;
819 TimerSpec.it_value.tv_nsec = 0;
820 TimerSpec.it_interval.tv_sec = 0;
821 TimerSpec.it_interval.tv_nsec = 0;
822 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
823 int rc = err == 0 ? VINF_SUCCESS : RTErrConvertFromErrno(errno);
824#endif /* IPRT_WITH_POSIX_TIMERS */
825
826 return rc;
827}
828
829
830RTDECL(int) RTTimerChangeInterval(PRTTIMER pTimer, uint64_t u64NanoInterval)
831{
832 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
833 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
834 NOREF(u64NanoInterval);
835 return VERR_NOT_SUPPORTED;
836}
837
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