2 * Copyright (c) 2011, 2012, 2013 Spectra Logic Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
12 * substantially similar to the "NO WARRANTY" disclaimer below
13 * ("Disclaimer") and any redistribution must be conditioned upon
14 * including a substantially similar Disclaimer requirement for further
15 * binary redistribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
27 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGES.
30 * Authors: Justin T. Gibbs (Spectra Logic Corporation)
38 * \brief Interface for timer based callback services.
40 * Header requirements:
42 * #include <sys/time.h>
51 * \brief Type of the function callback from a Callout.
53 typedef void CalloutFunc_t(void *);
56 * \brief Interface to a schedulable one-shot timer with the granularity
57 * of the system clock (see setitimer(2)).
59 * Determination of callback expiration is triggered by the SIGALRM
60 * signal. Callout callbacks are always delivered from Zfsd's event
63 * Periodic actions can be triggered via the Callout mechanisms by
64 * resetting the Callout from within its callback.
71 * Initialize the Callout subsystem.
76 * Function called (via SIGALRM) when our interval
79 static void AlarmSignalHandler(int);
82 * Execute callbacks for all callouts that have the same
83 * expiration time as the first callout in the list.
85 static void ExpireCallouts();
91 * Returns true if callout has not been stopped,
92 * or deactivated since the last time the callout was
95 bool IsActive() const;
98 * Returns true if callout is still waiting to expire.
100 bool IsPending() const;
103 * Disestablish a callout.
108 * \brief Establish or change a timeout.
110 * \param interval Timeval indicating the time which must elapse
111 * before this callout fires.
112 * \param func Pointer to the callback function
113 * \param arg Argument pointer to pass to callback function
115 * \return Cancellation status.
116 * true: The previous callback was pending and therefore
118 * false: The callout was not pending at the time of this
120 * In all cases, a new callout is established.
122 bool Reset(const timeval &interval, CalloutFunc_t *func, void *arg);
125 * \brief Calculate the remaining time until this Callout's timer
128 * The return value will be slightly greater than the actual time to
131 * If the callout is not pending, returns INT_MAX.
133 timeval TimeRemaining() const;
137 * All active callouts sorted by expiration time. The callout
138 * with the nearest expiration time is at the head of the list.
140 static std::list<Callout *> s_activeCallouts;
143 * The interval timer has expired. This variable is set from
144 * signal handler context and tested from Zfsd::EventLoop()
145 * context via ExpireCallouts().
147 static bool s_alarmFired;
150 * Time, relative to others in the active list, until
151 * this callout is fired.
155 /** Callback function argument. */
159 * The callback function associated with this timer
162 CalloutFunc_t *m_func;
164 /** State of this callout. */
168 //- Callout public const methods ----------------------------------------------
170 Callout::IsPending() const
175 //- Callout public methods ----------------------------------------------------
182 timerclear(&m_interval);
185 #endif /* CALLOUT_H_ */