mirror of
https://github.com/XRPLF/rippled.git
synced 2025-12-06 17:27:55 +00:00
Introduce replacement for getting and setting thread name: (#4312)
* In namespace ripple, introduces get_name function that takes a
std:🧵:native_handle_type and returns a std::string.
* In namespace ripple, introduces get_name function that takes a
std::thread or std::jthread and returns a std::string.
* In namespace ripple::this_thread, introduces get_name function
that takes no parameters and returns the name of the current
thread as a std::string.
* In namespace ripple::this_thread, introduces set_name function
that takes a std::string_view and sets the name of the current
thread.
* Intended to replace the beast utilities setCurrentThreadName
and getCurrentThreadName.
This commit is contained in:
@@ -17,15 +17,13 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/beast/core/CurrentThreadName.h>
|
||||
#include <ripple/basics/ThreadUtilities.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
|
||||
namespace ripple {
|
||||
namespace test {
|
||||
|
||||
class CurrentThreadName_test : public beast::unit_test::suite
|
||||
class ThreadName_test : public beast::unit_test::suite
|
||||
{
|
||||
private:
|
||||
static void
|
||||
@@ -34,26 +32,19 @@ private:
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -86,7 +77,7 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(CurrentThreadName, core, beast);
|
||||
BEAST_DEFINE_TESTSUITE(ThreadName, basics, ripple);
|
||||
|
||||
} // namespace test
|
||||
} // namespace ripple
|
||||
@@ -17,8 +17,8 @@
|
||||
*/
|
||||
//==============================================================================
|
||||
|
||||
#include <ripple/basics/ThreadUtilities.h>
|
||||
#include <ripple/basics/make_SSLContext.h>
|
||||
#include <ripple/beast/core/CurrentThreadName.h>
|
||||
#include <ripple/beast/unit_test.h>
|
||||
#include <test/jtx/envconfig.h>
|
||||
|
||||
@@ -630,7 +630,7 @@ public:
|
||||
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(""))
|
||||
|
||||
Reference in New Issue
Block a user