From: Andrii Grynenko Date: Thu, 4 Dec 2014 01:26:45 +0000 (-0800) Subject: Make folly::Singleton's destruction happen earlier X-Git-Tag: v0.22.0~135 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=24b9221197183fa3a54426814f1df04ed2214fd4;p=folly.git Make folly::Singleton's destruction happen earlier Summary: Schedule destroyInstances to be executed via atexit in registrationComplete. registrationComplete is called from main(), so this makes sure folly::Singleton's will be destoyed before all singletons constructed before main() (which should cover all third-party libraries' singletons). Test Plan: unit test Reviewed By: chip@fb.com Subscribers: trunkagent, njormrod, folly-diffs@ FB internal diff: D1717963 Tasks: 5666654 Signature: t1:1717963:1417659478:c50c271d1786be75499565f6ab9c0a1a6f6f347d --- diff --git a/folly/experimental/Singleton.h b/folly/experimental/Singleton.h index c42daf01..20416015 100644 --- a/folly/experimental/Singleton.h +++ b/folly/experimental/Singleton.h @@ -208,7 +208,7 @@ class SingletonVault { // Mark registration is complete; no more singletons can be // registered at this point. void registrationComplete() { - scheduleDestroyInstances(); + std::atexit([](){ SingletonVault::singleton()->destroyInstances(); }); RWSpinLock::WriteHolder wh(&stateMutex_); @@ -329,6 +329,9 @@ class SingletonVault { SingletonEntry(SingletonEntry&&) = delete; }; + // This method only matters if registrationComplete() is never called. + // Otherwise destroyInstances is scheduled to be executed atexit. + // // Initializes static object, which calls destroyInstances on destruction. // Used to have better deletion ordering with singleton not managed by // folly::Singleton. The desruction will happen in the following order: