- ret = clock_gettime(CLOCK_REALTIME, &timeout);
- if (ret < 0) {
- PERROR("clock_gettime spawn consumer");
- /* Infinite wait for the kconsumerd thread to be ready */
- ret = sem_wait(&consumer_data->sem);
- } else {
- /* Normal timeout if the gettime was successful */
- timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
- ret = sem_timedwait(&consumer_data->sem, &timeout);
+ clock_ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
+ /*
+ * Set the timeout for the condition timed wait even if the clock gettime
+ * call fails since we might loop on that call and we want to avoid to
+ * increment the timeout too many times.
+ */
+ timeout.tv_sec += DEFAULT_SEM_WAIT_TIMEOUT;
+
+ /*
+ * The following loop COULD be skipped in some conditions so this is why we
+ * set ret to 0 in order to make sure at least one round of the loop is
+ * done.
+ */
+ ret = 0;
+
+ /*
+ * Loop until the condition is reached or when a timeout is reached. Note
+ * that the pthread_cond_timedwait(P) man page specifies that EINTR can NOT
+ * be returned but the pthread_cond(3), from the glibc-doc, says that it is
+ * possible. This loop does not take any chances and works with both of
+ * them.
+ */
+ while (!consumer_data->consumer_thread_is_ready && ret != ETIMEDOUT) {
+ if (clock_ret < 0) {
+ PERROR("clock_gettime spawn consumer");
+ /* Infinite wait for the consumerd thread to be ready */
+ ret = pthread_cond_wait(&consumer_data->cond,
+ &consumer_data->cond_mutex);
+ } else {
+ ret = pthread_cond_timedwait(&consumer_data->cond,
+ &consumer_data->cond_mutex, &timeout);
+ }