Skip to content

Commit

Permalink
Fix: improve error handling for pthread_cond_timedwait #430
Browse files Browse the repository at this point in the history
  • Loading branch information
jcorporation committed Feb 20, 2021
1 parent c37c6c2 commit 68f4b7c
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions src/tiny_queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
https://github.com/jcorporation/mympd
*/

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <errno.h>
#include <assert.h>
#include <pthread.h>
#include <time.h>
#include <string.h>

#include "../dist/src/sds/sds.h"
#include "log.h"
Expand Down Expand Up @@ -83,7 +85,10 @@ unsigned tiny_queue_length(tiny_queue_t *queue, int timeout) {
}
if (timeout > 0 && queue->length == 0) {
struct timespec max_wait = {0, 0};
clock_gettime(CLOCK_REALTIME, &max_wait);
if (clock_gettime(CLOCK_REALTIME, &max_wait) == -1) {
LOG_ERROR("Error getting realtime: %s", strerror(errno));
assert(NULL);
}
//timeout in ms
if (max_wait.tv_nsec <= (999999999 - timeout)) {
max_wait.tv_nsec += timeout;
Expand All @@ -94,7 +99,8 @@ unsigned tiny_queue_length(tiny_queue_t *queue, int timeout) {
}
rc = pthread_cond_timedwait(&queue->wakeup, &queue->mutex, &max_wait);
if (rc != 0 && rc != ETIMEDOUT) {
LOG_ERROR("Error in pthread_cond_timedwait: %d", rc);
LOG_ERROR("Error in pthread_cond_timedwait: %s - %s", rc, strerror(errno));
LOG_ERROR("Max wait: %llu", (unsigned long long)max_wait.tv_nsec);
}
}
unsigned len = queue->length;
Expand All @@ -110,12 +116,15 @@ void *tiny_queue_shift(tiny_queue_t *queue, int timeout, long id) {
int rc = pthread_mutex_lock(&queue->mutex);
if (rc != 0) {
LOG_ERROR("Error in pthread_mutex_lock: %d", rc);
return 0;
assert(NULL);
}
if (queue->length == 0) {
if (timeout > 0) {
struct timespec max_wait = {0, 0};
clock_gettime(CLOCK_REALTIME, &max_wait);
if (clock_gettime(CLOCK_REALTIME, &max_wait) == -1) {
LOG_ERROR("Error getting realtime: %s", strerror(errno));
return NULL;
}
//timeout in ms
if (max_wait.tv_nsec <= (999999999 - timeout)) {
max_wait.tv_nsec += timeout;
Expand All @@ -127,8 +136,8 @@ void *tiny_queue_shift(tiny_queue_t *queue, int timeout, long id) {
rc = pthread_cond_timedwait(&queue->wakeup, &queue->mutex, &max_wait);
if (rc != 0) {
if (rc != ETIMEDOUT) {
LOG_ERROR("Error in pthread_cond_timedwait: %d", rc);
LOG_ERROR("nsec: %ld", max_wait.tv_nsec);
LOG_ERROR("Error in pthread_cond_timedwait: %s - %s", rc, strerror(errno));
LOG_ERROR("Max wait: %llu", (unsigned long long)max_wait.tv_nsec);
}
rc = pthread_mutex_unlock(&queue->mutex);
if (rc != 0) {
Expand Down

0 comments on commit 68f4b7c

Please sign in to comment.