Add thread names if possible #1100

This commit is contained in:
Ian Craggs 2022-09-26 20:29:18 +01:00
parent 14bc391398
commit 772a83ba1c
5 changed files with 42 additions and 2 deletions

View File

@ -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

View File

@ -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();

View File

@ -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();

View File

@ -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

View File

@ -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);