]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/impl/strand_executor_service.ipp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / asio / detail / impl / strand_executor_service.ipp
1 //
2 // detail/impl/strand_executor_service.ipp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2017 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 BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP
13
14 #if defined(_MSC_VER) && (_MSC_VER >= 1200)
15 # pragma once
16 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
17
18 #include <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/strand_executor_service.hpp>
20
21 #include <boost/asio/detail/push_options.hpp>
22
23 namespace boost {
24 namespace asio {
25 namespace detail {
26
27 strand_executor_service::strand_executor_service(execution_context& ctx)
28 : execution_context_service_base<strand_executor_service>(ctx),
29 mutex_(),
30 salt_(0),
31 impl_list_(0)
32 {
33 }
34
35 void strand_executor_service::shutdown()
36 {
37 op_queue<scheduler_operation> ops;
38
39 boost::asio::detail::mutex::scoped_lock lock(mutex_);
40
41 strand_impl* impl = impl_list_;
42 while (impl)
43 {
44 ops.push(impl->waiting_queue_);
45 ops.push(impl->ready_queue_);
46 impl = impl->next_;
47 }
48 }
49
50 strand_executor_service::implementation_type
51 strand_executor_service::create_implementation()
52 {
53 implementation_type new_impl(new strand_impl);
54 new_impl->locked_ = false;
55
56 boost::asio::detail::mutex::scoped_lock lock(mutex_);
57
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();
67
68 // Insert implementation into linked list of all implementations.
69 new_impl->next_ = impl_list_;
70 new_impl->prev_ = 0;
71 if (impl_list_)
72 impl_list_->prev_ = new_impl.get();
73 impl_list_ = new_impl.get();
74 new_impl->service_ = this;
75
76 return new_impl;
77 }
78
79 strand_executor_service::strand_impl::~strand_impl()
80 {
81 boost::asio::detail::mutex::scoped_lock lock(service_->mutex_);
82
83 // Remove implementation from linked list of all implementations.
84 if (service_->impl_list_ == this)
85 service_->impl_list_ = next_;
86 if (prev_)
87 prev_->next_ = next_;
88 if (next_)
89 next_->prev_= prev_;
90 }
91
92 bool strand_executor_service::enqueue(const implementation_type& impl,
93 scheduler_operation* op)
94 {
95 impl->mutex_->lock();
96 if (impl->locked_)
97 {
98 // Some other function already holds the strand lock. Enqueue for later.
99 impl->waiting_queue_.push(op);
100 impl->mutex_->unlock();
101 return false;
102 }
103 else
104 {
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);
110 return true;
111 }
112 }
113
114 bool strand_executor_service::running_in_this_thread(
115 const implementation_type& impl)
116 {
117 return !!call_stack<strand_impl>::contains(impl.get());
118 }
119
120 } // namespace detail
121 } // namespace asio
122 } // namespace boost
123
124 #include <boost/asio/detail/pop_options.hpp>
125
126 #endif // BOOST_ASIO_DETAIL_IMPL_STRAND_EXECUTOR_SERVICE_IPP