From 772a83ba1cae5d0c6378e5102dc9ec9f8b9d1a53 Mon Sep 17 00:00:00 2001 From: Ian Craggs Date: Mon, 26 Sep 2022 20:29:18 +0100 Subject: [PATCH] Add thread names if possible #1100 --- appveyor.yml | 1 + src/MQTTAsyncUtils.c | 2 ++ src/MQTTClient.c | 1 + src/Thread.c | 28 +++++++++++++++++++++++++++- src/Thread.h | 12 +++++++++++- 5 files changed, 42 insertions(+), 2 deletions(-) diff --git a/appveyor.yml b/appveyor.yml index 36d1425a..8e4b71be 100644 --- a/appveyor.yml +++ b/appveyor.yml @@ -23,6 +23,7 @@ environment: configuration: Debug install: + - cmd: ver - cmd: openssl version - cmd: C:\Python36\python --version - cmd: netsh advfirewall firewall add rule name="Python 3.6" dir=in action=allow program="C:\Python36\python.exe" enable=yes diff --git a/src/MQTTAsyncUtils.c b/src/MQTTAsyncUtils.c index 0142f9fd..d8675eb7 100644 --- a/src/MQTTAsyncUtils.c +++ b/src/MQTTAsyncUtils.c @@ -1774,6 +1774,7 @@ thread_return_type WINAPI MQTTAsync_sendThread(void* n) int timeout = 10; /* first time in we have a small timeout. Gets things started more quickly */ FUNC_ENTRY; + Thread_set_name("MQTTAsync_send"); MQTTAsync_lock_mutex(mqttasync_mutex); sendThread_state = RUNNING; sendThread_id = Thread_getid(); @@ -1993,6 +1994,7 @@ thread_return_type WINAPI MQTTAsync_receiveThread(void* n) long timeout = 10L; /* first time in we have a small timeout. Gets things started more quickly */ FUNC_ENTRY; + Thread_set_name("MQTTAsync_rcv"); MQTTAsync_lock_mutex(mqttasync_mutex); receiveThread_state = RUNNING; receiveThread_id = Thread_getid(); diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 7d1ebc9b..295505ec 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -801,6 +801,7 @@ static thread_return_type WINAPI MQTTClient_run(void* n) long timeout = 10L; /* first time in we have a small timeout. Gets things started more quickly */ FUNC_ENTRY; + Thread_set_name("MQTTClient_run"); Thread_lock_mutex(mqttclient_mutex); run_id = Thread_getid(); diff --git a/src/Thread.c b/src/Thread.c index 4bcca868..cecd6446 100644 --- a/src/Thread.c +++ b/src/Thread.c @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2021 IBM Corp. and Ian Craggs + * Copyright (c) 2009, 2022 IBM Corp. and Ian Craggs * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -80,6 +80,32 @@ void Thread_start(thread_fn fn, void* parameter) } +int Thread_set_name(const char* thread_name) +{ + int rc = 0; + FUNC_ENTRY; + +#if defined(_WIN32) || defined(_WIN64) +#if defined(_MSC_VER) && _MSC_VER >= 1920 + rc = (int)SetThreadDescription(GetCurrentThread(), (PCWSTR)thread_name); +#endif +#elif defined(OSX) + // pthread_setname_np __API_AVAILABLE(macos(10.6), ios(3.2)) +#if defined(__APPLE__) && __MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_6 + rc = pthread_setname_np(thread_name); +#endif +#else +#if defined(__GNUC__) && defined(__linux__) +#if __GLIBC__ >= 2 && __GLIBC_MINOR__ >= 12 + rc = pthread_setname_np(Thread_getid(), thread_name); +#endif +#endif +#endif + FUNC_EXIT_RC(rc); + return rc; +} + + /** * Create a new mutex * @param rc return code: 0 for success, negative otherwise diff --git a/src/Thread.h b/src/Thread.h index 3cd90d2f..23675a95 100644 --- a/src/Thread.h +++ b/src/Thread.h @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2020 IBM Corp. + * Copyright (c) 2009, 2022 IBM Corp. * * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v2.0 @@ -20,6 +20,15 @@ #if !defined(THREAD_H) #define THREAD_H +#if !defined(_WIN32) && !defined(_WIN64) +#if defined(__GNUC__) && defined(__linux__) +#if !defined(_GNU_SOURCE) +// for pthread_setname + #define _GNU_SOURCE +#endif +#endif +#endif + #include "MQTTExportDeclarations.h" #include "MQTTClient.h" @@ -60,6 +69,7 @@ #endif LIBMQTT_API void Thread_start(thread_fn, void*); +int Thread_set_name(const char* thread_name); LIBMQTT_API mutex_type Thread_create_mutex(int*); LIBMQTT_API int Thread_lock_mutex(mutex_type);