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