add LockTraits
[folly.git] / folly / Singleton.h
1 /*
2  * Copyright 2016 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 // SingletonVault - a library to manage the creation and destruction
18 // of interdependent singletons.
19 //
20 // Basic usage of this class is very simple; suppose you have a class
21 // called MyExpensiveService, and you only want to construct one (ie,
22 // it's a singleton), but you only want to construct it if it is used.
23 //
24 // In your .h file:
25 // class MyExpensiveService { ... };
26 //
27 // In your .cpp file:
28 // namespace { folly::Singleton<MyExpensiveService> the_singleton; }
29 //
30 // Code can access it via:
31 //
32 // MyExpensiveService* instance = Singleton<MyExpensiveService>::get();
33 // or
34 // std::weak_ptr<MyExpensiveService> instance =
35 //     Singleton<MyExpensiveService>::get_weak();
36 //
37 // You also can directly access it by the variable defining the
38 // singleton rather than via get(), and even treat that variable like
39 // a smart pointer (dereferencing it or using the -> operator).
40 //
41 // Please note, however, that all non-weak_ptr interfaces are
42 // inherently subject to races with destruction.  Use responsibly.
43 //
44 // The singleton will be created on demand.  If the constructor for
45 // MyExpensiveService actually makes use of *another* Singleton, then
46 // the right thing will happen -- that other singleton will complete
47 // construction before get() returns.  However, in the event of a
48 // circular dependency, a runtime error will occur.
49 //
50 // You can have multiple singletons of the same underlying type, but
51 // each must be given a unique tag. If no tag is specified - default tag is used
52 //
53 // namespace {
54 // struct Tag1 {};
55 // struct Tag2 {};
56 // folly::Singleton<MyExpensiveService> s_default;
57 // folly::Singleton<MyExpensiveService, Tag1> s1;
58 // folly::Singleton<MyExpensiveService, Tag2> s2;
59 // }
60 // ...
61 // MyExpensiveService* svc_default = s_default.get();
62 // MyExpensiveService* svc1 = s1.get();
63 // MyExpensiveService* svc2 = s2.get();
64 //
65 // By default, the singleton instance is constructed via new and
66 // deleted via delete, but this is configurable:
67 //
68 // namespace { folly::Singleton<MyExpensiveService> the_singleton(create,
69 //                                                                destroy); }
70 //
71 // Where create and destroy are functions, Singleton<T>::CreateFunc
72 // Singleton<T>::TeardownFunc.
73 //
74 // The above examples detail a situation where an expensive singleton is loaded
75 // on-demand (thus only if needed).  However if there is an expensive singleton
76 // that will likely be needed, and initialization takes a potentially long time,
77 // e.g. while initializing, parsing some files, talking to remote services,
78 // making uses of other singletons, and so on, the initialization of those can
79 // be scheduled up front, or "eagerly".
80 //
81 // In that case the singleton can be declared this way:
82 //
83 // namespace {
84 // auto the_singleton =
85 //     folly::Singleton<MyExpensiveService>(/* optional create, destroy args */)
86 //     .shouldEagerInit();
87 // }
88 //
89 // This way the singleton's instance is built at program initialization,
90 // if the program opted-in to that feature by calling "doEagerInit" or
91 // "doEagerInitVia" during its startup.
92 //
93 // What if you need to destroy all of your singletons?  Say, some of
94 // your singletons manage threads, but you need to fork?  Or your unit
95 // test wants to clean up all global state?  Then you can call
96 // SingletonVault::singleton()->destroyInstances(), which invokes the
97 // TeardownFunc for each singleton, in the reverse order they were
98 // created.  It is your responsibility to ensure your singletons can
99 // handle cases where the singletons they depend on go away, however.
100 // Singletons won't be recreated after destroyInstances call. If you
101 // want to re-enable singleton creation (say after fork was called) you
102 // should call reenableInstances.
103
104 #pragma once
105 #include <folly/Baton.h>
106 #include <folly/Exception.h>
107 #include <folly/Hash.h>
108 #include <folly/Memory.h>
109 #include <folly/RWSpinLock.h>
110 #include <folly/Demangle.h>
111 #include <folly/Executor.h>
112 #include <folly/experimental/ReadMostlySharedPtr.h>
113 #include <folly/detail/StaticSingletonManager.h>
114
115 #include <algorithm>
116 #include <atomic>
117 #include <condition_variable>
118 #include <functional>
119 #include <memory>
120 #include <mutex>
121 #include <string>
122 #include <thread>
123 #include <typeindex>
124 #include <typeinfo>
125 #include <unordered_map>
126 #include <unordered_set>
127 #include <vector>
128
129 #include <glog/logging.h>
130
131 // use this guard to handleSingleton breaking change in 3rd party code
132 #ifndef FOLLY_SINGLETON_TRY_GET
133 #define FOLLY_SINGLETON_TRY_GET
134 #endif
135
136 namespace folly {
137
138 // For actual usage, please see the Singleton<T> class at the bottom
139 // of this file; that is what you will actually interact with.
140
141 // SingletonVault is the class that manages singleton instances.  It
142 // is unaware of the underlying types of singletons, and simply
143 // manages lifecycles and invokes CreateFunc and TeardownFunc when
144 // appropriate.  In general, you won't need to interact with the
145 // SingletonVault itself.
146 //
147 // A vault goes through a few stages of life:
148 //
149 //   1. Registration phase; singletons can be registered:
150 //      a) Strict: no singleton can be created in this stage.
151 //      b) Relaxed: singleton can be created (the default vault is Relaxed).
152 //   2. registrationComplete() has been called; singletons can no
153 //      longer be registered, but they can be created.
154 //   3. A vault can return to stage 1 when destroyInstances is called.
155 //
156 // In general, you don't need to worry about any of the above; just
157 // ensure registrationComplete() is called near the top of your main()
158 // function, otherwise no singletons can be instantiated.
159
160 class SingletonVault;
161
162 namespace detail {
163
164 struct DefaultTag {};
165
166 // A TypeDescriptor is the unique handle for a given singleton.  It is
167 // a combinaiton of the type and of the optional name, and is used as
168 // a key in unordered_maps.
169 class TypeDescriptor {
170  public:
171   TypeDescriptor(const std::type_info& ti,
172                  const std::type_info& tag_ti)
173       : ti_(ti), tag_ti_(tag_ti) {
174   }
175
176   TypeDescriptor(const TypeDescriptor& other)
177       : ti_(other.ti_), tag_ti_(other.tag_ti_) {
178   }
179
180   TypeDescriptor& operator=(const TypeDescriptor& other) {
181     if (this != &other) {
182       ti_ = other.ti_;
183       tag_ti_ = other.tag_ti_;
184     }
185
186     return *this;
187   }
188
189   std::string name() const {
190     auto ret = demangle(ti_.name());
191     if (tag_ti_ != std::type_index(typeid(DefaultTag))) {
192       ret += "/";
193       ret += demangle(tag_ti_.name());
194     }
195     return ret.toStdString();
196   }
197
198   friend class TypeDescriptorHasher;
199
200   bool operator==(const TypeDescriptor& other) const {
201     return ti_ == other.ti_ && tag_ti_ == other.tag_ti_;
202   }
203
204  private:
205   std::type_index ti_;
206   std::type_index tag_ti_;
207 };
208
209 class TypeDescriptorHasher {
210  public:
211   size_t operator()(const TypeDescriptor& ti) const {
212     return folly::hash::hash_combine(ti.ti_, ti.tag_ti_);
213   }
214 };
215
216 // This interface is used by SingletonVault to interact with SingletonHolders.
217 // Having a non-template interface allows SingletonVault to keep a list of all
218 // SingletonHolders.
219 class SingletonHolderBase {
220  public:
221   explicit SingletonHolderBase(TypeDescriptor typeDesc) : type_(typeDesc) {}
222   virtual ~SingletonHolderBase() = default;
223
224   TypeDescriptor type() const {
225     return type_;
226   }
227   virtual bool hasLiveInstance() = 0;
228   virtual void createInstance() = 0;
229   virtual bool creationStarted() = 0;
230   virtual void destroyInstance() = 0;
231
232  private:
233   TypeDescriptor type_;
234 };
235
236 // An actual instance of a singleton, tracking the instance itself,
237 // its state as described above, and the create and teardown
238 // functions.
239 template <typename T>
240 struct SingletonHolder : public SingletonHolderBase {
241  public:
242   typedef std::function<void(T*)> TeardownFunc;
243   typedef std::function<T*(void)> CreateFunc;
244
245   template <typename Tag, typename VaultTag>
246   inline static SingletonHolder<T>& singleton();
247
248   inline T* get();
249   inline std::weak_ptr<T> get_weak();
250   inline std::shared_ptr<T> try_get();
251   inline folly::ReadMostlySharedPtr<T> try_get_fast();
252
253   void registerSingleton(CreateFunc c, TeardownFunc t);
254   void registerSingletonMock(CreateFunc c, TeardownFunc t);
255   virtual bool hasLiveInstance() override;
256   virtual void createInstance() override;
257   virtual bool creationStarted() override;
258   virtual void destroyInstance() override;
259
260  private:
261   SingletonHolder(TypeDescriptor type, SingletonVault& vault);
262
263   enum class SingletonHolderState {
264     NotRegistered,
265     Dead,
266     Living,
267   };
268
269   SingletonVault& vault_;
270
271   // mutex protects the entire entry during construction/destruction
272   std::mutex mutex_;
273
274   // State of the singleton entry. If state is Living, instance_ptr and
275   // instance_weak can be safely accessed w/o synchronization.
276   std::atomic<SingletonHolderState> state_{SingletonHolderState::NotRegistered};
277
278   // the thread creating the singleton (only valid while creating an object)
279   std::atomic<std::thread::id> creating_thread_;
280
281   // The singleton itself and related functions.
282
283   // holds a ReadMostlyMainPtr to singleton instance, set when state is changed
284   // from Dead to Living. Reset when state is changed from Living to Dead.
285   folly::ReadMostlyMainPtr<T> instance_;
286   // weak_ptr to the singleton instance, set when state is changed from Dead
287   // to Living. We never write to this object after initialization, so it is
288   // safe to read it from different threads w/o synchronization if we know
289   // that state is set to Living
290   std::weak_ptr<T> instance_weak_;
291   // Fast equivalent of instance_weak_
292   folly::ReadMostlyWeakPtr<T> instance_weak_fast_;
293   // Time we wait on destroy_baton after releasing Singleton shared_ptr.
294   std::shared_ptr<folly::Baton<>> destroy_baton_;
295   T* instance_ptr_ = nullptr;
296   CreateFunc create_ = nullptr;
297   TeardownFunc teardown_ = nullptr;
298
299   std::shared_ptr<std::atomic<bool>> print_destructor_stack_trace_;
300
301   SingletonHolder(const SingletonHolder&) = delete;
302   SingletonHolder& operator=(const SingletonHolder&) = delete;
303   SingletonHolder& operator=(SingletonHolder&&) = delete;
304   SingletonHolder(SingletonHolder&&) = delete;
305 };
306
307 }
308
309 class SingletonVault {
310  public:
311   enum class Type {
312     Strict, // Singletons can't be created before registrationComplete()
313     Relaxed, // Singletons can be created before registrationComplete()
314   };
315
316   /**
317    * Clears all singletons in the given vault at ctor and dtor times.
318    * Useful for unit-tests that need to clear the world.
319    *
320    * This need can arise when a unit-test needs to swap out an object used by a
321    * singleton for a test-double, but the singleton needing its dependency to be
322    * swapped has a type or a tag local to some other translation unit and
323    * unavailable in the current translation unit.
324    *
325    * Other, better approaches to this need are "plz 2 refactor" ....
326    */
327   struct ScopedExpunger {
328     SingletonVault* vault;
329     explicit ScopedExpunger(SingletonVault* v) : vault(v) { expunge(); }
330     ~ScopedExpunger() { expunge(); }
331     void expunge() {
332       vault->destroyInstances();
333       vault->reenableInstances();
334     }
335   };
336
337   explicit SingletonVault(Type type = Type::Relaxed) : type_(type) {}
338
339   // Destructor is only called by unit tests to check destroyInstances.
340   ~SingletonVault();
341
342   typedef std::function<void(void*)> TeardownFunc;
343   typedef std::function<void*(void)> CreateFunc;
344
345   // Ensure that Singleton has not been registered previously and that
346   // registration is not complete. If validations succeeds,
347   // register a singleton of a given type with the create and teardown
348   // functions.
349   void registerSingleton(detail::SingletonHolderBase* entry);
350
351   /**
352    * Called by `Singleton<T>.shouldEagerInit()` to ensure the instance
353    * is built when `doEagerInit[Via]` is called; see those methods
354    * for more info.
355    */
356   void addEagerInitSingleton(detail::SingletonHolderBase* entry);
357
358   // Mark registration is complete; no more singletons can be
359   // registered at this point.
360   void registrationComplete();
361
362   /**
363    * Initialize all singletons which were marked as eager-initialized
364    * (using `shouldEagerInit()`).  No return value.  Propagates exceptions
365    * from constructors / create functions, as is the usual case when calling
366    * for example `Singleton<Foo>::get_weak()`.
367    */
368   void doEagerInit();
369
370   /**
371    * Schedule eager singletons' initializations through the given executor.
372    * If baton ptr is not null, its `post` method is called after all
373    * early initialization has completed.
374    *
375    * If exceptions are thrown during initialization, this method will still
376    * `post` the baton to indicate completion.  The exception will not propagate
377    * and future attempts to `try_get` or `get_weak` the failed singleton will
378    * retry initialization.
379    *
380    * Sample usage:
381    *
382    *   wangle::IOThreadPoolExecutor executor(max_concurrency_level);
383    *   folly::Baton<> done;
384    *   doEagerInitVia(executor, &done);
385    *   done.wait();  // or 'timed_wait', or spin with 'try_wait'
386    *
387    */
388   void doEagerInitVia(Executor& exe, folly::Baton<>* done = nullptr);
389
390   // Destroy all singletons; when complete, the vault can't create
391   // singletons once again until reenableInstances() is called.
392   void destroyInstances();
393
394   // Enable re-creating singletons after destroyInstances() was called.
395   void reenableInstances();
396
397   // For testing; how many registered and living singletons we have.
398   size_t registeredSingletonCount() const {
399     RWSpinLock::ReadHolder rh(&mutex_);
400
401     return singletons_.size();
402   }
403
404   /**
405    * Flips to true if eager initialization was used, and has completed.
406    * Never set to true if "doEagerInit()" or "doEagerInitVia" never called.
407    */
408   bool eagerInitComplete() const;
409
410   size_t livingSingletonCount() const {
411     RWSpinLock::ReadHolder rh(&mutex_);
412
413     size_t ret = 0;
414     for (const auto& p : singletons_) {
415       if (p.second->hasLiveInstance()) {
416         ++ret;
417       }
418     }
419
420     return ret;
421   }
422
423   // A well-known vault; you can actually have others, but this is the
424   // default.
425   static SingletonVault* singleton() {
426     return singleton<>();
427   }
428
429   // Gets singleton vault for any Tag. Non-default tag should be used in unit
430   // tests only.
431   template <typename VaultTag = detail::DefaultTag>
432   static SingletonVault* singleton() {
433     static SingletonVault* vault =
434         detail::createGlobal<SingletonVault, VaultTag>();
435     return vault;
436   }
437
438   typedef std::string(*StackTraceGetterPtr)();
439
440   static std::atomic<StackTraceGetterPtr>& stackTraceGetter() {
441     static std::atomic<StackTraceGetterPtr>* stackTraceGetterPtr =
442         detail::createGlobal<std::atomic<StackTraceGetterPtr>,
443                              SingletonVault>();
444     return *stackTraceGetterPtr;
445   }
446
447  private:
448   template <typename T>
449   friend struct detail::SingletonHolder;
450
451   // The two stages of life for a vault, as mentioned in the class comment.
452   enum class SingletonVaultState {
453     Running,
454     Quiescing,
455   };
456
457   // Each singleton in the vault can be in two states: dead
458   // (registered but never created), living (CreateFunc returned an instance).
459
460   void stateCheck(SingletonVaultState expected,
461                   const char* msg="Unexpected singleton state change") {
462     if (expected != state_) {
463         throw std::logic_error(msg);
464     }
465   }
466
467   // This method only matters if registrationComplete() is never called.
468   // Otherwise destroyInstances is scheduled to be executed atexit.
469   //
470   // Initializes static object, which calls destroyInstances on destruction.
471   // Used to have better deletion ordering with singleton not managed by
472   // folly::Singleton. The desruction will happen in the following order:
473   // 1. Singletons, not managed by folly::Singleton, which were created after
474   //    any of the singletons managed by folly::Singleton was requested.
475   // 2. All singletons managed by folly::Singleton
476   // 3. Singletons, not managed by folly::Singleton, which were created before
477   //    any of the singletons managed by folly::Singleton was requested.
478   static void scheduleDestroyInstances();
479
480   typedef std::unordered_map<detail::TypeDescriptor,
481                              detail::SingletonHolderBase*,
482                              detail::TypeDescriptorHasher> SingletonMap;
483
484   mutable folly::RWSpinLock mutex_;
485   SingletonMap singletons_;
486   std::unordered_set<detail::SingletonHolderBase*> eagerInitSingletons_;
487   std::vector<detail::TypeDescriptor> creation_order_;
488   SingletonVaultState state_{SingletonVaultState::Running};
489   bool registrationComplete_{false};
490   folly::RWSpinLock stateMutex_;
491   Type type_{Type::Relaxed};
492 };
493
494 // This is the wrapper class that most users actually interact with.
495 // It allows for simple access to registering and instantiating
496 // singletons.  Create instances of this class in the global scope of
497 // type Singleton<T> to register your singleton for later access via
498 // Singleton<T>::try_get().
499 template <typename T,
500           typename Tag = detail::DefaultTag,
501           typename VaultTag = detail::DefaultTag /* for testing */>
502 class Singleton {
503  public:
504   typedef std::function<T*(void)> CreateFunc;
505   typedef std::function<void(T*)> TeardownFunc;
506
507   // Generally your program life cycle should be fine with calling
508   // get() repeatedly rather than saving the reference, and then not
509   // call get() during process shutdown.
510   FOLLY_DEPRECATED("Replaced by try_get")
511   static T* get() { return getEntry().get(); }
512
513   // If, however, you do need to hold a reference to the specific
514   // singleton, you can try to do so with a weak_ptr.  Avoid this when
515   // possible but the inability to lock the weak pointer can be a
516   // signal that the vault has been destroyed.
517   FOLLY_DEPRECATED("Replaced by try_get")
518   static std::weak_ptr<T> get_weak() { return getEntry().get_weak(); }
519
520   // Preferred alternative to get_weak, it returns shared_ptr that can be
521   // stored; a singleton won't be destroyed unless shared_ptr is destroyed.
522   // Avoid holding these shared_ptrs beyond the scope of a function;
523   // don't put them in member variables, always use try_get() instead
524   //
525   // try_get() can return nullptr if the singleton was destroyed, caller is
526   // responsible for handling nullptr return
527   static std::shared_ptr<T> try_get() {
528     return getEntry().try_get();
529   }
530
531   static folly::ReadMostlySharedPtr<T> try_get_fast() {
532     return getEntry().try_get_fast();
533   }
534
535   explicit Singleton(std::nullptr_t /* _ */ = nullptr,
536                      typename Singleton::TeardownFunc t = nullptr)
537       : Singleton([]() { return new T; }, std::move(t)) {}
538
539   explicit Singleton(typename Singleton::CreateFunc c,
540                      typename Singleton::TeardownFunc t = nullptr) {
541     if (c == nullptr) {
542       throw std::logic_error(
543         "nullptr_t should be passed if you want T to be default constructed");
544     }
545
546     auto vault = SingletonVault::singleton<VaultTag>();
547     getEntry().registerSingleton(std::move(c), getTeardownFunc(std::move(t)));
548     vault->registerSingleton(&getEntry());
549   }
550
551   /**
552    * Should be instantiated as soon as "doEagerInit[Via]" is called.
553    * Singletons are usually lazy-loaded (built on-demand) but for those which
554    * are known to be needed, to avoid the potential lag for objects that take
555    * long to construct during runtime, there is an option to make sure these
556    * are built up-front.
557    *
558    * Use like:
559    *   Singleton<Foo> gFooInstance = Singleton<Foo>(...).shouldEagerInit();
560    *
561    * Or alternately, define the singleton as usual, and say
562    *   gFooInstance.shouldEagerInit();
563    *
564    * at some point prior to calling registrationComplete().
565    * Then doEagerInit() or doEagerInitVia(Executor*) can be called.
566    */
567   Singleton& shouldEagerInit() {
568     auto vault = SingletonVault::singleton<VaultTag>();
569     vault->addEagerInitSingleton(&getEntry());
570     return *this;
571   }
572
573   /**
574   * Construct and inject a mock singleton which should be used only from tests.
575   * Unlike regular singletons which are initialized once per process lifetime,
576   * mock singletons live for the duration of a test. This means that one process
577   * running multiple tests can initialize and register the same singleton
578   * multiple times. This functionality should be used only from tests
579   * since it relaxes validation and performance in order to be able to perform
580   * the injection. The returned mock singleton is functionality identical to
581   * regular singletons.
582   */
583   static void make_mock(std::nullptr_t /* c */ = nullptr,
584                         typename Singleton<T>::TeardownFunc t = nullptr) {
585     make_mock([]() { return new T; }, t);
586   }
587
588   static void make_mock(CreateFunc c,
589                         typename Singleton<T>::TeardownFunc t = nullptr) {
590     if (c == nullptr) {
591       throw std::logic_error(
592         "nullptr_t should be passed if you want T to be default constructed");
593     }
594
595     auto& entry = getEntry();
596
597     entry.registerSingletonMock(c, getTeardownFunc(t));
598   }
599
600  private:
601   inline static detail::SingletonHolder<T>& getEntry() {
602     return detail::SingletonHolder<T>::template singleton<Tag, VaultTag>();
603   }
604
605   // Construct TeardownFunc.
606   static typename detail::SingletonHolder<T>::TeardownFunc getTeardownFunc(
607       TeardownFunc t)  {
608     if (t == nullptr) {
609       return  [](T* v) { delete v; };
610     } else {
611       return t;
612     }
613   }
614 };
615
616 template <typename T, typename Tag = detail::DefaultTag>
617 class LeakySingleton {
618  public:
619   using CreateFunc = std::function<T*()>;
620
621   LeakySingleton() : LeakySingleton([] { return new T(); }) {}
622
623   explicit LeakySingleton(CreateFunc createFunc) {
624     auto& entry = entryInstance();
625     if (entry.state != State::NotRegistered) {
626       LOG(FATAL) << "Double registration of singletons of the same "
627                  << "underlying type; check for multiple definitions "
628                  << "of type folly::LeakySingleton<" + entry.type_.name() + ">";
629     }
630     entry.createFunc = createFunc;
631     entry.state = State::Dead;
632   }
633
634   static T& get() { return instance(); }
635
636  private:
637   enum class State { NotRegistered, Dead, Living };
638
639   struct Entry {
640     Entry() {}
641     Entry(const Entry&) = delete;
642     Entry& operator=(const Entry&) = delete;
643
644     std::atomic<State> state{State::NotRegistered};
645     T* ptr{nullptr};
646     CreateFunc createFunc;
647     std::mutex mutex;
648     detail::TypeDescriptor type_{typeid(T), typeid(Tag)};
649   };
650
651   static Entry& entryInstance() {
652     static auto entry = detail::createGlobal<Entry, Tag>();
653     return *entry;
654   }
655
656   static T& instance() {
657     auto& entry = entryInstance();
658     if (UNLIKELY(entry.state != State::Living)) {
659       createInstance();
660     }
661
662     return *entry.ptr;
663   }
664
665   static void createInstance() {
666     auto& entry = entryInstance();
667
668     std::lock_guard<std::mutex> lg(entry.mutex);
669     if (entry.state == State::Living) {
670       return;
671     }
672
673     if (entry.state == State::NotRegistered) {
674       auto ptr = SingletonVault::stackTraceGetter().load();
675       LOG(FATAL) << "Creating instance for unregistered singleton: "
676                  << entry.type_.name() << "\n"
677                  << "Stacktrace:"
678                  << "\n" << (ptr ? (*ptr)() : "(not available)");
679     }
680
681     entry.ptr = entry.createFunc();
682     entry.state = State::Living;
683   }
684 };
685 }
686
687 #include <folly/Singleton-inl.h>