Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible patch for POSIX conditional wait issue #356

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -81,29 +81,31 @@ 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);
s->lockCount++;
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the else case here return an error code?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't handle the wm_SemUnlock return code anywhere in wolfMQTT. However I will run some tests and see if this case ever occurs. I suspect it might and that's okay and should not be considered an error.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our test cases aren't triggering it, however I think with the complicated locks in the state machine for lockRecv with the right abort this might be able to occur. Ignoring it is safe "in my opinion".

s->lockCount--;
pthread_cond_signal(&s->cond);
}
pthread_mutex_unlock(&s->mutex);
return 0;
}
Expand Down
Loading