From: Mark H Weaver <mhw@netris.org>
To: Isaac Jurado <diptongo@gmail.com>
Cc: guile-devel@gnu.org
Subject: Re: C calling Scheme and garbage collection
Date: Sun, 30 Jun 2019 21:26:24 -0400 [thread overview]
Message-ID: <8736jqk0jj.fsf@netris.org> (raw)
In-Reply-To: <CALqPu34qSydhR0e52P+6-efrmsoVBU2pSSEibAb1_5GynivYOQ@mail.gmail.com> (Isaac Jurado's message of "Thu, 27 Jun 2019 18:38:38 +0200")
Hi Isaac,
Isaac Jurado <diptongo@gmail.com> writes:
> 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.
That's right. When passing pointers to GC-allocated blocks to arbitrary
C code, you must take care to keep GC-visible references to those
pointers until they are no longer needed. This will not happen
automatically, unless the C code was specifically written to ensure that
the references will be visible to the GC.
BDW-GC does *not* scan arbitrary C data structures. In particular,
blocks allocated using C 'malloc' or C++ 'new' are not scanned.
> However, I've tried forcing (gc) between the two steps mentioned and
> it looks to be working fine.
As Greg mentioned, this isn't a sufficient test.
In general, I would avoid relying on experiments to determine what is
safe to do. Experiments can only tell you how the tested version(s)
behave. They cannot give you assurances about how future versions are
supposed to behave.
> I have also reviewed some of the code [2][3] and some additional weak
> references seem to be created.
Those weak references do not eliminate the need to keep GC-visible
references to the relevant objects. They merely ensure that *if* you
keep a reference to the returned pointer, the other associated data
structures are also kept alive.
Regards,
Mark
prev parent reply other threads:[~2019-07-01 1:26 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
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 [this message]
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=8736jqk0jj.fsf@netris.org \
--to=mhw@netris.org \
--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).