-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Revert "Revert ThreadName due to problems on Windows (4702)"
This reverts commit ce570c1.
- Loading branch information
Showing
17 changed files
with
245 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.