Skip to content

Commit

Permalink
Deprecate non-RAII init()
Browse files Browse the repository at this point in the history
Summary: RAII version has been around for a while and where adopted it has simplified shutdown management, let's deprecate the old function.

Reviewed By: yfeldblum, luciang

Differential Revision: D46561311

fbshipit-source-id: 53e8068e26206e1b2ee51df535356e5187dfaa52
  • Loading branch information
ot authored and facebook-github-bot committed Jun 16, 2023
1 parent bf4a53e commit 9079e21
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
23 changes: 15 additions & 8 deletions folly/init/Init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,7 @@ const unsigned long kAllFatalSignals =

InitOptions::InitOptions() noexcept : fatal_signals(kAllFatalSignals) {}

void init(int* argc, char*** argv, bool removeFlags) {
InitOptions options;
options.removeFlags(removeFlags);
init(argc, argv, options);
}
namespace {

#if FOLLY_USE_SYMBOLIZER
// Newer versions of glog require the function passed to InstallFailureFunction
Expand All @@ -65,7 +61,7 @@ wrapped_abort() {
}
#endif

void init(int* argc, char*** argv, InitOptions options) {
void initImpl(int* argc, char*** argv, InitOptions options) {
#if !defined(_WIN32)
// Install the handler now, to trap errors received during startup.
// The callbacks, if any, can be installed later
Expand Down Expand Up @@ -104,15 +100,26 @@ void init(int* argc, char*** argv, InitOptions options) {
folly::enable_hazptr_thread_pool_executor();
}

} // namespace

Init::Init(int* argc, char*** argv, bool removeFlags) {
init(argc, argv, removeFlags);
initImpl(argc, argv, InitOptions{}.removeFlags(removeFlags));
}

Init::Init(int* argc, char*** argv, InitOptions options) {
init(argc, argv, options);
initImpl(argc, argv, options);
}

Init::~Init() {
SingletonVault::singleton()->destroyInstancesFinal();
}

void init(int* argc, char*** argv, bool removeFlags) {
initImpl(argc, argv, InitOptions{}.removeFlags(removeFlags));
}

void init(int* argc, char*** argv, InitOptions options) {
initImpl(argc, argv, options);
}

} // namespace folly
36 changes: 16 additions & 20 deletions folly/init/Init.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#pragma once
#include <bitset>

#include <folly/CPortability.h>
#include <folly/Portability.h>

namespace folly {
class InitOptions {
Expand Down Expand Up @@ -51,35 +51,26 @@ class InitOptions {
}
};

/*
* Calls common init functions in the necessary order
* Among other things, this ensures that folly::Singletons are initialized
* correctly and installs signal handlers for a superior debugging experience.
* It also initializes gflags and glog.
*
* @param argc, argv arguments to your main
* @param removeFlags if true, will update argc,argv to remove recognized
* gflags passed on the command line
* @param options options
*/

void init(int* argc, char*** argv, bool removeFlags = true);

void init(int* argc, char*** argv, InitOptions options);

/*
* An RAII object to be constructed at the beginning of main() and destructed
* implicitly at the end of main().
*
* The constructor performs the same setup as folly::init(), including
* initializing singletons managed by folly::Singleton.
* The constructor calls common init functions in the necessary order
* Among other things, this ensures that folly::Singletons are initialized
* correctly and installs signal handlers for a superior debugging experience.
* It also initializes gflags and glog.
*
* The destructor destroys all singletons managed by folly::Singleton, yielding
* better shutdown behavior when performed at the end of main(). In particular,
* this guarantees that all singletons managed by folly::Singleton are destroyed
* before all Meyers singletons are destroyed.
*
* @param argc, argv arguments to your main
* @param removeFlags if true, will update argc,argv to remove recognized
* gflags passed on the command line
* @param options options
*/
class Init {
class FOLLY_NODISCARD Init {
public:
// Force ctor & dtor out of line for better stack traces even with LTO.
FOLLY_NOINLINE Init(int* argc, char*** argv, bool removeFlags = true);
Expand All @@ -94,4 +85,9 @@ class Init {
Init& operator=(Init&&) = delete;
};

[[deprecated("Use the RAII version Init")]] void init(
int* argc, char*** argv, bool removeFlags = true);
[[deprecated("Use the RAII version Init")]] void init(
int* argc, char*** argv, InitOptions options);

} // namespace folly

0 comments on commit 9079e21

Please sign in to comment.