timer-posix.cpp revision 67d93f1b0caf207d0e7f1ddf263f8eca84408544
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/* $Id$ */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** @file
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * IPRT - Timer, POSIX.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Copyright (C) 2006-2007 Sun Microsystems, Inc.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * available from http://www.virtualbox.org. This file is free software;
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * General Public License (GPL) as published by the Free Software
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
82bcaaf8077ba892f39afb721dca149353c63d2cvboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The contents of this file may alternatively be used under the terms
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * of the Common Development and Distribution License Version 1.0
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * VirtualBox OSE distribution, in which case the provisions of the
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * CDDL are applicable instead of those of the GPL.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * You may elect to license modified versions of this file under the
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * terms and conditions of either the GPL or the CDDL or both.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Clara, CA 95054 USA or visit http://www.sun.com if you need
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * additional information or have any questions.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Defined Constants And Macros *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** Enables the use of POSIX RT timers. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef RT_OS_SOLARIS /* Solaris 10 doesn't have SIGEV_THREAD */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync# define IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif /* !RT_OS_SOLARIS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** @def RT_TIMER_SIGNAL
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The signal number that the timers use.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * We currently use SIGALRM for both setitimer and posix real time timers
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * out of simplicity, but we might want change this later for the posix ones. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef IPRT_WITH_POSIX_TIMERS
500aaaf3dc1d98456808e7618db3fb2e7c8fb8e0vboxsync# define RT_TIMER_SIGNAL SIGALRM
8ffcab9595cc0d56977968cd496363502fd814aevboxsync#else
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync# define RT_TIMER_SIGNAL SIGALRM
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Header Files *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#define LOG_GROUP RTLOGGROUP_TIMER
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/timer.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/alloc.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/assert.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/thread.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/log.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/asm.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/semaphore.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/string.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/once.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/err.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <iprt/critsect.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include "internal/magics.h"
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <unistd.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <sys/fcntl.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <sys/ioctl.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef RT_OS_LINUX
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync# include <linux/rtc.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <sys/time.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <signal.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <errno.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#include <pthread.h>
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Global Variables *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** Init the critsect on first call. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic RTONCE g_TimerOnce = RTONCE_INITIALIZER;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** Global critsect that serializes timer creation and destruction.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This is lazily created on the first RTTimerCreateEx call and will not be
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * freed up (I'm afraid). */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic RTCRITSECT g_TimerCritSect;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/**
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Global counter of RTTimer instances. The signal thread is
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * started when it changes from 0 to 1. The signal thread
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * terminates when it becomes 0 again.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic uint32_t volatile g_cTimerInstances;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/** The signal handling thread. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic RTTHREAD g_TimerThread;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif /* IPRT_WITH_POSIX_TIMERS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/*******************************************************************************
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync* Structures and Typedefs *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync*******************************************************************************/
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/**
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The internal representation of a timer handle.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsynctypedef struct RTTIMER
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** Magic.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * This is RTTIMER_MAGIC, but changes to something else before the timer
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * is destroyed to indicate clearly that thread should exit. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint32_t volatile u32Magic;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** Flag indicating the the timer is suspended. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint8_t volatile fSuspended;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** Flag indicating that the timer has been destroyed. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint8_t volatile fDestroyed;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef IPRT_WITH_POSIX_TIMERS /** @todo We have to take the signals on a dedicated timer thread as
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * we (might) have code assuming that signals doesn't screw around
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * on existing threads. (It would be sufficient to have one thread
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * per signal of course since the signal will be masked while it's
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * running, however, it may just cause more compilcations than its
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * worth - sigwait/sigwaitinfo work atomically anyway...)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Also, must block the signal in the thread main procedure too. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** The timer thread. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTTHREAD Thread;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** Event semaphore on which the thread is blocked. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTSEMEVENT Event;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif /* !IPRT_WITH_POSIX_TIMERS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** User argument. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync void *pvUser;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** Callback. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync PFNRTTIMER pfnTimer;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** The timer interval. 0 if one-shot. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint64_t u64NanoInterval;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** The first shot interval. 0 if ASAP. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint64_t volatile u64NanoFirst;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif /* !IPRT_WITH_POSIX_TIMERS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** The current timer tick. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint64_t volatile iTick;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /** The error/status of the timer.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Initially -1, set to 0 when the timer have been successfully started, and
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * to errno on failure in starting the timer. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int volatile iError;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#else /* IPRT_WITH_POSIX_TIMERS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync timer_t NativeTimer;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif /* IPRT_WITH_POSIX_TIMERS */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync} RTTIMER;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/**
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * RTOnce callback that initalizes the critical section.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @returns RTCritSectInit return code.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param pvUser1 NULL, ignopred.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param pvUser2 NULL, ignopred.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic DECLCALLBACK(int) rtTimerOnce(void *pvUser1, void *pvUser2)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync NOREF(pvUser1);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync NOREF(pvUser2);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return RTCritSectInit(&g_TimerCritSect);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/**
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Signal handler which ignore everything it gets.
8ffcab9595cc0d56977968cd496363502fd814aevboxsync *
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * @param iSignal The signal number.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic void rttimerSignalIgnore(int iSignal)
8ffcab9595cc0d56977968cd496363502fd814aevboxsync{
8ffcab9595cc0d56977968cd496363502fd814aevboxsync //AssertBreakpoint();
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync}
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync/**
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * RT_TIMER_SIGNAL wait thread.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsyncstatic DECLCALLBACK(int) rttimerThread(RTTHREAD Thread, void *pvArg)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync{
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync PRTTIMER pTimer = (PRTTIMER)(void *)pvArg;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTTIMER Timer = *pTimer;
8ffcab9595cc0d56977968cd496363502fd814aevboxsync Assert(pTimer->u32Magic == RTTIMER_MAGIC);
8ffcab9595cc0d56977968cd496363502fd814aevboxsync#endif /* !IPRT_WITH_POSIX_TIMERS */
8ffcab9595cc0d56977968cd496363502fd814aevboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Install signal handler.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync struct sigaction SigAct;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync memset(&SigAct, 0, sizeof(SigAct));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync SigAct.sa_flags = SA_RESTART;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigemptyset(&SigAct.sa_mask);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync SigAct.sa_handler = rttimerSignalIgnore;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync SigAct.sa_flags &= ~SA_RESTART;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (sigaction(RT_TIMER_SIGNAL, &SigAct, NULL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("sigaction failed, errno=%d\n", errno));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Mask most signals except those which might be used by the pthread implementation (linux).
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigset_t SigSet;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigfillset(&SigSet);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, SIGTERM);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, SIGHUP);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, SIGINT);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, SIGABRT);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, SIGKILL);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef SIGRTMIN
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync for (int iSig = SIGRTMIN; iSig < SIGRTMAX; iSig++)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigdelset(&SigSet, iSig);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (sigprocmask(SIG_SETMASK, &SigSet, NULL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc = RTErrConvertFromErrno(errno);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#else
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc = pTimer->iError = RTErrConvertFromErrno(errno);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("sigprocmask -> errno=%d\n", errno));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync return rc;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * The work loop.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadUserSignal(Thread);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifndef IPRT_WITH_POSIX_TIMERS
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync while ( !pTimer->fDestroyed
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync && pTimer->u32Magic == RTTIMER_MAGIC)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Wait for a start or destroy event.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (pTimer->fSuspended)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync int rc = RTSemEventWait(pTimer->Event, RT_INDEFINITE_WAIT);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_FAILURE(rc) && rc != VERR_INTERRUPTED)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertRC(rc);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (pTimer->fDestroyed)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync continue;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadSleep(1000); /* Don't cause trouble! */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if ( pTimer->fSuspended
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync || pTimer->fDestroyed)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync continue;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
150e55a1de2d8702b09de9dd08e488cc9da197d9vboxsync * Start the timer.
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync *
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync * For some SunOS (/SysV?) threading compatibility Linux will only
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync * deliver the RT_TIMER_SIGNAL to the thread calling setitimer(). Therefore
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync * we have to call it here.
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync *
b4feef6ee36ff3c271b06e7e52e22580cc66174bvboxsync * It turns out this might not always be the case, see RT_TIMER_SIGNAL killing
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync * processes on RH 2.4.21.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync struct itimerval TimerVal;
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync if (pTimer->u64NanoFirst)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint64_t u64 = RT_MAX(1000, pTimer->u64NanoFirst);
9a12ad9a1028187595f21d9264898220c1ea565fvboxsync TimerVal.it_value.tv_sec = u64 / 1000000000;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_value.tv_usec = (u64 % 1000000000) / 1000;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync else
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_value.tv_sec = 0;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_value.tv_usec = 10;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (pTimer->u64NanoInterval)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync uint64_t u64 = RT_MAX(1000, pTimer->u64NanoInterval);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_interval.tv_sec = u64 / 1000000000;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_interval.tv_usec = (u64 % 1000000000) / 1000;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync else
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_interval.tv_sec = 0;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync TimerVal.it_interval.tv_usec = 0;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (setitimer(ITIMER_REAL, &TimerVal, NULL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync ASMAtomicXchgU8(&pTimer->fSuspended, true);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pTimer->iError = RTErrConvertFromErrno(errno);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadUserSignal(Thread);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync continue; /* back to suspended mode. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pTimer->iError = 0;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync RTThreadUserSignal(Thread);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Timer Service Loop.
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigemptyset(&SigSet);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync sigaddset(&SigSet, RT_TIMER_SIGNAL);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync do
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync siginfo_t SigInfo = {0};
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#ifdef RT_OS_DARWIN
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_LIKELY(sigwait(&SigSet, &SigInfo.si_signo) >= 0))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#else
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_LIKELY(SigInfo.si_signo == RT_TIMER_SIGNAL))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync#endif
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_UNLIKELY( pTimer->fSuspended
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync || pTimer->fDestroyed
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync || pTimer->u32Magic != RTTIMER_MAGIC))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync break;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /* auto suspend one-shot timers. */
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync if (RT_UNLIKELY(!pTimer->u64NanoInterval))
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync {
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync ASMAtomicWriteU8(&pTimer->fSuspended, true);
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync break;
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync }
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync else if (errno != EINTR)
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync AssertMsgFailed(("sigwaitinfo -> errno=%d\n", errno));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync } while (RT_LIKELY( !pTimer->fSuspended
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync && !pTimer->fDestroyed
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync && pTimer->u32Magic == RTTIMER_MAGIC));
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync /*
d1a00c93378091ef28db9d959b2d692cc8143a07vboxsync * Disable the timer.
*/
struct itimerval TimerVal2 = {{0,0}, {0,0}};
if (setitimer(ITIMER_REAL, &TimerVal2, NULL))
AssertMsgFailed(("setitimer(ITIMER_REAL,&{0}, NULL) failed, errno=%d\n", errno));
/*
* ACK any pending suspend request.
*/
if (!pTimer->fDestroyed)
{
pTimer->iError = 0;
RTThreadUserSignal(Thread);
}
}
/*
* Exit.
*/
pTimer->iError = 0;
RTThreadUserSignal(Thread);
#else /* IPRT_WITH_POSIX_TIMERS */
sigemptyset(&SigSet);
sigaddset(&SigSet, RT_TIMER_SIGNAL);
while (g_cTimerInstances)
{
siginfo_t SigInfo = {0};
if (RT_LIKELY(sigwaitinfo(&SigSet, &SigInfo) >= 0))
{
LogFlow(("rttimerThread: signo=%d pTimer=%p\n", SigInfo.si_signo, SigInfo.si_value.sival_ptr));
if (RT_LIKELY( SigInfo.si_signo == RT_TIMER_SIGNAL
&& SigInfo.si_code == SI_TIMER)) /* The SI_TIMER check is *essential* because of the pthread_kill. */
{
PRTTIMER pTimer = (PRTTIMER)SigInfo.si_value.sival_ptr;
AssertPtr(pTimer);
if (RT_UNLIKELY( !VALID_PTR(pTimer)
|| ASMAtomicUoReadU8(&pTimer->fSuspended)
|| ASMAtomicUoReadU8(&pTimer->fDestroyed)
|| pTimer->u32Magic != RTTIMER_MAGIC))
continue;
pTimer->pfnTimer(pTimer, pTimer->pvUser, ++pTimer->iTick);
/* auto suspend one-shot timers. */
if (RT_UNLIKELY(!pTimer->u64NanoInterval))
ASMAtomicWriteU8(&pTimer->fSuspended, true);
}
}
}
#endif /* IPRT_WITH_POSIX_TIMERS */
return VINF_SUCCESS;
}
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.
*/
struct itimerval TimerVal;
if (getitimer(ITIMER_REAL, &TimerVal))
{
AssertMsgFailed(("getitimer() -> errno=%d\n", errno));
return VERR_NOT_IMPLEMENTED;
}
if ( TimerVal.it_value.tv_usec
|| TimerVal.it_value.tv_sec
|| TimerVal.it_interval.tv_usec
|| TimerVal.it_interval.tv_sec)
{
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.
*/
sigset_t SigSet;
sigemptyset(&SigSet);
sigaddset(&SigSet, RT_TIMER_SIGNAL);
sigprocmask(SIG_BLOCK, &SigSet, NULL);
#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. */
TimerVal.it_interval.tv_sec = 0;
TimerVal.it_interval.tv_usec = 1000;
TimerVal.it_value = TimerVal.it_interval;
if ( setitimer(ITIMER_REAL, &TimerVal, NULL)
|| getitimer(ITIMER_REAL, &TimerVal)
|| TimerVal.it_interval.tv_usec > 1000)
{
/*
* Try open /dev/rtc to set the irq rate to 1024 and
* 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
int fh = open("/dev/rtc", O_RDONLY);
if (fh >= 0)
{
if ( ioctl(fh, RTC_IRQP_SET, 1024) < 0
|| ioctl(fh, RTC_PIE_ON, 0) < 0)
Log(("RTTimerCreate: couldn't configure rtc! errno=%d\n", errno));
ioctl(fh, F_SETFL, O_ASYNC);
ioctl(fh, F_SETOWN, getpid());
/* not so sure if closing it is a good idea... */
//close(fh);
}
else
Log(("RTTimerCreate: couldn't configure rtc! open failed with errno=%d\n", errno));
# endif
}
/* disable it */
TimerVal.it_interval.tv_sec = 0;
TimerVal.it_interval.tv_usec = 0;
TimerVal.it_value = TimerVal.it_interval;
setitimer(ITIMER_REAL, &TimerVal, NULL);
}
/*
* Create a new timer.
*/
int rc;
PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
if (pTimer)
{
pTimer->u32Magic = RTTIMER_MAGIC;
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
pTimer->Thread = NIL_RTTHREAD;
pTimer->Event = NIL_RTSEMEVENT;
pTimer->pfnTimer = pfnTimer;
pTimer->pvUser = pvUser;
pTimer->u64NanoInterval = u64NanoInterval;
pTimer->u64NanoFirst = 0;
pTimer->iTick = 0;
pTimer->iError = 0;
rc = RTSemEventCreate(&pTimer->Event);
AssertRC(rc);
if (RT_SUCCESS(rc))
{
rc = RTThreadCreate(&pTimer->Thread, rttimerThread, pTimer, 0, RTTHREADTYPE_TIMER, RTTHREADFLAGS_WAITABLE, "Timer");
AssertRC(rc);
if (RT_SUCCESS(rc))
{
/*
* Wait for the timer thread to initialize it self.
* This might take a little while...
*/
rc = RTThreadUserWait(pTimer->Thread, 45*1000);
AssertRC(rc);
if (RT_SUCCESS(rc))
{
rc = RTThreadUserReset(pTimer->Thread); AssertRC(rc);
rc = pTimer->iError;
AssertRC(rc);
if (RT_SUCCESS(rc))
{
RTThreadYield(); /* <-- Horrible hack to make tstTimer work. (linux 2.6.12) */
*ppTimer = pTimer;
return VINF_SUCCESS;
}
}
/* bail out */
ASMAtomicXchgU8(&pTimer->fDestroyed, true);
ASMAtomicXchgU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
RTThreadWait(pTimer->Thread, 45*1000, NULL);
}
RTSemEventDestroy(pTimer->Event);
pTimer->Event = NIL_RTSEMEVENT;
}
RTMemFree(pTimer);
}
else
rc = VERR_NO_MEMORY;
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Do the global init first.
*/
int rc = RTOnce(&g_TimerOnce, rtTimerOnce, NULL, NULL);
if (RT_FAILURE(rc))
return rc;
/*
* Create a new timer structure.
*/
LogFlow(("RTTimerCreateEx: u64NanoInterval=%llu fFlags=%lu\n", u64NanoInterval, fFlags));
PRTTIMER pTimer = (PRTTIMER)RTMemAlloc(sizeof(*pTimer));
if (pTimer)
{
/* Initialize timer structure. */
pTimer->u32Magic = RTTIMER_MAGIC;
pTimer->fSuspended = true;
pTimer->fDestroyed = false;
pTimer->pfnTimer = pfnTimer;
pTimer->pvUser = pvUser;
pTimer->u64NanoInterval = u64NanoInterval;
pTimer->iTick = 0;
/*
* Create a timer that deliver RT_TIMER_SIGNAL upon timer expiration.
*/
struct sigevent SigEvt;
SigEvt.sigev_notify = SIGEV_SIGNAL;
SigEvt.sigev_signo = RT_TIMER_SIGNAL;
SigEvt.sigev_value.sival_ptr = pTimer; /* sigev_value gets copied to siginfo. */
int err = timer_create(CLOCK_REALTIME, &SigEvt, &pTimer->NativeTimer);
if (!err)
{
/*
* Increment the timer count, do this behind the critsect to avoid races.
*/
RTCritSectEnter(&g_TimerCritSect);
if (ASMAtomicIncU32(&g_cTimerInstances) != 1)
{
Assert(g_cTimerInstances > 1);
RTCritSectLeave(&g_TimerCritSect);
LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
*ppTimer = pTimer;
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))
{
rc = RTThreadUserWait(g_TimerThread, 45*1000); /* this better not fail... */
if (RT_SUCCESS(rc))
{
RTCritSectLeave(&g_TimerCritSect);
LogFlow(("RTTimerCreateEx: rc=%Rrc pTimer=%p (thread already running)\n", rc, pTimer));
*ppTimer = pTimer;
return VINF_SUCCESS;
}
/* darn, what do we do here? */
}
/* bail out */
ASMAtomicDecU32(&g_cTimerInstances);
Assert(!g_cTimerInstances);
RTCritSectLeave(&g_TimerCritSect);
timer_delete(pTimer->NativeTimer);
}
else
{
rc = RTErrConvertFromErrno(err);
Log(("RTTimerCreateEx: err=%d (%Rrc)\n", err, rc));
}
RTMemFree(pTimer);
}
else
rc = VERR_NO_MEMORY;
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}
RTR3DECL(int) RTTimerDestroy(PRTTIMER pTimer)
{
LogFlow(("RTTimerDestroy: pTimer=%p\n", pTimer));
/*
* Validate input.
*/
/* NULL is ok. */
if (!pTimer)
return VINF_SUCCESS;
int rc = VINF_SUCCESS;
AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
#ifdef IPRT_WITH_POSIX_TIMERS
AssertReturn(g_TimerThread != RTThreadSelf(), VERR_INTERNAL_ERROR);
#else
AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
#endif
/*
* Mark the semaphore as destroyed.
*/
ASMAtomicWriteU8(&pTimer->fDestroyed, true);
ASMAtomicWriteU32(&pTimer->u32Magic, ~RTTIMER_MAGIC);
#ifdef IPRT_WITH_POSIX_TIMERS
/*
* Suspend the timer if it's running.
*/
if (pTimer->fSuspended)
{
struct itimerspec TimerSpec;
TimerSpec.it_value.tv_sec = 0;
TimerSpec.it_value.tv_nsec = 0;
int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL); NOREF(err);
AssertMsg(!err, ("%d\n", err));
}
#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
RTTHREAD Thread = NIL_RTTHREAD;
RTCritSectEnter(&g_TimerCritSect);
if (ASMAtomicDecU32(&g_cTimerInstances) == 0)
{
Thread = g_TimerThread;
g_TimerThread = NIL_RTTHREAD;
}
RTCritSectLeave(&g_TimerCritSect);
#else /* IPRT_WITH_POSIX_TIMERS */
RTTHREAD Thread = pTimer->Thread;
rc = RTSemEventSignal(pTimer->Event);
AssertRC(rc);
#endif /* IPRT_WITH_POSIX_TIMERS */
if (Thread != NIL_RTTHREAD)
{
/* Signal it so it gets out of the sigwait if it's stuck there... */
pthread_kill((pthread_t)RTThreadGetNative(Thread), RT_TIMER_SIGNAL);
/*
* Wait for the thread to complete.
*/
rc = RTThreadWait(Thread, 30 * 1000, NULL);
AssertRC(rc);
}
/*
* Free up the resources associated with the timer.
*/
#ifdef IPRT_WITH_POSIX_TIMERS
timer_delete(pTimer->NativeTimer);
#else
RTSemEventDestroy(pTimer->Event);
pTimer->Event = NIL_RTSEMEVENT;
#endif /* !IPRT_WITH_POSIX_TIMERS */
if (RT_SUCCESS(rc))
RTMemFree(pTimer);
return rc;
}
RTDECL(int) RTTimerStart(PRTTIMER pTimer, uint64_t u64First)
{
/*
* Validate input.
*/
AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
#ifndef IPRT_WITH_POSIX_TIMERS
AssertReturn(pTimer->Thread != RTThreadSelf(), VERR_INTERNAL_ERROR);
#endif
/*
* Already running?
*/
if (!ASMAtomicXchgU8(&pTimer->fSuspended, false))
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.
*/
RTThreadUserReset(pTimer->Thread);
ASMAtomicUoWriteU64(&pTimer->u64NanoFirst, u64First);
ASMAtomicUoWriteU64(&pTimer->iTick, 0);
ASMAtomicWriteU8(&pTimer->fSuspended, false);
int rc = RTSemEventSignal(pTimer->Event);
if (RT_SUCCESS(rc))
{
rc = RTThreadUserWait(pTimer->Thread, 45*1000);
AssertRC(rc);
RTThreadUserReset(pTimer->Thread);
}
else
AssertRC(rc);
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Start the timer.
*/
struct itimerspec TimerSpec;
TimerSpec.it_value.tv_sec = u64First / 1000000000; /* nanosec => sec */
TimerSpec.it_value.tv_nsec = u64First ? u64First % 1000000000 : 10; /* 0 means disable, replace it with 10. */
TimerSpec.it_interval.tv_sec = pTimer->u64NanoInterval / 1000000000;
TimerSpec.it_interval.tv_nsec = pTimer->u64NanoInterval % 1000000000;
int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
int rc = RTErrConvertFromErrno(err);
#endif /* IPRT_WITH_POSIX_TIMERS */
if (RT_FAILURE(rc))
ASMAtomicXchgU8(&pTimer->fSuspended, false);
return rc;
}
RTDECL(int) RTTimerStop(PRTTIMER pTimer)
{
/*
* Validate input.
*/
AssertPtrReturn(pTimer, VERR_INVALID_POINTER);
AssertReturn(pTimer->u32Magic == RTTIMER_MAGIC, VERR_INVALID_MAGIC);
/*
* Already running?
*/
if (ASMAtomicXchgU8(&pTimer->fSuspended, true))
return VERR_TIMER_SUSPENDED;
LogFlow(("RTTimerStop: pTimer=%p\n", pTimer));
#ifndef IPRT_WITH_POSIX_TIMERS
/*
* Tell the thread to stop servicing the timer.
*/
RTThreadUserReset(pTimer->Thread);
ASMAtomicXchgU8(&pTimer->fSuspended, true);
int rc = VINF_SUCCESS;
if (RTThreadSelf() != pTimer->Thread)
{
pthread_kill((pthread_t)RTThreadGetNative(pTimer->Thread), RT_TIMER_SIGNAL);
rc = RTThreadUserWait(pTimer->Thread, 45*1000);
AssertRC(rc);
RTThreadUserReset(pTimer->Thread);
}
#else /* IPRT_WITH_POSIX_TIMERS */
/*
* Stop the timer.
*/
struct itimerspec TimerSpec;
TimerSpec.it_value.tv_sec = 0;
TimerSpec.it_value.tv_nsec = 0;
int err = timer_settime(pTimer->NativeTimer, 0, &TimerSpec, NULL);
int rc = RTErrConvertFromErrno(err);
#endif /* IPRT_WITH_POSIX_TIMERS */
return rc;
}