Received: from eggs.gnu.org ([2001:4830:134:3::10]:57409) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsXmq-00036w-7P for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:33:05 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WsXmh-0001sn-6r for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:32:56 -0400 Received: from mail-wg0-x22d.google.com ([2a00:1450:400c:c00::22d]:54499) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WsXmh-0001se-0C for qemu-devel@nongnu.org; Thu, 05 Jun 2014 09:32:47 -0400 Received: by mail-wg0-f45.google.com with SMTP id m15so1094131wgh.4 for ; Thu, 05 Jun 2014 06:32:46 -0700 (PDT) Date: Thu, 5 Jun 2014 15:32:43 +0200 From: Stefan Hajnoczi Message-ID: <20140605133243.GB8759@stefanha-thinkpad.redhat.com> References: <1401561792-13410-1-git-send-email-mreitz@redhat.com> <1401561792-13410-4-git-send-email-mreitz@redhat.com> <20140604123748.GD11073@stefanha-thinkpad.redhat.com> <538F5F1E.4050706@redhat.com> <20140605081223.GB27366@stefanha-thinkpad.redhat.com> <53903815.3030809@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <53903815.3030809@redhat.com> Subject: Re: [Qemu-devel] [RFC 3/5] nbd: Use aio_set_fd_handler2() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: Kevin Wolf , Fam Zheng , qemu-devel@nongnu.org, Stefan Hajnoczi , Max Reitz On Thu, Jun 05, 2014 at 11:27:49AM +0200, Paolo Bonzini wrote: > Il 05/06/2014 10:12, Stefan Hajnoczi ha scritto: > >On Wed, Jun 04, 2014 at 08:02:06PM +0200, Paolo Bonzini wrote: > >>Il 04/06/2014 14:37, Stefan Hajnoczi ha scritto: > >>>Why is this design cleaner? Because NBD code doesn't have to worry > >>>about fd handlers. It uses straightforward coroutine send/recv for > >>>socket I/O inside nbd_read_req() and nbd_write_resp(). It's easy to see > >>>that only one coroutine receives from the socket and that only one > >>>coroutine writes to the socket. > >> > >>I don't understand how this would work without managing fd handlers. > > > >fd handlers still need to be managed, but not by NBD code. They must be > >managed by coroutine recv/send utility functions. In other words, fd > >handlers are used locally, not globally. > > > >def co_recv(fd, buf): > > while True: > > nbytes = recv(fd, buf, len(buf)) > > if nbytes == -1: > > if errno == EINTR: > > continue > > if errno == EAGAIN or errno == EWOULDBLOCK: > > aio_set_fd_read_handler(fd, co_recv_cb) > > qemu_coroutine_yield() > > aio_set_fd_read_handler(fd, NULL) > > continue > > return nbytes > > > >The send function is similar. > > I see what you mean now---this however is not how qemu_co_recv and > qemu_co_send work. NBD uses them, but they require the caller to set up the > handlers manually. Yep, and I think that's not a clean way to program since it forgoes the advantage of coroutines: functions that look just like their blocking equivalents. I think we should hide the fd handler management so coroutine socket users don't need to worry about it. Stefan