From nobody Fri Dec 19 04:01:21 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; 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=pass(p=none dis=none) header.from=gmail.com ARC-Seal: i=1; a=rsa-sha256; t=1744791964; cv=none; d=zohomail.com; s=zohoarc; b=ByUuhaqsERoeC76vPpnYbD+JERfjbbctSyjHLOPJHJRdlUZwHViJ++tNNEwWKjleqLgy7tmnV927tW2lS93dxd1Bm5Ymav+y50mVajtU05yyt9XcAoW6FiJhWBlUqTtoZJDPLrl3EPoUrMWpjgRawMIC9vwueIemYcHV//sWDKI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1744791964; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To; bh=oIbAMZeonD1y/THuAvolLW7txmOpN+fF9GLLZzGqdZQ=; b=JLkvW78r+0TCICZTn5cnia1lCSgkB+NlOS+otRohX/4W6E8m8dV6BzrLPZlxWUthtDovksq/QFAx2l5yuNc2B4/hUDXMVYfQdmSL3ItIZ0xpXO9Se+XkeAoNlom0DZy4tQ+74Mm91TzmXmYZGbPYIIwgEoVzwLQ984SuRrI1VWI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; 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=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1744791964695541.0053256186718; Wed, 16 Apr 2025 01:26:04 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1u4xzy-00067W-FA; Wed, 16 Apr 2025 04:20:06 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1u4xxJ-0002Xl-3G; Wed, 16 Apr 2025 04:17:29 -0400 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1u4xxG-0005ye-C1; Wed, 16 Apr 2025 04:17:12 -0400 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-223fb0f619dso70489485ad.1; Wed, 16 Apr 2025 01:17:08 -0700 (PDT) Received: from localhost.localdomain ([240d:1a:3b6:8b00:ee5a:e672:7d4c:d43]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-73bd21c3263sm9839445b3a.38.2025.04.16.01.16.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 16 Apr 2025 01:17:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1744791427; x=1745396227; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oIbAMZeonD1y/THuAvolLW7txmOpN+fF9GLLZzGqdZQ=; b=J/a+dx+JDpy+x8eWyQwXDmNCJ4+LX4g7/bvCenpJO6zsPIqtV+H/28k0kTv8WIhbxa Msfl787l8FwOTPa0x9qasqQ0gbTCnVsO12tghR3dWpkeH1prUEqmzxAOwr9ctGDfmJyE 3b0Pc186N8c54tGgDsvSjH69FxTHOM7zY9NxnN7LEC1yJTKfPJXOhhGJLbLSotmxR3km ggoqU+wVV7DmWQGKRnsnW1n+EEeCrkyFBAJDom7fpGBHbac2kBZ4pwkKnt4+jrDT0BOr b3MVdRvxmC3uViWMLwJZ1KpP+OhHEDfiNW87rWR/c1X5CStQ8Tb2ADWZ1SfhzXqiy471 jRsg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1744791427; x=1745396227; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=oIbAMZeonD1y/THuAvolLW7txmOpN+fF9GLLZzGqdZQ=; b=XAMBAoSlNlPyi3F04I9ahiJ5FBbDNddPiDDy0X1FUbbhHpGARcRxlZJfd+6RQ5bBN4 rQ2vpF6VJ4hWi1qWwuhk0kutgxf60eTJqRE27HQQG383LrlaVEWnVZcVHcqalY1oDWcM hbTmO8aFlAyrCs7ZlYxIeYC1DyDoHNvWdlFFj32oUoNLLyVf8catis9oPUx9ZTxGXSSo FBY2ljCaE6vKiV/LuHVNn7sJBjvynVoMOwGSo4xzW88hA76kH2/7iVEKzhLq89XZKbQw /BQdz6f1oJsFpzkBfxN1TSAaMdMmY+CVg1dB9lT0Ccka1l2DEmHYULcFSn4xdtam8Csp 1FZg== X-Forwarded-Encrypted: i=1; AJvYcCV//IyruN3q0KxvMGjQU0xhixoF7UHO+CPTyAiWN5jlkB8BWisKcSKJRRZvQWePMf/M66/z/N8mkjzxLQ==@nongnu.org, AJvYcCVUKyuMqxuX92DB8m32rf9qYEGcT5n7iJ9CNOKIB+D5yJfrAvacMziu27QQpqj3xYL7iuRxmbY1+o4=@nongnu.org, AJvYcCWijqgLNk2s0MKVDObAnczwE9aWjoaSLCnLKFYQ2tkOcXPvFAuOHa4x/XuBP5IAQd+bHBV2xkGgCA==@nongnu.org, AJvYcCXOLfAioAemkqobYc8ZQ1xDoYUKn1wQ/m2mGZDlqhTnD52OEm3EcMdjunhHu05nm8VeJhbkuiPsyEyFkw==@nongnu.org X-Gm-Message-State: AOJu0YxaA6aG8e9wjLbN5R3bcq/XvdSH+b8e6Hy5Q+LlFKQ7ZdcUz+Tw 1xBfl3euhvJP9tHYAew2j2XKFna9pdaJdJC0brQ1RcZcsoBJpgut7j/RsANL X-Gm-Gg: ASbGncslSF36zWQqoVZbd3S5FPC5zqGmnVRmDoywEwCy4i9ScM65PIT4BJAmSnanG/p /DwXhLfpdr8QJcZS2Tko4rZ+JqTLQoRiiwZgn8piMy3iy1VWPAi4Mqax67SNEeLKt1djuAf/unl 8skNQ1NUSn4lqiUOfKMVWCrJUNdpURHrslm//GJr6qTUKAsNFL/obXEFIXQoMWKIxFOXn7NSmfr LUeU7eqUxX0mdKhZqvFyGlFCIVr452DFnf5i+sxopi9YOhoj+6d8O+TOvJNVik3hc1Pxy74cW5d sln4vifLyKPYggKHcLrEMJ4nGP4K6l3xBVegHhz6Fxj2NdIGfDKO+A3DQg== X-Google-Smtp-Source: AGHT+IFYdhSnB9zITJ1aITjDAOjnlnlic7rRWk+WIB51c/0DcssgTKezLLv7xrxaBupxkzncoPYm3w== X-Received: by 2002:a17:902:ef01:b0:223:517c:bfa1 with SMTP id d9443c01a7336-22c3597ec25mr17871905ad.38.1744791426992; Wed, 16 Apr 2025 01:17:06 -0700 (PDT) From: Kohei Tokunaga To: qemu-devel@nongnu.org Cc: =?UTF-8?q?Alex=20Benn=C3=A9e?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= , Thomas Huth , Kevin Wolf , Hanna Reitz , Kohei Tokunaga , Paolo Bonzini , Alexandre Iooss , Mahmoud Mandour , Pierrick Bouvier , Pavel Pisa , Francisco Iglesias , Vikram Garhwal , Jason Wang , =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Eduardo Habkost , Peter Maydell , Zhao Liu , Nicholas Piggin , Daniel Henrique Barboza , David Hildenbrand , Richard Henderson , Ilya Leoshkevich , Stefan Hajnoczi , qemu-block@nongnu.org, qemu-arm@nongnu.org, qemu-ppc@nongnu.org, qemu-s390x@nongnu.org Subject: [PATCH 16/19] util: Add coroutine backend for emscripten Date: Wed, 16 Apr 2025 17:14:21 +0900 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: 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=2607:f8b0:4864:20::630; envelope-from=ktokunaga.mail@gmail.com; helo=mail-pl1-x630.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 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: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @gmail.com) X-ZM-MESSAGEID: 1744791965954019100 Content-Type: text/plain; charset="utf-8" Emscripten does not support couroutine methods currently used by QEMU but provides a coroutine implementation called "fiber". This commit introduces a coroutine backend using fiber. Note that fiber does not support submitting coroutines to other threads. Signed-off-by: Kohei Tokunaga --- util/coroutine-wasm.c | 127 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 util/coroutine-wasm.c diff --git a/util/coroutine-wasm.c b/util/coroutine-wasm.c new file mode 100644 index 0000000000..cb1ec92509 --- /dev/null +++ b/util/coroutine-wasm.c @@ -0,0 +1,127 @@ +/* + * emscripten fiber coroutine initialization code + * based on coroutine-ucontext.c + * + * Copyright (C) 2006 Anthony Liguori + * Copyright (C) 2011 Kevin Wolf + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.0 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see . + */ + +#include "qemu/osdep.h" +#include "qemu/coroutine_int.h" +#include "qemu/coroutine-tls.h" + +#include + +typedef struct { + Coroutine base; + void *stack; + size_t stack_size; + + void *asyncify_stack; + size_t asyncify_stack_size; + + CoroutineAction action; + + emscripten_fiber_t fiber; +} CoroutineEmscripten; + +/** + * Per-thread coroutine bookkeeping + */ +QEMU_DEFINE_STATIC_CO_TLS(Coroutine *, current); +QEMU_DEFINE_STATIC_CO_TLS(CoroutineEmscripten *, leader); +size_t leader_asyncify_stack_size =3D COROUTINE_STACK_SIZE; + +static void coroutine_trampoline(void *co_) +{ + Coroutine *co =3D co_; + + while (true) { + co->entry(co->entry_arg); + qemu_coroutine_switch(co, co->caller, COROUTINE_TERMINATE); + } +} + +Coroutine *qemu_coroutine_new(void) +{ + CoroutineEmscripten *co; + + co =3D g_malloc0(sizeof(*co)); + + co->stack_size =3D COROUTINE_STACK_SIZE; + co->stack =3D qemu_alloc_stack(&co->stack_size); + + co->asyncify_stack_size =3D COROUTINE_STACK_SIZE; + co->asyncify_stack =3D g_malloc0(co->asyncify_stack_size); + emscripten_fiber_init(&co->fiber, coroutine_trampoline, &co->base, + co->stack, co->stack_size, co->asyncify_stack, + co->asyncify_stack_size); + + return &co->base; +} + +void qemu_coroutine_delete(Coroutine *co_) +{ + CoroutineEmscripten *co =3D DO_UPCAST(CoroutineEmscripten, base, co_); + + qemu_free_stack(co->stack, co->stack_size); + g_free(co->asyncify_stack); + g_free(co); +} + +CoroutineAction qemu_coroutine_switch(Coroutine *from_, Coroutine *to_, + CoroutineAction action) +{ + CoroutineEmscripten *from =3D DO_UPCAST(CoroutineEmscripten, base, fro= m_); + CoroutineEmscripten *to =3D DO_UPCAST(CoroutineEmscripten, base, to_); + + set_current(to_); + to->action =3D action; + emscripten_fiber_swap(&from->fiber, &to->fiber); + return from->action; +} + +Coroutine *qemu_coroutine_self(void) +{ + Coroutine *self =3D get_current(); + + if (!self) { + CoroutineEmscripten *leaderp =3D get_leader(); + if (!leaderp) { + leaderp =3D g_malloc0(sizeof(*leaderp)); + leaderp->asyncify_stack =3D g_malloc0(leader_asyncify_stack_si= ze); + leaderp->asyncify_stack_size =3D leader_asyncify_stack_size; + emscripten_fiber_init_from_current_context( + &leaderp->fiber, + leaderp->asyncify_stack, + leaderp->asyncify_stack_size); + leaderp->stack =3D leaderp->fiber.stack_limit; + leaderp->stack_size =3D + leaderp->fiber.stack_base - leaderp->fiber.stack_limit; + set_leader(leaderp); + } + self =3D &leaderp->base; + set_current(self); + } + return self; +} + +bool qemu_in_coroutine(void) +{ + Coroutine *self =3D get_current(); + + return self && self->caller; +} --=20 2.25.1