timer-posix.cpp revision 2d15e419b0ca2c8fc87724cecf3bcd84567776e2
/* $Id$ */
/** @file
* IPRT - Timer, POSIX.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Enables the use of POSIX RT timers. */
#ifndef RT_OS_SOLARIS /* Solaris 10 doesn't have SIGEV_THREAD */
#define IPRT_WITH_POSIX_TIMERS
#endif /* !RT_OS_SOLARIS */
#define LOG_GROUP RTLOGGROUP_TIMER
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/semaphore.h>
#include <unistd.h>
#ifdef RT_OS_LINUX
#endif
#include <signal.h>
#include <errno.h>
#ifndef RT_OS_OS2
# include <pthread.h>
#endif
#ifdef IPRT_WITH_POSIX_TIMERS
#define RT_TIMER_SIGNAL SIGALRM
/**
* Global counter of RTTimer instances. The signal thread is
* started when it changes from 0 to 1. The signal thread
* terminates when it becomes 0 again.
*/
/** The signal handling thread. */
/**
* Event semaphore on which the calling thread is blocked until
* the signal thread has been initialized.
*/
#endif /* IPRT_WITH_POSIX_TIMERS */
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* The internal representation of a timer handle.
*/
typedef struct RTTIMER
{
/** Magic.
* This is RTTIMER_MAGIC, but changes to something else before the timer
* is destroyed to indicate clearly that thread should exit. */
/** Flag indicating the the timer is suspended. */
uint8_t volatile fSuspended;
/** Flag indicating that the timer has been destroyed. */
uint8_t volatile fDestroyed;
#ifndef IPRT_WITH_POSIX_TIMERS /** @todo We have to take the signals on a dedicated timer thread as
* we (might) have code assuming that signals doesn't screw around
* on existing threads. (It would be sufficient to have one thread
* per signal of course since the signal will be masked while it's
* running, however, it may just cause more compilcations than its
* worth - sigwait/sigwaitinfo work atomically anyway...)
* Also, must block the signal in the thread main procedure too. */
/** The timer thread. */
/** Event semaphore on which the thread is blocked. */
#endif /* !IPRT_WITH_POSIX_TIMERS */
/** User argument. */
void *pvUser;
/** Callback. */
/** The timer interval. 0 if one-shot. */
#ifndef IPRT_WITH_POSIX_TIMERS
/** The first shot interval. 0 if ASAP. */
uint64_t volatile u64NanoFirst;
#endif /* !IPRT_WITH_POSIX_TIMERS */
/** The current timer tick. */
#ifndef IPRT_WITH_POSIX_TIMERS
* Initially -1, set to 0 when the timer have been successfully started, and
* to errno on failure in starting the timer. */
int volatile iError;
#else /* IPRT_WITH_POSIX_TIMERS */
#endif /* IPRT_WITH_POSIX_TIMERS */
} RTTIMER;
/**
* Signal handler which ignore everything it gets.
*
* @param iSignal The signal number.
*/
static void rttimerSignalIgnore(int iSignal)
{
//AssertBreakpoint();
}
/**
* SIGALRM wait thread.
*/
{
#ifndef IPRT_WITH_POSIX_TIMERS
#endif /* !IPRT_WITH_POSIX_TIMERS */
/*
* Install signal handler.
*/
{
}
/*
* Mask most signals except those which might be used by the pthread implementation (linux).
*/
sigfillset(&SigSet);
#ifdef SIGRTMIN
#endif
{
#ifndef IPRT_WITH_POSIX_TIMERS
#else /* IPRT_WITH_POSIX_TIMERS */
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}
/*
* The work loop.
*/
#ifndef IPRT_WITH_POSIX_TIMERS
while ( !pTimer->fDestroyed
{
/*
* Wait for a start or destroy event.
*/
if (pTimer->fSuspended)
{
{
}
if ( pTimer->fSuspended
|| pTimer->fDestroyed)
continue;
}
/*
* Start the timer.
*
* For some SunOS (/SysV?) threading compatibility Linux will only
* deliver the SIGALRM to the thread calling setitimer(). Therefore
* we have to call it here.
*
* It turns out this might not always be the case, see SIGALRM killing
* processes on RH 2.4.21.
*/
if (pTimer->u64NanoFirst)
{
}
else
{
}
if (pTimer->u64NanoInterval)
{
}
else
{
}
{
continue; /* back to suspended mode. */
}
/*
* Timer Service Loop.
*/
do
{
#ifdef RT_OS_DARWIN
{
#else
{
#endif
{
|| pTimer->fDestroyed
break;
/* auto suspend one-shot timers. */
{
break;
}
}
}
&& !pTimer->fDestroyed
/*
* Disable the timer.
*/
/*
* ACK any pending suspend request.
*/
if (!pTimer->fDestroyed)
{
}
}
/*
* Exit.
*/
return VINF_SUCCESS;
}
#else /* IPRT_WITH_POSIX_TIMERS */
while (g_cTimerInstances)
{
{
LogFlow(("rttimerThread: signo=%d pTimer=%p\n", SigInfo.si_signo, SigInfo._sifields._timer.si_sigval.sival_ptr));
{
|| pTimer->fSuspended
|| pTimer->fDestroyed
continue;
/* auto suspend one-shot timers. */
{
//break;
}
}
}
}
return VINF_SUCCESS;
}
/**
* Create the SIGALRM handling thread and wait for it to get
* ready.
*/
static int rttimerCreateSignalThread()
{
int rc = RTThreadCreate(&g_Thread, rttimerThread, NULL, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
if (RT_SUCCESS(rc))
{
/* Let's wait for the thread to get ready to handle signals. */
}
return rc;
}
#endif /* IPRT_WITH_POSIX_TIMERS */
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
{
/*
* We don't support the fancy MP features.
*/
if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
return VERR_NOT_SUPPORTED;
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Check if timer is busy.
*/
{
return VERR_NOT_IMPLEMENTED;
}
)
{
AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
return VERR_TIMER_BUSY;
}
#endif /* !IPRT_WITH_POSIX_TIMERS */
/*
* Block SIGALRM from calling thread.
*/
#ifndef IPRT_WITH_POSIX_TIMERS
/** @todo Move this RTC hack else where... */
static bool fDoneRTC;
if (!fDoneRTC)
{
fDoneRTC = true;
/* check resolution. */
{
/*
* turn periodic
*/
Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
#ifdef RT_OS_LINUX
if (fh >= 0)
{
/* not so sure if closing it is a good idea... */
//close(fh);
}
else
#endif
}
/* disable it */
}
/*
* Create a new timer.
*/
int rc;
if (pTimer)
{
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
pTimer->u64NanoFirst = 0;
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
if (RT_SUCCESS(rc))
{
/*
* Wait for the timer thread to initialize it self.
* This might take a little while...
*/
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
return VINF_SUCCESS;
}
}
/* bail out */
}
}
}
else
rc = VERR_NO_MEMORY;
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Create a new timer.
*/
int rc = VINF_SUCCESS;
if (pTimer)
{
/* Initialize timer structure. */
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
/* Create the signal handling thread if it is the first instance. */
if (RT_SUCCESS(rc))
{
/* Ask to deliver RT_TIMER_SIGNAL upon timer expiration. */
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
}
/**
* Roll back the timer instance counter. This will cause
* termination of the signal handling thread if it is the only
* timer.
*/
}
else
rc = VERR_NO_MEMORY;
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}
{
/*
* Validate input.
*/
/* NULL is ok. */
if (!pTimer)
return VINF_SUCCESS;
int rc = VINF_SUCCESS;
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Tell the thread to terminate and wait for it do complete.
*/
if (!pTimer->fSuspended)
{
#ifndef RT_OS_OS2
#endif
}
#else /* IPRT_WITH_POSIX_TIMERS */
{
/* It is already being destroyed by another thread. */
return VINF_SUCCESS;
}
/**
* Decrement the timer instance counter. This will cause
* termination of the signal handling thread if it is the last
* remaining timer.
*/
if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
{
/* Wake up the timer thread so it can exit. */
}
#endif /* IPRT_WITH_POSIX_TIMERS */
if (RT_SUCCESS(rc))
return rc;
}
{
/*
* Validate input.
*/
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Already running?
*/
if (!pTimer->fSuspended)
return VERR_TIMER_ACTIVE;
/*
* Tell the thread to start servicing the timer.
*/
if (RT_SUCCESS(rc))
{
}
else
if (RT_FAILURE(rc))
#else /* IPRT_WITH_POSIX_TIMERS */
LogFlow(("RTTimerStart: pTimer=%p u64First=%llu u64NanoInterval=%llu\n", pTimer, u64First, pTimer->u64NanoInterval));
struct itimerspec ts;
return VERR_TIMER_ACTIVE;
ts.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}
{
/*
* Validate input.
*/
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Already running?
*/
if (pTimer->fSuspended)
return VERR_TIMER_SUSPENDED;
/*
* Tell the thread to stop servicing the timer.
*/
int rc = VINF_SUCCESS;
{
#ifndef RT_OS_OS2
#endif
}
#else /* IPRT_WITH_POSIX_TIMERS */
struct itimerspec ts;
return VERR_TIMER_SUSPENDED;
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}