From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9299FC433EF for ; Tue, 12 Oct 2021 08:52:34 +0000 (UTC) Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 1B6CE60724 for ; Tue, 12 Oct 2021 08:52:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 1B6CE60724 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=nongnu.org Received: from localhost ([::1]:43390 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1maDWS-00081n-KQ for qemu-devel@archiver.kernel.org; Tue, 12 Oct 2021 04:52:32 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:32978) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maDTh-00056N-Lp for qemu-devel@nongnu.org; Tue, 12 Oct 2021 04:49:41 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:24407) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1maDTe-0002Mw-RT for qemu-devel@nongnu.org; Tue, 12 Oct 2021 04:49:40 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1634028576; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xiTos55A0ypbNBhJ1wDIHq4nHbZ6g+YBGsaeillj6wI=; b=SPsf5WOTeGuXgSfgI/af+EYJPU+A+aaLdSunjz9pC5NdWg8mcK3pexaYL/BcUkg0YnNlya 0+OEaNAuFn1Ydf4VWDqKlhE2dSn5H9WV8VNRD4ia5SrYHZwki4eFb7ItWukBGmIe9rqjG/ In85HisL0mrPopSUlZPREfgUabF9ii0= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-54-DAqLbZ-kOfy9_QYaArrrhg-1; Tue, 12 Oct 2021 04:49:33 -0400 X-MC-Unique: DAqLbZ-kOfy9_QYaArrrhg-1 Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 7924E801A92; Tue, 12 Oct 2021 08:49:32 +0000 (UTC) Received: from virtlab701.virt.lab.eng.bos.redhat.com (virtlab701.virt.lab.eng.bos.redhat.com [10.19.152.228]) by smtp.corp.redhat.com (Postfix) with ESMTP id 2002F5FC08; Tue, 12 Oct 2021 08:49:31 +0000 (UTC) From: Emanuele Giuseppe Esposito To: qemu-block@nongnu.org Subject: [PATCH v3 01/25] main-loop.h: introduce qemu_in_main_thread() Date: Tue, 12 Oct 2021 04:48:42 -0400 Message-Id: <20211012084906.2060507-2-eesposit@redhat.com> In-Reply-To: <20211012084906.2060507-1-eesposit@redhat.com> References: <20211012084906.2060507-1-eesposit@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=eesposit@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" Received-SPF: pass client-ip=170.10.133.124; envelope-from=eesposit@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -8 X-Spam_score: -0.9 X-Spam_bar: / X-Spam_report: (-0.9 / 5.0 requ) DKIMWL_WL_HIGH=-0.049, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Kevin Wolf , Fam Zheng , Vladimir Sementsov-Ogievskiy , =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= , Eduardo Habkost , Juan Quintela , qemu-devel@nongnu.org, John Snow , Emanuele Giuseppe Esposito , Richard Henderson , Markus Armbruster , "Dr. David Alan Gilbert" , Hanna Reitz , Stefan Hajnoczi , Paolo Bonzini , Eric Blake Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" When invoked from the main loop, this function is the same as qemu_mutex_iothread_locked, and returns true if the BQL is held. When invoked from iothreads or tests, it returns true only if the current AioContext is the Main Loop. This essentially just extends qemu_mutex_iothread_locked to work also in unit tests or other users like storage-daemon, that run in the Main Loop but end up using the implementation in stubs/iothread-lock.c. Using qemu_mutex_iothread_locked in unit tests defaults to false because they use the implementation in stubs/iothread-lock, making all assertions added in next patches fail despite the AioContext is still the main loop. Signed-off-by: Emanuele Giuseppe Esposito Reviewed-by: Stefan Hajnoczi --- include/qemu/main-loop.h | 13 +++++++++++++ softmmu/cpus.c | 5 +++++ stubs/iothread-lock.c | 5 +++++ 3 files changed, 23 insertions(+) diff --git a/include/qemu/main-loop.h b/include/qemu/main-loop.h index 8dbc6fcb89..6b8fa57c5d 100644 --- a/include/qemu/main-loop.h +++ b/include/qemu/main-loop.h @@ -245,6 +245,19 @@ AioContext *iohandler_get_aio_context(void); */ bool qemu_mutex_iothread_locked(void); +/** + * qemu_in_main_thread: same as qemu_mutex_iothread_locked when + * softmmu/cpus.c implementation is linked. Otherwise this function + * checks that the current AioContext is the global AioContext + * (main loop). + * + * This is useful when checking that the BQL is held, to avoid + * returning false when invoked by unit tests or other users like + * storage-daemon that end up using stubs/iothread-lock.c + * implementation. + */ +bool qemu_in_main_thread(void); + /** * qemu_mutex_lock_iothread: Lock the main loop mutex. * diff --git a/softmmu/cpus.c b/softmmu/cpus.c index 071085f840..3f61a3c31d 100644 --- a/softmmu/cpus.c +++ b/softmmu/cpus.c @@ -481,6 +481,11 @@ bool qemu_mutex_iothread_locked(void) return iothread_locked; } +bool qemu_in_main_thread(void) +{ + return qemu_mutex_iothread_locked(); +} + /* * The BQL is taken from so many places that it is worth profiling the * callers directly, instead of funneling them all through a single function. diff --git a/stubs/iothread-lock.c b/stubs/iothread-lock.c index 5b45b7fc8b..ff7386e42c 100644 --- a/stubs/iothread-lock.c +++ b/stubs/iothread-lock.c @@ -6,6 +6,11 @@ bool qemu_mutex_iothread_locked(void) return false; } +bool qemu_in_main_thread(void) +{ + return qemu_get_current_aio_context() == qemu_get_aio_context(); +} + void qemu_mutex_lock_iothread_impl(const char *file, int line) { } -- 2.27.0