]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/asio/detail/strand_executor_service.hpp
import new upstream nautilus stable release 14.2.8
[ceph.git] / ceph / src / boost / boost / asio / detail / strand_executor_service.hpp
1 //
2 // detail/strand_executor_service.hpp
3 // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4 //
5 // Copyright (c) 2003-2019 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_STRAND_EXECUTOR_SERVICE_HPP
12 #define BOOST_ASIO_DETAIL_STRAND_EXECUTOR_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 <boost/asio/detail/config.hpp>
19 #include <boost/asio/detail/atomic_count.hpp>
20 #include <boost/asio/detail/executor_op.hpp>
21 #include <boost/asio/detail/memory.hpp>
22 #include <boost/asio/detail/mutex.hpp>
23 #include <boost/asio/detail/op_queue.hpp>
24 #include <boost/asio/detail/scheduler_operation.hpp>
25 #include <boost/asio/detail/scoped_ptr.hpp>
26 #include <boost/asio/execution_context.hpp>
27
28 #include <boost/asio/detail/push_options.hpp>
29
30 namespace boost {
31 namespace asio {
32 namespace detail {
33
34 // Default service implementation for a strand.
35 class strand_executor_service
36 : public execution_context_service_base<strand_executor_service>
37 {
38 public:
39 // The underlying implementation of a strand.
40 class strand_impl
41 {
42 public:
43 BOOST_ASIO_DECL ~strand_impl();
44
45 private:
46 friend class strand_executor_service;
47
48 // Mutex to protect access to internal data.
49 mutex* mutex_;
50
51 // Indicates whether the strand is currently "locked" by a handler. This
52 // means that there is a handler upcall in progress, or that the strand
53 // itself has been scheduled in order to invoke some pending handlers.
54 bool locked_;
55
56 // Indicates that the strand has been shut down and will accept no further
57 // handlers.
58 bool shutdown_;
59
60 // The handlers that are waiting on the strand but should not be run until
61 // after the next time the strand is scheduled. This queue must only be
62 // modified while the mutex is locked.
63 op_queue<scheduler_operation> waiting_queue_;
64
65 // The handlers that are ready to be run. Logically speaking, these are the
66 // handlers that hold the strand's lock. The ready queue is only modified
67 // from within the strand and so may be accessed without locking the mutex.
68 op_queue<scheduler_operation> ready_queue_;
69
70 // Pointers to adjacent handle implementations in linked list.
71 strand_impl* next_;
72 strand_impl* prev_;
73
74 // The strand service in where the implementation is held.
75 strand_executor_service* service_;
76 };
77
78 typedef shared_ptr<strand_impl> implementation_type;
79
80 // Construct a new strand service for the specified context.
81 BOOST_ASIO_DECL explicit strand_executor_service(execution_context& context);
82
83 // Destroy all user-defined handler objects owned by the service.
84 BOOST_ASIO_DECL void shutdown();
85
86 // Create a new strand_executor implementation.
87 BOOST_ASIO_DECL implementation_type create_implementation();
88
89 // Request invocation of the given function.
90 template <typename Executor, typename Function, typename Allocator>
91 static void dispatch(const implementation_type& impl, Executor& ex,
92 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
93
94 // Request invocation of the given function and return immediately.
95 template <typename Executor, typename Function, typename Allocator>
96 static void post(const implementation_type& impl, Executor& ex,
97 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
98
99 // Request invocation of the given function and return immediately.
100 template <typename Executor, typename Function, typename Allocator>
101 static void defer(const implementation_type& impl, Executor& ex,
102 BOOST_ASIO_MOVE_ARG(Function) function, const Allocator& a);
103
104 // Determine whether the strand is running in the current thread.
105 BOOST_ASIO_DECL static bool running_in_this_thread(
106 const implementation_type& impl);
107
108 private:
109 friend class strand_impl;
110 template <typename Executor> class invoker;
111
112 // Adds a function to the strand. Returns true if it acquires the lock.
113 BOOST_ASIO_DECL static bool enqueue(const implementation_type& impl,
114 scheduler_operation* op);
115
116 // Mutex to protect access to the service-wide state.
117 mutex mutex_;
118
119 // Number of mutexes shared between all strand objects.
120 enum { num_mutexes = 193 };
121
122 // Pool of mutexes.
123 scoped_ptr<mutex> mutexes_[num_mutexes];
124
125 // Extra value used when hashing to prevent recycled memory locations from
126 // getting the same mutex.
127 std::size_t salt_;
128
129 // The head of a linked list of all implementations.
130 strand_impl* impl_list_;
131 };
132
133 } // namespace detail
134 } // namespace asio
135 } // namespace boost
136
137 #include <boost/asio/detail/pop_options.hpp>
138
139 #include <boost/asio/detail/impl/strand_executor_service.hpp>
140 #if defined(BOOST_ASIO_HEADER_ONLY)
141 # include <boost/asio/detail/impl/strand_executor_service.ipp>
142 #endif // defined(BOOST_ASIO_HEADER_ONLY)
143
144 #endif // BOOST_ASIO_DETAIL_STRAND_EXECUTOR_SERVICE_HPP