From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:53226) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIK9p-0004pp-K5 for qemu-devel@nongnu.org; Tue, 25 Feb 2014 10:43:03 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WIK9j-000435-F5 for qemu-devel@nongnu.org; Tue, 25 Feb 2014 10:42:57 -0500 Received: from mx1.redhat.com ([209.132.183.28]:5375) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WIK9j-000430-6V for qemu-devel@nongnu.org; Tue, 25 Feb 2014 10:42:51 -0500 Date: Tue, 25 Feb 2014 16:42:45 +0100 From: Stefan Hajnoczi Message-ID: <20140225154245.GC2374@stefanha-thinkpad.redhat.com> References: <1392994280-9675-1-git-send-email-stefanha@redhat.com> <1392994280-9675-2-git-send-email-stefanha@redhat.com> <53076E46.3020109@redhat.com> <20140224155359.GH23185@stefanha-thinkpad.hitronhub.home> <530B77CD.9020502@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <530B77CD.9020502@redhat.com> Subject: Re: [Qemu-devel] [PATCH 1/2] iothread: stash thread ID away List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Paolo Bonzini Cc: Stefan Hajnoczi , "Shergill, Gurinder" , "Vinod, Chegu" , qemu-devel@nongnu.org, Luiz Capitulino On Mon, Feb 24, 2014 at 05:48:13PM +0100, Paolo Bonzini wrote: > Il 24/02/2014 16:53, Stefan Hajnoczi ha scritto: > >>>> >+ qemu_cond_destroy(&init_info.init_done_cond); > >>>> >+ qemu_mutex_destroy(&init_info.init_done_lock); > >>> > >>> Destroying the mutex here is racy. You need to keep it until the > >>> iothread is destroyed. > >I don't think so: > > > >qemu_cond_signal() is called with the mutex held. Therefore, our > >qemu_cond_wait() followed by qemu_mutex_unlock() will only complete once > >the thread has released the mutex. > > > >The thread will never touch the mutex again so it is safe to destroy it. > >There is no race condition. > > Could qemu_mutex_destroy run while the other thread has already > released the main thread, but before it returns? As far as I know, > the only time when it is safe to destroy the "last" synchronization > object (in this case the mutex is the last, the condvar is not) is > after pthread_join. I guess you're saying that while unlocking the mutex is atomic, that doesn't guarantee pthread won't access the mutex internal state some more after it has unlocked it. Therefore it's not safe for another thread to destroy the mutex even after it has acquired it. POSIX does say that: "It shall be safe to destroy an initialized mutex that is unlocked." But maybe I am reading too much into that? After poking around glibc a little I think you are right. I can't say for sure but it seems even after a futex call glibc might still mess with internal state. But if anyone knows for certain, please speak up. Stefan