Statistics
| Revision:

root / tmp / org.txm.statsengine.r.core.win32 / res / win32 / library / BH / include / boost / asio / detail / deadline_timer_service.hpp @ 2486

History | View | Annotate | Download (6.8 kB)

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