Realistic 3D camera system
3D camera system components
task_io_service.hpp
Go to the documentation of this file.
1 //
2 // detail/task_io_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_TASK_IO_SERVICE_HPP
12 #define ASIO_DETAIL_TASK_IO_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 
20 #if !defined(ASIO_HAS_IOCP)
21 
22 #include "asio/error_code.hpp"
23 #include "asio/io_service.hpp"
26 #include "asio/detail/event.hpp"
27 #include "asio/detail/mutex.hpp"
28 #include "asio/detail/op_queue.hpp"
31 
33 
34 namespace asio {
35 namespace detail {
36 
37 struct task_io_service_thread_info;
38 
40  : public asio::detail::service_base<task_io_service>
41 {
42 public:
43  typedef task_io_service_operation operation;
44 
45  // Constructor. Specifies the number of concurrent threads that are likely to
46  // run the io_service. If set to 1 certain optimisation are performed.
48  std::size_t concurrency_hint = 0);
49 
50  // Destroy all user-defined handler objects owned by the service.
52 
53  // Initialise the task, if required.
54  ASIO_DECL void init_task();
55 
56  // Run the event loop until interrupted or no more work.
57  ASIO_DECL std::size_t run(asio::error_code& ec);
58 
59  // Run until interrupted or one operation is performed.
60  ASIO_DECL std::size_t run_one(asio::error_code& ec);
61 
62  // Poll for operations without blocking.
63  ASIO_DECL std::size_t poll(asio::error_code& ec);
64 
65  // Poll for one operation without blocking.
66  ASIO_DECL std::size_t poll_one(asio::error_code& ec);
67 
68  // Interrupt the event processing loop.
69  ASIO_DECL void stop();
70 
71  // Determine whether the io_service is stopped.
72  ASIO_DECL bool stopped() const;
73 
74  // Reset in preparation for a subsequent run invocation.
75  ASIO_DECL void reset();
76 
77  // Notify that some work has started.
78  void work_started()
79  {
80  ++outstanding_work_;
81  }
82 
83  // Notify that some work has finished.
85  {
86  if (--outstanding_work_ == 0)
87  stop();
88  }
89 
90  // Return whether a handler can be dispatched immediately.
91  bool can_dispatch()
92  {
93  return thread_call_stack::contains(this) != 0;
94  }
95 
96  // Request invocation of the given handler.
97  template <typename Handler>
98  void dispatch(Handler& handler);
99 
100  // Request invocation of the given handler and return immediately.
101  template <typename Handler>
102  void post(Handler& handler);
103 
104  // Request invocation of the given operation and return immediately. Assumes
105  // that work_started() has not yet been called for the operation.
107  operation* op, bool is_continuation);
108 
109  // Request invocation of the given operation and return immediately. Assumes
110  // that work_started() was previously called for the operation.
111  ASIO_DECL void post_deferred_completion(operation* op);
112 
113  // Request invocation of the given operations and return immediately. Assumes
114  // that work_started() was previously called for each operation.
116 
117  // Process unfinished operations as part of a shutdown_service operation.
118  // Assumes that work_started() was previously called for the operations.
120 
121 private:
122  // Structure containing thread-specific data.
124 
125  // Enqueue the given operation following a failed attempt to dispatch the
126  // operation for immediate invocation.
127  ASIO_DECL void do_dispatch(operation* op);
128 
129  // Run at most one operation. May block.
130  ASIO_DECL std::size_t do_run_one(mutex::scoped_lock& lock,
131  thread_info& this_thread, const asio::error_code& ec);
132 
133  // Poll for at most one operation.
134  ASIO_DECL std::size_t do_poll_one(mutex::scoped_lock& lock,
135  thread_info& this_thread, const asio::error_code& ec);
136 
137  // Stop the task and all idle threads.
138  ASIO_DECL void stop_all_threads(mutex::scoped_lock& lock);
139 
140  // Wake a single idle thread, or the task, and always unlock the mutex.
141  ASIO_DECL void wake_one_thread_and_unlock(
142  mutex::scoped_lock& lock);
143 
144  // Helper class to perform task-related operations on block exit.
145  struct task_cleanup;
146  friend struct task_cleanup;
147 
148  // Helper class to call work-related operations on block exit.
149  struct work_cleanup;
150  friend struct work_cleanup;
151 
152  // Whether to optimise for single-threaded use cases.
153  const bool one_thread_;
154 
155  // Mutex to protect access to internal data.
156  mutable mutex mutex_;
157 
158  // Event to wake up blocked threads.
159  event wakeup_event_;
160 
161  // The task to be run by this service.
162  reactor* task_;
163 
164  // Operation object to represent the position of the task in the queue.
165  struct task_operation : operation
166  {
167  task_operation() : operation(0) {}
168  } task_operation_;
169 
170  // Whether the task has been interrupted.
171  bool task_interrupted_;
172 
173  // The count of unfinished work.
174  atomic_count outstanding_work_;
175 
176  // The queue of handlers that are ready to be delivered.
177  op_queue<operation> op_queue_;
178 
179  // Flag to indicate that the dispatcher has been stopped.
180  bool stopped_;
181 
182  // Flag to indicate that the dispatcher has been shut down.
183  bool shutdown_;
184 
185  // Per-thread call stack to track the state of each thread in the io_service.
187 };
188 
189 } // namespace detail
190 } // namespace asio
191 
193 
195 #if defined(ASIO_HEADER_ONLY)
197 #endif // defined(ASIO_HEADER_ONLY)
198 
199 #endif // !defined(ASIO_HAS_IOCP)
200 
201 #endif // ASIO_DETAIL_TASK_IO_SERVICE_HPP
Provides core I/O functionality.
Definition: io_service.hpp:184
ASIO_DECL bool stopped() const
ASIO_DECL void abandon_operations(op_queue< operation > &ops)
ASIO_DECL std::size_t run_one(asio::error_code &ec)
static Value * contains(Key *k)
Definition: call_stack.hpp:92
ASIO_DECL std::size_t poll_one(asio::error_code &ec)
ASIO_DECL task_io_service(asio::io_service &io_service, std::size_t concurrency_hint=0)
ASIO_DECL void post_deferred_completions(op_queue< operation > &ops)
ASIO_DECL std::size_t run(asio::error_code &ec)
class select_reactor reactor
Definition: reactor_fwd.hpp:34
void post(Handler &handler)
bool is_continuation(Context &context)
Class to represent an error code value.
Definition: error_code.hpp:80
#define ASIO_DECL
Definition: config.hpp:43
ASIO_DECL void shutdown_service()
Destroy all user-defined handler objects owned by the service.
void dispatch(Handler &handler)
ASIO_DECL std::size_t poll(asio::error_code &ec)
ASIO_DECL void post_deferred_completion(operation *op)
ASIO_DECL void post_immediate_completion(operation *op, bool is_continuation)
task_io_service_operation operation