Realistic 3D camera system
3D camera system components
deadline_timer_service.hpp
Go to the documentation of this file.
1 //
2 // detail/deadline_timer_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com)
6 //
7 // Distributed under the Boost Software License, Version 1.0. (See accompanying
8 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
9 //
10 
11 #ifndef ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
12 #define ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
18 #include "asio/detail/config.hpp"
19 #include <cstddef>
20 #include "asio/error.hpp"
21 #include "asio/io_service.hpp"
31 #include "asio/detail/wait_op.hpp"
32 
33 #if defined(ASIO_WINDOWS_RUNTIME)
34 # include <chrono>
35 # include <thread>
36 #endif // defined(ASIO_WINDOWS_RUNTIME)
37 
39 
40 namespace asio {
41 namespace detail {
42 
43 template <typename Time_Traits>
45 {
46 public:
47  // The time type.
48  typedef typename Time_Traits::time_type time_type;
49 
50  // The duration type.
51  typedef typename Time_Traits::duration_type duration_type;
52 
53  // The implementation type of the timer. This type is dependent on the
54  // underlying implementation of the timer service.
57  {
58  time_type expiry;
61  };
62 
63  // Constructor.
65  : scheduler_(asio::use_service<timer_scheduler>(io_service))
66  {
67  scheduler_.init_task();
68  scheduler_.add_timer_queue(timer_queue_);
69  }
70 
71  // Destructor.
73  {
74  scheduler_.remove_timer_queue(timer_queue_);
75  }
76 
77  // Destroy all user-defined handler objects owned by the service.
79  {
80  }
81 
82  // Construct a new timer implementation.
83  void construct(implementation_type& impl)
84  {
85  impl.expiry = time_type();
86  impl.might_have_pending_waits = false;
87  }
88 
89  // Destroy a timer implementation.
90  void destroy(implementation_type& impl)
91  {
93  cancel(impl, ec);
94  }
95 
96  // Cancel any asynchronous wait operations associated with the timer.
97  std::size_t cancel(implementation_type& impl, asio::error_code& ec)
98  {
99  if (!impl.might_have_pending_waits)
100  {
101  ec = asio::error_code();
102  return 0;
103  }
104 
105  ASIO_HANDLER_OPERATION(("deadline_timer", &impl, "cancel"));
106 
107  std::size_t count = scheduler_.cancel_timer(timer_queue_, impl.timer_data);
108  impl.might_have_pending_waits = false;
109  ec = asio::error_code();
110  return count;
111  }
112 
113  // Cancels one asynchronous wait operation associated with the timer.
114  std::size_t cancel_one(implementation_type& impl,
115  asio::error_code& ec)
116  {
117  if (!impl.might_have_pending_waits)
118  {
119  ec = asio::error_code();
120  return 0;
121  }
122 
123  ASIO_HANDLER_OPERATION(("deadline_timer", &impl, "cancel_one"));
124 
125  std::size_t count = scheduler_.cancel_timer(
126  timer_queue_, impl.timer_data, 1);
127  if (count == 0)
128  impl.might_have_pending_waits = false;
129  ec = asio::error_code();
130  return count;
131  }
132 
133  // Get the expiry time for the timer as an absolute time.
134  time_type expires_at(const implementation_type& impl) const
135  {
136  return impl.expiry;
137  }
138 
139  // Set the expiry time for the timer as an absolute time.
140  std::size_t expires_at(implementation_type& impl,
141  const time_type& expiry_time, asio::error_code& ec)
142  {
143  std::size_t count = cancel(impl, ec);
144  impl.expiry = expiry_time;
145  ec = asio::error_code();
146  return count;
147  }
148 
149  // Get the expiry time for the timer relative to now.
150  duration_type expires_from_now(const implementation_type& impl) const
151  {
152  return Time_Traits::subtract(expires_at(impl), Time_Traits::now());
153  }
154 
155  // Set the expiry time for the timer relative to now.
156  std::size_t expires_from_now(implementation_type& impl,
157  const duration_type& expiry_time, asio::error_code& ec)
158  {
159  return expires_at(impl,
160  Time_Traits::add(Time_Traits::now(), expiry_time), ec);
161  }
162 
163  // Perform a blocking wait on the timer.
164  void wait(implementation_type& impl, asio::error_code& ec)
165  {
166  time_type now = Time_Traits::now();
167  ec = asio::error_code();
168  while (Time_Traits::less_than(now, impl.expiry) && !ec)
169  {
170  this->do_wait(Time_Traits::to_posix_duration(
171  Time_Traits::subtract(impl.expiry, now)), ec);
172  now = Time_Traits::now();
173  }
174  }
175 
176  // Start an asynchronous wait on the timer.
177  template <typename Handler>
178  void async_wait(implementation_type& impl, Handler& handler)
179  {
180  // Allocate and construct an operation to wrap the handler.
181  typedef wait_handler<Handler> op;
182  typename op::ptr p = { asio::detail::addressof(handler),
184  sizeof(op), handler), 0 };
185  p.p = new (p.v) op(handler);
186 
187  impl.might_have_pending_waits = true;
188 
189  ASIO_HANDLER_CREATION((p.p, "deadline_timer", &impl, "async_wait"));
190 
191  scheduler_.schedule_timer(timer_queue_, impl.expiry, impl.timer_data, p.p);
192  p.v = p.p = 0;
193  }
194 
195 private:
196  // Helper function to wait given a duration type. The duration type should
197  // either be of type boost::posix_time::time_duration, or implement the
198  // required subset of its interface.
199  template <typename Duration>
200  void do_wait(const Duration& timeout, asio::error_code& ec)
201  {
202 #if defined(ASIO_WINDOWS_RUNTIME)
203  std::this_thread::sleep_for(
204  std::chrono::seconds(timeout.total_seconds())
205  + std::chrono::microseconds(timeout.total_microseconds()));
206  ec = asio::error_code();
207 #else // defined(ASIO_WINDOWS_RUNTIME)
208  ::timeval tv;
209  tv.tv_sec = timeout.total_seconds();
210  tv.tv_usec = timeout.total_microseconds() % 1000000;
211  socket_ops::select(0, 0, 0, 0, &tv, ec);
212 #endif // defined(ASIO_WINDOWS_RUNTIME)
213  }
214 
215  // The queue of timers.
216  timer_queue<Time_Traits> timer_queue_;
217 
218  // The object that schedules and executes timers. Usually a reactor.
219  timer_scheduler& scheduler_;
220 };
221 
222 } // namespace detail
223 } // namespace asio
224 
226 
227 #endif // ASIO_DETAIL_DEADLINE_TIMER_SERVICE_HPP
class select_reactor timer_scheduler
std::size_t expires_at(implementation_type &impl, const time_type &expiry_time, asio::error_code &ec)
Provides core I/O functionality.
Definition: io_service.hpp:184
std::size_t expires_from_now(implementation_type &impl, const duration_type &expiry_time, asio::error_code &ec)
Service & use_service(io_service &ios)
Definition: io_service.hpp:26
int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, timeval *timeout, asio::error_code &ec)
std::size_t cancel_one(implementation_type &impl, asio::error_code &ec)
deadline_timer_service(asio::io_service &io_service)
void wait(implementation_type &impl, asio::error_code &ec)
duration_type expires_from_now(const implementation_type &impl) const
void async_wait(implementation_type &impl, Handler &handler)
time_type expires_at(const implementation_type &impl) const
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_HANDLER_OPERATION(args)
void * allocate(std::size_t s, Handler &h)
void destroy(implementation_type &impl)
void construct(implementation_type &impl)
#define ASIO_HANDLER_CREATION(args)
std::size_t cancel(implementation_type &impl, asio::error_code &ec)