Prevent registering threads twice in logger
Important for web builds where audio thread coincides with main thread
This commit is contained in:
parent
151afec7f0
commit
fac3503820
1 changed files with 12 additions and 0 deletions
|
|
@ -139,10 +139,15 @@ namespace psemek::log
|
||||||
level max_;
|
level max_;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
thread_local bool this_thread_registered = false;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void register_thread(std::string name)
|
void register_thread(std::string name)
|
||||||
{
|
{
|
||||||
|
if (this_thread_registered)
|
||||||
|
return;
|
||||||
|
|
||||||
#ifdef _PTHREAD_H
|
#ifdef _PTHREAD_H
|
||||||
#if (defined _GNU_SOURCE)
|
#if (defined _GNU_SOURCE)
|
||||||
#if __APPLE__
|
#if __APPLE__
|
||||||
|
|
@ -167,6 +172,8 @@ namespace psemek::log
|
||||||
thread_names[id] = std::make_unique<std::string>(name);
|
thread_names[id] = std::make_unique<std::string>(name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this_thread_registered = true;
|
||||||
|
|
||||||
{
|
{
|
||||||
std::size_t current_max = max_thread_name_length.load();
|
std::size_t current_max = max_thread_name_length.load();
|
||||||
while(current_max < name.size() && !max_thread_name_length.compare_exchange_weak(current_max, name.size()))
|
while(current_max < name.size() && !max_thread_name_length.compare_exchange_weak(current_max, name.size()))
|
||||||
|
|
@ -178,6 +185,9 @@ namespace psemek::log
|
||||||
|
|
||||||
void unregister_thread(std::thread::id id)
|
void unregister_thread(std::thread::id id)
|
||||||
{
|
{
|
||||||
|
if (!this_thread_registered)
|
||||||
|
return;
|
||||||
|
|
||||||
std::string name;
|
std::string name;
|
||||||
{
|
{
|
||||||
std::lock_guard lock{thread_names_mutex};
|
std::lock_guard lock{thread_names_mutex};
|
||||||
|
|
@ -194,6 +204,8 @@ namespace psemek::log
|
||||||
thread_names.erase(it);
|
thread_names.erase(it);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this_thread_registered = false;
|
||||||
|
|
||||||
if (id == std::this_thread::get_id())
|
if (id == std::this_thread::get_id())
|
||||||
put_message(level::info, "Thread \"" + name + "\" unregistered", name);
|
put_message(level::info, "Thread \"" + name + "\" unregistered", name);
|
||||||
else
|
else
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue