Skip to content
Snippets Groups Projects
Commit 36aa62d7 authored by Rye Mutt's avatar Rye Mutt :bread:
Browse files

Replace LLMutex with absl::Mutex initialized as constexpr in LLError

parent 30db82df
No related branches found
No related tags found
No related merge requests found
...@@ -46,8 +46,10 @@ ...@@ -46,8 +46,10 @@
#include <vector> #include <vector>
#include <string_view> #include <string_view>
#include "string.h" #include "string.h"
#include <absl/container/flat_hash_map.h>
#include <absl/strings/str_format.h> #include "absl/synchronization/mutex.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/str_format.h"
#include "llapp.h" #include "llapp.h"
#include "llapr.h" #include "llapr.h"
...@@ -1276,16 +1278,26 @@ namespace { ...@@ -1276,16 +1278,26 @@ namespace {
LOG_MUTEX, LOG_MUTEX,
STACKS_MUTEX STACKS_MUTEX
}; };
// Some logging calls happen very early in processing -- so early that our
// module-static variables aren't yet initialized. getMutex() wraps a ABSL_CONST_INIT absl::Mutex sLogMutex(absl::kConstInit);
// function-static LLMutex so that early calls can still have a valid ABSL_CONST_INIT absl::Mutex sStackMutex(absl::kConstInit);
// LLMutex instance.
template <MutexDiscriminator MTX> template <MutexDiscriminator MTX>
LLMutex* getMutex() absl::Mutex* getMutex()
{
return nullptr;
}
template <>
absl::Mutex* getMutex<LOG_MUTEX>()
{
return &sLogMutex;
}
template <>
absl::Mutex* getMutex<STACKS_MUTEX>()
{ {
// guaranteed to be initialized the first time control reaches here return &sStackMutex;
static LLMutex sMutex;
return &sMutex;
} }
bool checkLevelMap(const LevelMap& map, const std::string& key, bool checkLevelMap(const LevelMap& map, const std::string& key,
...@@ -1334,7 +1346,7 @@ namespace LLError ...@@ -1334,7 +1346,7 @@ namespace LLError
bool Log::shouldLog(CallSite& site) bool Log::shouldLog(CallSite& site)
{ {
LLMutexTrylock lock(getMutex<LOG_MUTEX>(), 5); AbslMutexMaybeTrylock lock(getMutex<LOG_MUTEX>(), 5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return false; return false;
...@@ -1377,7 +1389,7 @@ namespace LLError ...@@ -1377,7 +1389,7 @@ namespace LLError
std::ostringstream* Log::out() std::ostringstream* Log::out()
{ {
LLMutexTrylock lock(getMutex<LOG_MUTEX>(),5); AbslMutexMaybeTrylock lock(getMutex<LOG_MUTEX>(),5);
if (lock.isLocked()) if (lock.isLocked())
{ {
...@@ -1395,7 +1407,7 @@ namespace LLError ...@@ -1395,7 +1407,7 @@ namespace LLError
void Log::flush(std::ostringstream* out, char* message) void Log::flush(std::ostringstream* out, char* message)
{ {
LLMutexTrylock lock(getMutex<LOG_MUTEX>(),5); AbslMutexMaybeTrylock lock(getMutex<LOG_MUTEX>(),5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return; return;
...@@ -1427,7 +1439,7 @@ namespace LLError ...@@ -1427,7 +1439,7 @@ namespace LLError
void Log::flush(std::ostringstream* out, const CallSite& site) void Log::flush(std::ostringstream* out, const CallSite& site)
{ {
LLMutexTrylock lock(getMutex<LOG_MUTEX>(),5); AbslMutexMaybeTrylock lock(getMutex<LOG_MUTEX>(),5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return; return;
...@@ -1616,7 +1628,7 @@ namespace LLError ...@@ -1616,7 +1628,7 @@ namespace LLError
//static //static
void LLCallStacks::push(const char* function, const int line) void LLCallStacks::push(const char* function, const int line)
{ {
LLMutexTrylock lock(getMutex<STACKS_MUTEX>(), 5); AbslMutexMaybeTrylock lock(getMutex<STACKS_MUTEX>(), 5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return; return;
...@@ -1650,7 +1662,7 @@ namespace LLError ...@@ -1650,7 +1662,7 @@ namespace LLError
//static //static
void LLCallStacks::end(std::ostringstream* _out) void LLCallStacks::end(std::ostringstream* _out)
{ {
LLMutexTrylock lock(getMutex<STACKS_MUTEX>(), 5); AbslMutexMaybeTrylock lock(getMutex<STACKS_MUTEX>(), 5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return; return;
...@@ -1672,7 +1684,7 @@ namespace LLError ...@@ -1672,7 +1684,7 @@ namespace LLError
//static //static
void LLCallStacks::print() void LLCallStacks::print()
{ {
LLMutexTrylock lock(getMutex<STACKS_MUTEX>(), 5); AbslMutexMaybeTrylock lock(getMutex<STACKS_MUTEX>(), 5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return; return;
...@@ -1715,7 +1727,7 @@ namespace LLError ...@@ -1715,7 +1727,7 @@ namespace LLError
bool debugLoggingEnabled(const std::string& tag) bool debugLoggingEnabled(const std::string& tag)
{ {
LLMutexTrylock lock(getMutex<LOG_MUTEX>(), 5); AbslMutexMaybeTrylock lock(getMutex<LOG_MUTEX>(), 5);
if (!lock.isLocked()) if (!lock.isLocked())
{ {
return false; return false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment