Skip to content

Commit d9ffaa6

Browse files
codablockrandom-zebra
authored andcommitted
Add helper to rename all threads of a ctpl::thread_pool
1 parent 9f9a3d7 commit d9ffaa6

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/util/threadnames.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515

1616
#include <util/threadnames.h>
1717

18+
#include "ctpl.h"
19+
#include "utiltime.h"
20+
#include "tinyformat.h"
21+
1822
#ifdef HAVE_SYS_PRCTL_H
1923
#include <sys/prctl.h> // For prctl, PR_SET_NAME, PR_GET_NAME
2024
#endif
@@ -64,3 +68,22 @@ void util::ThreadSetInternalName(std::string&& name)
6468
{
6569
SetInternalName(std::move(name));
6670
}
71+
72+
void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName)
73+
{
74+
auto cond = std::make_shared<std::condition_variable>();
75+
auto mutex = std::make_shared<std::mutex>();
76+
std::atomic<int> doneCnt(0);
77+
for (int i = 0; i < tp.size(); i++) {
78+
tp.push([baseName, i, cond, mutex, &doneCnt](int threadId) {
79+
util::ThreadRename(strprintf("%s-%d", baseName, i).c_str());
80+
doneCnt++;
81+
std::unique_lock<std::mutex> l(*mutex);
82+
cond->wait(l);
83+
});
84+
}
85+
while (doneCnt != tp.size()) {
86+
MilliSleep(10);
87+
}
88+
cond->notify_all();
89+
}

src/util/threadnames.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@ const std::string& ThreadGetInternalName();
2323

2424
} // namespace util
2525

26+
namespace ctpl {
27+
class thread_pool;
28+
}
29+
void RenameThreadPool(ctpl::thread_pool& tp, const char* baseName);
30+
2631
#endif // BITCOIN_UTIL_THREADNAMES_H

0 commit comments

Comments
 (0)