util: avoid using thread_local variable that has a destructor

Store the thread name in a `thread_local` variable of type `char[]`
instead of `std::string`. This avoids calling the destructor when
the thread exits. This is a workaround for
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=278701

For type-safety, return `std::string` from
`util::ThreadGetInternalName()` instead of `char[]`.

As a side effect of this change, we no longer store a reference
to a `thread_local` variable in `CLockLocation`. This was
dangerous because if the thread quits while the reference still
exists (in the global variable `lock_data`, see inside `GetLockData()`)
then the reference will become dangling.
This commit is contained in:
Vasil Dimov
2024-05-13 16:57:03 +02:00
parent b94061902e
commit d35ba1b3f1
4 changed files with 27 additions and 21 deletions

View File

@@ -37,11 +37,11 @@ struct CLockLocation {
const char* pszFile,
int nLine,
bool fTryIn,
const std::string& thread_name)
std::string&& thread_name)
: fTry(fTryIn),
mutexName(pszName),
sourceFile(pszFile),
m_thread_name(thread_name),
m_thread_name(std::move(thread_name)),
sourceLine(nLine) {}
std::string ToString() const
@@ -60,7 +60,7 @@ private:
bool fTry;
std::string mutexName;
std::string sourceFile;
const std::string& m_thread_name;
const std::string m_thread_name;
int sourceLine;
};