Received: from eggs.gnu.org ([2001:4830:134:3::10]:51335) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WucUx-0002zd-H3 for qemu-devel@nongnu.org; Wed, 11 Jun 2014 02:59:09 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WucUr-0002pS-B1 for qemu-devel@nongnu.org; Wed, 11 Jun 2014 02:59:03 -0400 Received: from mx1.redhat.com ([209.132.183.28]:63266) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WucUr-0002n0-2C for qemu-devel@nongnu.org; Wed, 11 Jun 2014 02:58:57 -0400 Received: from int-mx14.intmail.prod.int.phx2.redhat.com (int-mx14.intmail.prod.int.phx2.redhat.com [10.5.11.27]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id s5B6wt6Z008407 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK) for ; Wed, 11 Jun 2014 02:58:55 -0400 Date: Wed, 11 Jun 2014 14:59:11 +0800 From: Fam Zheng Message-ID: <20140611065911.GG5448@T430.nay.redhat.com> References: <1401813551-6667-1-git-send-email-pbonzini@redhat.com> <1401813551-6667-5-git-send-email-pbonzini@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1401813551-6667-5-git-send-email-pbonzini@redhat.com> Subject: Re: [Qemu-devel] [PATCH 4/6] qemu-char: make writes thread-safe List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: kwolf@redhat.com, kraxel@redhat.com, qemu-devel@nongnu.org, stefanha@redhat.com, lcapitulino@redhat.com On Tue, 06/03 18:39, Paolo Bonzini wrote: > diff --git a/qemu-char.c b/qemu-char.c > index b478a3d..dcd0765 100644 > --- a/qemu-char.c > +++ b/qemu-char.c > @@ -121,7 +121,12 @@ void qemu_chr_be_generic_open(CharDriverState *s) > > int qemu_chr_fe_write(CharDriverState *s, const uint8_t *buf, int len) > { > - return s->chr_write(s, buf, len); > + int ret; > + > + qemu_mutex_lock(&s->chr_write_lock); > + ret = s->chr_write(s, buf, len); > + qemu_mutex_unlock(&s->chr_write_lock); > + return ret; > } > > int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) > @@ -129,6 +134,7 @@ int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) > int offset = 0; > int res; > > + qemu_mutex_lock(&s->chr_write_lock); > while (offset < len) { > do { > res = s->chr_write(s, buf + offset, len - offset); > @@ -147,6 +153,7 @@ int qemu_chr_fe_write_all(CharDriverState *s, const uint8_t *buf, int len) More context in the loop: if (res == -1 && errno == EAGAIN) { g_usleep(100); } } while (res == -1 && errno == EAGAIN); if (res == 0) { break; } if (res < 0) { (*) return res; } Doesn't (*) need an unlock? Fam > > offset += res; > } > + qemu_mutex_unlock(&s->chr_write_lock); > > return offset; > }