Realistic 3D camera system
3D camera system components
strand_service.hpp
Go to the documentation of this file.
1 //
2 // detail/impl/strand_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_IMPL_STRAND_SERVICE_HPP
12 #define ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
13 
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17 
24 
26 
27 namespace asio {
28 namespace detail {
29 
31  : operation(&strand_service::do_complete),
32  locked_(false)
33 {
34 }
35 
37 {
40 
42  {
43  impl_->mutex_.lock();
44  impl_->ready_queue_.push(impl_->waiting_queue_);
45  bool more_handlers = impl_->locked_ = !impl_->ready_queue_.empty();
46  impl_->mutex_.unlock();
47 
48  if (more_handlers)
49  io_service_->post_immediate_completion(impl_, false);
50  }
51 };
52 
53 template <typename Handler>
55  Handler& handler)
56 {
57  // If we are already in the strand then the handler can run immediately.
59  {
61  asio_handler_invoke_helpers::invoke(handler, handler);
62  return;
63  }
64 
65  // Allocate and construct an operation to wrap the handler.
66  typedef completion_handler<Handler> op;
67  typename op::ptr p = { asio::detail::addressof(handler),
69  sizeof(op), handler), 0 };
70  p.p = new (p.v) op(handler);
71 
72  ASIO_HANDLER_CREATION((p.p, "strand", impl, "dispatch"));
73 
74  bool dispatch_immediately = do_dispatch(impl, p.p);
75  operation* o = p.p;
76  p.v = p.p = 0;
77 
78  if (dispatch_immediately)
79  {
80  // Indicate that this strand is executing on the current thread.
82 
83  // Ensure the next handler, if any, is scheduled on block exit.
84  on_dispatch_exit on_exit = { &io_service_, impl };
85  (void)on_exit;
86 
88  &io_service_, o, asio::error_code(), 0);
89  }
90 }
91 
92 // Request the io_service to invoke the given handler and return immediately.
93 template <typename Handler>
95  Handler& handler)
96 {
97  bool is_continuation =
99 
100  // Allocate and construct an operation to wrap the handler.
101  typedef completion_handler<Handler> op;
102  typename op::ptr p = { asio::detail::addressof(handler),
104  sizeof(op), handler), 0 };
105  p.p = new (p.v) op(handler);
106 
107  ASIO_HANDLER_CREATION((p.p, "strand", impl, "post"));
108 
109  do_post(impl, p.p, is_continuation);
110  p.v = p.p = 0;
111 }
112 
113 } // namespace detail
114 } // namespace asio
115 
117 
118 #endif // ASIO_DETAIL_IMPL_STRAND_SERVICE_HPP
static void do_complete(io_service_impl *owner, operation *base, const asio::error_code &, std::size_t)
void post(implementation_type &impl, Handler &handler)
asio::basic_streambuf< Allocator > & b
Definition: read.hpp:702
class task_io_service io_service_impl
Definition: io_service.hpp:48
void dispatch(implementation_type &impl, Handler &handler)
void invoke(Function &function, Context &context)
void push(Operation *h)
Definition: op_queue.hpp:104
bool is_continuation(Context &context)
bool empty() const
Definition: op_queue.hpp:136
Class to represent an error code value.
Definition: error_code.hpp:80
task_io_service_operation operation
Definition: operation.hpp:32
void * allocate(std::size_t s, Handler &h)
#define ASIO_HANDLER_CREATION(args)