refactor SignalHandler logger to avoid race during destruction (#682)

Signed-off-by: Dirk Thomas <dirk-thomas@users.noreply.github.com>
This commit is contained in:
Dirk Thomas 2019-04-04 09:31:59 -07:00 committed by GitHub
parent 0f25f714fe
commit ee7e642592
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -46,18 +46,38 @@ dispatch_semaphore_t SignalHandler::signal_handler_sem_;
sem_t SignalHandler::signal_handler_sem_; sem_t SignalHandler::signal_handler_sem_;
#endif #endif
SignalHandler & // The logger must be initialized before the local static variable signal_handler,
SignalHandler::get_global_signal_handler() // from the method get_global_signal_handler(), so that it is destructed after
{ // it, because the destructor of SignalHandler uses this logger object.
static SignalHandler signal_handler; static rclcpp::Logger g_logger = rclcpp::get_logger("rclcpp");
return signal_handler;
}
rclcpp::Logger & rclcpp::Logger &
SignalHandler::get_logger() SignalHandler::get_logger()
{ {
static rclcpp::Logger logger = rclcpp::get_logger("rclcpp"); return g_logger;
return logger; }
SignalHandler &
SignalHandler::get_global_signal_handler()
{
// This is initialized after the g_logger static global, ensuring
// SignalHandler::get_logger() may be called from the destructor of
// SignalHandler, according to this:
//
// Variables declared at block scope with the specifier static have static
// storage duration but are initialized the first time control passes
// through their declaration (unless their initialization is zero- or
// constant-initialization, which can be performed before the block is
// first entered). On all further calls, the declaration is skipped.
//
// -- https://en.cppreference.com/w/cpp/language/storage_duration#Static_local_variables
//
// Which is guaranteed to occur after static initialization for global (see:
// https://en.cppreference.com/w/cpp/language/initialization#Static_initialization),
// which is when g_logger will be initialized.
// And destruction will occur in the reverse order.
static SignalHandler signal_handler;
return signal_handler;
} }
bool bool