Skip to content

Commit

Permalink
Fix thread utilities for Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
ximinez committed Feb 7, 2025
1 parent 90948eb commit f9ea619
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 9 deletions.
12 changes: 10 additions & 2 deletions src/libxrpl/basics/ThreadUtilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
*/
//==============================================================================

#include <ripple/basics/ThreadUtilities.h>
#include <xrpl/basics/ThreadUtilities.h>

#include <cassert>
#include <stdexcept>

namespace ripple {
Expand Down Expand Up @@ -46,6 +48,7 @@ get_name()
void
set_name(std::string s)
{
assert(s.size() <= 15);
s.resize(15);
if (pthread_setname_np(s.data()) != 0)
throw std::runtime_error("this_thread::set_name failed\n");
Expand Down Expand Up @@ -79,6 +82,7 @@ get_name()
void
set_name(std::string s)
{
assert(s.size() <= 15);
s.resize(15);
if (pthread_setname_np(pthread_self(), s.data()) != 0)
throw std::runtime_error("this_thread::set_name failed\n");
Expand All @@ -90,11 +94,15 @@ set_name(std::string s)

#ifdef _WIN64

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

// clang-format off
#include <memory>
#include <processthreadsapi.h>
#include <windows.h>
#include <processthreadsapi.h>
// clang-format on

std::string
get_name(std::thread::native_handle_type t)
Expand Down
4 changes: 3 additions & 1 deletion src/xrpld/app/main/BasicApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ BasicApp::BasicApp(std::size_t numberOfThreads)
while (numberOfThreads--)
{
threads_.emplace_back([this, numberOfThreads]() {
// pretty unlikely we'll have 7 digit numbers of threads, but play
// it safe
ripple::this_thread::set_name(
"io svc #" + std::to_string(numberOfThreads));
"io svc #" + std::to_string(numberOfThreads).substr(0, 7));
this->io_service_.run();
});
}
Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/app/main/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,8 @@ run(int argc, char** argv)
{
using namespace std;

this_thread::set_name("main " + BuildInfo::getVersionString());
this_thread::set_name(
"main " + BuildInfo::getVersionString().substr(0, 10));

po::variables_map vm;

Expand Down
2 changes: 1 addition & 1 deletion src/xrpld/core/detail/Job.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ Job::queue_time() const
void
Job::doJob()
{
this_thread::set_name("doJob: " + mName);
this_thread::set_name("doJob: " + mName.substr(0, 8));
m_loadEvent->start();
m_loadEvent->setName(mName);

Expand Down
4 changes: 2 additions & 2 deletions src/xrpld/core/detail/Workers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ Workers::Worker::run()
for (;;)
{
// Put the name back in case the callback changed it
this_thread::set_name(threadName_);
this_thread::set_name(threadName_.substr(0, 15));

// Acquire a task or "internal task."
//
Expand Down Expand Up @@ -262,7 +262,7 @@ Workers::Worker::run()
}

// Set inactive thread name.
this_thread::set_name("(" + threadName_ + ")");
this_thread::set_name("(" + threadName_.substr(0, 13) + ")");

// [1] We will be here when the paused list is popped
//
Expand Down
2 changes: 1 addition & 1 deletion src/xrpld/nodestore/backend/RocksDBFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class RocksDBEnv : public rocksdb::EnvWrapper
std::size_t const id(++n);
std::stringstream ss;
ss << "rocksdb #" << id;
this_thread::set_name(ss.str());
this_thread::set_name(ss.str().substr(0, 15));

(*f)(a);
}
Expand Down
5 changes: 4 additions & 1 deletion src/xrpld/nodestore/detail/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,10 @@ Database::Database(
[this](int i) {
runningThreads_++;

this_thread::set_name("prefetch " + std::to_string(i));
// pretty unlikely we'll have 6 digit numbers of threads, but
// play it safe
this_thread::set_name(
"prefetch " + std::to_string(i).substr(0, 6));

decltype(read_) read;

Expand Down

0 comments on commit f9ea619

Please sign in to comment.