From edcad09dee6daf119f3e29b0a63837500e7b8b85 Mon Sep 17 00:00:00 2001 From: sapier Date: Sat, 5 Apr 2014 15:27:33 +0200 Subject: [PATCH] Add support for named threads (atm linux only) --- src/client.cpp | 4 +++- src/connection.cpp | 4 ++++ src/emerge.cpp | 2 ++ src/httpfetch.cpp | 2 ++ src/porting.h | 20 ++++++++++++++++++++ src/script/lua_api/l_async_events.cpp | 3 +++ src/server.cpp | 2 ++ 7 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/client.cpp b/src/client.cpp index 5b3ebed6..56f55ef6 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -169,6 +169,8 @@ void * MeshUpdateThread::Thread() BEGIN_DEBUG_EXCEPTION_HANDLER + porting::setThreadName("MeshUpdateThread"); + while(!StopRequested()) { QueuedMeshUpdate *q = m_queue_in.pop(); @@ -1212,7 +1214,7 @@ void Client::ProcessData(u8 *data, u32 datasize, u16 sender_peer_id) m_time_of_day_set = true; u32 dr = m_env.getDayNightRatio(); - verbosestream<<"Client: time_of_day="<getDesc() << "]"); + porting::setThreadName("ConnectionSend"); + /* if stop is requested don't stop immediately but try to send all */ /* packets first */ while(!StopRequested() || packetsQueued()) { @@ -1955,6 +1957,8 @@ void * ConnectionReceiveThread::Thread() PROFILE(std::stringstream ThreadIdentifier); PROFILE(ThreadIdentifier << "ConnectionReceive: [" << m_connection->getDesc() << "]"); + porting::setThreadName("ConnectionReceive"); + #ifdef DEBUG_CONNECTION_KBPS u32 curtime = porting::getTimeMs(); u32 lasttime = curtime; diff --git a/src/emerge.cpp b/src/emerge.cpp index 0e805c95..c26fab83 100644 --- a/src/emerge.cpp +++ b/src/emerge.cpp @@ -472,6 +472,8 @@ void *EmergeThread::Thread() { mapgen = emerge->mapgen[id]; enable_mapgen_debug_info = emerge->mapgen_debug_info; + porting::setThreadName("EmergeThread"); + while (!StopRequested()) try { if (!popBlockEmerge(&p, &flags)) { diff --git a/src/httpfetch.cpp b/src/httpfetch.cpp index 2eca363d..751a4471 100644 --- a/src/httpfetch.cpp +++ b/src/httpfetch.cpp @@ -566,6 +566,8 @@ protected: log_register_thread("CurlFetchThread"); DSTACK(__FUNCTION_NAME); + porting::setThreadName("CurlFetchThread"); + CurlHandlePool pool; m_multi = curl_multi_init(); diff --git a/src/porting.h b/src/porting.h index c03ae40a..5739b687 100644 --- a/src/porting.h +++ b/src/porting.h @@ -266,6 +266,26 @@ inline u32 getTime(TimePrecision prec) return 0; } +#if (defined(linux) || defined(__linux)) + +#include + +inline void setThreadName(const char* name) { + prctl(PR_SET_NAME,name); +} +#elif defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) +/* BSD doesn't seem to support thread names. If you know about a way + * to add this feature please create a pull request. + * "setproctitle" doesn't work for threadnames. + */ +#define setThreadName(a) +#elif defined(_WIN32) +// threadnames are not supported on windows +#define setThreadName(a) +#else +#warning "Unknown platform for setThreadName support, you wont have threadname support." +#define setThreadName(a) +#endif } // namespace porting diff --git a/src/script/lua_api/l_async_events.cpp b/src/script/lua_api/l_async_events.cpp index 2425f22b..f5c27a23 100644 --- a/src/script/lua_api/l_async_events.cpp +++ b/src/script/lua_api/l_async_events.cpp @@ -262,6 +262,9 @@ void* AsyncWorkerThread::worker_thread_main() { snprintf(number,sizeof(number),"%d",m_threadnum); log_register_thread(std::string("AsyncWorkerThread_") + number); + porting::setThreadName( + std::string(std::string("AsyncWorkTh_") + number).c_str()); + /** prepare job lua environment **/ lua_newtable(m_LuaStack); lua_setglobal(m_LuaStack, "engine"); diff --git a/src/server.cpp b/src/server.cpp index ebb76b08..0cd8630c 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -99,6 +99,8 @@ void * ServerThread::Thread() ThreadStarted(); + porting::setThreadName("ServerThread"); + while(!StopRequested()) { try{ -- 2.30.2