2 * Copyright 2015 Facebook, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 template <typename Tag, typename VaultTag>
23 SingletonHolder<T>& SingletonHolder<T>::singleton() {
24 static auto entry = new SingletonHolder<T>(
25 {typeid(T), typeid(Tag)},
26 *SingletonVault::singleton<VaultTag>());
31 void SingletonHolder<T>::registerSingleton(CreateFunc c, TeardownFunc t) {
32 std::lock_guard<std::mutex> entry_lock(mutex_);
34 if (state_ != SingletonHolderState::NotRegistered) {
35 LOG(FATAL) << "Double registration of singleton: " << type_.name();
38 create_ = std::move(c);
39 teardown_ = std::move(t);
41 state_ = SingletonHolderState::Dead;
45 void SingletonHolder<T>::registerSingletonMock(CreateFunc c, TeardownFunc t) {
46 if (state_ == SingletonHolderState::NotRegistered) {
48 << "Registering mock before singleton was registered: " << type_.name();
52 std::lock_guard<std::mutex> entry_lock(mutex_);
54 create_ = std::move(c);
55 teardown_ = std::move(t);
59 T* SingletonHolder<T>::get() {
60 if (LIKELY(state_ == SingletonHolderState::Living)) {
65 if (instance_weak_.expired()) {
66 throw std::runtime_error(
67 "Raw pointer to a singleton requested after its destruction.");
74 std::weak_ptr<T> SingletonHolder<T>::get_weak() {
75 if (UNLIKELY(state_ != SingletonHolderState::Living)) {
79 return instance_weak_;
83 TypeDescriptor SingletonHolder<T>::type() {
88 bool SingletonHolder<T>::hasLiveInstance() {
89 return !instance_weak_.expired();
93 void SingletonHolder<T>::destroyInstance() {
94 state_ = SingletonHolderState::Dead;
97 auto wait_result = destroy_baton_->timed_wait(
98 std::chrono::steady_clock::now() + kDestroyWaitTime);
100 print_destructor_stack_trace_->store(true);
101 LOG(ERROR) << "Singleton of type " << type_.name() << " has a "
102 << "living reference at destroyInstances time; beware! Raw "
103 << "pointer is " << instance_ptr_ << ". It is very likely "
104 << "that some other singleton is holding a shared_ptr to it. "
105 << "Make sure dependencies between these singletons are "
106 << "properly defined.";
111 template <typename T>
112 SingletonHolder<T>::SingletonHolder(TypeDescriptor type__,
113 SingletonVault& vault) :
114 type_(type__), vault_(vault) {
117 template <typename T>
118 void SingletonHolder<T>::createInstance() {
119 // There's no synchronization here, so we may not see the current value
120 // for creating_thread if it was set by other thread, but we only care about
121 // it if it was set by current thread anyways.
122 if (creating_thread_ == std::this_thread::get_id()) {
123 LOG(FATAL) << "circular singleton dependency: " << type_.name();
126 std::lock_guard<std::mutex> entry_lock(mutex_);
127 if (state_ == SingletonHolderState::Living) {
130 if (state_ == SingletonHolderState::NotRegistered) {
131 auto ptr = SingletonVault::stackTraceGetter().load();
132 LOG(FATAL) << "Creating instance for unregistered singleton: "
133 << type_.name() << "\n"
135 << "\n" << (ptr ? (*ptr)() : "(not available)");
138 if (state_ == SingletonHolderState::Living) {
143 // Clean up creator thread when complete, and also, in case of errors here,
144 // so that subsequent attempts don't think this is still in the process of
146 creating_thread_ = std::thread::id();
149 creating_thread_ = std::this_thread::get_id();
151 RWSpinLock::ReadHolder rh(&vault_.stateMutex_);
152 if (vault_.state_ == SingletonVault::SingletonVaultState::Quiescing) {
156 auto destroy_baton = std::make_shared<folly::Baton<>>();
157 auto print_destructor_stack_trace =
158 std::make_shared<std::atomic<bool>>(false);
159 auto teardown = teardown_;
160 auto type_name = type_.name();
162 // Can't use make_shared -- no support for a custom deleter, sadly.
163 instance_ = std::shared_ptr<T>(
165 [destroy_baton, print_destructor_stack_trace, teardown, type_name]
166 (T* instance_ptr) mutable {
167 teardown(instance_ptr);
168 destroy_baton->post();
169 if (print_destructor_stack_trace->load()) {
170 std::string output = "Singleton " + type_name + " was destroyed.\n";
172 auto stack_trace_getter = SingletonVault::stackTraceGetter().load();
173 auto stack_trace = stack_trace_getter ? stack_trace_getter() : "";
174 if (stack_trace.empty()) {
175 output += "Failed to get destructor stack trace.";
177 output += "Destructor stack trace:\n";
178 output += stack_trace;
181 LOG(ERROR) << output;
185 // We should schedule destroyInstances() only after the singleton was
186 // created. This will ensure it will be destroyed before singletons,
187 // not managed by folly::Singleton, which were initialized in its
189 SingletonVault::scheduleDestroyInstances();
191 instance_weak_ = instance_;
192 instance_ptr_ = instance_.get();
193 destroy_baton_ = std::move(destroy_baton);
194 print_destructor_stack_trace_ = std::move(print_destructor_stack_trace);
196 // This has to be the last step, because once state is Living other threads
197 // may access instance and instance_weak w/o synchronization.
198 state_.store(SingletonHolderState::Living);
201 RWSpinLock::WriteHolder wh(&vault_.mutex_);
202 vault_.creation_order_.push_back(type_);