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.
52 #include <devdctl/guid.h>
53 #include <devdctl/event.h>
54 #include <devdctl/event_factory.h>
55 #include <devdctl/consumer.h>
56 #include <devdctl/exception.h>
59 #include "vdev_iterator.h"
61 #include "zfsd_exception.h"
63 std::list<Callout *> Callout::s_activeCallouts;
64 bool Callout::s_alarmFired(false);
69 signal(SIGALRM, Callout::AlarmSignalHandler);
78 for (std::list<Callout *>::iterator it(s_activeCallouts.begin());
79 it != s_activeCallouts.end(); it++) {
83 it = s_activeCallouts.erase(it);
84 if (it != s_activeCallouts.end()) {
87 * Maintain correct interval for the
88 * callouts that follow the just removed
91 timeradd(&(*it)->m_interval, &m_interval,
101 Callout::Reset(const timeval &interval, CalloutFunc_t *func, void *arg)
103 bool cancelled(false);
105 if (!timerisset(&interval))
106 throw ZfsdException("Callout::Reset: interval of 0");
110 m_interval = interval;
115 std::list<Callout *>::iterator it(s_activeCallouts.begin());
116 for (; it != s_activeCallouts.end(); it++) {
118 if (timercmp(&(*it)->m_interval, &m_interval, <=)) {
120 * Decrease our interval by those that come
123 timersub(&m_interval, &(*it)->m_interval, &m_interval);
126 * Account for the time between the newly
127 * inserted event and those that follow.
129 timersub(&(*it)->m_interval, &m_interval,
134 s_activeCallouts.insert(it, this);
137 if (s_activeCallouts.front() == this) {
138 itimerval timerval = { {0, 0}, m_interval };
140 setitimer(ITIMER_REAL, &timerval, NULL);
147 Callout::AlarmSignalHandler(int)
150 ZfsDaemon::WakeEventLoop();
154 Callout::ExpireCallouts()
159 s_alarmFired = false;
160 if (s_activeCallouts.empty()) {
161 /* Callout removal/SIGALRM race was lost. */
166 * Expire the first callout (the one we used to set the
167 * interval timer) as well as any callouts following that
168 * expire at the same time (have a zero interval from
169 * the callout before it).
172 Callout *cur(s_activeCallouts.front());
173 s_activeCallouts.pop_front();
174 cur->m_pending = false;
175 cur->m_func(cur->m_arg);
176 } while (!s_activeCallouts.empty()
177 && timerisset(&s_activeCallouts.front()->m_interval) == 0);
179 if (!s_activeCallouts.empty()) {
180 Callout *next(s_activeCallouts.front());
181 itimerval timerval = { { 0, 0 }, next->m_interval };
183 setitimer(ITIMER_REAL, &timerval, NULL);
188 Callout::TimeRemaining() const
191 * Outline: Add the m_interval for each callout in s_activeCallouts
192 * ahead of this, except for the first callout. Add to that the result
193 * of getitimer (That's because the first callout stores its original
194 * interval setting while the timer is ticking).
196 itimerval timervalToAlarm;
197 timeval timeToExpiry;
198 std::list<Callout *>::iterator it;
201 timeToExpiry.tv_sec = INT_MAX;
202 timeToExpiry.tv_usec = 999999; /*maximum normalized value*/
203 return (timeToExpiry);
206 timerclear(&timeToExpiry);
207 getitimer(ITIMER_REAL, &timervalToAlarm);
208 timeval& timeToAlarm = timervalToAlarm.it_value;
209 timeradd(&timeToExpiry, &timeToAlarm, &timeToExpiry);
211 it =s_activeCallouts.begin();
212 it++; /*skip the first callout in the list*/
213 for (; it != s_activeCallouts.end(); it++) {
214 timeradd(&timeToExpiry, &(*it)->m_interval, &timeToExpiry);
218 return (timeToExpiry);