Chris@16: // Chris@16: // detail/impl/task_io_service.ipp Chris@16: // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Chris@16: // Chris@101: // Copyright (c) 2003-2015 Christopher M. Kohlhoff (chris at kohlhoff dot com) Chris@16: // Chris@16: // Distributed under the Boost Software License, Version 1.0. (See accompanying Chris@16: // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: // Chris@16: Chris@16: #ifndef BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP Chris@16: #define BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP Chris@16: Chris@16: #if defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: # pragma once Chris@16: #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) Chris@16: Chris@16: #include Chris@16: Chris@16: #if !defined(BOOST_ASIO_HAS_IOCP) Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@16: #include Chris@16: Chris@16: namespace boost { Chris@16: namespace asio { Chris@16: namespace detail { Chris@16: Chris@16: struct task_io_service::task_cleanup Chris@16: { Chris@16: ~task_cleanup() Chris@16: { Chris@16: if (this_thread_->private_outstanding_work > 0) Chris@16: { Chris@16: boost::asio::detail::increment( Chris@16: task_io_service_->outstanding_work_, Chris@16: this_thread_->private_outstanding_work); Chris@16: } Chris@16: this_thread_->private_outstanding_work = 0; Chris@16: Chris@16: // Enqueue the completed operations and reinsert the task at the end of Chris@16: // the operation queue. Chris@16: lock_->lock(); Chris@16: task_io_service_->task_interrupted_ = true; Chris@16: task_io_service_->op_queue_.push(this_thread_->private_op_queue); Chris@16: task_io_service_->op_queue_.push(&task_io_service_->task_operation_); Chris@16: } Chris@16: Chris@16: task_io_service* task_io_service_; Chris@16: mutex::scoped_lock* lock_; Chris@16: thread_info* this_thread_; Chris@16: }; Chris@16: Chris@16: struct task_io_service::work_cleanup Chris@16: { Chris@16: ~work_cleanup() Chris@16: { Chris@16: if (this_thread_->private_outstanding_work > 1) Chris@16: { Chris@16: boost::asio::detail::increment( Chris@16: task_io_service_->outstanding_work_, Chris@16: this_thread_->private_outstanding_work - 1); Chris@16: } Chris@16: else if (this_thread_->private_outstanding_work < 1) Chris@16: { Chris@16: task_io_service_->work_finished(); Chris@16: } Chris@16: this_thread_->private_outstanding_work = 0; Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: if (!this_thread_->private_op_queue.empty()) Chris@16: { Chris@16: lock_->lock(); Chris@16: task_io_service_->op_queue_.push(this_thread_->private_op_queue); Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: } Chris@16: Chris@16: task_io_service* task_io_service_; Chris@16: mutex::scoped_lock* lock_; Chris@16: thread_info* this_thread_; Chris@16: }; Chris@16: Chris@16: task_io_service::task_io_service( Chris@16: boost::asio::io_service& io_service, std::size_t concurrency_hint) Chris@16: : boost::asio::detail::service_base(io_service), Chris@16: one_thread_(concurrency_hint == 1), Chris@16: mutex_(), Chris@16: task_(0), Chris@16: task_interrupted_(true), Chris@16: outstanding_work_(0), Chris@16: stopped_(false), Chris@101: shutdown_(false) Chris@16: { Chris@16: BOOST_ASIO_HANDLER_TRACKING_INIT; Chris@16: } Chris@16: Chris@16: void task_io_service::shutdown_service() Chris@16: { Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: shutdown_ = true; Chris@16: lock.unlock(); Chris@16: Chris@16: // Destroy handler objects. Chris@16: while (!op_queue_.empty()) Chris@16: { Chris@16: operation* o = op_queue_.front(); Chris@16: op_queue_.pop(); Chris@16: if (o != &task_operation_) Chris@16: o->destroy(); Chris@16: } Chris@16: Chris@16: // Reset to initial state. Chris@16: task_ = 0; Chris@16: } Chris@16: Chris@16: void task_io_service::init_task() Chris@16: { Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: if (!shutdown_ && !task_) Chris@16: { Chris@16: task_ = &use_service(this->get_io_service()); Chris@16: op_queue_.push(&task_operation_); Chris@16: wake_one_thread_and_unlock(lock); Chris@16: } Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::run(boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::system::error_code(); Chris@16: if (outstanding_work_ == 0) Chris@16: { Chris@16: stop(); Chris@16: return 0; Chris@16: } Chris@16: Chris@16: thread_info this_thread; Chris@16: this_thread.private_outstanding_work = 0; Chris@16: thread_call_stack::context ctx(this, this_thread); Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: Chris@16: std::size_t n = 0; Chris@16: for (; do_run_one(lock, this_thread, ec); lock.lock()) Chris@16: if (n != (std::numeric_limits::max)()) Chris@16: ++n; Chris@16: return n; Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::run_one(boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::system::error_code(); Chris@16: if (outstanding_work_ == 0) Chris@16: { Chris@16: stop(); Chris@16: return 0; Chris@16: } Chris@16: Chris@16: thread_info this_thread; Chris@16: this_thread.private_outstanding_work = 0; Chris@16: thread_call_stack::context ctx(this, this_thread); Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: Chris@16: return do_run_one(lock, this_thread, ec); Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::poll(boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::system::error_code(); Chris@16: if (outstanding_work_ == 0) Chris@16: { Chris@16: stop(); Chris@16: return 0; Chris@16: } Chris@16: Chris@16: thread_info this_thread; Chris@16: this_thread.private_outstanding_work = 0; Chris@16: thread_call_stack::context ctx(this, this_thread); Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: // We want to support nested calls to poll() and poll_one(), so any handlers Chris@16: // that are already on a thread-private queue need to be put on to the main Chris@16: // queue now. Chris@16: if (one_thread_) Chris@16: if (thread_info* outer_thread_info = ctx.next_by_key()) Chris@16: op_queue_.push(outer_thread_info->private_op_queue); Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: Chris@16: std::size_t n = 0; Chris@16: for (; do_poll_one(lock, this_thread, ec); lock.lock()) Chris@16: if (n != (std::numeric_limits::max)()) Chris@16: ++n; Chris@16: return n; Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::poll_one(boost::system::error_code& ec) Chris@16: { Chris@16: ec = boost::system::error_code(); Chris@16: if (outstanding_work_ == 0) Chris@16: { Chris@16: stop(); Chris@16: return 0; Chris@16: } Chris@16: Chris@16: thread_info this_thread; Chris@16: this_thread.private_outstanding_work = 0; Chris@16: thread_call_stack::context ctx(this, this_thread); Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: // We want to support nested calls to poll() and poll_one(), so any handlers Chris@16: // that are already on a thread-private queue need to be put on to the main Chris@16: // queue now. Chris@16: if (one_thread_) Chris@16: if (thread_info* outer_thread_info = ctx.next_by_key()) Chris@16: op_queue_.push(outer_thread_info->private_op_queue); Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: Chris@16: return do_poll_one(lock, this_thread, ec); Chris@16: } Chris@16: Chris@16: void task_io_service::stop() Chris@16: { Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: stop_all_threads(lock); Chris@16: } Chris@16: Chris@16: bool task_io_service::stopped() const Chris@16: { Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: return stopped_; Chris@16: } Chris@16: Chris@16: void task_io_service::reset() Chris@16: { Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: stopped_ = false; Chris@16: } Chris@16: Chris@16: void task_io_service::post_immediate_completion( Chris@16: task_io_service::operation* op, bool is_continuation) Chris@16: { Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: if (one_thread_ || is_continuation) Chris@16: { Chris@16: if (thread_info* this_thread = thread_call_stack::contains(this)) Chris@16: { Chris@16: ++this_thread->private_outstanding_work; Chris@16: this_thread->private_op_queue.push(op); Chris@16: return; Chris@16: } Chris@16: } Chris@101: #else // defined(BOOST_ASIO_HAS_THREADS) Chris@101: (void)is_continuation; Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: Chris@16: work_started(); Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: op_queue_.push(op); Chris@16: wake_one_thread_and_unlock(lock); Chris@16: } Chris@16: Chris@16: void task_io_service::post_deferred_completion(task_io_service::operation* op) Chris@16: { Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: if (one_thread_) Chris@16: { Chris@16: if (thread_info* this_thread = thread_call_stack::contains(this)) Chris@16: { Chris@16: this_thread->private_op_queue.push(op); Chris@16: return; Chris@16: } Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: op_queue_.push(op); Chris@16: wake_one_thread_and_unlock(lock); Chris@16: } Chris@16: Chris@16: void task_io_service::post_deferred_completions( Chris@16: op_queue& ops) Chris@16: { Chris@16: if (!ops.empty()) Chris@16: { Chris@16: #if defined(BOOST_ASIO_HAS_THREADS) Chris@16: if (one_thread_) Chris@16: { Chris@16: if (thread_info* this_thread = thread_call_stack::contains(this)) Chris@16: { Chris@16: this_thread->private_op_queue.push(ops); Chris@16: return; Chris@16: } Chris@16: } Chris@16: #endif // defined(BOOST_ASIO_HAS_THREADS) Chris@16: Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: op_queue_.push(ops); Chris@16: wake_one_thread_and_unlock(lock); Chris@16: } Chris@16: } Chris@16: Chris@16: void task_io_service::do_dispatch( Chris@16: task_io_service::operation* op) Chris@16: { Chris@16: work_started(); Chris@16: mutex::scoped_lock lock(mutex_); Chris@16: op_queue_.push(op); Chris@16: wake_one_thread_and_unlock(lock); Chris@16: } Chris@16: Chris@16: void task_io_service::abandon_operations( Chris@16: op_queue& ops) Chris@16: { Chris@16: op_queue ops2; Chris@16: ops2.push(ops); Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::do_run_one(mutex::scoped_lock& lock, Chris@16: task_io_service::thread_info& this_thread, Chris@16: const boost::system::error_code& ec) Chris@16: { Chris@16: while (!stopped_) Chris@16: { Chris@16: if (!op_queue_.empty()) Chris@16: { Chris@16: // Prepare to execute first handler from queue. Chris@16: operation* o = op_queue_.front(); Chris@16: op_queue_.pop(); Chris@16: bool more_handlers = (!op_queue_.empty()); Chris@16: Chris@16: if (o == &task_operation_) Chris@16: { Chris@16: task_interrupted_ = more_handlers; Chris@16: Chris@16: if (more_handlers && !one_thread_) Chris@101: wakeup_event_.unlock_and_signal_one(lock); Chris@16: else Chris@16: lock.unlock(); Chris@16: Chris@16: task_cleanup on_exit = { this, &lock, &this_thread }; Chris@16: (void)on_exit; Chris@16: Chris@16: // Run the task. May throw an exception. Only block if the operation Chris@16: // queue is empty and we're not polling, otherwise we want to return Chris@16: // as soon as possible. Chris@16: task_->run(!more_handlers, this_thread.private_op_queue); Chris@16: } Chris@16: else Chris@16: { Chris@16: std::size_t task_result = o->task_result_; Chris@16: Chris@16: if (more_handlers && !one_thread_) Chris@16: wake_one_thread_and_unlock(lock); Chris@16: else Chris@16: lock.unlock(); Chris@16: Chris@16: // Ensure the count of outstanding work is decremented on block exit. Chris@16: work_cleanup on_exit = { this, &lock, &this_thread }; Chris@16: (void)on_exit; Chris@16: Chris@16: // Complete the operation. May throw an exception. Deletes the object. Chris@16: o->complete(*this, ec, task_result); Chris@16: Chris@16: return 1; Chris@16: } Chris@16: } Chris@16: else Chris@16: { Chris@101: wakeup_event_.clear(lock); Chris@101: wakeup_event_.wait(lock); Chris@16: } Chris@16: } Chris@16: Chris@16: return 0; Chris@16: } Chris@16: Chris@16: std::size_t task_io_service::do_poll_one(mutex::scoped_lock& lock, Chris@16: task_io_service::thread_info& this_thread, Chris@16: const boost::system::error_code& ec) Chris@16: { Chris@16: if (stopped_) Chris@16: return 0; Chris@16: Chris@16: operation* o = op_queue_.front(); Chris@16: if (o == &task_operation_) Chris@16: { Chris@16: op_queue_.pop(); Chris@16: lock.unlock(); Chris@16: Chris@16: { Chris@16: task_cleanup c = { this, &lock, &this_thread }; Chris@16: (void)c; Chris@16: Chris@16: // Run the task. May throw an exception. Only block if the operation Chris@16: // queue is empty and we're not polling, otherwise we want to return Chris@16: // as soon as possible. Chris@16: task_->run(false, this_thread.private_op_queue); Chris@16: } Chris@16: Chris@16: o = op_queue_.front(); Chris@16: if (o == &task_operation_) Chris@16: { Chris@101: wakeup_event_.maybe_unlock_and_signal_one(lock); Chris@16: return 0; Chris@16: } Chris@16: } Chris@16: Chris@16: if (o == 0) Chris@16: return 0; Chris@16: Chris@16: op_queue_.pop(); Chris@16: bool more_handlers = (!op_queue_.empty()); Chris@16: Chris@16: std::size_t task_result = o->task_result_; Chris@16: Chris@16: if (more_handlers && !one_thread_) Chris@16: wake_one_thread_and_unlock(lock); Chris@16: else Chris@16: lock.unlock(); Chris@16: Chris@16: // Ensure the count of outstanding work is decremented on block exit. Chris@16: work_cleanup on_exit = { this, &lock, &this_thread }; Chris@16: (void)on_exit; Chris@16: Chris@16: // Complete the operation. May throw an exception. Deletes the object. Chris@16: o->complete(*this, ec, task_result); Chris@16: Chris@16: return 1; Chris@16: } Chris@16: Chris@16: void task_io_service::stop_all_threads( Chris@16: mutex::scoped_lock& lock) Chris@16: { Chris@16: stopped_ = true; Chris@101: wakeup_event_.signal_all(lock); Chris@16: Chris@16: if (!task_interrupted_ && task_) Chris@16: { Chris@16: task_interrupted_ = true; Chris@16: task_->interrupt(); Chris@16: } Chris@16: } Chris@16: Chris@16: void task_io_service::wake_one_thread_and_unlock( Chris@16: mutex::scoped_lock& lock) Chris@16: { Chris@101: if (!wakeup_event_.maybe_unlock_and_signal_one(lock)) Chris@16: { Chris@16: if (!task_interrupted_ && task_) Chris@16: { Chris@16: task_interrupted_ = true; Chris@16: task_->interrupt(); Chris@16: } Chris@16: lock.unlock(); Chris@16: } Chris@16: } Chris@16: Chris@16: } // namespace detail Chris@16: } // namespace asio Chris@16: } // namespace boost Chris@16: Chris@16: #include Chris@16: Chris@16: #endif // !defined(BOOST_ASIO_HAS_IOCP) Chris@16: Chris@16: #endif // BOOST_ASIO_DETAIL_IMPL_TASK_IO_SERVICE_IPP