Skip to content

Commit

Permalink
Revert "Revert ThreadName due to problems on Windows (4702)"
Browse files Browse the repository at this point in the history
This reverts commit ce570c1.
  • Loading branch information
ximinez committed Feb 27, 2025
1 parent 0a1ca06 commit f5cb05e
Show file tree
Hide file tree
Showing 17 changed files with 245 additions and 51 deletions.
50 changes: 50 additions & 0 deletions include/xrpl/basics/ThreadUtilities.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2022 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

#ifndef RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED
#define RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED

#include <string>
#include <thread>

namespace ripple {

std::string
get_name(std::thread::native_handle_type t);

template <class Thread>
inline auto
get_name(Thread& t) -> decltype(t.native_handle(), t.join(), std::string{})
{
return get_name(t.native_handle());
}

namespace this_thread {

std::string
get_name();

void
set_name(std::string s);

} // namespace this_thread

} // namespace ripple

#endif
45 changes: 22 additions & 23 deletions include/xrpl/beast/core/CurrentThreadName.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
//------------------------------------------------------------------------------
/*
This file is part of Beast: https://github.com/vinniefalco/Beast
Copyright 2013, Vinnie Falco <[email protected]>
Portions of this file are from JUCE.
Copyright (c) 2013 - Raw Material Software Ltd.
Please visit http://www.juce.com
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2022 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -21,31 +17,34 @@
*/
//==============================================================================

#ifndef BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED
#define BEAST_CORE_CURRENT_THREAD_NAME_H_INCLUDED
#ifndef RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED
#define RIPPLE_BASICS_THREADUTILITIES_H_INCLUDED

#include <string>
#include <string_view>
#include <thread>

namespace beast {
namespace ripple {

/** Changes the name of the caller thread.
Different OSes may place different length or content limits on this name.
*/
void
setCurrentThreadName(std::string_view newThreadName);
std::string
get_name(std::thread::native_handle_type t);

/** Returns the name of the caller thread.
template <class Thread>
inline auto
get_name(Thread& t) -> decltype(t.native_handle(), t.join(), std::string{})
{
return get_name(t.native_handle());
}

The name returned is the name as set by a call to setCurrentThreadName().
If the thread name is set by an external force, then that name change
will not be reported.
namespace this_thread {

If no name has ever been set, then the empty string is returned.
*/
std::string
getCurrentThreadName();
get_name();

void
set_name(std::string s);

} // namespace this_thread

} // namespace beast
} // namespace ripple

#endif
140 changes: 140 additions & 0 deletions src/libxrpl/basics/ThreadUtilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
//------------------------------------------------------------------------------
/*
This file is part of rippled: https://github.com/ripple/rippled
Copyright (c) 2022 Ripple Labs Inc.
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
//==============================================================================

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

namespace ripple {

#ifdef __APPLE__

#include <pthread.h>

std::string
get_name(std::thread::native_handle_type t)
{
char buffer[64];
if (pthread_getname_np(t, buffer, sizeof(buffer)) != 0)
throw std::runtime_error("get_name failed\n");
return buffer;
}

namespace this_thread {

std::string
get_name()
{
return ripple::get_name(pthread_self());
}

void
set_name(std::string s)
{
s.resize(15);
if (pthread_setname_np(s.data()) != 0)
throw std::runtime_error("this_thread::set_name failed\n");
}

} // namespace this_thread

#endif // __APPLE__

#ifdef __linux__

#include <pthread.h>

std::string
get_name(std::thread::native_handle_type t)
{
char buffer[64];
if (pthread_getname_np(t, buffer, sizeof(buffer)) != 0)
throw std::runtime_error("get_name failed\n");
return buffer;
}

namespace this_thread {

std::string
get_name()
{
return ripple::get_name(pthread_self());
}

void
set_name(std::string s)
{
s.resize(15);
if (pthread_setname_np(pthread_self(), s.data()) != 0)
throw std::runtime_error("this_thread::set_name failed\n");
}

} // namespace this_thread

#endif // __linux__

#ifdef _WIN64

#define WIN32_LEAN_AND_MEAN

#include <memory>
#include <processthreadsapi.h>
#include <windows.h>

std::string
get_name(std::thread::native_handle_type t)
{
wchar_t* unhandled_data{};
HRESULT r = GetThreadDescription(t, &unhandled_data);
std::unique_ptr<wchar_t, HLOCAL (*)(HLOCAL)> data{
unhandled_data, LocalFree};
if (FAILED(r))
throw std::runtime_error("get_name failed\n");
std::string s;
auto p = data.get();
while (*p)
s.push_back(static_cast<char>(*p++));
return s;
}

namespace this_thread {

std::string
get_name()
{
return ripple::get_name(GetCurrentThread());
}

void
set_name(std::string s)
{
assert(s.size() <= 15);
s.resize(15);
std::wstring ws;
for (auto c : s)
ws += c;
HRESULT r = SetThreadDescription(GetCurrentThread(), ws.data());
if (FAILED(r))
throw std::runtime_error("this_thread::set_name failed\n");
}

} // namespace this_thread

#endif // __WINDOWS__

} // namespace ripple
3 changes: 2 additions & 1 deletion src/libxrpl/resource/ResourceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <xrpl/basics/Log.h>
#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/basics/chrono.h>
#include <xrpl/beast/core/CurrentThreadName.h>
#include <xrpl/beast/net/IPAddressConversion.h>
Expand Down Expand Up @@ -148,7 +149,7 @@ class ManagerImp : public Manager
void
run()
{
beast::setCurrentThreadName("Resource::Manager");
this_thread::set_name("Resrc::Manager");
for (;;)
{
logic_.periodicActivity();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
*/
//==============================================================================

