From 42b45094a28ba55d89eb8a150139d995b7d7b7cf Mon Sep 17 00:00:00 2001 From: David Garske Date: Fri, 29 Sep 2023 09:35:11 -0700 Subject: [PATCH] Possible patch for POSIX conditional wait issue. ZD 16769 --- src/mqtt_client.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/src/mqtt_client.c b/src/mqtt_client.c index b2ab4747e..d9fd0a57a 100644 --- a/src/mqtt_client.c +++ b/src/mqtt_client.c @@ -81,18 +81,18 @@ static int MqttClient_Publish_ReadPayload(MqttClient* client, } #elif defined(WOLFMQTT_POSIX_SEMAPHORES) /* Posix style semaphore */ - int wm_SemInit(wm_Sem *s){ + int wm_SemInit(wm_Sem *s) { s->lockCount = 0; pthread_mutex_init(&s->mutex, NULL); pthread_cond_init(&s->cond, NULL); return 0; } - int wm_SemFree(wm_Sem *s){ + int wm_SemFree(wm_Sem *s) { pthread_mutex_destroy(&s->mutex); pthread_cond_destroy(&s->cond); return 0; } - int wm_SemLock(wm_Sem *s){ + int wm_SemLock(wm_Sem *s) { pthread_mutex_lock(&s->mutex); while (s->lockCount > 0) pthread_cond_wait(&s->cond, &s->mutex); @@ -100,10 +100,12 @@ static int MqttClient_Publish_ReadPayload(MqttClient* client, pthread_mutex_unlock(&s->mutex); return 0; } - int wm_SemUnlock(wm_Sem *s){ + int wm_SemUnlock(wm_Sem *s) { pthread_mutex_lock(&s->mutex); - s->lockCount--; - pthread_cond_signal(&s->cond); + if (s->lockCount > 0) { + s->lockCount--; + pthread_cond_signal(&s->cond); + } pthread_mutex_unlock(&s->mutex); return 0; }