From 8cd75c4d6b229bb4e3de9264466731e3a32e0133 Mon Sep 17 00:00:00 2001 From: Richard Braun Date: Tue, 26 Feb 2013 22:24:47 +0100 Subject: Add io_select_timeout to the io interface This change fixes a problem that can occur with non-blocking (and also blocking for very short times) select/poll calls. The problem occurs because the timeout is implemented at the client side. For a non-blocking call, this means that (depending on the code path taken in the C library) the client could get a timeout without a full RPC round-trip to the server. Moving the implementation of the timeout to the servers guarantees a full round-trip, and correct results for non-blocking calls. The modifications in this change depend on the availability of the recently added pthread_hurd_cond_timedwait_np function in libpthread. * boot/boot.c (io_select_common): New static function. (S_io_select): Use io_select_common. (S_io_select_timeout): New function which makes use of io_select_common. * console-client/kbd-repeat.c (repeater_select): Add a timeout parameter. * console-client/pc-mouse.c (repeater_select): Likewise. * console-client/trans.c (io_select_common): New static function. (netfs_S_io_select): Use io_select_common. (netfs_S_io_select_timeout): New function which makes use of io_select_common. * console-client/trans.h (struct consnode): Add a timeout parameter. * hurd/io.defs (io_select_timeout): New MIG routine. * hurd/io_reply.defs (io_select_timeout_reply): New MIG simpleroutine. * hurd/io_request.defs (io_select_timeout_request): Likewise. * libdiskfs/io-select.c (diskfs_S_io_select_timeout): New function. * libnetfs/io-select.c (netfs_S_io_select_timeout): Likewise. * libpipe/pipe.c (pipe_pair_select): Add a timeout parameter. * libpipe/pipe.h (pipe_select_readable): Likewise. (pipe_select_writable): Likewise. (pipe_pair_select): Likewise. * libpipe/pq.h: Include . * libtrivfs/io-select.c (trivfs_S_io_select_timeout): New function. * pfinet/glue-include/linux/sched.h: Include . (interruptible_sleep_on): Function removed, replaced with ... (interruptible_sleep_on_timeout): New function. (schedule): Update to use interruptible_sleep_on_timeout. (schedule_timeout): Likewise. * pfinet/io-ops.c (io_select_common): New static function. (S_io_select): Use io_select_common. (S_io_select_timeout): New function which makes use of io_select_common. * pfinet/tunnel.c (io_select_common): New static function. (trivfs_S_io_select): Use io_select_common. (trivfs_S_io_select_timeout): New function which makes use of io_select_common. * pflocal/connq.c (connq_listen): Replace noblock with a timeout parameter. * pflocal/connq.h: Include . (connq_listen): Update declaration to replace noblock with a timeout parameter. * pflocal/io.c (io_select_common): New static function. (S_io_select): Use io_select_common. (S_io_select_timeout): New function which makes use of io_select_common. * pflocal/socket.c (S_socket_accept): Update call to connq_listen to match new declaration. * storeio/io.c (trivfs_S_io_select_timeout): New function. * term/ptyio.c (pty_io_select): Add a timeout parameter. * term/term.h (pty_io_select): Likewise. * term/users.c (io_select_common): New static function. (trivfs_S_io_select): Use io_select_common. (trivfs_S_io_select_timeout): New function which makes use of io_select_common. * trans/fifo.c (io_select_common): New static function. (trivfs_S_io_select): Use io_select_common. (trivfs_S_io_select_timeout): New function which makes use of io_select_common. * trans/firmlink.c (trivfs_S_io_select_timeout): New function. * trans/new-fifo.c (io_select_common): New static function. (trivfs_S_io_select): Use io_select_common. (trivfs_S_io_select_timeout): New function which makes use of io_select_common. * trans/null.c (trivfs_S_io_select_timeout): New function. * trans/streamio.c (io_select_common): New static function. (trivfs_S_io_select): Use io_select_common. (trivfs_S_io_select_timeout): New function which makes use of io_select_common. --- pflocal/connq.c | 17 +++++++++++------ pflocal/connq.h | 9 ++++++--- pflocal/io.c | 39 ++++++++++++++++++++++++++++++++------- pflocal/socket.c | 4 +++- 4 files changed, 52 insertions(+), 17 deletions(-) (limited to 'pflocal') diff --git a/pflocal/connq.c b/pflocal/connq.c index ab2b3a55..d88711e3 100644 --- a/pflocal/connq.c +++ b/pflocal/connq.c @@ -141,16 +141,19 @@ connq_destroy (struct connq *cq) /* ---------------------------------------------------------------- */ /* Return a connection request on CQ. If SOCK is NULL, the request is - left in the queue. If NOBLOCK is true, EWOULDBLOCK is returned - when there are no immediate connections available. */ + left in the queue. If TIMEOUT denotes a value of 0, EWOULDBLOCK is + returned when there are no immediate connections available. + Otherwise this value is used to limit the wait duration. If TIMEOUT + is NULL, the wait duration isn't bounded. */ error_t -connq_listen (struct connq *cq, int noblock, struct sock **sock) +connq_listen (struct connq *cq, struct timespec *tsp, struct sock **sock) { error_t err = 0; pthread_mutex_lock (&cq->lock); - if (noblock && cq->count == 0 && cq->num_connectors == 0) + if (tsp && tsp->tv_sec == 0 && tsp->tv_nsec == 0 && cq->count == 0 + && cq->num_connectors == 0) { pthread_mutex_unlock (&cq->lock); return EWOULDBLOCK; @@ -176,12 +179,14 @@ connq_listen (struct connq *cq, int noblock, struct sock **sock) pthread_cond_signal (&cq->connectors); do - if (pthread_hurd_cond_wait_np (&cq->listeners, &cq->lock)) + { + err = pthread_hurd_cond_timedwait_np (&cq->listeners, &cq->lock, tsp); + if (err) { cq->num_listeners--; - err = EINTR; goto out; } + } while (cq->count == 0); } diff --git a/pflocal/connq.h b/pflocal/connq.h index 9278d007..71583800 100644 --- a/pflocal/connq.h +++ b/pflocal/connq.h @@ -36,9 +36,12 @@ error_t connq_create (struct connq **cq); void connq_destroy (struct connq *cq); /* Return a connection request on CQ. If SOCK is NULL, the request is - left in the queue. If NOBLOCK is true, EWOULDBLOCK is returned - when there are no immediate connections available. */ -error_t connq_listen (struct connq *cq, int noblock, struct sock **sock); + left in the queue. If TIMEOUT denotes a value of 0, EWOULDBLOCK is + returned when there are no immediate connections available. + Otherwise this value is used to limit the wait duration. If TIMEOUT + is NULL, the wait duration isn't bounded. */ +error_t connq_listen (struct connq *cq, struct timespec *tsp, + struct sock **sock); /* Try to connect SOCK with the socket listening on CQ. If NOBLOCK is true, then return EWOULDBLOCK if there are no connections diff --git a/pflocal/io.c b/pflocal/io.c index 2301611e..0dec56e0 100644 --- a/pflocal/io.c +++ b/pflocal/io.c @@ -172,10 +172,10 @@ S_io_duplicate (struct sock_user *user, /* SELECT_TYPE is the bitwise OR of SELECT_READ, SELECT_WRITE, and SELECT_URG. Block until one of the indicated types of i/o can be done "quickly", and return the types that are then available. */ -error_t -S_io_select (struct sock_user *user, - mach_port_t reply, mach_msg_type_name_t reply_type, - int *select_type) +static error_t +io_select_common (struct sock_user *user, + mach_port_t reply, mach_msg_type_name_t reply_type, + struct timespec *tsp, int *select_type) { error_t err = 0; struct sock *sock; @@ -199,16 +199,24 @@ S_io_select (struct sock_user *user, if (*select_type & SELECT_READ) { + struct timespec noblock = {0, 0}; + /* Wait for a connect. Passing in NULL for SOCK means that the request won't be dequeued. */ - if (connq_listen (sock->listen_queue, 1, NULL) == 0) + if (connq_listen (sock->listen_queue, &noblock, NULL) == 0) /* We can satisfy this request immediately. */ return 0; else /* Gotta wait... */ { ports_interrupt_self_on_port_death (user, reply); - return connq_listen (sock->listen_queue, 0, NULL); + err = connq_listen (sock->listen_queue, tsp, NULL); + if (err == ETIMEDOUT) + { + *select_type = 0; + err = 0; + } + return err; } } } @@ -261,7 +269,7 @@ S_io_select (struct sock_user *user, /* Wait for something to change. */ { ports_interrupt_self_on_port_death (user, reply); - err = pipe_pair_select (read_pipe, write_pipe, select_type, 1); + err = pipe_pair_select (read_pipe, write_pipe, tsp, select_type, 1); } if (valid & SELECT_READ) @@ -272,6 +280,23 @@ S_io_select (struct sock_user *user, return err; } + +error_t +S_io_select (struct sock_user *user, + mach_port_t reply, mach_msg_type_name_t reply_type, + int *select_type) +{ + return io_select_common (user, reply, reply_type, NULL, select_type); +} + +error_t +S_io_select_timeout (struct sock_user *user, + mach_port_t reply, mach_msg_type_name_t reply_type, + struct timespec ts, + int *select_type) +{ + return io_select_common (user, reply, reply_type, &ts, select_type); +} /* Return the current status of the object. Not all the fields of the io_statuf_t are meaningful for all objects; however, the access and diff --git a/pflocal/socket.c b/pflocal/socket.c index 65da7487..ce1edd82 100644 --- a/pflocal/socket.c +++ b/pflocal/socket.c @@ -178,9 +178,11 @@ S_socket_accept (struct sock_user *user, err = ensure_connq (sock); if (!err) { + struct timespec noblock = {0, 0}; struct sock *peer_sock; - err = connq_listen (sock->listen_queue, sock->flags & SOCK_NONBLOCK, + err = connq_listen (sock->listen_queue, + (sock->flags & SOCK_NONBLOCK) ? &noblock : NULL, &peer_sock); if (!err) { -- cgit v1.2.3