*/
#define cleanup_pop(run) ((void)0); pthread_cleanup_pop(run)
+#define read_thread_exists(h) (h->read_thr_exists)
+
static void *read_thread(void *arg);
#else /* !defined(USE_PTHREAD) */
#define condvar_wait(c,m,hnd) read_message(hnd)
#define cleanup_push(f, a) ((void)0)
#define cleanup_pop(run) ((void)0)
+#define read_thread_exists(h) (0)
#endif
{
struct xs_stored_msg *msg;
char *body;
+ int read_from_thread;
+
+ read_from_thread = read_thread_exists(h);
#ifdef USE_PTHREAD
/* Read from comms channel ourselves if there is no reader thread. */
- if (!h->read_thr_exists && (read_message(h) == -1))
+ if (!read_from_thread && (read_message(h) == -1))
return NULL;
#endif
mutex_lock(&h->reply_mutex);
- while (list_empty(&h->reply_list))
+ while (list_empty(&h->reply_list) && (!read_from_thread || read_thread_exists(h)))
condvar_wait(&h->reply_condvar, &h->reply_mutex, h);
+ if (read_from_thread && !read_thread_exists(h)) {
+ mutex_unlock(&h->reply_mutex);
+ errno = EINVAL;
+ return NULL;
+ }
+ assert(!list_empty(&h->reply_list));
msg = list_top(&h->reply_list, struct xs_stored_msg, list);
list_del(&msg->list);
assert(list_empty(&h->reply_list));
mutex_lock(&h->watch_mutex);
/* Wait on the condition variable for a watch to fire. */
- while (list_empty(&h->watch_list))
+ while (list_empty(&h->watch_list) && read_thread_exists(h))
condvar_wait(&h->watch_condvar, &h->watch_mutex, h);
+ if (!read_thread_exists(h)) {
+ mutex_unlock(&h->watch_mutex);
+ errno = EINVAL;
+ return NULL;
+ }
msg = list_top(&h->watch_list, struct xs_stored_msg, list);
list_del(&msg->list);
while (read_message(h) != -1)
continue;
+ /* Kick anyone waiting for a reply */
+ pthread_mutex_lock(&h->request_mutex);
+ h->read_thr_exists = 0;
+ pthread_mutex_unlock(&h->request_mutex);
+
+ pthread_mutex_lock(&h->reply_mutex);
+ pthread_cond_signal(&h->reply_condvar);
+ pthread_mutex_unlock(&h->reply_mutex);
+
+ pthread_mutex_lock(&h->watch_mutex);
+ pthread_cond_signal(&h->watch_condvar);
+ pthread_mutex_unlock(&h->watch_mutex);
+
return NULL;
}
#endif