unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Isaac Jurado <diptongo@gmail.com>
To: guile-devel@gnu.org
Subject: C calling Scheme and garbage collection
Date: Thu, 27 Jun 2019 18:38:38 +0200	[thread overview]
Message-ID: <CALqPu34qSydhR0e52P+6-efrmsoVBU2pSSEibAb1_5GynivYOQ@mail.gmail.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1793 bytes --]

Hello,

I'm playing with event loop libraries implemented in C (libev, libevent,
etc... in my case libsystemd), but configuring them from Guile.

The qsort example in the documentation [1] seems safe because the qsort C
function directly calls back, so the callback Scheme bindings stay
referenced (by the Scheme code calling qsort) during all the C code
execution.

Now, in C event loops the situation is different.  There is one call to
configure the event callback, in which the function and data pointers are
lent to the loop; and then there is the main loop or the single iteration
call.

The way I see it, suppose I add a timer.  I call one C function passing a
(proceudre->pointer) and an (scm->pointer).  In a future time, those
pointers will be used by the C event loop.  If a garbage collection happens
in the middle, the results of (procedure->pointer) and (scm->pointer) may
have been reclaimed by the time the C event loop calls back.

However, I've tried forcing (gc) between the two steps mentioned and it
looks to be working fine.  I have also reviewed some of the code [2][3] and
some additional weak references seem to be created.

I'll try a couple of combinations to see if I can "break" it, in order to
learn the limitations of C calling Scheme within Guile.  But if someone in
the list has experienced with this already, I would very glad to read some
advice about it.

Best regards.

References:

[1]
https://www.gnu.org/software/guile/manual/html_node/Dynamic-FFI.html#index-procedure_002d_003epointer
[2]
http://git.savannah.gnu.org/cgit/guile.git/tree/libguile/foreign.c?h=stable-2.2#n185
[3]
http://git.savannah.gnu.org/cgit/guile.git/tree/libguile/foreign.c?h=stable-2.2#n1080

-- 
Isaac Jurado

"The noblest pleasure is the joy of understanding"
Leonardo da Vinci

[-- Attachment #2: Type: text/html, Size: 2523 bytes --]

             reply	other threads:[~2019-06-27 16:38 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27 16:38 Isaac Jurado [this message]
2019-06-27 19:52 ` C calling Scheme and garbage collection Greg Troxel
2019-06-28 11:07   ` Isaac Jurado
2019-06-29 17:44     ` Greg Troxel
2019-06-30 12:17       ` David Pirotte
2019-06-30 20:05       ` Isaac Jurado
2019-06-30 12:51   ` Hans Åberg
2019-07-01  1:26 ` Mark H Weaver

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=CALqPu34qSydhR0e52P+6-efrmsoVBU2pSSEibAb1_5GynivYOQ@mail.gmail.com \
    --to=diptongo@gmail.com \
    --cc=guile-devel@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).