Replaced platform-specific mutex creation/destruction with calls to code in Thread.c

This commit is contained in:
fpagliughi 2025-01-11 15:49:32 -05:00
parent f6bf57e39e
commit 6a71f1c216
4 changed files with 44 additions and 58 deletions

View File

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

View File

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

View File

@ -247,6 +247,8 @@ thread_id_type Paho_thread_getid(void)
#endif
}
#if !defined(_WIN32)
/**
* Create a new event
* @return the event struct

View File

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