unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Greg Troxel <gdt@lexort.com>
To: Isaac Jurado <diptongo@gmail.com>
Cc: guile-devel@gnu.org
Subject: Re: C calling Scheme and garbage collection
Date: Thu, 27 Jun 2019 15:52:24 -0400	[thread overview]
Message-ID: <rmia7e2yfev.fsf@s1.lexort.com> (raw)
In-Reply-To: <CALqPu34qSydhR0e52P+6-efrmsoVBU2pSSEibAb1_5GynivYOQ@mail.gmail.com> (Isaac Jurado's message of "Thu, 27 Jun 2019 18:38:38 +0200")

Isaac Jurado <diptongo@gmail.com> writes:

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

I have been down this path before, with guile and with lua.  Basically,
if C (or non-scheme) has a pointer to a scheme object, then you need to
hold a logical reference for it and protect the scheme object, and when
the C pointer is dropped decrease the refcnt.

I am unclear on the details of how you have a ref that gc is made aware
of.  One way is to have a scheme array of the object and a count, and
have the code null out the object when the count goes to zero or
something like that.  But the point is that you need to have  a proxy in
the scheme world, visible to gc, when a pointer to a scheme object is
held outside of the scheme world.

Forcing gc is not going to be reliable.   If you have a reliable scheme,
gc can happen at any random time and things will be ok.



  reply	other threads:[~2019-06-27 19:52 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27 16:38 C calling Scheme and garbage collection Isaac Jurado
2019-06-27 19:52 ` Greg Troxel [this message]
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=rmia7e2yfev.fsf@s1.lexort.com \
    --to=gdt@lexort.com \
    --cc=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).