From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from eggs.gnu.org ([2001:4830:134:3::10]:51660) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ1ik-0000xq-AK for qemu-devel@nongnu.org; Fri, 01 Jul 2016 12:55:15 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bJ1ig-00047f-7K for qemu-devel@nongnu.org; Fri, 01 Jul 2016 12:55:13 -0400 Received: from mail-lf0-x243.google.com ([2a00:1450:4010:c07::243]:36163) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bJ1if-00047T-Vj for qemu-devel@nongnu.org; Fri, 01 Jul 2016 12:55:10 -0400 Received: by mail-lf0-x243.google.com with SMTP id a2so11848263lfe.3 for ; Fri, 01 Jul 2016 09:55:09 -0700 (PDT) References: <1466375313-7562-1-git-send-email-sergey.fedorov@linaro.org> <1466375313-7562-8-git-send-email-sergey.fedorov@linaro.org> From: Sergey Fedorov Message-ID: <5776A065.1090107@gmail.com> Date: Fri, 1 Jul 2016 19:55:01 +0300 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [Qemu-devel] [RFC 7/8] cpu-exec-common: Introduce async_safe_run_on_cpu() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: Alvise Rigo , Sergey Fedorov Cc: QEMU Developers , Riku Voipio , Peter Crosthwaite , "patches@linaro.org" , Paolo Bonzini , Richard Henderson On 01/07/16 19:29, Alvise Rigo wrote: > Hi Sergey, > > On Mon, Jun 20, 2016 at 12:28 AM, Sergey Fedorov > wrote: >> diff --git a/cpu-exec-common.c b/cpu-exec-common.c >> index 8184e0662cbd..3056324738f8 100644 >> --- a/cpu-exec-common.c >> +++ b/cpu-exec-common.c >> @@ -25,6 +25,7 @@ >> >> bool exit_request; >> CPUState *tcg_current_cpu; >> +int tcg_pending_cpus; >> >> /* exit the current TB, but without causing any exception to be raised */ >> void cpu_loop_exit_noexc(CPUState *cpu) >> @@ -78,6 +79,15 @@ void cpu_loop_exit_restore(CPUState *cpu, uintptr_t pc) >> siglongjmp(cpu->jmp_env, 1); >> } >> >> +static int safe_work_pending; >> + >> +void wait_safe_cpu_work(void) >> +{ >> + while (atomic_mb_read(&safe_work_pending) > 0) { >> + wait_cpu_work(); >> + } >> +} >> + > Is this piece of code deadlock-safe once we are in mttcg mode? It is supposed to be deadlock-safe. > What happens when two threads call simultaneously async_safe_run_on_cpu? > In this case each thread will roughly: - exit its execution loop; - take BQL; - decrement 'tcg_pending_cpus', signal 'qemu_work_cond' if zero; - start processing its work queue; - encountering safe work wait on 'qemu_work_cond' for 'tcg_pending_cpus' to become zero; - reacquire BQL; - process the safe work; - decrement 'safe_work_pending', signal 'qemu_work_cond' if zero; - when finished processing work, wait on 'qemu_work_cond' for 'safe_work_pending' to become zero; - reacquire BQL; - continue execution (releasing BQL). Hope this will help. Kind regards, Sergey.