folly/singleton: fatal in unrecoverable error cases
[folly.git] / folly / Singleton-inl.h
1 /*
2  * Copyright 2015 Facebook, Inc.
3  *
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
7  *
8  *   http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 namespace folly {
18
19 namespace detail {
20
21 template <typename T>
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>());
27   return *entry;
28 }
29
30 template <typename T>
31 void SingletonHolder<T>::registerSingleton(CreateFunc c, TeardownFunc t) {
32   std::lock_guard<std::mutex> entry_lock(mutex_);
33
34   if (state_ != SingletonHolderState::NotRegistered) {
35     LOG(FATAL) << "Double registration of singleton: " << type_.name();
36   }
37
38   create_ = std::move(c);
39   teardown_ = std::move(t);
40
41   state_ = SingletonHolderState::Dead;
42 }
43
44 template <typename T>
45 void SingletonHolder<T>::registerSingletonMock(CreateFunc c, TeardownFunc t) {
46   if (state_ == SingletonHolderState::NotRegistered) {
47     LOG(FATAL)
48         << "Registering mock before singleton was registered: " << type_.name();
49   }
50   destroyInstance();
51
52   std::lock_guard<std::mutex> entry_lock(mutex_);
53
54   create_ = std::move(c);
55   teardown_ = std::move(t);
56 }
57
58 template <typename T>
59 T* SingletonHolder<T>::get() {
60   if (LIKELY(state_ == SingletonHolderState::Living)) {
61     return instance_ptr_;
62   }
63   createInstance();
64
65   if (instance_weak_.expired()) {
66     throw std::runtime_error(
67       "Raw pointer to a singleton requested after its destruction.");
68   }
69
70   return instance_ptr_;
71 }
72
73 template <typename T>
74 std::weak_ptr<T> SingletonHolder<T>::get_weak() {
75   if (UNLIKELY(state_ != SingletonHolderState::Living)) {
76     createInstance();
77   }
78
79   return instance_weak_;
80 }
81
82 template <typename T>
83 TypeDescriptor SingletonHolder<T>::type() {
84   return type_;
85 }
86
87 template <typename T>
88 bool SingletonHolder<T>::hasLiveInstance() {
89   return !instance_weak_.expired();
90 }
91
92 template <typename T>
93 void SingletonHolder<T>::destroyInstance() {
94   state_ = SingletonHolderState::Dead;
95   instance_.reset();
96   if (destroy_baton_) {
97     auto wait_result = destroy_baton_->timed_wait(
98       std::chrono::steady_clock::now() + kDestroyWaitTime);
99     if (!wait_result) {
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.";
107     }
108   }
109 }
110
111 template <typename T>
112 SingletonHolder<T>::SingletonHolder(TypeDescriptor type__,
113                                     SingletonVault& vault) :
114     type_(type__), vault_(vault) {
115 }
116
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();
124   }
125
126   std::lock_guard<std::mutex> entry_lock(mutex_);
127   if (state_ == SingletonHolderState::Living) {
128     return;
129   }
130   if (state_ == SingletonHolderState::NotRegistered) {
131     auto ptr = SingletonVault::stackTraceGetter().load();
132     LOG(FATAL) << "Creating instance for unregistered singleton: "
133                << type_.name() << "\n"
134                << "Stacktrace:"
135                << "\n" << (ptr ? (*ptr)() : "(not available)");
136   }
137
138   if (state_ == SingletonHolderState::Living) {
139     return;
140   }
141
142   creating_thread_ = std::this_thread::get_id();
143
144   RWSpinLock::ReadHolder rh(&vault_.stateMutex_);
145   if (vault_.state_ == SingletonVault::SingletonVaultState::Quiescing) {
146     creating_thread_ = std::thread::id();
147     return;
148   }
149
150   auto destroy_baton = std::make_shared<folly::Baton<>>();
151   auto print_destructor_stack_trace =
152     std::make_shared<std::atomic<bool>>(false);
153   auto teardown = teardown_;
154   auto type_name = type_.name();
155
156   // Can't use make_shared -- no support for a custom deleter, sadly.
157   instance_ = std::shared_ptr<T>(
158     create_(),
159     [destroy_baton, print_destructor_stack_trace, teardown, type_name]
160     (T* instance_ptr) mutable {
161       teardown(instance_ptr);
162       destroy_baton->post();
163       if (print_destructor_stack_trace->load()) {
164         std::string output = "Singleton " + type_name + " was destroyed.\n";
165
166         auto stack_trace_getter = SingletonVault::stackTraceGetter().load();
167         auto stack_trace = stack_trace_getter ? stack_trace_getter() : "";
168         if (stack_trace.empty()) {
169           output += "Failed to get destructor stack trace.";
170         } else {
171           output += "Destructor stack trace:\n";
172           output += stack_trace;
173         }
174
175         LOG(ERROR) << output;
176       }
177     });
178
179   // We should schedule destroyInstances() only after the singleton was
180   // created. This will ensure it will be destroyed before singletons,
181   // not managed by folly::Singleton, which were initialized in its
182   // constructor
183   SingletonVault::scheduleDestroyInstances();
184
185   instance_weak_ = instance_;
186   instance_ptr_ = instance_.get();
187   creating_thread_ = std::thread::id();
188   destroy_baton_ = std::move(destroy_baton);
189   print_destructor_stack_trace_ = std::move(print_destructor_stack_trace);
190
191   // This has to be the last step, because once state is Living other threads
192   // may access instance and instance_weak w/o synchronization.
193   state_.store(SingletonHolderState::Living);
194
195   {
196     RWSpinLock::WriteHolder wh(&vault_.mutex_);
197     vault_.creation_order_.push_back(type_);
198   }
199 }
200
201 }
202
203 }