From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:42851) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V4owC-0000dk-T5 for qemu-devel@nongnu.org; Thu, 01 Aug 2013 05:12:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1V4ohr-0000Nb-P0 for qemu-devel@nongnu.org; Thu, 01 Aug 2013 04:58:05 -0400 Received: from mx3-phx2.redhat.com ([209.132.183.24]:54979) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1V4ohr-0000NU-Fc for qemu-devel@nongnu.org; Thu, 01 Aug 2013 04:57:59 -0400 Date: Thu, 1 Aug 2013 04:57:52 -0400 (EDT) From: Paolo Bonzini Message-ID: <391717867.8023244.1375347472193.JavaMail.root@redhat.com> In-Reply-To: References: <1375067768-11342-1-git-send-email-pingfank@linux.vnet.ibm.com> <1375067768-11342-4-git-send-email-pingfank@linux.vnet.ibm.com> <51F60C02.6020008@redhat.com> <51F65044.4040706@redhat.com> <51F78491.708@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [RFC v2 3/5] timer: make qemu_clock_enable sync between disable and timer's cb List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: liu ping fan Cc: Kevin Wolf , Stefan Hajnoczi , Jan Kiszka , qemu-devel@nongnu.org, Alex Bligh , Anthony Liguori > > Hmm, do we even need clock->using at this point? For example: > > > > qemu_clock_enable() > > { > > clock->enabled = enabled; > > ... > > if (!enabled) { > > /* If another thread is within qemu_run_timers, > > * wait for it to finish. > > */ > > qemu_event_wait(&clock->callbacks_done_event); > > } > > } > > > > qemu_run_timers() > > { > > qemu_event_reset(&clock->callbacks_done_event); > > if (!clock->enabled) { > > goto out; > > } > > ... > > out: > > qemu_event_set(&eclock->callbacks_done_event); > > } > > > > In the fast path this only does two atomic operations (an OR for reset, > > and XCHG for set). > > > There is race condition, suppose the following scenario with A/B thread > A: qemu_event_reset() > B: qemu_event_reset() > A: qemu_event_set() ----> B is still in flight when > qemu_clock_enable() is notified > B: qemu_event_set() > > I had tried to build something around futex(2) like qemu_event, but failed. True, qemu_event basically works only when a single thread resets it. But there is no race condition here because qemu_run_timers cannot be executed concurrently by multiple threads (like aio_poll in your bottom half patches). Paolo