Use thread_local instead of ThreadLocalStorage::Slot.

This also significantly simplifies the implementation, since we don't
really need the ThreadLogMessagesMaster class at all.

Bug: chromium:1416710
Change-Id: I85849230015f901dfbf084d140e639f14cb872a7
Reviewed-on: https://chromium-review.googlesource.com/c/crashpad/crashpad/+/4313281
Reviewed-by: Mark Mentovai <mark@chromium.org>
Commit-Queue: Peter Kasting <pkasting@chromium.org>
This commit is contained in:
Peter Kasting 2023-03-06 18:27:34 -08:00 committed by Crashpad LUCI CQ
parent 3e54a2cd1b
commit 322eaa5850
2 changed files with 25 additions and 55 deletions

View File

@ -18,53 +18,20 @@
#include "base/check_op.h" #include "base/check_op.h"
#include "base/logging.h" #include "base/logging.h"
#include "base/threading/thread_local_storage.h"
namespace crashpad { namespace crashpad {
namespace { namespace {
// While an object of this class exists, it will be set as the log message thread_local std::vector<std::string>* thread_local_log_messages;
// handler. A thread may register its thread-specific log message list to
// receive messages produced just on that thread.
//
// Only one object of this class may exist in the program at a time as created
// by GetInstance(). There must not be any log message handler in effect when it
// is created, and nothing else can be set as a log message handler while an
// object of this class exists.
class ThreadLogMessagesMaster {
public:
ThreadLogMessagesMaster(const ThreadLogMessagesMaster&) = delete;
ThreadLogMessagesMaster& operator=(const ThreadLogMessagesMaster&) = delete;
void SetThreadMessageList(std::vector<std::string>* message_list) { bool LogMessageHandler(logging::LogSeverity severity,
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() {
DCHECK(!logging::GetLogMessageHandler());
logging::SetLogMessageHandler(LogMessageHandler);
}
~ThreadLogMessagesMaster() = delete;
static bool LogMessageHandler(logging::LogSeverity severity,
const char* file_path, const char* file_path,
int line, int line,
size_t message_start, size_t message_start,
const std::string& string) { const std::string& string) {
std::vector<std::string>* log_messages = if (thread_local_log_messages) {
reinterpret_cast<std::vector<std::string>*>(GetInstance()->tls_.Get()); thread_local_log_messages->push_back(string);
if (log_messages) {
log_messages->push_back(string);
} }
// Dont consume the message. Allow it to be logged as if nothing was set as // Dont consume the message. Allow it to be logged as if nothing was set as
@ -72,17 +39,17 @@ class ThreadLogMessagesMaster {
return false; return false;
} }
base::ThreadLocalStorage::Slot tls_;
};
} // namespace } // namespace
ThreadLogMessages::ThreadLogMessages() : log_messages_() { ThreadLogMessages::ThreadLogMessages()
ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(&log_messages_); : log_messages_(),
} reset_thread_local_log_messages_(&thread_local_log_messages,
&log_messages_) {
ThreadLogMessages::~ThreadLogMessages() { [[maybe_unused]] static bool initialized = [] {
ThreadLogMessagesMaster::GetInstance()->SetThreadMessageList(nullptr); DCHECK(!logging::GetLogMessageHandler());
logging::SetLogMessageHandler(LogMessageHandler);
return true;
}();
} }
} // namespace crashpad } // namespace crashpad

View File

@ -18,6 +18,7 @@
#include <string> #include <string>
#include <vector> #include <vector>
#include "base/auto_reset.h"
namespace crashpad { namespace crashpad {
@ -34,7 +35,7 @@ class ThreadLogMessages {
ThreadLogMessages(const ThreadLogMessages&) = delete; ThreadLogMessages(const ThreadLogMessages&) = delete;
ThreadLogMessages& operator=(const ThreadLogMessages&) = delete; ThreadLogMessages& operator=(const ThreadLogMessages&) = delete;
~ThreadLogMessages(); ~ThreadLogMessages() = default;
//! \return The log messages collected on the thread that this object was //! \return The log messages collected on the thread that this object was
//! created on since the time it was created. //! created on since the time it was created.
@ -42,6 +43,8 @@ class ThreadLogMessages {
private: private:
std::vector<std::string> log_messages_; std::vector<std::string> log_messages_;
const base::AutoReset<std::vector<std::string>*>
reset_thread_local_log_messages_;
}; };
} // namespace crashpad } // namespace crashpad