]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/basic_deadline_timer.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / basic_deadline_timer.hpp
1 //
2 // basic_deadline_timer.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
12 #define BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19
20 #if defined(BOOST_ASIO_HAS_BOOST_DATE_TIME) \
21 || defined(GENERATING_DOCUMENTATION)
22
23 #include <cstddef>
24 #include <boost/asio/basic_io_object.hpp>
25 #include <boost/asio/detail/handler_type_requirements.hpp>
26 #include <boost/asio/detail/throw_error.hpp>
27 #include <boost/asio/error.hpp>
28 #include <boost/asio/time_traits.hpp>
29
30 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
31 # include <boost/asio/deadline_timer_service.hpp>
32 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
33 # include <boost/asio/detail/deadline_timer_service.hpp>
34 # define BOOST_ASIO_SVC_T detail::deadline_timer_service<TimeTraits>
35 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
36
37 #include <boost/asio/detail/push_options.hpp>
38
39 namespace boost {
40 namespace asio {
41
42 /// Provides waitable timer functionality.
43 /**
44 * The basic_deadline_timer class template provides the ability to perform a
45 * blocking or asynchronous wait for a timer to expire.
46 *
47 * A deadline timer is always in one of two states: "expired" or "not expired".
48 * If the wait() or async_wait() function is called on an expired timer, the
49 * wait operation will complete immediately.
50 *
51 * Most applications will use the boost::asio::deadline_timer typedef.
52 *
53 * @par Thread Safety
54 * @e Distinct @e objects: Safe.@n
55 * @e Shared @e objects: Unsafe.
56 *
57 * @par Examples
58 * Performing a blocking wait:
59 * @code
60 * // Construct a timer without setting an expiry time.
61 * boost::asio::deadline_timer timer(io_context);
62 *
63 * // Set an expiry time relative to now.
64 * timer.expires_from_now(boost::posix_time::seconds(5));
65 *
66 * // Wait for the timer to expire.
67 * timer.wait();
68 * @endcode
69 *
70 * @par
71 * Performing an asynchronous wait:
72 * @code
73 * void handler(const boost::system::error_code& error)
74 * {
75 * if (!error)
76 * {
77 * // Timer expired.
78 * }
79 * }
80 *
81 * ...
82 *
83 * // Construct a timer with an absolute expiry time.
84 * boost::asio::deadline_timer timer(io_context,
85 * boost::posix_time::time_from_string("2005-12-07 23:59:59.000"));
86 *
87 * // Start an asynchronous wait.
88 * timer.async_wait(handler);
89 * @endcode
90 *
91 * @par Changing an active deadline_timer's expiry time
92 *
93 * Changing the expiry time of a timer while there are pending asynchronous
94 * waits causes those wait operations to be cancelled. To ensure that the action
95 * associated with the timer is performed only once, use something like this:
96 * used:
97 *
98 * @code
99 * void on_some_event()
100 * {
101 * if (my_timer.expires_from_now(seconds(5)) > 0)
102 * {
103 * // We managed to cancel the timer. Start new asynchronous wait.
104 * my_timer.async_wait(on_timeout);
105 * }
106 * else
107 * {
108 * // Too late, timer has already expired!
109 * }
110 * }
111 *
112 * void on_timeout(const boost::system::error_code& e)
113 * {
114 * if (e != boost::asio::error::operation_aborted)
115 * {
116 * // Timer was not cancelled, take necessary action.
117 * }
118 * }
119 * @endcode
120 *
121 * @li The boost::asio::basic_deadline_timer::expires_from_now() function
122 * cancels any pending asynchronous waits, and returns the number of
123 * asynchronous waits that were cancelled. If it returns 0 then you were too
124 * late and the wait handler has already been executed, or will soon be
125 * executed. If it returns 1 then the wait handler was successfully cancelled.
126 *
127 * @li If a wait handler is cancelled, the boost::system::error_code passed to
128 * it contains the value boost::asio::error::operation_aborted.
129 */
130 template <typename Time,
131 typename TimeTraits = boost::asio::time_traits<Time>
132 BOOST_ASIO_SVC_TPARAM_DEF2(= deadline_timer_service<Time, TimeTraits>)>
133 class basic_deadline_timer
134 : BOOST_ASIO_SVC_ACCESS basic_io_object<BOOST_ASIO_SVC_T>
135 {
136 public:
137 /// The type of the executor associated with the object.
138 typedef io_context::executor_type executor_type;
139
140 /// The time traits type.
141 typedef TimeTraits traits_type;
142
143 /// The time type.
144 typedef typename traits_type::time_type time_type;
145
146 /// The duration type.
147 typedef typename traits_type::duration_type duration_type;
148
149 /// Constructor.
150 /**
151 * This constructor creates a timer without setting an expiry time. The
152 * expires_at() or expires_from_now() functions must be called to set an
153 * expiry time before the timer can be waited on.
154 *
155 * @param io_context The io_context object that the timer will use to dispatch
156 * handlers for any asynchronous operations performed on the timer.
157 */
158 explicit basic_deadline_timer(boost::asio::io_context& io_context)
159 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
160 {
161 }
162
163 /// Constructor to set a particular expiry time as an absolute time.
164 /**
165 * This constructor creates a timer and sets the expiry time.
166 *
167 * @param io_context The io_context object that the timer will use to dispatch
168 * handlers for any asynchronous operations performed on the timer.
169 *
170 * @param expiry_time The expiry time to be used for the timer, expressed
171 * as an absolute time.
172 */
173 basic_deadline_timer(boost::asio::io_context& io_context,
174 const time_type& expiry_time)
175 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
176 {
177 boost::system::error_code ec;
178 this->get_service().expires_at(this->get_implementation(), expiry_time, ec);
179 boost::asio::detail::throw_error(ec, "expires_at");
180 }
181
182 /// Constructor to set a particular expiry time relative to now.
183 /**
184 * This constructor creates a timer and sets the expiry time.
185 *
186 * @param io_context The io_context object that the timer will use to dispatch
187 * handlers for any asynchronous operations performed on the timer.
188 *
189 * @param expiry_time The expiry time to be used for the timer, relative to
190 * now.
191 */
192 basic_deadline_timer(boost::asio::io_context& io_context,
193 const duration_type& expiry_time)
194 : basic_io_object<BOOST_ASIO_SVC_T>(io_context)
195 {
196 boost::system::error_code ec;
197 this->get_service().expires_from_now(
198 this->get_implementation(), expiry_time, ec);
199 boost::asio::detail::throw_error(ec, "expires_from_now");
200 }
201
202 #if defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
203 /// Move-construct a basic_deadline_timer from another.
204 /**
205 * This constructor moves a timer from one object to another.
206 *
207 * @param other The other basic_deadline_timer object from which the move will
208 * occur.
209 *
210 * @note Following the move, the moved-from object is in the same state as if
211 * constructed using the @c basic_deadline_timer(io_context&) constructor.
212 */
213 basic_deadline_timer(basic_deadline_timer&& other)
214 : basic_io_object<BOOST_ASIO_SVC_T>(std::move(other))
215 {
216 }
217
218 /// Move-assign a basic_deadline_timer from another.
219 /**
220 * This assignment operator moves a timer from one object to another. Cancels
221 * any outstanding asynchronous operations associated with the target object.
222 *
223 * @param other The other basic_deadline_timer object from which the move will
224 * occur.
225 *
226 * @note Following the move, the moved-from object is in the same state as if
227 * constructed using the @c basic_deadline_timer(io_context&) constructor.
228 */
229 basic_deadline_timer& operator=(basic_deadline_timer&& other)
230 {
231 basic_io_object<BOOST_ASIO_SVC_T>::operator=(std::move(other));
232 return *this;
233 }
234 #endif // defined(BOOST_ASIO_HAS_MOVE) || defined(GENERATING_DOCUMENTATION)
235
236 /// Destroys the timer.
237 /**
238 * This function destroys the timer, cancelling any outstanding asynchronous
239 * wait operations associated with the timer as if by calling @c cancel.
240 */
241 ~basic_deadline_timer()
242 {
243 }
244
245 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
246 // These functions are provided by basic_io_object<>.
247 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
248 #if !defined(BOOST_ASIO_NO_DEPRECATED)
249 /// (Deprecated: Use get_executor().) Get the io_context associated with the
250 /// object.
251 /**
252 * This function may be used to obtain the io_context object that the I/O
253 * object uses to dispatch handlers for asynchronous operations.
254 *
255 * @return A reference to the io_context object that the I/O object will use
256 * to dispatch handlers. Ownership is not transferred to the caller.
257 */
258 boost::asio::io_context& get_io_context()
259 {
260 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_context();
261 }
262
263 /// (Deprecated: Use get_executor().) Get the io_context associated with the
264 /// object.
265 /**
266 * This function may be used to obtain the io_context object that the I/O
267 * object uses to dispatch handlers for asynchronous operations.
268 *
269 * @return A reference to the io_context object that the I/O object will use
270 * to dispatch handlers. Ownership is not transferred to the caller.
271 */
272 boost::asio::io_context& get_io_service()
273 {
274 return basic_io_object<BOOST_ASIO_SVC_T>::get_io_service();
275 }
276 #endif // !defined(BOOST_ASIO_NO_DEPRECATED)
277
278 /// Get the executor associated with the object.
279 executor_type get_executor() BOOST_ASIO_NOEXCEPT
280 {
281 return basic_io_object<BOOST_ASIO_SVC_T>::get_executor();
282 }
283 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
284
285 /// Cancel any asynchronous operations that are waiting on the timer.
286 /**
287 * This function forces the completion of any pending asynchronous wait
288 * operations against the timer. The handler for each cancelled operation will
289 * be invoked with the boost::asio::error::operation_aborted error code.
290 *
291 * Cancelling the timer does not change the expiry time.
292 *
293 * @return The number of asynchronous operations that were cancelled.
294 *
295 * @throws boost::system::system_error Thrown on failure.
296 *
297 * @note If the timer has already expired when cancel() is called, then the
298 * handlers for asynchronous wait operations will:
299 *
300 * @li have already been invoked; or
301 *
302 * @li have been queued for invocation in the near future.
303 *
304 * These handlers can no longer be cancelled, and therefore are passed an
305 * error code that indicates the successful completion of the wait operation.
306 */
307 std::size_t cancel()
308 {
309 boost::system::error_code ec;
310 std::size_t s = this->get_service().cancel(this->get_implementation(), ec);
311 boost::asio::detail::throw_error(ec, "cancel");
312 return s;
313 }
314
315 /// Cancel any asynchronous operations that are waiting on the timer.
316 /**
317 * This function forces the completion of any pending asynchronous wait
318 * operations against the timer. The handler for each cancelled operation will
319 * be invoked with the boost::asio::error::operation_aborted error code.
320 *
321 * Cancelling the timer does not change the expiry time.
322 *
323 * @param ec Set to indicate what error occurred, if any.
324 *
325 * @return The number of asynchronous operations that were cancelled.
326 *
327 * @note If the timer has already expired when cancel() is called, then the
328 * handlers for asynchronous wait operations will:
329 *
330 * @li have already been invoked; or
331 *
332 * @li have been queued for invocation in the near future.
333 *
334 * These handlers can no longer be cancelled, and therefore are passed an
335 * error code that indicates the successful completion of the wait operation.
336 */
337 std::size_t cancel(boost::system::error_code& ec)
338 {
339 return this->get_service().cancel(this->get_implementation(), ec);
340 }
341
342 /// Cancels one asynchronous operation that is waiting on the timer.
343 /**
344 * This function forces the completion of one pending asynchronous wait
345 * operation against the timer. Handlers are cancelled in FIFO order. The
346 * handler for the cancelled operation will be invoked with the
347 * boost::asio::error::operation_aborted error code.
348 *
349 * Cancelling the timer does not change the expiry time.
350 *
351 * @return The number of asynchronous operations that were cancelled. That is,
352 * either 0 or 1.
353 *
354 * @throws boost::system::system_error Thrown on failure.
355 *
356 * @note If the timer has already expired when cancel_one() is called, then
357 * the handlers for asynchronous wait operations will:
358 *
359 * @li have already been invoked; or
360 *
361 * @li have been queued for invocation in the near future.
362 *
363 * These handlers can no longer be cancelled, and therefore are passed an
364 * error code that indicates the successful completion of the wait operation.
365 */
366 std::size_t cancel_one()
367 {
368 boost::system::error_code ec;
369 std::size_t s = this->get_service().cancel_one(
370 this->get_implementation(), ec);
371 boost::asio::detail::throw_error(ec, "cancel_one");
372 return s;
373 }
374
375 /// Cancels one asynchronous operation that is waiting on the timer.
376 /**
377 * This function forces the completion of one pending asynchronous wait
378 * operation against the timer. Handlers are cancelled in FIFO order. The
379 * handler for the cancelled operation will be invoked with the
380 * boost::asio::error::operation_aborted error code.
381 *
382 * Cancelling the timer does not change the expiry time.
383 *
384 * @param ec Set to indicate what error occurred, if any.
385 *
386 * @return The number of asynchronous operations that were cancelled. That is,
387 * either 0 or 1.
388 *
389 * @note If the timer has already expired when cancel_one() is called, then
390 * the handlers for asynchronous wait operations will:
391 *
392 * @li have already been invoked; or
393 *
394 * @li have been queued for invocation in the near future.
395 *
396 * These handlers can no longer be cancelled, and therefore are passed an
397 * error code that indicates the successful completion of the wait operation.
398 */
399 std::size_t cancel_one(boost::system::error_code& ec)
400 {
401 return this->get_service().cancel_one(this->get_implementation(), ec);
402 }
403
404 /// Get the timer's expiry time as an absolute time.
405 /**
406 * This function may be used to obtain the timer's current expiry time.
407 * Whether the timer has expired or not does not affect this value.
408 */
409 time_type expires_at() const
410 {
411 return this->get_service().expires_at(this->get_implementation());
412 }
413
414 /// Set the timer's expiry time as an absolute time.
415 /**
416 * This function sets the expiry time. Any pending asynchronous wait
417 * operations will be cancelled. The handler for each cancelled operation will
418 * be invoked with the boost::asio::error::operation_aborted error code.
419 *
420 * @param expiry_time The expiry time to be used for the timer.
421 *
422 * @return The number of asynchronous operations that were cancelled.
423 *
424 * @throws boost::system::system_error Thrown on failure.
425 *
426 * @note If the timer has already expired when expires_at() is called, then
427 * the handlers for asynchronous wait operations will:
428 *
429 * @li have already been invoked; or
430 *
431 * @li have been queued for invocation in the near future.
432 *
433 * These handlers can no longer be cancelled, and therefore are passed an
434 * error code that indicates the successful completion of the wait operation.
435 */
436 std::size_t expires_at(const time_type& expiry_time)
437 {
438 boost::system::error_code ec;
439 std::size_t s = this->get_service().expires_at(
440 this->get_implementation(), expiry_time, ec);
441 boost::asio::detail::throw_error(ec, "expires_at");
442 return s;
443 }
444
445 /// Set the timer's expiry time as an absolute time.
446 /**
447 * This function sets the expiry time. Any pending asynchronous wait
448 * operations will be cancelled. The handler for each cancelled operation will
449 * be invoked with the boost::asio::error::operation_aborted error code.
450 *
451 * @param expiry_time The expiry time to be used for the timer.
452 *
453 * @param ec Set to indicate what error occurred, if any.
454 *
455 * @return The number of asynchronous operations that were cancelled.
456 *
457 * @note If the timer has already expired when expires_at() is called, then
458 * the handlers for asynchronous wait operations will:
459 *
460 * @li have already been invoked; or
461 *
462 * @li have been queued for invocation in the near future.
463 *
464 * These handlers can no longer be cancelled, and therefore are passed an
465 * error code that indicates the successful completion of the wait operation.
466 */
467 std::size_t expires_at(const time_type& expiry_time,
468 boost::system::error_code& ec)
469 {
470 return this->get_service().expires_at(
471 this->get_implementation(), expiry_time, ec);
472 }
473
474 /// Get the timer's expiry time relative to now.
475 /**
476 * This function may be used to obtain the timer's current expiry time.
477 * Whether the timer has expired or not does not affect this value.
478 */
479 duration_type expires_from_now() const
480 {
481 return this->get_service().expires_from_now(this->get_implementation());
482 }
483
484 /// Set the timer's expiry time relative to now.
485 /**
486 * This function sets the expiry time. Any pending asynchronous wait
487 * operations will be cancelled. The handler for each cancelled operation will
488 * be invoked with the boost::asio::error::operation_aborted error code.
489 *
490 * @param expiry_time The expiry time to be used for the timer.
491 *
492 * @return The number of asynchronous operations that were cancelled.
493 *
494 * @throws boost::system::system_error Thrown on failure.
495 *
496 * @note If the timer has already expired when expires_from_now() is called,
497 * then the handlers for asynchronous wait operations will:
498 *
499 * @li have already been invoked; or
500 *
501 * @li have been queued for invocation in the near future.
502 *
503 * These handlers can no longer be cancelled, and therefore are passed an
504 * error code that indicates the successful completion of the wait operation.
505 */
506 std::size_t expires_from_now(const duration_type& expiry_time)
507 {
508 boost::system::error_code ec;
509 std::size_t s = this->get_service().expires_from_now(
510 this->get_implementation(), expiry_time, ec);
511 boost::asio::detail::throw_error(ec, "expires_from_now");
512 return s;
513 }
514
515 /// Set the timer's expiry time relative to now.
516 /**
517 * This function sets the expiry time. Any pending asynchronous wait
518 * operations will be cancelled. The handler for each cancelled operation will
519 * be invoked with the boost::asio::error::operation_aborted error code.
520 *
521 * @param expiry_time The expiry time to be used for the timer.
522 *
523 * @param ec Set to indicate what error occurred, if any.
524 *
525 * @return The number of asynchronous operations that were cancelled.
526 *
527 * @note If the timer has already expired when expires_from_now() is called,
528 * then the handlers for asynchronous wait operations will:
529 *
530 * @li have already been invoked; or
531 *
532 * @li have been queued for invocation in the near future.
533 *
534 * These handlers can no longer be cancelled, and therefore are passed an
535 * error code that indicates the successful completion of the wait operation.
536 */
537 std::size_t expires_from_now(const duration_type& expiry_time,
538 boost::system::error_code& ec)
539 {
540 return this->get_service().expires_from_now(
541 this->get_implementation(), expiry_time, ec);
542 }
543
544 /// Perform a blocking wait on the timer.
545 /**
546 * This function is used to wait for the timer to expire. This function
547 * blocks and does not return until the timer has expired.
548 *
549 * @throws boost::system::system_error Thrown on failure.
550 */
551 void wait()
552 {
553 boost::system::error_code ec;
554 this->get_service().wait(this->get_implementation(), ec);
555 boost::asio::detail::throw_error(ec, "wait");
556 }
557
558 /// Perform a blocking wait on the timer.
559 /**
560 * This function is used to wait for the timer to expire. This function
561 * blocks and does not return until the timer has expired.
562 *
563 * @param ec Set to indicate what error occurred, if any.
564 */
565 void wait(boost::system::error_code& ec)
566 {
567 this->get_service().wait(this->get_implementation(), ec);
568 }
569
570 /// Start an asynchronous wait on the timer.
571 /**
572 * This function may be used to initiate an asynchronous wait against the
573 * timer. It always returns immediately.
574 *
575 * For each call to async_wait(), the supplied handler will be called exactly
576 * once. The handler will be called when:
577 *
578 * @li The timer has expired.
579 *
580 * @li The timer was cancelled, in which case the handler is passed the error
581 * code boost::asio::error::operation_aborted.
582 *
583 * @param handler The handler to be called when the timer expires. Copies
584 * will be made of the handler as required. The function signature of the
585 * handler must be:
586 * @code void handler(
587 * const boost::system::error_code& error // Result of operation.
588 * ); @endcode
589 * Regardless of whether the asynchronous operation completes immediately or
590 * not, the handler will not be invoked from within this function. Invocation
591 * of the handler will be performed in a manner equivalent to using
592 * boost::asio::io_context::post().
593 */
594 template <typename WaitHandler>
595 BOOST_ASIO_INITFN_RESULT_TYPE(WaitHandler,
596 void (boost::system::error_code))
597 async_wait(BOOST_ASIO_MOVE_ARG(WaitHandler) handler)
598 {
599 // If you get an error on the following line it means that your handler does
600 // not meet the documented type requirements for a WaitHandler.
601 BOOST_ASIO_WAIT_HANDLER_CHECK(WaitHandler, handler) type_check;
602
603 #if defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
604 return this->get_service().async_wait(this->get_implementation(),
605 BOOST_ASIO_MOVE_CAST(WaitHandler)(handler));
606 #else // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
607 async_completion<WaitHandler,
608 void (boost::system::error_code)> init(handler);
609
610 this->get_service().async_wait(this->get_implementation(),
611 init.completion_handler);
612
613 return init.result.get();
614 #endif // defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
615 }
616 };
617
618 } // namespace asio
619 } // namespace boost
620
621 #include <boost/asio/detail/pop_options.hpp>
622
623 #if !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
624 # undef BOOST_ASIO_SVC_T
625 #endif // !defined(BOOST_ASIO_ENABLE_OLD_SERVICES)
626
627 #endif // defined(BOOST_ASIO_HAS_BOOST_DATE_TIME)
628 // || defined(GENERATING_DOCUMENTATION)
629
630 #endif // BOOST_ASIO_BASIC_DEADLINE_TIMER_HPP