#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/beast/core/CurrentThreadName.h>
#include <xrpl/beast/unit_test.h>
#include <atomic>
Expand All @@ -25,7 +26,7 @@
namespace ripple {
namespace test {

class CurrentThreadName_test : public beast::unit_test::suite
class ThreadName_test : public beast::unit_test::suite
{
private:
static void
Expand All @@ -34,26 +35,19 @@ class CurrentThreadName_test : public beast::unit_test::suite
std::atomic<bool>* stop,
std::atomic<int>* state)
{
// Verify that upon creation a thread has no name.
auto const initialThreadName = beast::getCurrentThreadName();

// Set the new name.
beast::setCurrentThreadName(myName);
this_thread::set_name(myName);

// Indicate to caller that the name is set.
*state = 1;

// If there is an initial thread name then we failed.
if (!initialThreadName.empty())
return;

// Wait until all threads have their names.
while (!*stop)
;

// Make sure the thread name that we set before is still there
// (not overwritten by, for instance, another thread).
if (beast::getCurrentThreadName() == myName)
if (this_thread::get_name() == myName)
*state = 2;
}

Expand Down Expand Up @@ -86,7 +80,7 @@ class CurrentThreadName_test : public beast::unit_test::suite
}
};

BEAST_DEFINE_TESTSUITE(CurrentThreadName, core, beast);
BEAST_DEFINE_TESTSUITE(ThreadName, basics, ripple);

} // namespace test
} // namespace ripple
3 changes: 2 additions & 1 deletion src/test/overlay/short_read_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <test/jtx/envconfig.h>
#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/basics/make_SSLContext.h>
#include <xrpl/beast/core/CurrentThreadName.h>
#include <xrpl/beast/unit_test.h>
Expand Down Expand Up @@ -629,7 +630,7 @@ class short_read_test : public beast::unit_test::suite
short_read_test()
: work_(io_context_.get_executor())
, thread_(std::thread([this]() {
beast::setCurrentThreadName("io_context");
this_thread::set_name("io_context");
this->io_context_.run();
}))
, context_(make_SSLContext(""))
Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/app/ledger/detail/LedgerCleaner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <xrpld/app/ledger/LedgerCleaner.h>
#include <xrpld/app/ledger/LedgerMaster.h>
#include <xrpld/app/misc/LoadFeeTrack.h>
#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/beast/core/CurrentThreadName.h>
#include <xrpl/protocol/jss.h>

Expand Down Expand Up @@ -218,7 +219,7 @@ class LedgerCleanerImp : public LedgerCleaner
void
run()
{
beast::setCurrentThreadName("LedgerCleaner");
this_thread::set_name("LedgerCleaner");
JLOG(j_.debug()) << "Started";

while (true)
Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/app/main/BasicApp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <xrpld/app/main/BasicApp.h>
#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/beast/core/CurrentThreadName.h>

BasicApp::BasicApp(std::size_t numberOfThreads)
Expand All @@ -28,7 +29,7 @@ BasicApp::BasicApp(std::size_t numberOfThreads)
while (numberOfThreads--)
{
threads_.emplace_back([this, numberOfThreads]() {
beast::setCurrentThreadName(
ripple::this_thread::set_name(
"io svc #" + std::to_string(numberOfThreads));
this->io_service_.run();
});
Expand Down
3 changes: 2 additions & 1 deletion src/xrpld/app/main/GRPCServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
//==============================================================================

#include <xrpld/app/main/GRPCServer.h>
#include <xrpl/basics/ThreadUtilities.h>
#include <xrpl/beast/core/CurrentThreadName.h>
#include <xrpl/resource/Fees.h>

Expand Down Expand Up @@ -600,7 +601,7 @@ GRPCServer::start()
{
thread_ = std::thread([this]() {
// Start the event loop and begin handling requests
beast::setCurrentThreadName("rippled: grpc");
this_thread::set_name("rippled: grpc");
this->impl_.handleRpcs();
});
}
Expand Down
Loading

0 comments on commit f5cb05e

Please sign in to comment.