From 53d24713a31d59d9534c1c1a84a7ad46f44ee95f Mon Sep 17 00:00:00 2001 From: Kristian Høgsberg Date: Thu, 4 Oct 2012 16:54:22 -0400 Subject: Change filedescriptor API to be thread safe The update callback for the file descriptors was always a bit awkward and un-intuitive. The idea was that whenever the protocol code needed to write data to the fd it would call the 'update' function. This function would adjust the mainloop so that it polls for POLLOUT on the fd so we can eventually flush the data to the socket. The problem is that in multi-threaded applications, any thread can issue a request, which writes data to the output buffer and thus triggers the update callback. Thus, we'll be calling out with the display mutex held and may call from any thread. The solution is to eliminate the udpate callback and just require that the application or server flushes all connection buffers before blocking. This turns out to be a simpler API, although we now require clients to deal with EAGAIN and non-blocking writes. It also saves a few syscalls, since the socket will be writable most of the time and most writes will complete, so we avoid changing epoll to poll for POLLOUT, then write and then change it back for each write. --- tests/os-wrappers-test.c | 31 +++++-------------------------- 1 file changed, 5 insertions(+), 26 deletions(-) (limited to 'tests/os-wrappers-test.c') diff --git a/tests/os-wrappers-test.c b/tests/os-wrappers-test.c index b9be2b4..515fd81 100644 --- a/tests/os-wrappers-test.c +++ b/tests/os-wrappers-test.c @@ -211,33 +211,17 @@ struct marshal_data { int wrapped_calls; }; -static int -update_func(struct wl_connection *connection, uint32_t mask, void *data) -{ - uint32_t *m = data; - - *m = mask; - - return 0; -} - static void setup_marshal_data(struct marshal_data *data) { assert(socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, data->s) == 0); - data->read_connection = - wl_connection_create(data->s[0], - update_func, &data->read_mask); + data->read_connection = wl_connection_create(data->s[0]); assert(data->read_connection); - assert(data->read_mask == WL_CONNECTION_READABLE); - data->write_connection = - wl_connection_create(data->s[1], - update_func, &data->write_mask); + data->write_connection = wl_connection_create(data->s[1]); assert(data->write_connection); - assert(data->write_mask == WL_CONNECTION_READABLE); } static void @@ -260,14 +244,9 @@ marshal_demarshal(struct marshal_data *data, assert(closure); assert(wl_closure_send(closure, data->write_connection) == 0); wl_closure_destroy(closure); - assert(data->write_mask == - (WL_CONNECTION_WRITABLE | WL_CONNECTION_READABLE)); - assert(wl_connection_data(data->write_connection, - WL_CONNECTION_WRITABLE) == 0); - assert(data->write_mask == WL_CONNECTION_READABLE); - - assert(wl_connection_data(data->read_connection, - WL_CONNECTION_READABLE) == size); + assert(wl_connection_flush(data->write_connection) == size); + + assert(wl_connection_read(data->read_connection) == size); wl_map_init(&objects); object.id = msg[0]; -- cgit v1.2.3