Received: from eggs.gnu.org ([2001:4830:134:3::10]:41423) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfP7N-00077G-9x for qemu-devel@nongnu.org; Thu, 01 Sep 2016 06:21:18 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bfP7D-0003VP-99 for qemu-devel@nongnu.org; Thu, 01 Sep 2016 06:21:08 -0400 Received: from mail-wm0-x244.google.com ([2a00:1450:400c:c09::244]:33401) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bfP7D-0003VL-2g for qemu-devel@nongnu.org; Thu, 01 Sep 2016 06:20:59 -0400 Received: by mail-wm0-x244.google.com with SMTP id w207so7697755wmw.0 for ; Thu, 01 Sep 2016 03:20:59 -0700 (PDT) Sender: Paolo Bonzini From: Paolo Bonzini Date: Thu, 1 Sep 2016 12:20:25 +0200 Message-Id: <1472725227-10374-11-git-send-email-pbonzini@redhat.com> In-Reply-To: <1472725227-10374-1-git-send-email-pbonzini@redhat.com> References: <1472725227-10374-1-git-send-email-pbonzini@redhat.com> Subject: [Qemu-devel] [PATCH 10/12] cpus-common: Introduce async_safe_run_on_cpu() List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , Sergey Fedorov , Sergey Fedorov , "Emilio G. Cota" Signed-off-by: Paolo Bonzini --- cpus-common.c | 25 +++++++++++++++++++++++++ include/qom/cpu.h | 12 ++++++++++++ 2 files changed, 37 insertions(+) diff --git a/cpus-common.c b/cpus-common.c index 59c8dc8..88cf5ec 100644 --- a/cpus-common.c +++ b/cpus-common.c @@ -144,6 +144,11 @@ void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data) queue_work_on_cpu(cpu, wi); } +typedef struct SafeWorkItem { + run_on_cpu_func func; + void *data; +} SafeWorkItem; + /* Wait for pending exclusive operations to complete. The exclusive lock must be held. */ static inline void exclusive_idle(void) @@ -208,6 +213,26 @@ void cpu_exec_end(CPUState *cpu) qemu_mutex_unlock(&qemu_cpu_list_mutex); } +static void async_safe_run_on_cpu_fn(CPUState *cpu, void *data) +{ + SafeWorkItem *w = data; + + start_exclusive(); + w->func(cpu, w->data); + end_exclusive(); + g_free(w); +} + +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data) +{ + SafeWorkItem *w = g_new(SafeWorkItem, 1); + + w->func = func; + w->data = data; + + async_run_on_cpu(cpu, async_safe_run_on_cpu_fn, w); +} + void process_queued_cpu_work(CPUState *cpu) { struct qemu_work_item *wi; diff --git a/include/qom/cpu.h b/include/qom/cpu.h index 0e04e8f..54a875e 100644 --- a/include/qom/cpu.h +++ b/include/qom/cpu.h @@ -663,6 +663,18 @@ void run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); void async_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); /** + * async_safe_run_on_cpu: + * @cpu: The vCPU to run on. + * @func: The function to be executed. + * @data: Data to pass to the function. + * + * Schedules the function @func for execution on the vCPU @cpu asynchronously, + * while all other vCPUs are sleeping. @func is called with the CPU list lock + * taken (and for system emulation the BQL); any other lock can be taken safely. + */ +void async_safe_run_on_cpu(CPUState *cpu, run_on_cpu_func func, void *data); + +/** * qemu_get_cpu: * @index: The CPUState@cpu_index value of the CPU to obtain. * -- 2.7.4