unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
From: Zhu Zihao <all_but_last@163.com>
To: 58498@debbugs.gnu.org
Subject: bug#58498: Foreign callback returned by procedure->pointer cannot be executed in foreign thread.
Date: Fri, 14 Oct 2022 00:01:08 +0800	[thread overview]
Message-ID: <86bkqfiu7s.fsf@163.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 1340 bytes --]

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".


[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 255 bytes --]

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Allow-closure-returned-by-procedure-pointer-executed.patch --]
[-- Type: text/x-patch, Size: 1900 bytes --]

From c6ede90552019a5851e4ba0de41fa9dfd3269b38 Mon Sep 17 00:00:00 2001
From: Zhu Zihao <all_but_last@163.com>
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


[-- Attachment #3: Type: text/plain, Size: 100 bytes --]


-- 
Retrieve my PGP public key:

  gpg --recv-keys B3EBC086AB0EBC0F45E0B4D433DB374BCEE4D9DC

Zihao

             reply	other threads:[~2022-10-13 16:01 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-13 16:01 Zhu Zihao [this message]
     [not found] ` <handler.58498.B.166567814724498.ack@debbugs.gnu.org>
2023-01-24 13:20   ` bug#58498: Acknowledgement (Foreign callback returned by procedure->pointer cannot be executed in foreign thread.) Zhu Zihao

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=86bkqfiu7s.fsf@163.com \
    --to=all_but_last@163.com \
    --cc=58498@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).