Move Singleton out of folly/experimental into folly/
[folly.git] / folly / Singleton.cpp
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 #include <folly/Singleton.h>
18
19 #include <string>
20
21 namespace folly {
22
23 namespace detail {
24
25 constexpr std::chrono::seconds SingletonHolderBase::kDestroyWaitTime;
26
27 }
28
29 namespace {
30
31 struct FatalHelper {
32   ~FatalHelper() {
33     if (!leakedSingletons_.empty()) {
34       std::string leakedTypes;
35       for (const auto& singleton : leakedSingletons_) {
36         leakedTypes += "\t" + singleton.name() + "\n";
37       }
38       LOG(DFATAL) << "Singletons of the following types had living references "
39                   << "after destroyInstances was finished:\n" << leakedTypes
40                   << "beware! It is very likely that those singleton instances "
41                   << "are leaked.";
42     }
43   }
44
45   std::vector<detail::TypeDescriptor> leakedSingletons_;
46 };
47
48 FatalHelper __attribute__ ((__init_priority__ (101))) fatalHelper;
49
50 }
51
52 SingletonVault::~SingletonVault() { destroyInstances(); }
53
54 void SingletonVault::destroyInstances() {
55   RWSpinLock::WriteHolder state_wh(&stateMutex_);
56
57   if (state_ == SingletonVaultState::Quiescing) {
58     return;
59   }
60   state_ = SingletonVaultState::Quiescing;
61
62   RWSpinLock::ReadHolder state_rh(std::move(state_wh));
63
64   {
65     RWSpinLock::ReadHolder rh(&mutex_);
66
67     CHECK_GE(singletons_.size(), creation_order_.size());
68
69     for (auto type_iter = creation_order_.rbegin();
70          type_iter != creation_order_.rend();
71          ++type_iter) {
72       singletons_[*type_iter]->destroyInstance();
73     }
74
75     for (auto& singleton_type: creation_order_) {
76       auto singleton = singletons_[singleton_type];
77       if (!singleton->hasLiveInstance()) {
78         continue;
79       }
80
81       fatalHelper.leakedSingletons_.push_back(singleton->type());
82     }
83   }
84
85   {
86     RWSpinLock::WriteHolder wh(&mutex_);
87     creation_order_.clear();
88   }
89 }
90
91 void SingletonVault::reenableInstances() {
92   RWSpinLock::WriteHolder state_wh(&stateMutex_);
93
94   stateCheck(SingletonVaultState::Quiescing);
95
96   state_ = SingletonVaultState::Running;
97 }
98
99 void SingletonVault::scheduleDestroyInstances() {
100   RequestContext::getStaticContext();
101
102   class SingletonVaultDestructor {
103    public:
104     ~SingletonVaultDestructor() {
105       SingletonVault::singleton()->destroyInstances();
106     }
107   };
108
109   // Here we intialize a singleton, which calls destroyInstances in its
110   // destructor. Because of singleton destruction order - it will be destroyed
111   // before all the singletons, which were initialized before it and after all
112   // the singletons initialized after it.
113   static SingletonVaultDestructor singletonVaultDestructor;
114 }
115
116 }