folly Singleton: clear some state if creator function fails
[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   SCOPE_EXIT {
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
145     // being built.
146     creating_thread_ = std::thread::id();
147   };
148
149   creating_thread_ = std::this_thread::get_id();
150
151   RWSpinLock::ReadHolder rh(&vault_.stateMutex_);
152   if (vault_.state_ == SingletonVault::SingletonVaultState::Quiescing) {
153     return;
154   }
155
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();
161
162   // Can't use make_shared -- no support for a custom deleter, sadly.
163   instance_ = std::shared_ptr<T>(
164     create_(),
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";
171
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.";
176         } else {
177           output += "Destructor stack trace:\n";
178           output += stack_trace;
179         }
180
181         LOG(ERROR) << output;
182       }
183     });
184
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
188   // constructor
189   SingletonVault::scheduleDestroyInstances();
190
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);
195
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);
199
200   {
201     RWSpinLock::WriteHolder wh(&vault_.mutex_);
202     vault_.creation_order_.push_back(type_);
203   }
204 }
205
206 }
207
208 }