# HG changeset patch # User Ian Campbell # Date 1207658379 -3600 # Node ID 46073fa39b7bc6cb914a1bc4b4ec2397f9018374 # Parent 40310b53f5d0a456f12302aa07b592fd27736cb5 xenstore: re-enable watch support when threading is disabled. The caller can select on the watch fd in the normal way. This patch is only build tested. diff -r 40310b53f5d0 -r 46073fa39b7b tools/xenstore/xs.c --- a/tools/xenstore/xs.c Tue Apr 08 13:39:36 2008 +0100 +++ b/tools/xenstore/xs.c Tue Apr 08 13:39:39 2008 +0100 @@ -118,7 +118,13 @@ struct xs_handle { struct xs_handle { int fd; struct list_head reply_list; + struct list_head watch_list; + /* Clients can select() on this pipe to wait for a watch to fire. */ + int watch_pipe[2]; }; + +static void watch_mutex_lock(struct xs_handle *h) {} +static void watch_mutex_unlock(struct xs_handle *h) {} static void request_mutex_lock(struct xs_handle *h) {} static void request_mutex_unlock(struct xs_handle *h) {} @@ -130,7 +136,6 @@ static void reply_mutex_unlock(struct xs static int read_message(struct xs_handle *h); -#ifdef USE_PTHREAD int xs_fileno(struct xs_handle *h) { char c = 0; @@ -148,7 +153,6 @@ int xs_fileno(struct xs_handle *h) return h->watch_pipe[0]; } -#endif static int get_socket(const char *connect_to) { @@ -215,12 +219,12 @@ static struct xs_handle *get_handle(cons h->fd = fd; INIT_LIST_HEAD(&h->reply_list); - -#ifdef USE_PTHREAD + INIT_LIST_HEAD(&h->watch_list); + /* Watch pipe is allocated on demand in xs_fileno(). */ h->watch_pipe[0] = h->watch_pipe[1] = -1; - INIT_LIST_HEAD(&h->watch_list); +#ifdef USE_PTHREAD pthread_mutex_init(&h->watch_mutex, NULL); pthread_cond_init(&h->watch_condvar, NULL); @@ -269,21 +273,21 @@ void xs_daemon_close(struct xs_handle *h free(msg); } -#ifdef USE_PTHREAD list_for_each_entry_safe(msg, tmsg, &h->watch_list, list) { free(msg->body); free(msg); } +#ifdef USE_PTHREAD request_mutex_unlock(h); reply_mutex_unlock(h); watch_mutex_unlock(h); +#endif if (h->watch_pipe[0] != -1) { close(h->watch_pipe[0]); close(h->watch_pipe[1]); } -#endif close(h->fd); @@ -612,7 +616,6 @@ unwind: return false; } -#ifdef USE_PTHREAD /* Watch a node for changes (poll on fd to detect, or call read_watch()). * When the node (or any child) changes, fd will become readable. * Token is returned when watch is read, to allow matching. @@ -622,6 +625,7 @@ bool xs_watch(struct xs_handle *h, const { struct iovec iov[2]; +#ifdef USE_PTHREAD /* We dynamically create a reader thread on demand. */ request_mutex_lock(h); if (!h->read_thr_exists) { @@ -632,6 +636,7 @@ bool xs_watch(struct xs_handle *h, const h->read_thr_exists = 1; } request_mutex_unlock(h); +#endif iov[0].iov_base = (void *)path; iov[0].iov_len = strlen(path) + 1; @@ -656,7 +661,11 @@ char **xs_read_watch(struct xs_handle *h /* Wait on the condition variable for a watch to fire. */ while (list_empty(&h->watch_list)) +#ifdef USE_PTHREAD pthread_cond_wait(&h->watch_condvar, &h->watch_mutex); +#else + read_message(h); +#endif msg = list_top(&h->watch_list, struct xs_stored_msg, list); list_del(&msg->list); @@ -708,7 +717,6 @@ bool xs_unwatch(struct xs_handle *h, con return xs_bool(xs_talkv(h, XBT_NULL, XS_UNWATCH, iov, ARRAY_SIZE(iov), NULL)); } -#endif /* Start a transaction: changes by others will not be seen during this * transaction, and changes will not be visible to others until end. @@ -868,7 +876,6 @@ static int read_message(struct xs_handle body[msg->hdr.len] = '\0'; if (msg->hdr.type == XS_WATCH_EVENT) { -#ifdef USE_PTHREAD watch_mutex_lock(h); /* Kick users out of their select() loop. */ @@ -878,14 +885,12 @@ static int read_message(struct xs_handle continue; list_add_tail(&msg->list, &h->watch_list); + +#ifdef USE_PTHREAD pthread_cond_signal(&h->watch_condvar); +#endif watch_mutex_unlock(h); -#else - /* Cannot handle this event, but should never have been registered. */ - errno = -EIO; - goto error; -#endif } else { reply_mutex_lock(h);