From 4f1d13e9222de8e148c9de07ade73b6606bbc2f1 Mon Sep 17 00:00:00 2001 From: Con Kolivas Date: Mon, 5 May 2014 13:19:36 +1000 Subject: [PATCH] Send the last remaining send request without checking if it is ready for writes to avoid busy looping --- src/connector.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/src/connector.c b/src/connector.c index d12a2756..2990d997 100644 --- a/src/connector.c +++ b/src/connector.c @@ -310,8 +310,8 @@ void *sender(void *arg) sender_send_t *sender_send; client_instance_t *client; tv_t timeout_tv = {0, 0}; + bool only_send = false; int ret, fd, ofs = 0; - fd_set writefds; mutex_lock(&sender_lock); if (!sender_sends) { @@ -324,6 +324,8 @@ void *sender(void *arg) sender_send = sender_sends; if (likely(sender_send)) DL_DELETE(sender_sends, sender_send); + if (!sender_send) + only_send = true; mutex_unlock(&sender_lock); if (!sender_send) @@ -341,18 +343,25 @@ void *sender(void *arg) free(sender_send); continue; } - FD_ZERO(&writefds); - FD_SET(fd, &writefds); - ret = select(fd + 1, NULL, &writefds, NULL, &timeout_tv); - if (ret < 1) { - LOGDEBUG("Client %d not ready for writes", client->id); - - /* Append it to the tail of the list */ - mutex_lock(&sender_lock); - DL_APPEND(sender_sends, sender_send); - mutex_unlock(&sender_lock); - - continue; + /* If there are other sends pending and this socket is not + * ready to receive data from us, put the send back on the + * list. */ + if (!only_send) { + fd_set writefds; + + FD_ZERO(&writefds); + FD_SET(fd, &writefds); + ret = select(fd + 1, NULL, &writefds, NULL, &timeout_tv); + if (ret < 1) { + LOGDEBUG("Client %d not ready for writes", client->id); + + /* Append it to the tail of the list */ + mutex_lock(&sender_lock); + DL_APPEND(sender_sends, sender_send); + mutex_unlock(&sender_lock); + + continue; + } } while (sender_send->len) { ret = send(fd, sender_send->buf + ofs, sender_send->len , 0);