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 Implementation of the Callout class - multi-client
39 * timer services built on top of the POSIX interval timer.
42 #include <sys/byteorder.h>
53 #include <devdctl/guid.h>
54 #include <devdctl/event.h>
55 #include <devdctl/event_factory.h>
56 #include <devdctl/consumer.h>
57 #include <devdctl/exception.h>
60 #include "vdev_iterator.h"
62 #include "zfsd_exception.h"
64 std::list<Callout *> Callout::s_activeCallouts;
65 bool Callout::s_alarmFired(false);
70 signal(SIGALRM, Callout::AlarmSignalHandler);
79 for (std::list<Callout *>::iterator it(s_activeCallouts.begin());
80 it != s_activeCallouts.end(); it++) {
84 it = s_activeCallouts.erase(it);
85 if (it != s_activeCallouts.end()) {
88 * Maintain correct interval for the
89 * callouts that follow the just removed
92 timeradd(&(*it)->m_interval, &m_interval,
102 Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg)
104 bool cancelled(false);
106 if (!timerisset(&interval))
107 throw ZfsdException("Callout::Reset: interval of 0");
111 m_interval = interval;
116 std::list<Callout *>::iterator it(s_activeCallouts.begin());
117 for (; it != s_activeCallouts.end(); it++) {
119 if (timercmp(&(*it)->m_interval, &m_interval, <=)) {
121 * Decrease our interval by those that come
124 timersub(&m_interval, &(*it)->m_interval, &m_interval);
127 * Account for the time between the newly
128 * inserted event and those that follow.
130 timersub(&(*it)->m_interval, &m_interval,
135 s_activeCallouts.insert(it, this);
138 if (s_activeCallouts.front() == this) {
139 itimerval timerval = { {0, 0}, m_interval };
141 setitimer(ITIMER_REAL, &timerval, NULL);
148 Callout::AlarmSignalHandler(int)
151 ZfsDaemon::WakeEventLoop();
155 Callout::ExpireCallouts()
160 s_alarmFired = false;
161 if (s_activeCallouts.empty()) {
162 /* Callout removal/SIGALRM race was lost. */
167 * Expire the first callout (the one we used to set the
168 * interval timer) as well as any callouts following that
169 * expire at the same time (have a zero interval from
170 * the callout before it).
173 Callout *cur(s_activeCallouts.front());
174 s_activeCallouts.pop_front();
175 cur->m_pending = false;
176 cur->m_func(cur->m_arg);
177 } while (!s_activeCallouts.empty()
178 && timerisset(&s_activeCallouts.front()->m_interval) == 0);
180 if (!s_activeCallouts.empty()) {
181 Callout *next(s_activeCallouts.front());
182 itimerval timerval = { { 0, 0 }, next->m_interval };
184 setitimer(ITIMER_REAL, &timerval, NULL);
189 Callout::TimeRemaining() const
192 * Outline: Add the m_interval for each callout in s_activeCallouts
193 * ahead of this, except for the first callout. Add to that the result
194 * of getitimer (That's because the first callout stores its original
195 * interval setting while the timer is ticking).
197 itimerval timervalToAlarm;
198 timeval timeToExpiry;
199 std::list<Callout *>::iterator it;
202 timeToExpiry.tv_sec = INT_MAX;
203 timeToExpiry.tv_usec = 999999; /*maximum normalized value*/
204 return (timeToExpiry);
207 timerclear(&timeToExpiry);
208 getitimer(ITIMER_REAL, &timervalToAlarm);
209 timeval& timeToAlarm = timervalToAlarm.it_value;
210 timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry);
212 it =s_activeCallouts.begin();
213 it++; /*skip the first callout in the list*/
214 for (; it != s_activeCallouts.end(); it++) {
215 timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry);
219 return (timeToExpiry);