timer-posix.cpp revision 0f77dc54d7ec617480988ccdfcd080f480e79698
bb5e3b2f129cc39517b925419c22f69a378ec023eh/* $Id$ */
bb5e3b2f129cc39517b925419c22f69a378ec023eh/** @file
bb5e3b2f129cc39517b925419c22f69a378ec023eh * IPRT - Timer, POSIX.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Copyright (C) 2006-2007 Sun Microsystems, Inc.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
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 *
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 *
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 *
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
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*******************************************************************************
bb5e3b2f129cc39517b925419c22f69a378ec023eh* Header Files *
bb5e3b2f129cc39517b925419c22f69a378ec023eh*******************************************************************************/
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/timer.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/alloc.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/assert.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/thread.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/log.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/asm.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/semaphore.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/string.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <iprt/err.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include "internal/magics.h"
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <unistd.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <sys/fcntl.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <sys/ioctl.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifdef RT_OS_LINUX
bb5e3b2f129cc39517b925419c22f69a378ec023eh# include <linux/rtc.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <sys/time.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <signal.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#include <errno.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifndef RT_OS_OS2
bb5e3b2f129cc39517b925419c22f69a378ec023eh# include <pthread.h>
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/*******************************************************************************
bb5e3b2f129cc39517b925419c22f69a378ec023eh* Structures and Typedefs *
bb5e3b2f129cc39517b925419c22f69a378ec023eh*******************************************************************************/
bb5e3b2f129cc39517b925419c22f69a378ec023eh/**
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The internal representation of a timer handle.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023ehtypedef struct RTTIMER
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
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 uint32_t volatile u32Magic;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Flag indicating the the timer is suspended. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint8_t volatile fSuspended;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Flag indicating that the timer has been destroyed. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint8_t volatile fDestroyed;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The timer thread. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTTHREAD Thread;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Event semaphore on which the thread is blocked. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTSEMEVENT Event;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** User argument. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh void *pvUser;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** Callback. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh PFNRTTIMER pfnTimer;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The timer interval. 0 if one-shot. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint64_t u64NanoInterval;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The first shot interval. 0 if ASAP. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint64_t volatile u64NanoFirst;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** The current timer tick. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint64_t volatile iTick;
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
bb5e3b2f129cc39517b925419c22f69a378ec023eh} RTTIMER;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/**
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Signal handler which ignore everything it gets.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * @param iSignal The signal number.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic void rttimerSignalIgnore(int iSignal)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh //AssertBreakpoint();
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh/**
bb5e3b2f129cc39517b925419c22f69a378ec023eh * SIGALRM wait thread.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023ehstatic DECLCALLBACK(int) rttimerThread(RTTHREAD Thread, void *pvArg)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh PRTTIMER pTimer = (PRTTIMER)(void *)pvArg;
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTTIMER Timer = *pTimer;
bb5e3b2f129cc39517b925419c22f69a378ec023eh Assert(pTimer->u32Magic == RTTIMER_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Install signal handler.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh struct sigaction SigAct;
bb5e3b2f129cc39517b925419c22f69a378ec023eh memset(&SigAct, 0, sizeof(SigAct));
bb5e3b2f129cc39517b925419c22f69a378ec023eh SigAct.sa_flags = SA_RESTART;
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigemptyset(&SigAct.sa_mask);
bb5e3b2f129cc39517b925419c22f69a378ec023eh SigAct.sa_handler = rttimerSignalIgnore;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (sigaction(SIGALRM, &SigAct, NULL))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh SigAct.sa_flags &= ~SA_RESTART;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (sigaction(SIGALRM, &SigAct, NULL))
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("sigaction failed, errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Mask most signals except those which might be used by the pthread implementation (linux).
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigset_t SigSet;
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigfillset(&SigSet);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, SIGTERM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, SIGHUP);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, SIGINT);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, SIGABRT);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, SIGKILL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifdef SIGRTMIN
bb5e3b2f129cc39517b925419c22f69a378ec023eh for (int iSig = SIGRTMIN; iSig < SIGRTMAX; iSig++)
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigdelset(&SigSet, iSig);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (sigprocmask(SIG_SETMASK, &SigSet, NULL))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = pTimer->iError = RTErrConvertFromErrno(errno);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("sigprocmask -> errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh return rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * The work loop.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserSignal(Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh while ( !pTimer->fDestroyed
bb5e3b2f129cc39517b925419c22f69a378ec023eh && pTimer->u32Magic == RTTIMER_MAGIC)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Wait for a start or destroy event.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pTimer->fSuspended)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadSleep(1000); /* Don't cause trouble! */
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ( pTimer->fSuspended
bb5e3b2f129cc39517b925419c22f69a378ec023eh || pTimer->fDestroyed)
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Start the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh *
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 *
bb5e3b2f129cc39517b925419c22f69a378ec023eh * It turns out this might not always be the case, see SIGALRM killing
bb5e3b2f129cc39517b925419c22f69a378ec023eh * processes on RH 2.4.21.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh struct itimerval TimerVal;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pTimer->u64NanoFirst)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint64_t u64 = RT_MAX(1000, pTimer->u64NanoFirst);
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value.tv_sec = u64 / 1000000000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value.tv_usec = (u64 % 1000000000) / 1000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value.tv_sec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value.tv_usec = 10;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pTimer->u64NanoInterval)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh uint64_t u64 = RT_MAX(1000, pTimer->u64NanoInterval);
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_sec = u64 / 1000000000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_usec = (u64 % 1000000000) / 1000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_sec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_usec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (setitimer(ITIMER_REAL, &TimerVal, NULL))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fSuspended, true);
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iError = RTErrConvertFromErrno(errno);
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserSignal(Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh continue; /* back to suspended mode. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iError = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserSignal(Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Timer Service Loop.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigemptyset(&SigSet);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigaddset(&SigSet, SIGALRM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh do
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh siginfo_t SigInfo = {0};
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifdef RT_OS_DARWIN
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_LIKELY(sigwait(&SigSet, &SigInfo.si_signo) >= 0))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh#else
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_LIKELY(SigInfo.si_signo == SIGALRM))
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_UNLIKELY( pTimer->fSuspended
bb5e3b2f129cc39517b925419c22f69a378ec023eh || pTimer->fDestroyed
bb5e3b2f129cc39517b925419c22f69a378ec023eh || pTimer->u32Magic != RTTIMER_MAGIC))
bb5e3b2f129cc39517b925419c22f69a378ec023eh break;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* auto suspend one-shot timers. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_UNLIKELY(!pTimer->u64NanoInterval))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fSuspended, true);
bb5e3b2f129cc39517b925419c22f69a378ec023eh break;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else if (errno != EINTR)
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("sigwaitinfo -> errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh } while (RT_LIKELY( !pTimer->fSuspended
bb5e3b2f129cc39517b925419c22f69a378ec023eh && !pTimer->fDestroyed
bb5e3b2f129cc39517b925419c22f69a378ec023eh && pTimer->u32Magic == RTTIMER_MAGIC));
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Disable the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh struct itimerval TimerVal2 = {{0,0}, {0,0}};
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (setitimer(ITIMER_REAL, &TimerVal2, NULL))
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * ACK any pending suspend request.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (!pTimer->fDestroyed)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iError = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserSignal(Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Exit.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iError = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserSignal(Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VINF_SUCCESS;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTDECL(int) RTTimerCreateEx(PRTTIMER *ppTimer, uint64_t u64NanoInterval, unsigned fFlags, PFNRTTIMER pfnTimer, void *pvUser)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * We don't support the fancy MP features.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (fFlags & RTTIMER_FLAGS_CPU_SPECIFIC)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VERR_NOT_SUPPORTED;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Check if timer is busy.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh struct itimerval TimerVal;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (getitimer(ITIMER_REAL, &TimerVal))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("getitimer() -> errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VERR_NOT_IMPLEMENTED;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ( TimerVal.it_value.tv_usec || TimerVal.it_value.tv_sec
bb5e3b2f129cc39517b925419c22f69a378ec023eh || TimerVal.it_interval.tv_usec || TimerVal.it_interval.tv_sec
bb5e3b2f129cc39517b925419c22f69a378ec023eh )
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertMsgFailed(("A timer is running. System limit is one timer per process!\n"));
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VERR_TIMER_BUSY;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Block SIGALRM from calling thread.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigset_t SigSet;
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigemptyset(&SigSet);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigaddset(&SigSet, SIGALRM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh sigprocmask(SIG_BLOCK, &SigSet, NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /** @todo Move this RTC hack else where... */
bb5e3b2f129cc39517b925419c22f69a378ec023eh static bool fDoneRTC;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (!fDoneRTC)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh fDoneRTC = true;
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* check resolution. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_sec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_usec = 1000;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value = TimerVal.it_interval;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ( setitimer(ITIMER_REAL, &TimerVal, NULL)
bb5e3b2f129cc39517b925419c22f69a378ec023eh || getitimer(ITIMER_REAL, &TimerVal)
bb5e3b2f129cc39517b925419c22f69a378ec023eh || TimerVal.it_interval.tv_usec > 1000)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Try open /dev/rtc to set the irq rate to 1024 and
bb5e3b2f129cc39517b925419c22f69a378ec023eh * turn periodic
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: interval={%ld,%ld} trying to adjust /dev/rtc!\n", TimerVal.it_interval.tv_sec, TimerVal.it_interval.tv_usec));
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifdef RT_OS_LINUX
bb5e3b2f129cc39517b925419c22f69a378ec023eh int fh = open("/dev/rtc", O_RDONLY);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (fh >= 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh if ( ioctl(fh, RTC_IRQP_SET, 1024) < 0
bb5e3b2f129cc39517b925419c22f69a378ec023eh || ioctl(fh, RTC_PIE_ON, 0) < 0)
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh ioctl(fh, F_SETFL, O_ASYNC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ioctl(fh, F_SETOWN, getpid());
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* not so sure if closing it is a good idea... */
bb5e3b2f129cc39517b925419c22f69a378ec023eh //close(fh);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else
bb5e3b2f129cc39517b925419c22f69a378ec023eh Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* disable it */
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_sec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_interval.tv_usec = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh TimerVal.it_value = TimerVal.it_interval;
bb5e3b2f129cc39517b925419c22f69a378ec023eh setitimer(ITIMER_REAL, &TimerVal, NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Create a new timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pTimer)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->u32Magic = RTTIMER_MAGIC;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->fSuspended = true;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->fDestroyed = false;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->Thread = NIL_RTTHREAD;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->Event = NIL_RTSEMEVENT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->pfnTimer = pfnTimer;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->pvUser = pvUser;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->u64NanoInterval = u64NanoInterval;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->u64NanoFirst = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iTick = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->iError = 0;
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTSemEventCreate(&pTimer->Event);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Wait for the timer thread to initialize it self.
bb5e3b2f129cc39517b925419c22f69a378ec023eh * This might take a little while...
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadUserWait(pTimer->Thread, 45*1000);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadUserReset(pTimer->Thread); AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = pTimer->iError;
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
bb5e3b2f129cc39517b925419c22f69a378ec023eh *ppTimer = pTimer;
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VINF_SUCCESS;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* bail out */
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fDestroyed, true);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU32(&pTimer->u32Magic, RTTIMER_MAGIC + 1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadWait(pTimer->Thread, 45*1000, NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTSemEventDestroy(pTimer->Event);
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->Event = NIL_RTSEMEVENT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTMemFree(pTimer);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = VERR_NO_MEMORY;
bb5e3b2f129cc39517b925419c22f69a378ec023eh return rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTR3DECL(int) RTTimerDestroy(PRTTIMER pTimer)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh LogFlow(("RTTimerDestroy: pTimer=%p\n", pTimer));
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh /* NULL is ok. */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (!pTimer)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VINF_SUCCESS;
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = VINF_SUCCESS;
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to terminate and wait for it do complete.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fDestroyed, true);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU32(&pTimer->u32Magic, RTTIMER_MAGIC + 1);
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTSemEventSignal(pTimer->Event);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (!pTimer->fSuspended)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifndef RT_OS_OS2
bb5e3b2f129cc39517b925419c22f69a378ec023eh pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), SIGALRM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadWait(pTimer->Thread, 30 * 1000, NULL);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTSemEventDestroy(pTimer->Event);
bb5e3b2f129cc39517b925419c22f69a378ec023eh pTimer->Event = NIL_RTSEMEVENT;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTMemFree(pTimer);
bb5e3b2f129cc39517b925419c22f69a378ec023eh return rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Already running?
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (!pTimer->fSuspended)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VERR_TIMER_ACTIVE;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to start servicing the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserReset(pTimer->Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicUoWriteU64(&pTimer->u64NanoFirst, u64First);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicUoWriteU64(&pTimer->iTick, 0);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicWriteU8(&pTimer->fSuspended, false);
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = RTSemEventSignal(pTimer->Event);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_SUCCESS(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadUserWait(pTimer->Thread, 45*1000);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserReset(pTimer->Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh else
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RT_FAILURE(rc))
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fSuspended, false);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023ehRTDECL(int) RTTimerStop(PRTTIMER pTimer)
bb5e3b2f129cc39517b925419c22f69a378ec023eh{
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Validate input.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Already running?
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (pTimer->fSuspended)
bb5e3b2f129cc39517b925419c22f69a378ec023eh return VERR_TIMER_SUSPENDED;
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh /*
bb5e3b2f129cc39517b925419c22f69a378ec023eh * Tell the thread to stop servicing the timer.
bb5e3b2f129cc39517b925419c22f69a378ec023eh */
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserReset(pTimer->Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh ASMAtomicXchgU8(&pTimer->fSuspended, true);
bb5e3b2f129cc39517b925419c22f69a378ec023eh int rc = VINF_SUCCESS;
bb5e3b2f129cc39517b925419c22f69a378ec023eh if (RTThreadSelf() != pTimer->Thread)
bb5e3b2f129cc39517b925419c22f69a378ec023eh {
bb5e3b2f129cc39517b925419c22f69a378ec023eh#ifndef RT_OS_OS2
bb5e3b2f129cc39517b925419c22f69a378ec023eh pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), SIGALRM);
bb5e3b2f129cc39517b925419c22f69a378ec023eh#endif
bb5e3b2f129cc39517b925419c22f69a378ec023eh rc = RTThreadUserWait(pTimer->Thread, 45*1000);
bb5e3b2f129cc39517b925419c22f69a378ec023eh AssertRC(rc);
bb5e3b2f129cc39517b925419c22f69a378ec023eh RTThreadUserReset(pTimer->Thread);
bb5e3b2f129cc39517b925419c22f69a378ec023eh }
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh return rc;
bb5e3b2f129cc39517b925419c22f69a378ec023eh}
bb5e3b2f129cc39517b925419c22f69a378ec023eh
bb5e3b2f129cc39517b925419c22f69a378ec023eh