Remove LazyInstance usage

R=mark@chromium.org
BUG=chromium:686866

Change-Id: I067988694f15d93b064d0b10b1bc5b908c9e5f52
Reviewed-on: https://chromium-review.googlesource.com/435441
Reviewed-by: Mark Mentovai <mark@chromium.org>
This commit is contained in:
Scott Graham 2017-01-31 14:12:19 -08:00
parent 56020daea9
commit 7050c55fca

View File

@ -16,7 +16,6 @@
#include <sys/types.h> #include <sys/types.h>
#include "base/lazy_instance.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/threading/thread_local_storage.h" #include "base/threading/thread_local_storage.h"
@ -28,14 +27,24 @@ namespace {
// handler. A thread may register its thread-specific log message list to // handler. A thread may register its thread-specific log message list to
// receive messages produced just on that thread. // receive messages produced just on that thread.
// //
// Only one object of this class may exist in the program at a time. There must // Only one object of this class may exist in the program at a time as created
// not be any log message handler in effect when it is created, and nothing else // by GetInstance(). There must not be any log message handler in effect when it
// can be set as a log message handler while an object of this class exists. // is created, and nothing else can be set as a log message handler while an
// // object of this class exists.
// Practically, the only object of this class that might exist is managed by the
// g_master lazy instance, which will create it upon first use.
class ThreadLogMessagesMaster { class ThreadLogMessagesMaster {
public: public:
void SetThreadMessageList(std::vector<std::string>* message_list) {
DCHECK_EQ(logging::GetLogMessageHandler(), &LogMessageHandler);
DCHECK_NE(tls_.Get() != nullptr, message_list != nullptr);
tls_.Set(message_list);
}
static ThreadLogMessagesMaster* GetInstance() {
static auto master = new ThreadLogMessagesMaster();
return master;
}
private:
ThreadLogMessagesMaster() { ThreadLogMessagesMaster() {
DCHECK(!tls_.initialized()); DCHECK(!tls_.initialized());
tls_.Initialize(nullptr); tls_.Initialize(nullptr);
@ -45,20 +54,8 @@ class ThreadLogMessagesMaster {
logging::SetLogMessageHandler(LogMessageHandler); logging::SetLogMessageHandler(LogMessageHandler);
} }
~ThreadLogMessagesMaster() { ~ThreadLogMessagesMaster() = delete;
DCHECK_EQ(logging::GetLogMessageHandler(), &LogMessageHandler);
logging::SetLogMessageHandler(nullptr);
tls_.Free();
}
void SetThreadMessageList(std::vector<std::string>* message_list) {
DCHECK_EQ(logging::GetLogMessageHandler(), &LogMessageHandler);
DCHECK_NE(tls_.Get() != nullptr, message_list != nullptr);
tls_.Set(message_list);
}
private:
static bool LogMessageHandler(logging::LogSeverity severity, static bool LogMessageHandler(logging::LogSeverity severity,
const char* file_path, const char* file_path,
int line, int line,
@ -84,18 +81,14 @@ class ThreadLogMessagesMaster {
base::ThreadLocalStorage::StaticSlot ThreadLogMessagesMaster::tls_ base::ThreadLocalStorage::StaticSlot ThreadLogMessagesMaster::tls_
= TLS_INITIALIZER; = TLS_INITIALIZER;
base::LazyInstance<ThreadLogMessagesMaster>::Leaky g_master =
LAZY_INSTANCE_INITIALIZER;
} // namespace } // namespace
ThreadLogMessages::ThreadLogMessages() ThreadLogMessages::ThreadLogMessages() : log_messages_() {
: log_messages_() { ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(&log_messages_);
g_master.Get().SetThreadMessageList(&log_messages_);
} }
ThreadLogMessages::~ThreadLogMessages() { ThreadLogMessages::~ThreadLogMessages() {
g_master.Get().SetThreadMessageList(nullptr); ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(nullptr);
} }
} // namespace crashpad } // namespace crashpad