2 // detail/impl/strand_executor_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 // Copyright (c) 2003-2017 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_STRAND_EXECUTOR_SERVICE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_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>
19 #include <boost/asio/detail/strand_executor_service.hpp>
21 #include <boost/asio/detail/push_options.hpp>
27 strand_executor_service::strand_executor_service(execution_context& ctx)
28 : execution_context_service_base<strand_executor_service>(ctx),
35 void strand_executor_service::shutdown()
37 op_queue<scheduler_operation> ops;
39 boost::asio::detail::mutex::scoped_lock lock(mutex_);
41 strand_impl* impl = impl_list_;
44 ops.push(impl->waiting_queue_);
45 ops.push(impl->ready_queue_);
50 strand_executor_service::implementation_type
51 strand_executor_service::create_implementation()
53 implementation_type new_impl(new strand_impl);
54 new_impl->locked_ = false;
56 boost::asio::detail::mutex::scoped_lock lock(mutex_);
58 // Select a mutex from the pool of shared mutexes.
59 std::size_t salt = salt_++;
60 std::size_t mutex_index = reinterpret_cast<std::size_t>(new_impl.get());
61 mutex_index += (reinterpret_cast<std::size_t>(new_impl.get()) >> 3);
62 mutex_index ^= salt + 0x9e3779b9 + (mutex_index << 6) + (mutex_index >> 2);
63 mutex_index = mutex_index % num_mutexes;
64 if (!mutexes_[mutex_index].get())
65 mutexes_[mutex_index].reset(new mutex);
66 new_impl->mutex_ = mutexes_[mutex_index].get();
68 // Insert implementation into linked list of all implementations.
69 new_impl->next_ = impl_list_;
72 impl_list_->prev_ = new_impl.get();
73 impl_list_ = new_impl.get();
74 new_impl->service_ = this;
79 strand_executor_service::strand_impl::~strand_impl()
81 boost::asio::detail::mutex::scoped_lock lock(service_->mutex_);
83 // Remove implementation from linked list of all implementations.
84 if (service_->impl_list_ == this)
85 service_->impl_list_ = next_;
92 bool strand_executor_service::enqueue(const implementation_type& impl,
93 scheduler_operation* op)
98 // Some other function already holds the strand lock. Enqueue for later.
99 impl->waiting_queue_.push(op);
100 impl->mutex_->unlock();
105 // The function is acquiring the strand lock and so is responsible for
106 // scheduling the strand.
107 impl->locked_ = true;
108 impl->mutex_->unlock();
109 impl->ready_queue_.push(op);
114 bool strand_executor_service::running_in_this_thread(
115 const implementation_type& impl)
117 return !!call_stack<strand_impl>::contains(impl.get());
120 } // namespace detail
124 #include <boost/asio/detail/pop_options.hpp>
126 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP