2 // detail/impl/task_io_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2016 Christopher M. Kohlhoff (chris at kohlhoff dot com)
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)
11 #ifndef BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
18 #include <boost/asio/detail/config.hpp>
20 #if !defined(BOOST_ASIO_HAS_IOCP)
22 #include <boost/asio/detail/event.hpp>
23 #include <boost/asio/detail/limits.hpp>
24 #include <boost/asio/detail/reactor.hpp>
25 #include <boost/asio/detail/task_io_service.hpp>
26 #include <boost/asio/detail/task_io_service_thread_info.hpp>
28 #include <boost/asio/detail/push_options.hpp>
34 struct task_io_service::task_cleanup
38 if (this_thread_->private_outstanding_work > 0)
40 boost::asio::detail::increment(
41 task_io_service_->outstanding_work_,
42 this_thread_->private_outstanding_work);
44 this_thread_->private_outstanding_work = 0;
46 // Enqueue the completed operations and reinsert the task at the end of
47 // the operation queue.
49 task_io_service_->task_interrupted_ = true;
50 task_io_service_->op_queue_.push(this_thread_->private_op_queue);
51 task_io_service_->op_queue_.push(&task_io_service_->task_operation_);
54 task_io_service* task_io_service_;
55 mutex::scoped_lock* lock_;
56 thread_info* this_thread_;
59 struct task_io_service::work_cleanup
63 if (this_thread_->private_outstanding_work > 1)
65 boost::asio::detail::increment(
66 task_io_service_->outstanding_work_,
67 this_thread_->private_outstanding_work - 1);
69 else if (this_thread_->private_outstanding_work < 1)
71 task_io_service_->work_finished();
73 this_thread_->private_outstanding_work = 0;
75 #if defined(BOOST_ASIO_HAS_THREADS)
76 if (!this_thread_->private_op_queue.empty())
79 task_io_service_->op_queue_.push(this_thread_->private_op_queue);
81 #endif // defined(BOOST_ASIO_HAS_THREADS)
84 task_io_service* task_io_service_;
85 mutex::scoped_lock* lock_;
86 thread_info* this_thread_;
89 task_io_service::task_io_service(
90 boost::asio::io_service& io_service, std::size_t concurrency_hint)
91 : boost::asio::detail::service_base<task_io_service>(io_service),
92 one_thread_(concurrency_hint == 1),
95 task_interrupted_(true),
100 BOOST_ASIO_HANDLER_TRACKING_INIT;
103 void task_io_service::shutdown_service()
105 mutex::scoped_lock lock(mutex_);
109 // Destroy handler objects.
110 while (!op_queue_.empty())
112 operation* o = op_queue_.front();
114 if (o != &task_operation_)
118 // Reset to initial state.
122 void task_io_service::init_task()
124 mutex::scoped_lock lock(mutex_);
125 if (!shutdown_ && !task_)
127 task_ = &use_service<reactor>(this->get_io_service());
128 op_queue_.push(&task_operation_);
129 wake_one_thread_and_unlock(lock);
133 std::size_t task_io_service::run(boost::system::error_code& ec)
135 ec = boost::system::error_code();
136 if (outstanding_work_ == 0)
142 thread_info this_thread;
143 this_thread.private_outstanding_work = 0;
144 thread_call_stack::context ctx(this, this_thread);
146 mutex::scoped_lock lock(mutex_);
149 for (; do_run_one(lock, this_thread, ec); lock.lock())
150 if (n != (std::numeric_limits<std::size_t>::max)())
155 std::size_t task_io_service::run_one(boost::system::error_code& ec)
157 ec = boost::system::error_code();
158 if (outstanding_work_ == 0)
164 thread_info this_thread;
165 this_thread.private_outstanding_work = 0;
166 thread_call_stack::context ctx(this, this_thread);
168 mutex::scoped_lock lock(mutex_);
170 return do_run_one(lock, this_thread, ec);
173 std::size_t task_io_service::poll(boost::system::error_code& ec)
175 ec = boost::system::error_code();
176 if (outstanding_work_ == 0)
182 thread_info this_thread;
183 this_thread.private_outstanding_work = 0;
184 thread_call_stack::context ctx(this, this_thread);
186 mutex::scoped_lock lock(mutex_);
188 #if defined(BOOST_ASIO_HAS_THREADS)
189 // We want to support nested calls to poll() and poll_one(), so any handlers
190 // that are already on a thread-private queue need to be put on to the main
193 if (thread_info* outer_thread_info = ctx.next_by_key())
194 op_queue_.push(outer_thread_info->private_op_queue);
195 #endif // defined(BOOST_ASIO_HAS_THREADS)
198 for (; do_poll_one(lock, this_thread, ec); lock.lock())
199 if (n != (std::numeric_limits<std::size_t>::max)())
204 std::size_t task_io_service::poll_one(boost::system::error_code& ec)
206 ec = boost::system::error_code();
207 if (outstanding_work_ == 0)
213 thread_info this_thread;
214 this_thread.private_outstanding_work = 0;
215 thread_call_stack::context ctx(this, this_thread);
217 mutex::scoped_lock lock(mutex_);
219 #if defined(BOOST_ASIO_HAS_THREADS)
220 // We want to support nested calls to poll() and poll_one(), so any handlers
221 // that are already on a thread-private queue need to be put on to the main
224 if (thread_info* outer_thread_info = ctx.next_by_key())
225 op_queue_.push(outer_thread_info->private_op_queue);
226 #endif // defined(BOOST_ASIO_HAS_THREADS)
228 return do_poll_one(lock, this_thread, ec);
231 void task_io_service::stop()
233 mutex::scoped_lock lock(mutex_);
234 stop_all_threads(lock);
237 bool task_io_service::stopped() const
239 mutex::scoped_lock lock(mutex_);
243 void task_io_service::reset()
245 mutex::scoped_lock lock(mutex_);
249 void task_io_service::post_immediate_completion(
250 task_io_service::operation* op, bool is_continuation)
252 #if defined(BOOST_ASIO_HAS_THREADS)
253 if (one_thread_ || is_continuation)
255 if (thread_info* this_thread = thread_call_stack::contains(this))
257 ++this_thread->private_outstanding_work;
258 this_thread->private_op_queue.push(op);
262 #else // defined(BOOST_ASIO_HAS_THREADS)
263 (void)is_continuation;
264 #endif // defined(BOOST_ASIO_HAS_THREADS)
267 mutex::scoped_lock lock(mutex_);
269 wake_one_thread_and_unlock(lock);
272 void task_io_service::post_deferred_completion(task_io_service::operation* op)
274 #if defined(BOOST_ASIO_HAS_THREADS)
277 if (thread_info* this_thread = thread_call_stack::contains(this))
279 this_thread->private_op_queue.push(op);
283 #endif // defined(BOOST_ASIO_HAS_THREADS)
285 mutex::scoped_lock lock(mutex_);
287 wake_one_thread_and_unlock(lock);
290 void task_io_service::post_deferred_completions(
291 op_queue<task_io_service::operation>& ops)
295 #if defined(BOOST_ASIO_HAS_THREADS)
298 if (thread_info* this_thread = thread_call_stack::contains(this))
300 this_thread->private_op_queue.push(ops);
304 #endif // defined(BOOST_ASIO_HAS_THREADS)
306 mutex::scoped_lock lock(mutex_);
308 wake_one_thread_and_unlock(lock);
312 void task_io_service::do_dispatch(
313 task_io_service::operation* op)
316 mutex::scoped_lock lock(mutex_);
318 wake_one_thread_and_unlock(lock);
321 void task_io_service::abandon_operations(
322 op_queue<task_io_service::operation>& ops)
324 op_queue<task_io_service::operation> ops2;
328 std::size_t task_io_service::do_run_one(mutex::scoped_lock& lock,
329 task_io_service::thread_info& this_thread,
330 const boost::system::error_code& ec)
334 if (!op_queue_.empty())
336 // Prepare to execute first handler from queue.
337 operation* o = op_queue_.front();
339 bool more_handlers = (!op_queue_.empty());
341 if (o == &task_operation_)
343 task_interrupted_ = more_handlers;
345 if (more_handlers && !one_thread_)
346 wakeup_event_.unlock_and_signal_one(lock);
350 task_cleanup on_exit = { this, &lock, &this_thread };
353 // Run the task. May throw an exception. Only block if the operation
354 // queue is empty and we're not polling, otherwise we want to return
355 // as soon as possible.
356 task_->run(!more_handlers, this_thread.private_op_queue);
360 std::size_t task_result = o->task_result_;
362 if (more_handlers && !one_thread_)
363 wake_one_thread_and_unlock(lock);
367 // Ensure the count of outstanding work is decremented on block exit.
368 work_cleanup on_exit = { this, &lock, &this_thread };
371 // Complete the operation. May throw an exception. Deletes the object.
372 o->complete(*this, ec, task_result);
379 wakeup_event_.clear(lock);
380 wakeup_event_.wait(lock);
387 std::size_t task_io_service::do_poll_one(mutex::scoped_lock& lock,
388 task_io_service::thread_info& this_thread,
389 const boost::system::error_code& ec)
394 operation* o = op_queue_.front();
395 if (o == &task_operation_)
401 task_cleanup c = { this, &lock, &this_thread };
404 // Run the task. May throw an exception. Only block if the operation
405 // queue is empty and we're not polling, otherwise we want to return
406 // as soon as possible.
407 task_->run(false, this_thread.private_op_queue);
410 o = op_queue_.front();
411 if (o == &task_operation_)
413 wakeup_event_.maybe_unlock_and_signal_one(lock);
422 bool more_handlers = (!op_queue_.empty());
424 std::size_t task_result = o->task_result_;
426 if (more_handlers && !one_thread_)
427 wake_one_thread_and_unlock(lock);
431 // Ensure the count of outstanding work is decremented on block exit.
432 work_cleanup on_exit = { this, &lock, &this_thread };
435 // Complete the operation. May throw an exception. Deletes the object.
436 o->complete(*this, ec, task_result);
441 void task_io_service::stop_all_threads(
442 mutex::scoped_lock& lock)
445 wakeup_event_.signal_all(lock);
447 if (!task_interrupted_ && task_)
449 task_interrupted_ = true;
454 void task_io_service::wake_one_thread_and_unlock(
455 mutex::scoped_lock& lock)
457 if (!wakeup_event_.maybe_unlock_and_signal_one(lock))
459 if (!task_interrupted_ && task_)
461 task_interrupted_ = true;
468 } // namespace detail
472 #include <boost/asio/detail/pop_options.hpp>
474 #endif // !defined(BOOST_ASIO_HAS_IOCP)
476 #endif // BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP