timer-posix.cpp revision 0cde281779e97ead3181bbd3b628451fa2b1efe1
/* $Id$ */
/** @file
* IPRT - Timer, POSIX.
*/
/*
* Copyright (C) 2006-2007 Oracle Corporation
*
* 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.
*/
/*******************************************************************************
* 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 */
/** @def RT_TIMER_SIGNAL
* The signal number that the timers use.
* We currently use SIGALRM for both setitimer and posix real time timers
* out of simplicity, but we might want change this later for the posix ones. */
#ifdef IPRT_WITH_POSIX_TIMERS
# define RT_TIMER_SIGNAL SIGALRM
#else
# define RT_TIMER_SIGNAL SIGALRM
#endif
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP RTLOGGROUP_TIMER
#include <iprt/semaphore.h>
#include <iprt/critsect.h>
#include <unistd.h>
#ifdef RT_OS_LINUX
#endif
#include <signal.h>
#include <errno.h>
#include <pthread.h>
/*******************************************************************************
* Global Variables *
*******************************************************************************/
#ifdef IPRT_WITH_POSIX_TIMERS
/** Init the critsect on first call. */
/** Global critsect that serializes timer creation and destruction.
* This is lazily created on the first RTTimerCreateEx call and will not be
* freed up (I'm afraid). */
static RTCRITSECT g_TimerCritSect;
/**
* 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.
*/
static uint32_t volatile g_cTimerInstances;
/** The signal handling thread. */
static RTTHREAD g_TimerThread;
#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 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;
#ifdef IPRT_WITH_POSIX_TIMERS
/**
* RTOnce callback that initalizes the critical section.
*
* @returns RTCritSectInit return code.
* @param pvUser1 NULL, ignopred.
* @param pvUser2 NULL, ignopred.
*
*/
{
return RTCritSectInit(&g_TimerCritSect);
}
#endif
/**
* Signal handler which ignore everything it gets.
*
* @param iSignal The signal number.
*/
static void rttimerSignalIgnore(int iSignal)
{
//AssertBreakpoint();
}
/**
* RT_TIMER_SIGNAL 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
{
#ifdef IPRT_WITH_POSIX_TIMERS
#else
#endif
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->fDestroyed)
continue;
}
if ( pTimer->fSuspended
|| pTimer->fDestroyed)
continue;
}
/*
* Start the timer.
*
* For some SunOS (/SysV?) threading compatibility Linux will only
* deliver the RT_TIMER_SIGNAL to the thread calling setitimer(). Therefore
* we have to call it here.
*
* It turns out this might not always be the case, see RT_TIMER_SIGNAL 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.
*/
#else /* IPRT_WITH_POSIX_TIMERS */
while (g_cTimerInstances)
{
{
&& SigInfo.si_code == SI_TIMER)) /* The SI_TIMER check is *essential* because of the pthread_kill. */
{
continue;
/* auto suspend one-shot timers. */
}
}
}
#endif /* IPRT_WITH_POSIX_TIMERS */
return VINF_SUCCESS;
}
RTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, uint32_t 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 RT_TIMER_SIGNAL from calling thread.
*/
#ifndef IPRT_WITH_POSIX_TIMERS /** @todo combine more of the setitimer/timer_create code. setitimer could also use the global thread. */
/** @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 */
/*
* Do the global init first.
*/
if (RT_FAILURE(rc))
return rc;
/*
* Create a new timer structure.
*/
if (pTimer)
{
/* Initialize timer structure. */
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
/*
* Create a timer that deliver RT_TIMER_SIGNAL upon timer expiration.
*/
if (!err)
{
/*
* Increment the timer count, do this behind the critsect to avoid races.
*/
{
return VINF_SUCCESS;
}
/*
* Create the signal handling thread. It will wait for the signal
* and execute the timer functions.
*/
rc = RTThreadCreate(&g_TimerThread, rttimerThread, NULL, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
/* darn, what do we do here? */
}
/* bail out */
}
else
{
}
}
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;
#ifdef IPRT_WITH_POSIX_TIMERS
#else
#endif
/*
* Mark the semaphore as destroyed.
*/
#ifdef IPRT_WITH_POSIX_TIMERS
/*
* Suspend the timer if it's running.
*/
if (pTimer->fSuspended)
{
struct itimerspec TimerSpec;
}
#endif
/*
* Poke the thread and wait for it to finish.
* This is only done for the last timer when using posix timers.
*/
#ifdef IPRT_WITH_POSIX_TIMERS
if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
{
}
#else /* IPRT_WITH_POSIX_TIMERS */
#endif /* IPRT_WITH_POSIX_TIMERS */
if (Thread != NIL_RTTHREAD)
{
/* Signal it so it gets out of the sigwait if it's stuck there... */
/*
* Wait for the thread to complete.
*/
}
/*
* Free up the resources associated with the timer.
*/
#ifdef IPRT_WITH_POSIX_TIMERS
#else
#endif /* !IPRT_WITH_POSIX_TIMERS */
if (RT_SUCCESS(rc))
return rc;
}
{
/*
* Validate input.
*/
#ifndef IPRT_WITH_POSIX_TIMERS
#endif
/*
* Already running?
*/
return VERR_TIMER_ACTIVE;
LogFlow(("RTTimerStart: pTimer=%p u64First=%llu u64NanoInterval=%llu\n", pTimer, u64First, pTimer->u64NanoInterval));
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Tell the thread to start servicing the timer.
* Wait for it to ACK the request to avoid reset races.
*/
if (RT_SUCCESS(rc))
{
}
else
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Start the timer.
*/
struct itimerspec TimerSpec;
TimerSpec.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
#endif /* IPRT_WITH_POSIX_TIMERS */
if (RT_FAILURE(rc))
return rc;
}
{
/*
* Validate input.
*/
/*
* Already running?
*/
return VERR_TIMER_SUSPENDED;
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Tell the thread to stop servicing the timer.
*/
int rc = VINF_SUCCESS;
{
}
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Stop the timer.
*/
struct itimerspec TimerSpec;
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}
{
return VERR_NOT_SUPPORTED;
}