Add support for named threads (atm linux only)
authorsapier <Sapier at GMX dot net>
Sat, 5 Apr 2014 13:27:33 +0000 (15:27 +0200)
committersapier <Sapier at GMX dot net>
Wed, 9 Apr 2014 19:32:21 +0000 (21:32 +0200)
src/client.cpp
src/connection.cpp
src/emerge.cpp
src/httpfetch.cpp
src/porting.h
src/script/lua_api/l_async_events.cpp
src/server.cpp

index 5b3ebed66e0322a12ee5abe910a48ab1b73f19f6..56f55ef6ce6ec92c78ec7e395fab8a7d5fbe23f6 100644 (file)
@@ -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="<<time_of_day
+               infostream<<"Client: time_of_day="<<time_of_day
                                <<" time_speed="<<time_speed
                                <<" dr="<<dr<<std::endl;
        }
index 290e2cb21f58cce76b554ddf5aa6bb72fc96d6e1..c77ee7f4da2e98ce0d82d99032f9b9f5c247b865 100644 (file)
@@ -1227,6 +1227,8 @@ void * ConnectionSendThread::Thread()
        PROFILE(std::stringstream ThreadIdentifier);
        PROFILE(ThreadIdentifier << "ConnectionSend: [" << m_connection->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;
index 0e805c95a3c924403e5c0f2ef0a63da6540b8f45..c26fab8375006f3c066f4907d868dcdf83047e3b 100644 (file)
@@ -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)) {
index 2eca363d7bb9a34bb415a431cc2df00ddedd8d30..751a4471a811cab1faeb94ed9d98bcbac9f16b1c 100644 (file)
@@ -566,6 +566,8 @@ protected:
                log_register_thread("CurlFetchThread");
                DSTACK(__FUNCTION_NAME);
 
+               porting::setThreadName("CurlFetchThread");
+
                CurlHandlePool pool;
 
                m_multi = curl_multi_init();
index c03ae40aa4ea7be66ea9403fce2d29ab3213949a..5739b6876a4298058963ce96589165f6772d3ae9 100644 (file)
@@ -266,6 +266,26 @@ inline u32 getTime(TimePrecision prec)
        return 0;
 }
 
+#if (defined(linux) || defined(__linux))
+
+#include <sys/prctl.h>
+
+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
 
index 2425f22b253bc6ce83bd974bc07d006191421086..f5c27a235be023a5119f1d8b55c710f195618575 100644 (file)
@@ -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");
index ebb76b08723960038b612b9808843ec83c22e88a..0cd8630c334468ba339af95da6657bde04ea9da6 100644 (file)
@@ -99,6 +99,8 @@ void * ServerThread::Thread()
 
        ThreadStarted();
 
+       porting::setThreadName("ServerThread");
+
        while(!StopRequested())
        {
                try{