/*-1* Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions, and the following disclaimer,9* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* substantially similar to the "NO WARRANTY" disclaimer below12* ("Disclaimer") and any redistribution must be conditioned upon13* including a substantially similar Disclaimer requirement for further14* binary redistribution.15*16* NO WARRANTY17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR20* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,25* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING26* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGES.28*29* Authors: Justin T. Gibbs (Spectra Logic Corporation)30*/3132/**33* \file callout.h34*35* \brief Interface for timer based callback services.36*37* Header requirements:38*39* #include <sys/time.h>40*41* #include <list>42*/4344#ifndef _CALLOUT_H_45#define _CALLOUT_H_4647/**48* \brief Type of the function callback from a Callout.49*/50typedef void CalloutFunc_t(void *);5152/**53* \brief Interface to a schedulable one-shot timer with the granularity54* of the system clock (see setitimer(2)).55*56* Determination of callback expiration is triggered by the SIGALRM57* signal. Callout callbacks are always delivered from Zfsd's event58* processing loop.59*60* Periodic actions can be triggered via the Callout mechanisms by61* resetting the Callout from within its callback.62*/63class Callout64{65public:6667/**68* Initialize the Callout subsystem.69*/70static void Init();7172/**73* Function called (via SIGALRM) when our interval74* timer expires.75*/76static void AlarmSignalHandler(int);7778/**79* Execute callbacks for all callouts that have the same80* expiration time as the first callout in the list.81*/82static void ExpireCallouts();8384/** Constructor. */85Callout();8687/**88* Returns true if callout has not been stopped,89* or deactivated since the last time the callout was90* reset.91*/92bool IsActive() const;9394/**95* Returns true if callout is still waiting to expire.96*/97bool IsPending() const;9899/**100* Disestablish a callout.101*/102bool Stop();103104/**105* \brief Establish or change a timeout.106*107* \param interval Timeval indicating the time which must elapse108* before this callout fires.109* \param func Pointer to the callback function110* \param arg Argument pointer to pass to callback function111*112* \return Cancellation status.113* true: The previous callback was pending and therefore114* was cancelled.115* false: The callout was not pending at the time of this116* reset request.117* In all cases, a new callout is established.118*/119bool Reset(const timeval &interval, CalloutFunc_t *func, void *arg);120121/**122* \brief Calculate the remaining time until this Callout's timer123* expires.124*125* The return value will be slightly greater than the actual time to126* expiry.127*128* If the callout is not pending, returns INT_MAX.129*/130timeval TimeRemaining() const;131132private:133/**134* All active callouts sorted by expiration time. The callout135* with the nearest expiration time is at the head of the list.136*/137static std::list<Callout *> s_activeCallouts;138139/**140* The interval timer has expired. This variable is set from141* signal handler context and tested from Zfsd::EventLoop()142* context via ExpireCallouts().143*/144static bool s_alarmFired;145146/**147* Time, relative to others in the active list, until148* this callout is fired.149*/150timeval m_interval;151152/** Callback function argument. */153void *m_arg;154155/**156* The callback function associated with this timer157* entry.158*/159CalloutFunc_t *m_func;160161/** State of this callout. */162bool m_pending;163};164165//- Callout public const methods ----------------------------------------------166inline bool167Callout::IsPending() const168{169return (m_pending);170}171172//- Callout public methods ----------------------------------------------------173inline174Callout::Callout()175: m_arg(0),176m_func(NULL),177m_pending(false)178{179timerclear(&m_interval);180}181182#endif /* CALLOUT_H_ */183184185