2 // detail/impl/service_registry.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_SERVICE_REGISTRY_IPP
12 #define BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_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 #include <boost/asio/detail/service_registry.hpp>
21 #include <boost/asio/detail/throw_exception.hpp>
23 #include <boost/asio/detail/push_options.hpp>
29 service_registry::service_registry(execution_context& owner)
35 service_registry::~service_registry()
39 void service_registry::shutdown_services()
41 execution_context::service* service = first_service_;
45 service = service->next_;
49 void service_registry::destroy_services()
51 while (first_service_)
53 execution_context::service* next_service = first_service_->next_;
54 destroy(first_service_);
55 first_service_ = next_service;
59 void service_registry::notify_fork(execution_context::fork_event fork_ev)
61 // Make a copy of all of the services while holding the lock. We don't want
62 // to hold the lock while calling into each service, as it may try to call
63 // back into this class.
64 std::vector<execution_context::service*> services;
66 boost::asio::detail::mutex::scoped_lock lock(mutex_);
67 execution_context::service* service = first_service_;
70 services.push_back(service);
71 service = service->next_;
75 // If processing the fork_prepare event, we want to go in reverse order of
76 // service registration, which happens to be the existing order of the
77 // services in the vector. For the other events we want to go in the other
79 std::size_t num_services = services.size();
80 if (fork_ev == execution_context::fork_prepare)
81 for (std::size_t i = 0; i < num_services; ++i)
82 services[i]->notify_fork(fork_ev);
84 for (std::size_t i = num_services; i > 0; --i)
85 services[i - 1]->notify_fork(fork_ev);
88 void service_registry::init_key_from_id(execution_context::service::key& key,
89 const execution_context::id& id)
95 bool service_registry::keys_match(
96 const execution_context::service::key& key1,
97 const execution_context::service::key& key2)
99 if (key1.id_ && key2.id_)
100 if (key1.id_ == key2.id_)
102 if (key1.type_info_ && key2.type_info_)
103 if (*key1.type_info_ == *key2.type_info_)
108 void service_registry::destroy(execution_context::service* service)
113 execution_context::service* service_registry::do_use_service(
114 const execution_context::service::key& key,
115 factory_type factory, void* owner)
117 boost::asio::detail::mutex::scoped_lock lock(mutex_);
119 // First see if there is an existing service object with the given key.
120 execution_context::service* service = first_service_;
123 if (keys_match(service->key_, key))
125 service = service->next_;
128 // Create a new service object. The service registry's mutex is not locked
129 // at this time to allow for nested calls into this function from the new
130 // service's constructor.
132 auto_service_ptr new_service = { factory(owner) };
133 new_service.ptr_->key_ = key;
136 // Check that nobody else created another service object of the same type
137 // while the lock was released.
138 service = first_service_;
141 if (keys_match(service->key_, key))
143 service = service->next_;
146 // Service was successfully initialised, pass ownership to registry.
147 new_service.ptr_->next_ = first_service_;
148 first_service_ = new_service.ptr_;
149 new_service.ptr_ = 0;
150 return first_service_;
153 void service_registry::do_add_service(
154 const execution_context::service::key& key,
155 execution_context::service* new_service)
157 if (&owner_ != &new_service->context())
158 boost::asio::detail::throw_exception(invalid_service_owner());
160 boost::asio::detail::mutex::scoped_lock lock(mutex_);
162 // Check if there is an existing service object with the given key.
163 execution_context::service* service = first_service_;
166 if (keys_match(service->key_, key))
167 boost::asio::detail::throw_exception(service_already_exists());
168 service = service->next_;
171 // Take ownership of the service object.
172 new_service->key_ = key;
173 new_service->next_ = first_service_;
174 first_service_ = new_service;
177 bool service_registry::do_has_service(
178 const execution_context::service::key& key) const
180 boost::asio::detail::mutex::scoped_lock lock(mutex_);
182 execution_context::service* service = first_service_;
185 if (keys_match(service->key_, key))
187 service = service->next_;
193 } // namespace detail
197 #include <boost/asio/detail/pop_options.hpp>
199 #endif // BOOST_ASIO_DETAIL_IMPL_SERVICE_REGISTRY_IPP