From nobody Sun Feb 8 12:39:08 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 1648805721831546.2282384301955; Fri, 1 Apr 2022 02:35:21 -0700 (PDT) Received: from localhost ([::1]:36862 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1naDgd-0007uQ-EB for importer@patchew.org; Fri, 01 Apr 2022 05:35:19 -0400 Received: from eggs.gnu.org ([209.51.188.92]:57926) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1naDRY-0000Hk-F6 for qemu-devel@nongnu.org; Fri, 01 Apr 2022 05:19:46 -0400 Received: from [2a00:1450:4864:20::236] (port=34686 helo=mail-lj1-x236.google.com) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1naDRV-0006tB-1k for qemu-devel@nongnu.org; Fri, 01 Apr 2022 05:19:43 -0400 Received: by mail-lj1-x236.google.com with SMTP id 17so3120279lji.1 for ; Fri, 01 Apr 2022 02:19:38 -0700 (PDT) Received: from fedora.. ([185.215.60.153]) by smtp.gmail.com with ESMTPSA id d16-20020a2eb050000000b002461d8f365bsm155135ljl.38.2022.04.01.02.19.36 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Apr 2022 02:19:36 -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=nFVdnuw+3o5G9R5rQeBbXGWA5P0kYYacFZZwVwvOqB4=; b=mvBWJ30ufOsXXse3c/eA0VSPunDQWrxy8eZ8H8hANsIQpjQiEnUXwegydFIU37ukTI 2Gk3265mgDLtNTIyAFwptOUs4AVfcHea4k4gXLm9foltiETFTYbut0ySuoruM3Sje0wv +8CZAA/Pjkt/59SxZOuLHm/8OzI9H3ViGuxV2xo+dDU/jAbyrxbT/zRbwMVG2YABscPo KRYLPAtg7SaDTKei4zdw/xBc+r08fMH57aCHVBVFp3uQso2R/9/5uRemvCRlrfl7Rmgr SNA/EUInxNXf7N1wHn1jq5b2wJda/krttuGLKw7RsS9/AHwRfzVZfOvonZ/eMat4Gmnw T2xA== 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=nFVdnuw+3o5G9R5rQeBbXGWA5P0kYYacFZZwVwvOqB4=; b=1qg0hgFwvCd+4OVZZnx1hlB5P8K7v1Rb4kikAtCjsVusjKwS0ByAfxzdHI8LggvrGY cID2sC+N4spQ4NPEgbva/7g27CvTS7aVVMOsJx0wUjpu1nWammN0J02tn4cKOzbq2Axn MTIwZzpfjpHaMLgpIZp3+k0rtaDc2/jMW7eZTnUHA/BgaQvK3+53XIqKcQOEbUgU3X4U CL86D7+xQPU9/fJj11LrSSzzrvx2yNcI7Bfu4ItEX8v/CC5UAYp4h0opijeafgBnZSkC 9Ve7gkqq/uz+jyBMGGRh9EegiK1FBAV1llmBUgF+sLp5WGE0CUyMrp/p2lzwMyScj/Ky W8+Q== X-Gm-Message-State: AOAM533/iUh47lQdZUPwaaAyfhB5xrK1Iw1jpipGMtb4Q0wRD8y/7Zzf WDsdeWNo4Y9JTmrxi8ZZGkORtA== X-Google-Smtp-Source: ABdhPJz4sOBktV+pwqvdsj3bp1TswVB2eUCWxYpk2D4ES5if6GZ3BNIDdn3Y8+YS/y20WTZ8TuW/tg== X-Received: by 2002:a2e:6e0d:0:b0:247:fc9c:284e with SMTP id j13-20020a2e6e0d000000b00247fc9c284emr13326088ljc.251.1648804776920; Fri, 01 Apr 2022 02:19:36 -0700 (PDT) From: Vladimir Sementsov-Ogievskiy X-Google-Original-From: Vladimir Sementsov-Ogievskiy To: qemu-block@nongnu.org Subject: [PATCH v2 4/7] util: add qemu-co-timeout Date: Fri, 1 Apr 2022 12:19:17 +0300 Message-Id: <20220401091920.287612-5-vsementsov@openvz.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220401091920.287612-1-vsementsov@openvz.org> References: <20220401091920.287612-1-vsementsov@openvz.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Host-Lookup-Failed: Reverse DNS lookup failed for 2a00:1450:4864:20::236 (failed) 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::236; envelope-from=vladimir.sementsov-ogievskiy@openvz.org; helo=mail-lj1-x236.google.com X-Spam_score_int: -4 X-Spam_score: -0.5 X-Spam_bar: / X-Spam_report: (-0.5 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, PDS_HP_HELO_NORDNS=0.659, RCVD_IN_DNSWL_NONE=-0.0001, RDNS_NONE=0.793, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=no 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: 1648805724380100001 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 --- include/qemu/coroutine.h | 13 ++++++ util/meson.build | 1 + util/qemu-co-timeout.c | 89 ++++++++++++++++++++++++++++++++++++++++ 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/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')) 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; +} --=20 2.35.1