client: Add wl_display_dispatch_pending() for dispatching without reading

If the main thread ends up dispatching a non-main queue, and not in
a wl_display_dispatch() callback, we may queue up main queue events and read
all data from the socket fd.  When we get back to the main loop, the
socket fd is no longer readable and nothing will trigger dispatching of
the queued up events.

The new function wl_display_dispatch_pending() will dispatch any pending
events, but not attempt to read from the socket.  Clients that integrate
the wayland socket fd into a main loop should call
wl_display_dispatch_pending() and then wl_display_flush()
before going back to blocking in poll(2) or similar mechanism.
This commit is contained in:
Kristian Høgsberg 2012-10-11 17:15:08 -04:00
parent 1849534736
commit 78cfa96768
2 changed files with 22 additions and 6 deletions

View File

@ -552,9 +552,9 @@ dispatch_event(struct wl_display *display, struct wl_event_queue *queue)
}
WL_EXPORT int
wl_display_dispatch_queue(struct wl_display *display,
struct wl_event_queue *queue)
static int
dispatch_queue(struct wl_display *display,
struct wl_event_queue *queue, int block)
{
int len, size;
@ -563,7 +563,7 @@ wl_display_dispatch_queue(struct wl_display *display,
/* FIXME: Handle flush errors, EAGAIN... */
wl_connection_flush(display->connection);
if (wl_list_empty(&queue->event_list) &&
if (block && wl_list_empty(&queue->event_list) &&
pthread_equal(display->display_thread, pthread_self())) {
len = wl_connection_read(display->connection);
if (len == -1) {
@ -576,7 +576,7 @@ wl_display_dispatch_queue(struct wl_display *display,
break;
len -= size;
}
} else if (wl_list_empty(&queue->event_list)) {
} else if (block && wl_list_empty(&queue->event_list)) {
pthread_cond_wait(&queue->cond, &display->mutex);
}
@ -588,12 +588,27 @@ wl_display_dispatch_queue(struct wl_display *display,
return 0;
}
WL_EXPORT int
wl_display_dispatch_queue(struct wl_display *display,
struct wl_event_queue *queue)
{
return dispatch_queue(display, queue, 1);
}
WL_EXPORT int
wl_display_dispatch(struct wl_display *display)
{
display->display_thread = pthread_self();
return wl_display_dispatch_queue(display, &display->queue);
return dispatch_queue(display, &display->queue, 1);
}
WL_EXPORT int
wl_display_dispatch_pending(struct wl_display *display)
{
display->display_thread = pthread_self();
return dispatch_queue(display, &display->queue, 0);
}
WL_EXPORT int

View File

@ -60,6 +60,7 @@ int wl_display_get_fd(struct wl_display *display);
int wl_display_dispatch(struct wl_display *display);
int wl_display_dispatch_queue(struct wl_display *display,
struct wl_event_queue *queue);
int wl_display_dispatch_pending(struct wl_display *display);
int wl_display_flush(struct wl_display *display);
void wl_display_roundtrip(struct wl_display *display);