2 * Copyright 2014 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.
17 #include <folly/experimental/Singleton.h>
25 constexpr std::chrono::seconds SingletonHolderBase::kDestroyWaitTime;
29 SingletonVault::~SingletonVault() { destroyInstances(); }
31 void SingletonVault::destroyInstances() {
32 RWSpinLock::WriteHolder state_wh(&stateMutex_);
34 if (state_ == SingletonVaultState::Quiescing) {
37 state_ = SingletonVaultState::Quiescing;
39 RWSpinLock::ReadHolder state_rh(std::move(state_wh));
42 RWSpinLock::ReadHolder rh(&mutex_);
44 CHECK_GE(singletons_.size(), creation_order_.size());
46 for (auto type_iter = creation_order_.rbegin();
47 type_iter != creation_order_.rend();
49 singletons_[*type_iter]->destroyInstance();
54 RWSpinLock::WriteHolder wh(&mutex_);
55 creation_order_.clear();
59 void SingletonVault::reenableInstances() {
60 RWSpinLock::WriteHolder state_wh(&stateMutex_);
62 stateCheck(SingletonVaultState::Quiescing);
64 state_ = SingletonVaultState::Running;
67 void SingletonVault::scheduleDestroyInstances() {
68 RequestContext::getStaticContext();
70 class SingletonVaultDestructor {
72 ~SingletonVaultDestructor() {
73 SingletonVault::singleton()->destroyInstances();
77 // Here we intialize a singleton, which calls destroyInstances in its
78 // destructor. Because of singleton destruction order - it will be destroyed
79 // before all the singletons, which were initialized before it and after all
80 // the singletons initialized after it.
81 static SingletonVaultDestructor singletonVaultDestructor;