From debbugs-submit-bounces@debbugs.gnu.org Thu Oct 13 12:22:27 2022 Received: (at submit) by debbugs.gnu.org; 13 Oct 2022 16:22:27 +0000 Received: from localhost ([127.0.0.1]:34727 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oj0yZ-0006N3-4Q for submit@debbugs.gnu.org; Thu, 13 Oct 2022 12:22:27 -0400 Received: from lists.gnu.org ([209.51.188.17]:58256) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oj0yO-0006MZ-Fm for submit@debbugs.gnu.org; Thu, 13 Oct 2022 12:22:25 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:37522) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oj0yO-0003VF-8v for bug-guile@gnu.org; Thu, 13 Oct 2022 12:22:16 -0400 Received: from mail-m974.mail.163.com ([123.126.97.4]:54185) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oj0yI-00044z-3W for bug-guile@gnu.org; Thu, 13 Oct 2022 12:22:14 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version; bh=pG/Pc FDseFCJju06lHoAI1SCoBY8gojs1WvROK/3DYI=; b=QM2a4sKODCW5ljbpkicOl IGIn0Z1tp78s4h3HOcsYvn8QeaiBIPZx9QDKNXjaOk4pJ2JgClWytr4uPpVjT+x4 xxScjnHz1lE3zJv3XjIc+G5IZxb92mZPK4PJ5R8WJKyis87Yc8h3xvlS5is92l9P k1igLSi3LRVedNdMddPJsI= Received: from asus-laptop (unknown [163.125.249.49]) by smtp4 (Coremail) with SMTP id HNxpCgBXqIiTN0hjSBadkg--.76S2; Fri, 14 Oct 2022 00:06:46 +0800 (CST) User-agent: mu4e 1.8.9; emacs 29.0.50 From: Zhu Zihao To: bug-guile@gnu.org Subject: Foreign callback returned by procedure->pointer cannot be executed in foreign thread. Date: Fri, 14 Oct 2022 00:01:08 +0800 Message-ID: <86bkqfiu7s.fsf@163.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-CM-TRANSID: HNxpCgBXqIiTN0hjSBadkg--.76S2 X-Coremail-Antispam: 1Uf129KBjvdXoW7Jr1ftw4fGr13JrWrurWrGrg_yoWkWrc_WF WfAr4UJr47CF43Gw4akr1DAF4fWwnxZw1fXrW8tryxtry8K39xXFWv9rsFka13Wrn2vr98 CrW09FWY93WxWjkaLaAFLSUrUUUUUb8apTn2vfkv8UJUUUU8Yxn0WfASr-VFAUDa7-sFnT 9fnUUvcSsGvfC2KfnxnUUI43ZEXa7IUjqXd5UUUUU== X-Originating-IP: [163.125.249.49] X-CM-SenderInfo: pdoosuxxwbztlvw6il2tof0z/xtbBoQeZr2I0VKkzRgABsP Received-SPF: pass client-ip=123.126.97.4; envelope-from=all_but_last@163.com; helo=mail-m974.mail.163.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-Spam-Score: -1.6 (-) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -2.4 (--) --=-=-= Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha256; protocol="application/pgp-signature" --==-=-= Content-Type: text/plain The foreign function pointer returned by "procedure->pointer" can only executed in a thread which Scheme context is already initialized. This may not so convenient because foreign library may create its own thread and run our callback in foreign thread. To show what will happen, this is a small example using Guile FFI & pthread API to create a foreign thread and run foreign callback in it. ``` (begin (use-modules (rnrs bytevectors) (system foreign) (system foreign-library)) (define libc (load-foreign-library #f)) (define pthread_create (foreign-library-function libc "pthread_create" #:return-type int #:arg-types `(* * * *))) (define pthread-handle-ret (make-bytevector 4)) (define thread-runner (procedure->pointer '* (lambda (_) (display "I' m from a foreign thread!\n") %null-pointer) `(*))) (pthread_create (bytevector->pointer pthread-handle-ret) %null-pointer thread-runner %null-pointer)) ``` In Guile 3.0.8. This will cause segfault and crash. My patch to fix this problem is attached below. It wraps the real execution of foreign callback in "scm_with_guile". --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iIsEARYIADMWIQT4UAIrVkIEZilSHr2K2nJqP6LM8gUCY0g3hxUcYWxsX2J1dF9s YXN0QDE2My5jb20ACgkQitpyaj+izPJ16wD8Didfhvla24rMdHSM3v01gSu7J48D t0TjU/iHLs3LJJ4BAKqDTTEirF0eGZhG/THxjcCvftF17NWFegBnJFDUdVUA =Tob6 -----END PGP SIGNATURE----- --==-=-=-- --=-=-= Content-Type: text/x-patch Content-Disposition: inline; filename=0001-Allow-closure-returned-by-procedure-pointer-executed.patch >From c6ede90552019a5851e4ba0de41fa9dfd3269b38 Mon Sep 17 00:00:00 2001 From: Zhu Zihao Date: Fri, 14 Oct 2022 00:00:32 +0800 Subject: [PATCH] Allow closure returned by procedure->pointer executed in foreign thread. * libguile/foreign.c (invoke_closure): Move the core logci to "do_invoke_closure". Wrap it by "scm_with_guile". (do_invoke_closure): New function. --- libguile/foreign.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/libguile/foreign.c b/libguile/foreign.c index 1f594b0e4..2f9a8469a 100644 --- a/libguile/foreign.c +++ b/libguile/foreign.c @@ -1148,13 +1148,19 @@ scm_i_foreign_call (SCM cif_scm, SCM pointer_scm, int *errno_ret, #ifdef FFI_CLOSURES -/* Trampoline to invoke a libffi closure that wraps a Scheme - procedure. */ -static void -invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) +static void * +do_invoke_closure (void *outer_data) { + ffi_cif *cif; + void *ret, **args, *data; size_t i; SCM proc, *argv, result; + void ** outer_args = (void **) outer_data; + + cif = outer_args[0]; + ret = outer_args[1]; + args = outer_args[2]; + data = outer_args[3]; proc = SCM_PACK_POINTER (data); @@ -1167,6 +1173,21 @@ invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) result = scm_call_n (proc, argv, cif->nargs); unpack (cif->rtype, ret, result, 1); + + return NULL; +} + +/* Trampoline to invoke a libffi closure that wraps a Scheme + procedure. */ +static void +invoke_closure (ffi_cif *cif, void *ret, void **args, void *data) +{ + void *outer_args[4] = { cif, ret, args, data }; + + /* Foreign code may call this Scheme closure in a context which Guile is not + initialized. */ + scm_with_guile (do_invoke_closure, outer_args); + } SCM_DEFINE (scm_procedure_to_pointer, "procedure->pointer", 3, 0, 0, -- 2.38.0 --=-=-= Content-Type: text/plain -- Retrieve my PGP public key: gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC Zihao --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Tue Jan 24 08:21:49 2023 Received: (at 58498) by debbugs.gnu.org; 24 Jan 2023 13:21:49 +0000 Received: from localhost ([127.0.0.1]:56134 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pKJFF-0004Xm-5S for submit@debbugs.gnu.org; Tue, 24 Jan 2023 08:21:49 -0500 Received: from m12.mail.163.com ([220.181.12.196]:41064) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pKJFA-0004XM-VX for 58498@debbugs.gnu.org; Tue, 24 Jan 2023 08:21:48 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=q3EujCyGZegqipeoQtJgEQ0lwdVdbJEWgXcOzj9Hvek=; b=cBmW3XwBCOJtPT2JuRf2+SzcqfMaRWejiUWTwtWm3ys0qzd11VWzbxcKePOea4 LKrD+dxn13ghZUr67HXSoVsfvPe4N0uU3tZz3SZDzcO3RRUhQkbrE8oeaj+dQT95 yCx85bhKKiepfV9kdfeUYLa0MTttRAxlvsrn6ze1BUYDQ= Received: from asus-laptop (unknown [14.121.132.109]) by zwqz-smtp-mta-g0-3 (Coremail) with SMTP id _____wBnFNpZ289jw4cLBg--.44330S2; Tue, 24 Jan 2023 21:21:30 +0800 (CST) References: <86bkqfiu7s.fsf@163.com> User-agent: mu4e 1.8.13; emacs 30.0.50 From: Zhu Zihao To: 58498@debbugs.gnu.org Subject: Re: bug#58498: Acknowledgement (Foreign callback returned by procedure->pointer cannot be executed in foreign thread.) Date: Tue, 24 Jan 2023 21:20:46 +0800 In-reply-to: Message-ID: <863580vzja.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID: _____wBnFNpZ289jw4cLBg--.44330S2 X-Coremail-Antispam: 1Uf129KBjDUn29KB7ZKAUJUUUUU529EdanIXcx71UUUUU7v73 VFW2AGmfu7bjvjm3AaLaJ3UbIYCTnIWIevJa73UjIFyTuYvj4RgjjkUUUUU X-Originating-IP: [14.121.132.109] X-CM-SenderInfo: pdoosuxxwbztlvw6il2tof0z/xtbBZhwBr1aECO996QAAs1 X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 58498 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Ping. Can someone hear me? -- Retrieve my PGP public key: gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC Zihao