template <typename T>
T* SingletonHolder<T>::get() {
- if (LIKELY(state_ == SingletonHolderState::Living)) {
+ if (LIKELY(state_.load(std::memory_order_acquire) ==
+ SingletonHolderState::Living)) {
return instance_ptr_;
}
createInstance();
template <typename T>
std::weak_ptr<T> SingletonHolder<T>::get_weak() {
- if (UNLIKELY(state_ != SingletonHolderState::Living)) {
+ if (UNLIKELY(state_.load(std::memory_order_acquire) !=
+ SingletonHolderState::Living)) {
createInstance();
}
return instance_weak_;
}
+template <typename T>
+std::shared_ptr<T> SingletonHolder<T>::try_get() {
+ if (UNLIKELY(state_.load(std::memory_order_acquire) !=
+ SingletonHolderState::Living)) {
+ createInstance();
+ }
+
+ return instance_weak_.lock();
+}
+
+template <typename T>
+folly::ReadMostlySharedPtr<T> SingletonHolder<T>::try_get_fast() {
+ if (UNLIKELY(state_.load(std::memory_order_acquire) !=
+ SingletonHolderState::Living)) {
+ createInstance();
+ }
+
+ return instance_weak_fast_.lock();
+}
+
template <typename T>
TypeDescriptor SingletonHolder<T>::type() {
return type_;
auto type_name = type_.name();
// Can't use make_shared -- no support for a custom deleter, sadly.
- instance_ = std::shared_ptr<T>(
+ std::shared_ptr<T> instance(
create_(),
[destroy_baton, print_destructor_stack_trace, teardown, type_name]
(T* instance_ptr) mutable {
// constructor
SingletonVault::scheduleDestroyInstances();
- instance_weak_ = instance_;
- instance_ptr_ = instance_.get();
+ instance_weak_ = instance;
+ instance_ptr_ = instance.get();
+ instance_.reset(std::move(instance));
+ instance_weak_fast_ = instance_;
+
destroy_baton_ = std::move(destroy_baton);
print_destructor_stack_trace_ = std::move(print_destructor_stack_trace);