Kea 2.2.0
interval_timer.cc
Go to the documentation of this file.
1// Copyright (C) 2011-2020 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#include <config.h>
10#include <asiolink/io_service.h>
11
12#include <boost/enable_shared_from_this.hpp>
13#include <boost/noncopyable.hpp>
14#include <boost/shared_ptr.hpp>
15
17
18#include <atomic>
19#include <functional>
20#include <mutex>
21
22using namespace std;
23namespace ph = std::placeholders;
24
25namespace isc {
26namespace asiolink {
27
36 public boost::enable_shared_from_this<IntervalTimerImpl>,
37 public boost::noncopyable {
38public:
39
43 IntervalTimerImpl(IOService& io_service);
44
47
53 void setup(const IntervalTimer::Callback& cbfunc, const long interval,
54 const IntervalTimer::Mode& interval_mode
56
60 void callback(const boost::system::error_code& error);
61
63 void cancel() {
64 lock_guard<mutex> lk (mutex_);
65 timer_.cancel();
66 interval_ = 0;
67 }
68
72 long getInterval() const { return (interval_); }
73
74private:
75
79 void update();
80
83
85 std::atomic<long> interval_;
86
88 boost::asio::deadline_timer timer_;
89
92
94 std::mutex mutex_;
95
100 static const long INVALIDATED_INTERVAL = -1;
101};
102
104 interval_(0), timer_(io_service.get_io_service()),
105 mode_(IntervalTimer::REPEATING) {
106}
107
109 interval_ = INVALIDATED_INTERVAL;
110}
111
112void
114 const long interval,
115 const IntervalTimer::Mode& mode) {
116 // Interval should not be less than 0.
117 if (interval < 0) {
118 isc_throw(isc::BadValue, "Interval should not be less than or "
119 "equal to 0");
120 }
121 // Call back function should not be empty.
122 if (!cbfunc) {
123 isc_throw(isc::InvalidParameter, "Callback function is empty");
124 }
125
126 lock_guard<mutex> lk(mutex_);
127 cbfunc_ = cbfunc;
128 interval_ = interval;
129 mode_ = mode;
130
131 // Set initial expire time.
132 // At this point the timer is not running yet and will not expire.
133 // After calling IOService::run(), the timer will expire.
134 update();
135}
136
137void
138IntervalTimerImpl::update() {
139 try {
140 // Update expire time to (current time + interval_).
141 timer_.expires_from_now(boost::posix_time::millisec(long(interval_)));
142 // Reset timer.
143 // Pass a function bound with a shared_ptr to this.
144 timer_.async_wait(std::bind(&IntervalTimerImpl::callback,
145 shared_from_this(),
146 ph::_1)); //error
147 } catch (const boost::system::system_error& e) {
148 isc_throw(isc::Unexpected, "Failed to update timer: " << e.what());
149 } catch (const boost::bad_weak_ptr&) {
150 // Can't happen. It means a severe internal bug.
151 }
152}
153
154void
155IntervalTimerImpl::callback(const boost::system::error_code& ec) {
156 if (interval_ == INVALIDATED_INTERVAL) {
157 isc_throw(isc::BadValue, "Interval internal state");
158 }
159 if (interval_ == 0 || ec) {
160 // timer has been canceled. Do nothing.
161 } else {
162 {
163 lock_guard<mutex> lk(mutex_);
164 // If we should repeat, set next expire time.
165 if (mode_ == IntervalTimer::REPEATING) {
166 update();
167 }
168 }
169
170 // Invoke the call back function.
171 cbfunc_();
172 }
173}
174
175IntervalTimer::IntervalTimer(IOService& io_service) :
176 impl_(new IntervalTimerImpl(io_service)) {
177}
178
180 // Cancel the timer to make sure cbfunc_() will not be called any more.
181 cancel();
182}
183
184void
185IntervalTimer::setup(const Callback& cbfunc, const long interval,
186 const IntervalTimer::Mode& mode) {
187 return (impl_->setup(cbfunc, interval, mode));
188}
189
190void
192 impl_->cancel();
193}
194
195long
197 return (impl_->getInterval());
198}
199
200} // namespace asiolink
201} // namespace isc
A generic exception that is thrown if a parameter given to a method is considered invalid in that con...
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
A generic exception that is thrown if a parameter given to a method or function is considered invalid...
A generic exception that is thrown when an unexpected error condition occurs.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
@ error
Definition: db_log.h:115
Defines the logger used by the top-level component of kea-lfc.
CompressMode mode_
Definition: rdatafields.cc:97