From nobody Mon Feb 9 15:10:15 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=openvz.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1649338703193149.91368890373758; Thu, 7 Apr 2022 06:38:23 -0700 (PDT) Received: from localhost ([::1]:56890 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ncSL7-0001OU-G3 for importer@patchew.org; Thu, 07 Apr 2022 09:38:21 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:49494) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ncSAr-00077w-1X for qemu-devel@nongnu.org; Thu, 07 Apr 2022 09:27:45 -0400 Received: from mail-lf1-x136.google.com ([2a00:1450:4864:20::136]:33539) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1ncSAo-0007uL-G0 for qemu-devel@nongnu.org; Thu, 07 Apr 2022 09:27:44 -0400 Received: by mail-lf1-x136.google.com with SMTP id bu29so9723505lfb.0 for ; Thu, 07 Apr 2022 06:27:42 -0700 (PDT) Received: from fedora.. ([185.215.60.161]) by smtp.gmail.com with ESMTPSA id o15-20020a2e90cf000000b0024b0437ed8asm1929271ljg.7.2022.04.07.06.27.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 07 Apr 2022 06:27:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=openvz-org.20210112.gappssmtp.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=/10aFJpOBf3AAl4GLfLE1OLig8oWXVs06ng3GxyYuOk=; b=yxFoKi4F0PVwofhIUCxXddXPaogZ+K1B9fix3plHnLiYIKTx4DHmsiuVOIEmFOcJUI LxC6RH1OkfAmIEPRA/4+80lyvdt3u/CbbYkCW51WKKw2WKstgJbdwTZOVLzR5HM7YFHN MKr1imoBGsObma3DnN5tnOcaIoS8ee8/+Nt7Nzh6AA4WdaGL2QdG5Gufd3PyLsfTb/cJ YkWf2VBOJDpfBjPy7bFYsBtv2esnbcQlwSyZIRK6hdTf9Bmsrthohq94y0yI2qvTP/mB 9jAnJIDnTtkvXfoSP3fcg6YNCFG2NMbryv2vBi0QxgCHeql2nfEM4chlgVNrfuNOv/9Z iL5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=/10aFJpOBf3AAl4GLfLE1OLig8oWXVs06ng3GxyYuOk=; b=GJj3bNRLSWxCOOTSH72CIEagrgDhXCaGCRPe65z+wTVxXOpRfNm9uTGPS581gPNPjE 9n8YNpm3nlTDD4XAHq3nBJ8vefqn8myh6QB+r62Ib1jejLeKJznk1ZYpz3oYt4fhhXWC QdX+OzPOrIy9PJEwStHycUmUarySc6LfhYO8JgVRFBUPGSEosb+Xp06+trswoaxnWYv2 zBgaUCXbpp5q0/AXGHJF4skQ/gNU7p5fz+lPspkR1GjxHiHPU4/LIs+26OBPuZq5kz5l +sIpqAQpbQCcfnEZ7b/u4nQY91bmNULGzv6iAlONT6ZjWfBWmajt8kkqAXG55p+PBrih FNDA== X-Gm-Message-State: AOAM530NyU0REJjTGn5r2UN/EmfGEHRftjTeLQb4MSr2wvWdsJIfKgSW AsgCH/jnJOb1LDx4jL0Omf+Rzg== X-Google-Smtp-Source: ABdhPJySJ27iUfqqURvs5WzlpIhU9CG8v7rQiWukMT4D40wDCYJNEz/eSUJo69T+qsVFLSTYy0xbgw== X-Received: by 2002:a05:6512:400e:b0:450:e09d:c9a6 with SMTP id br14-20020a056512400e00b00450e09dc9a6mr10197620lfb.243.1649338060550; Thu, 07 Apr 2022 06:27:40 -0700 (PDT) From: Vladimir Sementsov-Ogievskiy X-Google-Original-From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v4 4/7] util: add qemu-co-timeout Date: Thu, 7 Apr 2022 16:27:23 +0300 Message-Id: <20220407132726.85114-5-vsementsov@openvz.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220407132726.85114-1-vsementsov@openvz.org> References: <20220407132726.85114-1-vsementsov@openvz.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a00:1450:4864:20::136; envelope-from=vladimir.sementsov-ogievskiy@openvz.org; helo=mail-lf1-x136.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: kwolf@redhat.com, v.sementsov-og@mail.ru, jsnow@redhat.com, qemu-devel@nongnu.org, armbru@redhat.com, hreitz@redhat.com, vsementsov@openvz.org, stefanha@redhat.com, eblake@redhat.com Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1649338704942100001 Content-Type: text/plain; charset="utf-8" Add new API, to make a time limited call of the coroutine. Signed-off-by: Vladimir Sementsov-Ogievskiy Reviewed-by: Hanna Reitz --- include/qemu/coroutine.h | 13 ++++++ util/qemu-co-timeout.c | 89 ++++++++++++++++++++++++++++++++++++++++ util/meson.build | 1 + 3 files changed, 103 insertions(+) create mode 100644 util/qemu-co-timeout.c diff --git a/include/qemu/coroutine.h b/include/qemu/coroutine.h index c828a95ee0..8704b05da8 100644 --- a/include/qemu/coroutine.h +++ b/include/qemu/coroutine.h @@ -316,6 +316,19 @@ static inline void coroutine_fn qemu_co_sleep_ns(QEMUC= lockType type, int64_t ns) qemu_co_sleep_ns_wakeable(&w, type, ns); } =20 +typedef void CleanupFunc(void *opaque); +/** + * Run entry in a coroutine and start timer. Wait for entry to finish or f= or + * timer to elapse, what happen first. If entry finished, return 0, if tim= er + * elapsed earlier, return -ETIMEDOUT. + * + * Be careful, entry execution is not canceled, user should handle it some= how. + * If @clean is provided, it's called after coroutine finish if timeout + * happened. + */ +int coroutine_fn qemu_co_timeout(CoroutineEntry *entry, void *opaque, + uint64_t timeout_ns, CleanupFunc clean); + /** * Wake a coroutine if it is sleeping in qemu_co_sleep_ns. The timer will = be * deleted. @sleep_state must be the variable whose address was given to diff --git a/util/qemu-co-timeout.c b/util/qemu-co-timeout.c new file mode 100644 index 0000000000..00cd335649 --- /dev/null +++ b/util/qemu-co-timeout.c @@ -0,0 +1,89 @@ +/* + * Helper functionality for distributing a fixed total amount of + * an abstract resource among multiple coroutines. + * + * Copyright (c) 2022 Virtuozzo International GmbH + * + * Permission is hereby granted, free of charge, to any person obtaining a= copy + * of this software and associated documentation files (the "Software"), t= o deal + * in the Software without restriction, including without limitation the r= ights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or se= ll + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included= in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS= OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OT= HER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING= FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS = IN + * THE SOFTWARE. + */ + +#include "qemu/osdep.h" +#include "qemu/coroutine.h" +#include "block/aio.h" + +typedef struct QemuCoTimeoutState { + CoroutineEntry *entry; + void *opaque; + QemuCoSleep sleep_state; + bool marker; + CleanupFunc *clean; +} QemuCoTimeoutState; + +static void coroutine_fn qemu_co_timeout_entry(void *opaque) +{ + QemuCoTimeoutState *s =3D opaque; + + s->entry(s->opaque); + + if (s->marker) { + assert(!s->sleep_state.to_wake); + /* .marker set by qemu_co_timeout, it have been failed */ + if (s->clean) { + s->clean(s->opaque); + } + g_free(s); + } else { + s->marker =3D true; + qemu_co_sleep_wake(&s->sleep_state); + } +} + +int coroutine_fn qemu_co_timeout(CoroutineEntry *entry, void *opaque, + uint64_t timeout_ns, CleanupFunc clean) +{ + QemuCoTimeoutState *s; + Coroutine *co; + + if (timeout_ns =3D=3D 0) { + entry(opaque); + return 0; + } + + s =3D g_new(QemuCoTimeoutState, 1); + *s =3D (QemuCoTimeoutState) { + .entry =3D entry, + .opaque =3D opaque, + .clean =3D clean + }; + + co =3D qemu_coroutine_create(qemu_co_timeout_entry, s); + + aio_co_enter(qemu_get_current_aio_context(), co); + qemu_co_sleep_ns_wakeable(&s->sleep_state, QEMU_CLOCK_REALTIME, timeou= t_ns); + + if (s->marker) { + /* .marker set by qemu_co_timeout_entry, success */ + g_free(s); + return 0; + } + + /* Don't free s, as we can't cancel qemu_co_timeout_entry execution */ + s->marker =3D true; + return -ETIMEDOUT; +} diff --git a/util/meson.build b/util/meson.build index f6ee74ad0c..249891db72 100644 --- a/util/meson.build +++ b/util/meson.build @@ -83,6 +83,7 @@ if have_block util_ss.add(files('block-helpers.c')) util_ss.add(files('qemu-coroutine-sleep.c')) util_ss.add(files('qemu-co-shared-resource.c')) + util_ss.add(files('qemu-co-timeout.c')) util_ss.add(files('thread-pool.c', 'qemu-timer.c')) util_ss.add(files('readline.c')) util_ss.add(files('throttle.c')) --=20 2.35.1