VirtualBox

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

Last change on this file since 24006 was 14298, checked in by vboxsync, 16 years ago

Corrected a couple of grammos.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 26.2 KB
Line 
1/* $Id: timer-posix.cpp 14298 2008-11-18 12:47:26Z 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 = {0};
313#ifdef RT_OS_DARWIN
314 if (RT_LIKELY(sigwait(&SigSet, &SigInfo.si_signo) >= 0))
315 {
316#else
317 if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
318 {
319 if (RT_LIKELY(SigInfo.si_signo == RT_TIMER_SIGNAL))
320#endif
321 {
322 if (RT_UNLIKELY( pTimer->fSuspended
323 || pTimer->fDestroyed
324 || pTimer->u32Magic != RTTIMER_MAGIC))
325 break;
326
327 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
328
329 /* auto suspend one-shot timers. */
330 if (RT_UNLIKELY(!pTimer->u64NanoInterval))
331 {
332 ASMAtomicWriteU8(&pTimer->fSuspended, true);
333 break;
334 }
335 }
336 }
337 else if (errno != EINTR)
338 AssertMsgFailed(("sigwaitinfo -> errno=%d\n", errno));
339 } while (RT_LIKELY( !pTimer->fSuspended
340 && !pTimer->fDestroyed
341 && pTimer->u32Magic == RTTIMER_MAGIC));
342
343 /*
344 * Disable the timer.
345 */
346 struct itimerval TimerVal2 = {{0,0}, {0,0}};
347 if (setitimer(ITIMER_REAL, &TimerVal2, NULL))
348 AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
349
350 /*
351 * ACK any pending suspend request.
352 */
353 if (!pTimer->fDestroyed)
354 {
355 pTimer->iError = 0;
356 RTThreadUserSignal(Thread);
357 }
358 }
359
360 /*
361 * Exit.
362 */
363 pTimer->iError = 0;
364 RTThreadUserSignal(Thread);
365
366#else /* IPRT_WITH_POSIX_TIMERS */
367
368 sigemptyset(&SigSet);
369 sigaddset(&SigSet, RT_TIMER_SIGNAL);
370 while (g_cTimerInstances)
371 {
372 siginfo_t SigInfo = {0};
373 if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
374 {
375 LogFlow(("rttimerThread: signo=%d pTimer=%p\n", SigInfo.si_signo, SigInfo.si_value.sival_ptr));
376 if (RT_LIKELY( SigInfo.si_signo == RT_TIMER_SIGNAL
377 && SigInfo.si_code == SI_TIMER)) /* The SI_TIMER check is *essential* because of the pthread_kill. */
378 {
379 PRTTIMER pTimer = (PRTTIMER)SigInfo.si_value.sival_ptr;
380 AssertPtr(pTimer);
381 if (RT_UNLIKELY( !VALID_PTR(pTimer)
382 || ASMAtomicUoReadU8(&pTimer->fSuspended)
383 || ASMAtomicUoReadU8(&pTimer->fDestroyed)
384 || pTimer->u32Magic != RTTIMER_MAGIC))
385 continue;
386
387 pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
388
389 /* auto suspend one-shot timers. */
390 if (RT_UNLIKELY(!pTimer->u64NanoInterval))
391 ASMAtomicWriteU8(&pTimer->fSuspended, true);
392 }
393 }
394 }
395#endif /* IPRT_WITH_POSIX_TIMERS */
396
397 return VINF_SUCCESS;
398}
399
400
401RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
402{
403 /*
404 * We don't support the fancy MP features.
405 */
406 if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
407 return VERR_NOT_SUPPORTED;
408
409#ifndef IPRT_WITH_POSIX_TIMERS
410 /*
411 * Check if timer is busy.
412 */
413 struct itimerval TimerVal;
414 if (getitimer(ITIMER_REAL, &TimerVal))
415 {
416 AssertMsgFailed(("getitimer() -> errno=%d\n", errno));
417 return VERR_NOT_IMPLEMENTED;
418 }
419 if ( TimerVal.it_value.tv_usec
420 || TimerVal.it_value.tv_sec
421 || TimerVal.it_interval.tv_usec
422 || TimerVal.it_interval.tv_sec)
423 {
424 AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
425 return VERR_TIMER_BUSY;
426 }
427#endif /* !IPRT_WITH_POSIX_TIMERS */
428
429 /*
430 * Block RT_TIMER_SIGNAL from calling thread.
431 */
432 sigset_t SigSet;
433 sigemptyset(&SigSet);
434 sigaddset(&SigSet, RT_TIMER_SIGNAL);
435 sigprocmask(SIG_BLOCK, &SigSet, NULL);
436
437#ifndef IPRT_WITH_POSIX_TIMERS /** @todo combine more of the setitimer/timer_create code. setitimer could also use the global thread. */
438 /** @todo Move this RTC hack else where... */
439 static bool fDoneRTC;
440 if (!fDoneRTC)
441 {
442 fDoneRTC = true;
443 /* check resolution. */
444 TimerVal.it_interval.tv_sec = 0;
445 TimerVal.it_interval.tv_usec = 1000;
446 TimerVal.it_value = TimerVal.it_interval;
447 if ( setitimer(ITIMER_REAL, &TimerVal, NULL)
448 || getitimer(ITIMER_REAL, &TimerVal)
449 || TimerVal.it_interval.tv_usec > 1000)
450 {
451 /*
452 * Try open /dev/rtc to set the irq rate to 1024 and
453 * turn periodic
454 */
455 Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
456# ifdef RT_OS_LINUX
457 int fh = open("/dev/rtc", O_RDONLY);
458 if (fh >= 0)
459 {
460 if ( ioctl(fh, RTC_IRQP_SET, 1024) < 0
461 || ioctl(fh, RTC_PIE_ON, 0) < 0)
462 Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
463 ioctl(fh, F_SETFL, O_ASYNC);
464 ioctl(fh, F_SETOWN, getpid());
465 /* not so sure if closing it is a good idea... */
466 //close(fh);
467 }
468 else
469 Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
470# endif
471 }
472 /* disable it */
473 TimerVal.it_interval.tv_sec = 0;
474 TimerVal.it_interval.tv_usec = 0;
475 TimerVal.it_value = TimerVal.it_interval;
476 setitimer(ITIMER_REAL, &TimerVal, NULL);
477 }
478
479 /*
480 * Create a new timer.
481 */
482 int rc;
483 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
484 if (pTimer)
485 {
486 pTimer->u32Magic = RTTIMER_MAGIC;
487 pTimer->fSuspended = true;
488 pTimer->fDestroyed = false;
489 pTimer->Thread = NIL_RTTHREAD;
490 pTimer->Event = NIL_RTSEMEVENT;
491 pTimer->pfnTimer = pfnTimer;
492 pTimer->pvUser = pvUser;
493 pTimer->u64NanoInterval = u64NanoInterval;
494 pTimer->u64NanoFirst = 0;
495 pTimer->iTick = 0;
496 pTimer->iError = 0;
497 rc = RTSemEventCreate(&pTimer->Event);
498 AssertRC(rc);
499 if (RT_SUCCESS(rc))
500 {
501 rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
502 AssertRC(rc);
503 if (RT_SUCCESS(rc))
504 {
505 /*
506 * Wait for the timer thread to initialize it self.
507 * This might take a little while...
508 */
509 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
510 AssertRC(rc);
511 if (RT_SUCCESS(rc))
512 {
513 rc = RTThreadUserReset(pTimer->Thread); AssertRC(rc);
514 rc = pTimer->iError;
515 AssertRC(rc);
516 if (RT_SUCCESS(rc))
517 {
518 RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
519 *ppTimer = pTimer;
520 return VINF_SUCCESS;
521 }
522 }
523
524 /* bail out */
525 ASMAtomicXchgU8(&pTimer->fDestroyed, true);
526 ASMAtomicXchgU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
527 RTThreadWait(pTimer->Thread, 45*1000, NULL);
528 }
529 RTSemEventDestroy(pTimer->Event);
530 pTimer->Event = NIL_RTSEMEVENT;
531 }
532 RTMemFree(pTimer);
533 }
534 else
535 rc = VERR_NO_MEMORY;
536
537#else /* IPRT_WITH_POSIX_TIMERS */
538
539 /*
540 * Do the global init first.
541 */
542 int rc = RTOnce(&g_TimerOnce, rtTimerOnce, NULL, NULL);
543 if (RT_FAILURE(rc))
544 return rc;
545
546 /*
547 * Create a new timer structure.
548 */
549 LogFlow(("RTTimerCreateEx: u64NanoInterval=%llu fFlags=%lu\n", u64NanoInterval, fFlags));
550 PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
551 if (pTimer)
552 {
553 /* Initialize timer structure. */
554 pTimer->u32Magic = RTTIMER_MAGIC;
555 pTimer->fSuspended = true;
556 pTimer->fDestroyed = false;
557 pTimer->pfnTimer = pfnTimer;
558 pTimer->pvUser = pvUser;
559 pTimer->u64NanoInterval = u64NanoInterval;
560 pTimer->iTick = 0;
561
562 /*
563 * Create a timer that deliver RT_TIMER_SIGNAL upon timer expiration.
564 */
565 struct sigevent SigEvt;
566 SigEvt.sigev_notify = SIGEV_SIGNAL;
567 SigEvt.sigev_signo = RT_TIMER_SIGNAL;
568 SigEvt.sigev_value.sival_ptr = pTimer; /* sigev_value gets copied to siginfo. */
569 int err = timer_create(CLOCK_REALTIME, &SigEvt, &pTimer->NativeTimer);
570 if (!err)
571 {
572 /*
573 * Increment the timer count, do this behind the critsect to avoid races.
574 */
575 RTCritSectEnter(&g_TimerCritSect);
576
577 if (ASMAtomicIncU32(&g_cTimerInstances) != 1)
578 {
579 Assert(g_cTimerInstances > 1);
580 RTCritSectLeave(&g_TimerCritSect);
581
582 LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
583 *ppTimer = pTimer;
584 return VINF_SUCCESS;
585 }
586
587 /*
588 * Create the signal handling thread. It will wait for the signal
589 * and execute the timer functions.
590 */
591 rc = RTThreadCreate(&g_TimerThread, rttimerThread, NULL, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
592 if (RT_SUCCESS(rc))
593 {
594 rc = RTThreadUserWait(g_TimerThread, 45*1000); /* this better not fail... */
595 if (RT_SUCCESS(rc))
596 {
597 RTCritSectLeave(&g_TimerCritSect);
598
599 LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
600 *ppTimer = pTimer;
601 return VINF_SUCCESS;
602 }
603 /* darn, what do we do here? */
604 }
605
606 /* bail out */
607 ASMAtomicDecU32(&g_cTimerInstances);
608 Assert(!g_cTimerInstances);
609
610 RTCritSectLeave(&g_TimerCritSect);
611
612 timer_delete(pTimer->NativeTimer);
613 }
614 else
615 {
616 rc = RTErrConvertFromErrno(err);
617 Log(("RTTimerCreateEx: err=%d (%Rrc)\n", err, rc));
618 }
619
620 RTMemFree(pTimer);
621 }
622 else
623 rc = VERR_NO_MEMORY;
624
625#endif /* IPRT_WITH_POSIX_TIMERS */
626 return rc;
627}
628
629
630RTR3DECL(int) RTTimerDestroy(PRTTIMER pTimer)
631{
632 LogFlow(("RTTimerDestroy: pTimer=%p\n", pTimer));
633
634 /*
635 * Validate input.
636 */
637 /* NULL is ok. */
638 if (!pTimer)
639 return VINF_SUCCESS;
640 int rc = VINF_SUCCESS;
641 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
642 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
643#ifdef IPRT_WITH_POSIX_TIMERS
644 AssertReturn(g_TimerThread != RTThreadSelf(), VERR_INTERNAL_ERROR);
645#else
646 AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
647#endif
648
649 /*
650 * Mark the semaphore as destroyed.
651 */
652 ASMAtomicWriteU8(&pTimer->fDestroyed, true);
653 ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
654
655#ifdef IPRT_WITH_POSIX_TIMERS
656 /*
657 * Suspend the timer if it's running.
658 */
659 if (pTimer->fSuspended)
660 {
661 struct itimerspec TimerSpec;
662 TimerSpec.it_value.tv_sec = 0;
663 TimerSpec.it_value.tv_nsec = 0;
664 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL); NOREF(err);
665 AssertMsg(!err, ("%d\n", err));
666 }
667#endif
668
669 /*
670 * Poke the thread and wait for it to finish.
671 * This is only done for the last timer when using posix timers.
672 */
673#ifdef IPRT_WITH_POSIX_TIMERS
674 RTTHREAD Thread = NIL_RTTHREAD;
675 RTCritSectEnter(&g_TimerCritSect);
676 if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
677 {
678 Thread = g_TimerThread;
679 g_TimerThread = NIL_RTTHREAD;
680 }
681 RTCritSectLeave(&g_TimerCritSect);
682#else /* IPRT_WITH_POSIX_TIMERS */
683 RTTHREAD Thread = pTimer->Thread;
684 rc = RTSemEventSignal(pTimer->Event);
685 AssertRC(rc);
686#endif /* IPRT_WITH_POSIX_TIMERS */
687 if (Thread != NIL_RTTHREAD)
688 {
689 /* Signal it so it gets out of the sigwait if it's stuck there... */
690 pthread_kill((pthread_t)RTThreadGetNative(Thread), RT_TIMER_SIGNAL);
691
692 /*
693 * Wait for the thread to complete.
694 */
695 rc = RTThreadWait(Thread, 30 * 1000, NULL);
696 AssertRC(rc);
697 }
698
699
700 /*
701 * Free up the resources associated with the timer.
702 */
703#ifdef IPRT_WITH_POSIX_TIMERS
704 timer_delete(pTimer->NativeTimer);
705#else
706 RTSemEventDestroy(pTimer->Event);
707 pTimer->Event = NIL_RTSEMEVENT;
708#endif /* !IPRT_WITH_POSIX_TIMERS */
709 if (RT_SUCCESS(rc))
710 RTMemFree(pTimer);
711 return rc;
712}
713
714
715RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
716{
717 /*
718 * Validate input.
719 */
720 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
721 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
722#ifndef IPRT_WITH_POSIX_TIMERS
723 AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
724#endif
725
726 /*
727 * Already running?
728 */
729 if (!ASMAtomicXchgU8(&pTimer->fSuspended, false))
730 return VERR_TIMER_ACTIVE;
731 LogFlow(("RTTimerStart: pTimer=%p u64First=%llu u64NanoInterval=%llu\n", pTimer, u64First, pTimer->u64NanoInterval));
732
733#ifndef IPRT_WITH_POSIX_TIMERS
734 /*
735 * Tell the thread to start servicing the timer.
736 * Wait for it to ACK the request to avoid reset races.
737 */
738 RTThreadUserReset(pTimer->Thread);
739 ASMAtomicUoWriteU64(&pTimer->u64NanoFirst, u64First);
740 ASMAtomicUoWriteU64(&pTimer->iTick, 0);
741 ASMAtomicWriteU8(&pTimer->fSuspended, false);
742 int rc = RTSemEventSignal(pTimer->Event);
743 if (RT_SUCCESS(rc))
744 {
745 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
746 AssertRC(rc);
747 RTThreadUserReset(pTimer->Thread);
748 }
749 else
750 AssertRC(rc);
751
752#else /* IPRT_WITH_POSIX_TIMERS */
753 /*
754 * Start the timer.
755 */
756 struct itimerspec TimerSpec;
757 TimerSpec.it_value.tv_sec = u64First / 1000000000; /* nanosec => sec */
758 TimerSpec.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
759 TimerSpec.it_interval.tv_sec = pTimer->u64NanoInterval / 1000000000;
760 TimerSpec.it_interval.tv_nsec = pTimer->u64NanoInterval % 1000000000;
761 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
762 int rc = RTErrConvertFromErrno(err);
763#endif /* IPRT_WITH_POSIX_TIMERS */
764
765 if (RT_FAILURE(rc))
766 ASMAtomicXchgU8(&pTimer->fSuspended, false);
767 return rc;
768}
769
770
771RTDECL(int) RTTimerStop(PRTTIMER pTimer)
772{
773 /*
774 * Validate input.
775 */
776 AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
777 AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
778
779 /*
780 * Already running?
781 */
782 if (ASMAtomicXchgU8(&pTimer->fSuspended, true))
783 return VERR_TIMER_SUSPENDED;
784 LogFlow(("RTTimerStop: pTimer=%p\n", pTimer));
785
786#ifndef IPRT_WITH_POSIX_TIMERS
787 /*
788 * Tell the thread to stop servicing the timer.
789 */
790 RTThreadUserReset(pTimer->Thread);
791 ASMAtomicXchgU8(&pTimer->fSuspended, true);
792 int rc = VINF_SUCCESS;
793 if (RTThreadSelf() != pTimer->Thread)
794 {
795 pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), RT_TIMER_SIGNAL);
796 rc = RTThreadUserWait(pTimer->Thread, 45*1000);
797 AssertRC(rc);
798 RTThreadUserReset(pTimer->Thread);
799 }
800
801#else /* IPRT_WITH_POSIX_TIMERS */
802 /*
803 * Stop the timer.
804 */
805 struct itimerspec TimerSpec;
806 TimerSpec.it_value.tv_sec = 0;
807 TimerSpec.it_value.tv_nsec = 0;
808 int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
809 int rc = RTErrConvertFromErrno(err);
810#endif /* IPRT_WITH_POSIX_TIMERS */
811
812 return rc;
813}
814
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