From 6a71f1c2162e58664613268acb4de06cf66f01af Mon Sep 17 00:00:00 2001 From: fpagliughi Date: Sat, 11 Jan 2025 15:49:32 -0500 Subject: [PATCH] Replaced platform-specific mutex creation/destruction with calls to code in Thread.c --- src/MQTTAsync.c | 45 ++++++++++++++++++++------------------------- src/MQTTClient.c | 44 ++++++++++++++++++++++---------------------- src/Thread.c | 2 ++ test/thread.c | 11 ----------- 4 files changed, 44 insertions(+), 58 deletions(-) diff --git a/src/MQTTAsync.c b/src/MQTTAsync.c index 44883af7..d3445820 100644 --- a/src/MQTTAsync.c +++ b/src/MQTTAsync.c @@ -142,52 +142,47 @@ int MQTTAsync_init(void) if (mqttasync_mutex == NULL) { - if ((mqttasync_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + mqttasync_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("mqttasync_mutex error %d\n", rc); goto exit; } - if ((mqttcommand_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + mqttcommand_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("mqttcommand_mutex error %d\n", rc); goto exit; } - if ((send_evt = CreateEvent( - NULL, /* default security attributes */ - FALSE, /* manual-reset event? */ - FALSE, /* initial state is nonsignaled */ - NULL /* object name */ - )) == NULL) + send_evt = Thread_create_evt(&rc); + if (rc != 0) { - rc = GetLastError(); printf("send_evt error %d\n", rc); goto exit; } #if !defined(NO_HEAP_TRACKING) - if ((stack_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + stack_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("stack_mutex error %d\n", rc); goto exit; } - if ((heap_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + heap_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("heap_mutex error %d\n", rc); goto exit; } #endif - if ((log_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + log_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("log_mutex error %d\n", rc); goto exit; } - if ((socket_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + socket_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("socket_mutex error %d\n", rc); goto exit; } @@ -203,19 +198,19 @@ exit: void MQTTAsync_cleanup(void) { if (send_evt) - CloseHandle(send_evt); + Thread_destroy_evt(send_evt); #if !defined(NO_HEAP_TRACKING) if (stack_mutex) - CloseHandle(stack_mutex); + Paho_thread_destroy_mutex(stack_mutex); if (heap_mutex) - CloseHandle(heap_mutex); + Paho_thread_destroy_mutex(heap_mutex); #endif if (log_mutex) - CloseHandle(log_mutex); + Paho_thread_destroy_mutex(log_mutex); if (socket_mutex) - CloseHandle(socket_mutex); + Paho_thread_destroy_mutex(socket_mutex); if (mqttasync_mutex) - CloseHandle(mqttasync_mutex); + Paho_thread_destroy_mutex(mqttasync_mutex); } #if defined(PAHO_MQTT_STATIC) diff --git a/src/MQTTClient.c b/src/MQTTClient.c index 3d9bec56..34196718 100644 --- a/src/MQTTClient.c +++ b/src/MQTTClient.c @@ -128,51 +128,51 @@ extern mutex_type log_mutex; int MQTTClient_init(void) { - DWORD rc = 0; + int rc = 0; if (mqttclient_mutex == NULL) { - if ((mqttclient_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + mqttclient_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("mqttclient_mutex error %d\n", rc); goto exit; } - if ((subscribe_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + subscribe_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("subscribe_mutex error %d\n", rc); goto exit; } - if ((connect_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + connect_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("connect_mutex error %d\n", rc); goto exit; } #if !defined(NO_HEAP_TRACKING) - if ((stack_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + stack_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("stack_mutex error %d\n", rc); goto exit; } - if ((heap_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + heap_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("heap_mutex error %d\n", rc); goto exit; } #endif - if ((log_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + log_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("log_mutex error %d\n", rc); goto exit; } - if ((socket_mutex = CreateMutex(NULL, 0, NULL)) == NULL) + socket_mutex = Paho_thread_create_mutex(&rc); + if (rc != 0) { - rc = GetLastError(); printf("socket_mutex error %d\n", rc); goto exit; } @@ -184,21 +184,21 @@ exit: void MQTTClient_cleanup(void) { if (connect_mutex) - CloseHandle(connect_mutex); + Paho_thread_destroy_mutex(connect_mutex); if (subscribe_mutex) - CloseHandle(subscribe_mutex); + Paho_thread_destroy_mutex(subscribe_mutex); #if !defined(NO_HEAP_TRACKING) if (stack_mutex) - CloseHandle(stack_mutex); + Paho_thread_destroy_mutex(stack_mutex); if (heap_mutex) - CloseHandle(heap_mutex); + Paho_thread_destroy_mutex(heap_mutex); #endif if (log_mutex) - CloseHandle(log_mutex); + Paho_thread_destroy_mutex(log_mutex); if (socket_mutex) - CloseHandle(socket_mutex); + Paho_thread_destroy_mutex(socket_mutex); if (mqttclient_mutex) - CloseHandle(mqttclient_mutex); + Paho_thread_destroy_mutex(mqttclient_mutex); } #if defined(PAHO_MQTT_STATIC) diff --git a/src/Thread.c b/src/Thread.c index 11d284f2..d515177e 100644 --- a/src/Thread.c +++ b/src/Thread.c @@ -247,6 +247,8 @@ thread_id_type Paho_thread_getid(void) #endif } +#if !defined(_WIN32) + /** * Create a new event * @return the event struct diff --git a/test/thread.c b/test/thread.c index 835aa55d..38ac493a 100644 --- a/test/thread.c +++ b/test/thread.c @@ -272,11 +272,7 @@ int test_evt(struct Options options) MyLog(LOGA_DEBUG, "Check timeout"); start = start_clock(); -<<<<<<< HEAD - rc = Thread_wait_cond(cond, 2000); -======= rc = Thread_wait_evt(evt, 2000); ->>>>>>> 1ed408e (Started merging semaphores and condition variables into a single 'event' type.) duration = elapsed(start); assert("rc ETIMEDOUT from lock mutex", rc == ETIMEDOUT, "rc was %d", rc); MyLog(LOGA_INFO, "Lock duration was %ld", duration); @@ -400,14 +396,7 @@ int main(int argc, char** argv) int (*tests[])(struct Options) = { NULL, test_mutex, -<<<<<<< HEAD - test_sem, -#if !defined(_WIN32) - test_cond -#endif -======= test_evt ->>>>>>> 1ed408e (Started merging semaphores and condition variables into a single 'event' type.) }; /* indexed starting from 1 */ int i;