unofficial mirror of bug-guile@gnu.org 
 help / color / mirror / Atom feed
* bug#58498: Foreign callback returned by procedure->pointer cannot be executed in foreign thread.
@ 2022-10-13 16:01 Zhu Zihao
  0 siblings, 0 replies; only message in thread
From: Zhu Zihao @ 2022-10-13 16:01 UTC (permalink / raw)
  To: 58498


[-- 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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2022-10-13 16:01 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-13 16:01 bug#58498: Foreign callback returned by procedure->pointer cannot be executed in foreign thread Zhu Zihao

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