timer-posix.cpp revision 0f77dc54d7ec617480988ccdfcd080f480e79698
bb5e3b2f129cc39517b925419c22f69a378ec023eh * IPRT - Timer, POSIX.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright (C) 2006-2007 Sun Microsystems, Inc.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * This file is part of VirtualBox Open Source Edition (OSE), as
bb5e3b2f129cc39517b925419c22f69a378ec023eh * available from http://www.virtualbox.org. This file is free software;
bb5e3b2f129cc39517b925419c22f69a378ec023eh * you can redistribute it and/or modify it under the terms of the GNU
bb5e3b2f129cc39517b925419c22f69a378ec023eh * General Public License (GPL) as published by the Free Software
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Foundation, in version 2 as it comes in the "COPYING" file of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The contents of this file may alternatively be used under the terms
bb5e3b2f129cc39517b925419c22f69a378ec023eh * of the Common Development and Distribution License Version 1.0
bb5e3b2f129cc39517b925419c22f69a378ec023eh * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * VirtualBox OSE distribution, in which case the provisions of the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * CDDL are applicable instead of those of the GPL.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * You may elect to license modified versions of this file under the
bb5e3b2f129cc39517b925419c22f69a378ec023eh * terms and conditions of either the GPL or the CDDL or both.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Clara, CA 95054 USA or visit http://www.sun.com if you need
bb5e3b2f129cc39517b925419c22f69a378ec023eh * additional information or have any questions.
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*******************************************************************************
bb5e3b2f129cc39517b925419c22f69a378ec023eh* Header Files *
bb5e3b2f129cc39517b925419c22f69a378ec023eh*******************************************************************************/
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*******************************************************************************
bb5e3b2f129cc39517b925419c22f69a378ec023eh* Structures and Typedefs *
bb5e3b2f129cc39517b925419c22f69a378ec023eh*******************************************************************************/
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The internal representation of a timer handle.
bb5e3b2f129cc39517b925419c22f69a378ec023ehtypedef struct RTTIMER
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Magic.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * This is RTTIMER_MAGIC, but changes to something else before the timer
bb5e3b2f129cc39517b925419c22f69a378ec023eh * is destroyed to indicate clearly that thread should exit. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Flag indicating the the timer is suspended. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Flag indicating that the timer has been destroyed. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The timer thread. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Event semaphore on which the thread is blocked. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** User argument. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Callback. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The timer interval. 0 if one-shot. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The first shot interval. 0 if ASAP. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The current timer tick. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The error/status of the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Initially -1, set to 0 when the timer have been successfully started, and
bb5e3b2f129cc39517b925419c22f69a378ec023eh * to errno on failure in starting the timer. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh int volatile iError;
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Signal handler which ignore everything it gets.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * @param iSignal The signal number.
bb5e3b2f129cc39517b925419c22f69a378ec023eh //AssertBreakpoint();
bb5e3b2f129cc39517b925419c22f69a378ec023eh * SIGALRM wait thread.
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic DECLCALLBACK(int) rttimerThread(RTTHREAD Thread, void *pvArg)
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Install signal handler.
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("sigaction failed, errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Mask most signals except those which might be used by the pthread implementation (linux).
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The work loop.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Wait for a start or destroy event.
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Start the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * For some SunOS (/SysV?) threading compatibility Linux will only
bb5e3b2f129cc39517b925419c22f69a378ec023eh * deliver the SIGALRM to the thread calling setitimer(). Therefore
bb5e3b2f129cc39517b925419c22f69a378ec023eh * we have to call it here.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * It turns out this might not always be the case, see SIGALRM killing
bb5e3b2f129cc39517b925419c22f69a378ec023eh * processes on RH 2.4.21.
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_usec = (u64 % 1000000000) / 1000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue; /* back to suspended mode. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Timer Service Loop.
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* auto suspend one-shot timers. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Disable the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh * ACK any pending suspend request.
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
bb5e3b2f129cc39517b925419c22f69a378ec023eh * We don't support the fancy MP features.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Check if timer is busy.
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ( TimerVal.it_value.tv_usec || TimerVal.it_value.tv_sec
bb5e3b2f129cc39517b925419c22f69a378ec023eh || TimerVal.it_interval.tv_usec || TimerVal.it_interval.tv_sec
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Block SIGALRM from calling thread.
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** @todo Move this RTC hack else where... */
bb5e3b2f129cc39517b925419c22f69a378ec023eh static bool fDoneRTC;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* check resolution. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Try open /dev/rtc to set the irq rate to 1024 and
bb5e3b2f129cc39517b925419c22f69a378ec023eh * turn periodic
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (fh >= 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* not so sure if closing it is a good idea... */
bb5e3b2f129cc39517b925419c22f69a378ec023eh //close(fh);
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* disable it */
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Create a new timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Wait for the timer thread to initialize it self.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * This might take a little while...
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* bail out */
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* NULL is ok. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to terminate and wait for it do complete.
bb5e3b2f129cc39517b925419c22f69a378ec023eh pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), SIGALRM);
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Already running?
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to start servicing the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Already running?
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to stop servicing the timer.