VirtualBox

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

Last change on this file since 26257 was 26257, checked in by vboxsync, 15 years ago

timer-posix.cpp: Another initializer warning.

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