Skip to content

Commit

Permalink
Add support for thread names
Browse files Browse the repository at this point in the history
  • Loading branch information
paullouisageneau committed May 14, 2023
1 parent d3938a7 commit 80d1ae9
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/impl/dtlstransport.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include <functional>
#include <memory>
#include <mutex>
#include <thread>

namespace rtc::impl {

Expand Down
5 changes: 3 additions & 2 deletions src/impl/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
*/

#include "init.hpp"
#include "internals.hpp"

#include "certificate.hpp"
#include "dtlstransport.hpp"
#include "icetransport.hpp"
#include "internals.hpp"
#include "pollservice.hpp"
#include "sctptransport.hpp"
#include "threadpool.hpp"
#include "tls.hpp"
#include "utils.hpp"

#if RTC_ENABLE_WEBSOCKET
#include "tlstransport.hpp"
Expand Down Expand Up @@ -43,6 +43,7 @@ struct Init::TokenPayload {
~TokenPayload() {
std::thread t(
[](std::promise<void> promise) {
utils::this_thread::set_name("RTC cleanup");
try {
Init::Instance().doCleanup();
promise.set_value();
Expand Down
3 changes: 2 additions & 1 deletion src/impl/peerconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

#include "peerconnection.hpp"
#include "certificate.hpp"
#include "common.hpp"
#include "dtlstransport.hpp"
#include "icetransport.hpp"
#include "internals.hpp"
Expand All @@ -18,6 +17,7 @@
#include "processor.hpp"
#include "rtp.hpp"
#include "sctptransport.hpp"
#include "utils.hpp"

#if RTC_ENABLE_MEDIA
#include "dtlssrtptransport.hpp"
Expand Down Expand Up @@ -1061,6 +1061,7 @@ void PeerConnection::processRemoteCandidate(Candidate candidate) {
if ((iceTransport = std::atomic_load(&mIceTransport))) {
weak_ptr<IceTransport> weakIceTransport{iceTransport};
std::thread t([weakIceTransport, candidate = std::move(candidate)]() mutable {
utils::this_thread::set_name("RTC resolver");
if (candidate.resolve(Candidate::ResolveMode::Lookup))
if (auto iceTransport = weakIceTransport.lock())
iceTransport->addRemoteCandidate(std::move(candidate));
Expand Down
6 changes: 4 additions & 2 deletions src/impl/pollservice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#include "pollservice.hpp"
#include "internals.hpp"
#include "utils.hpp"

#if RTC_ENABLE_WEBSOCKET

Expand Down Expand Up @@ -158,10 +159,11 @@ void PollService::process(std::vector<struct pollfd> &pfds) {
}

void PollService::runLoop() {
utils::this_thread::set_name("RTC poll");
PLOG_DEBUG << "Poll service started";

try {
PLOG_DEBUG << "Poll service started";
assert(mSocks);

std::vector<struct pollfd> pfds;
optional<clock::time_point> next;
while (!mStopped) {
Expand Down
2 changes: 2 additions & 0 deletions src/impl/threadpool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include "threadpool.hpp"
#include "utils.hpp"

namespace rtc::impl {

Expand Down Expand Up @@ -54,6 +55,7 @@ void ThreadPool::clear() {
}

void ThreadPool::run() {
utils::this_thread::set_name("RTC worker");
++mBusyWorkers;
scope_guard guard([&]() { --mBusyWorkers; });
while (runOne()) {
Expand Down
31 changes: 31 additions & 0 deletions src/impl/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@
#include <sstream>
#include <thread>

#if defined(__linux__)
#include <sys/prctl.h> // for prctl(PR_SET_NAME)
#endif
#if defined(__FreeBSD__)
#include <pthread_np.h> // for pthread_set_name_np
#endif

namespace rtc::impl::utils {

using std::to_integer;
Expand Down Expand Up @@ -179,4 +186,28 @@ std::multimap<string, string> parseHttpHeaders(const std::list<string> &lines) {
return headers;
}

namespace {

void thread_set_name_self(const char *name) {
#if defined(_WIN32)
(void)name;
#elif defined(__linux__)
prctl(PR_SET_NAME, name);
#elif defined(__APPLE__)
pthread_setname_np(name);
#elif defined(__FreeBSD__)
pthread_set_name_np(pthread_self(), name);
#else
(void)name;
#endif
}

} // namespace

namespace this_thread {
void set_name(const string &name) {
thread_set_name_self(name.c_str());
}
}

} // namespace rtc::impl::utils
4 changes: 4 additions & 0 deletions src/impl/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ template <typename Generator = std::mt19937> auto random_bytes_engine() {
return random_engine<char_independent_bits_engine, uint8_t>();
}

namespace this_thread {
void set_name(const string &name);
}

} // namespace rtc::impl::utils

#endif
6 changes: 4 additions & 2 deletions src/impl/websocketserver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "common.hpp"
#include "internals.hpp"
#include "threadpool.hpp"
#include "utils.hpp"

namespace rtc::impl {

Expand Down Expand Up @@ -41,8 +42,8 @@ WebSocketServer::WebSocketServer(Configuration config_)
}
}

const char* bindAddress = nullptr;
if(config.bindAddress){
const char *bindAddress = nullptr;
if (config.bindAddress) {
bindAddress = config.bindAddress->c_str();
}
// Create TCP server
Expand All @@ -67,6 +68,7 @@ void WebSocketServer::stop() {
}

void WebSocketServer::runLoop() {
utils::this_thread::set_name("RTC server");
PLOG_INFO << "Starting WebSocketServer";

try {
Expand Down

0 comments on commit 80d1ae9

Please sign in to comment.