From patchwork Sat Mar 10 16:32:29 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Michael Tokarev X-Patchwork-Id: 145867 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (Client did not present a certificate) by ozlabs.org (Postfix) with ESMTPS id 90D78B6FA5 for ; Sun, 11 Mar 2012 03:52:25 +1100 (EST) Received: from localhost ([::1]:57172 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PEj-0000k0-Kx for incoming@patchwork.ozlabs.org; Sat, 10 Mar 2012 11:33:41 -0500 Received: from eggs.gnu.org ([208.118.235.92]:58868) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PET-0000aa-D4 for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:26 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1S6PE7-0006NE-Ft for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:24 -0500 Received: from isrv.corpit.ru ([86.62.121.231]:45355) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1S6PE7-0006Mi-4s for qemu-devel@nongnu.org; Sat, 10 Mar 2012 11:33:03 -0500 Received: from gandalf.tls.msk.ru (mjt.vpn.tls.msk.ru [192.168.177.99]) by isrv.corpit.ru (Postfix) with ESMTP id 8A869A0750; Sat, 10 Mar 2012 20:33:01 +0400 (MSK) Received: by gandalf.tls.msk.ru (Postfix, from userid 1000) id 82FA22BB5; Sat, 10 Mar 2012 20:33:00 +0400 (MSK) From: Michael Tokarev To: qemu-devel@nongnu.org Date: Sat, 10 Mar 2012 20:32:29 +0400 Message-Id: <1331397150-25700-5-git-send-email-mjt@msgid.tls.msk.ru> X-Mailer: git-send-email 1.7.9.1 In-Reply-To: <1331397150-25700-1-git-send-email-mjt@tls.msk.ru> References: <1331397150-25700-1-git-send-email-mjt@tls.msk.ru> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 86.62.121.231 Cc: Michael Tokarev Subject: [Qemu-devel] [PATCH 4/5] cleanup qemu_co_sendv(), qemu_co_recvv() and friends X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org The same as for non-coroutine versions in previous patches: rename arguments to be more obvious, change type of arguments from int to size_t where appropriate, and use common code for send and receive paths (with one extra argument) since these are exactly the same. Use common qemu_sendv_recvv() directly. Also constify buf arg of qemu_co_send(). qemu_co_sendv(), qemu_co_recvv(), and qemu_co_recv() are now trivial #define's merely adding one extra arg. qemu_co_send() is an inline function due to `buf' arg de-constification. Signed-off-by: Michael Tokarev --- qemu-common.h | 37 +++++++++++----------- qemu-coroutine-io.c | 83 +++++++++++++------------------------------------- 2 files changed, 40 insertions(+), 80 deletions(-) diff --git a/qemu-common.h b/qemu-common.h index 474cbcc..2bf630d 100644 --- a/qemu-common.h +++ b/qemu-common.h @@ -306,31 +306,30 @@ struct qemu_work_item { void qemu_init_vcpu(void *env); #endif -/** - * Sends an iovec (or optionally a part of it) down a socket, yielding - * when the socket is full. - */ -int qemu_co_sendv(int sockfd, struct iovec *iov, - int len, int iov_offset); - -/** - * Receives data into an iovec (or optionally into a part of it) from - * a socket, yielding when there is no data in the socket. - */ -int qemu_co_recvv(int sockfd, struct iovec *iov, - int len, int iov_offset); - /** - * Sends a buffer down a socket, yielding when the socket is full. + * Sends a (part of) iovec down a socket, yielding when the socket is full, or + * Receives data into a (part of) iovec from a socket, + * yielding when there is no data in the socket. + * XXX should mark these as coroutine_fn */ -int qemu_co_send(int sockfd, void *buf, int len); +int qemu_co_sendv_recvv(int sockfd, struct iovec *iov, + size_t bytes, size_t offset, bool do_send); +#define qemu_co_recvv(sockfd, iov, bytes, offset) \ + qemu_co_sendv_recvv(sockfd, iov, bytes, offset, false) +#define qemu_co_sendv(sockfd, iov, bytes, offset) \ + qemu_co_sendv_recvv(sockfd, iov, bytes, offset, true) /** - * Receives data into a buffer from a socket, yielding when there - * is no data in the socket. + * The same as above, but with just a single buffer */ -int qemu_co_recv(int sockfd, void *buf, int len); +int qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send); +#define qemu_co_recv(sockfd, buf, bytes) \ + qemu_co_send_recv(sockfd, buf, bytes, false) +static inline int qemu_co_send(int sockfd, const void *buf, size_t bytes) +{ + return qemu_co_send_recv(sockfd, (void*)buf, bytes, true); +} typedef struct QEMUIOVector { diff --git a/qemu-coroutine-io.c b/qemu-coroutine-io.c index 40fd514..5631c84 100644 --- a/qemu-coroutine-io.c +++ b/qemu-coroutine-io.c @@ -26,71 +26,32 @@ #include "qemu_socket.h" #include "qemu-coroutine.h" -int coroutine_fn qemu_co_recvv(int sockfd, struct iovec *iov, - int len, int iov_offset) +int coroutine_fn +qemu_co_sendv_recvv(int sockfd, struct iovec *iov, + size_t bytes, size_t offset, bool do_send) { - int total = 0; + size_t done = 0; int ret; - while (len) { - ret = qemu_recvv(sockfd, iov, len, iov_offset + total); - if (ret < 0) { - if (errno == EAGAIN) { - qemu_coroutine_yield(); - continue; - } - if (total == 0) { - total = -1; - } - break; - } - if (ret == 0) { - break; - } - total += ret, len -= ret; + while (done < bytes) { + ret = qemu_sendv_recvv(sockfd, iov, bytes - done, offset + done, do_send); + if (ret > 0) { + done += ret; + } else if (!ret) { + break; + } else if (errno == EAGAIN) { + qemu_coroutine_yield(); + } else if (done == 0) { + return -1; + } else { + break; + } } - - return total; -} - -int coroutine_fn qemu_co_sendv(int sockfd, struct iovec *iov, - int len, int iov_offset) -{ - int total = 0; - int ret; - while (len) { - ret = qemu_sendv(sockfd, iov, len, iov_offset + total); - if (ret < 0) { - if (errno == EAGAIN) { - qemu_coroutine_yield(); - continue; - } - if (total == 0) { - total = -1; - } - break; - } - total += ret, len -= ret; - } - - return total; + return done; } -int coroutine_fn qemu_co_recv(int sockfd, void *buf, int len) +int coroutine_fn +qemu_co_send_recv(int sockfd, void *buf, size_t bytes, bool do_send) { - struct iovec iov; - - iov.iov_base = buf; - iov.iov_len = len; - - return qemu_co_recvv(sockfd, &iov, len, 0); -} - -int coroutine_fn qemu_co_send(int sockfd, void *buf, int len) -{ - struct iovec iov; - - iov.iov_base = buf; - iov.iov_len = len; - - return qemu_co_sendv(sockfd, &iov, len, 0); + struct iovec iov = { buf, bytes }; + return qemu_co_sendv_recvv(sockfd, &iov, bytes, 0, do_send); }