From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Isaac Jurado Newsgroups: gmane.lisp.guile.devel Subject: C calling Scheme and garbage collection Date: Thu, 27 Jun 2019 18:38:38 +0200 Message-ID: Mime-Version: 1.0 Content-Type: multipart/alternative; boundary="000000000000e52efa058c50cba3" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="116962"; mail-complaints-to="usenet@blaine.gmane.org" To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Jun 27 18:47:21 2019 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1hgXYO-000UG7-C9 for guile-devel@m.gmane.org; Thu, 27 Jun 2019 18:47:21 +0200 Original-Received: from localhost ([::1]:52702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgXYN-0007x7-7Q for guile-devel@m.gmane.org; Thu, 27 Jun 2019 12:47:19 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:50776) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgXQE-0008E7-4Z for guile-devel@gnu.org; Thu, 27 Jun 2019 12:38:55 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hgXQC-0006Yl-Li for guile-devel@gnu.org; Thu, 27 Jun 2019 12:38:54 -0400 Original-Received: from mail-lj1-x234.google.com ([2a00:1450:4864:20::234]:32833) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1hgXQC-0006Vq-CD for guile-devel@gnu.org; Thu, 27 Jun 2019 12:38:52 -0400 Original-Received: by mail-lj1-x234.google.com with SMTP id h10so3067762ljg.0 for ; Thu, 27 Jun 2019 09:38:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:from:date:message-id:subject:to; bh=AyDnpDCQN+1ievb+MvTo4z6wkDzkkPXZHF76c2WWpU8=; b=V+bi60RTt9wHthUabV+wbMCqoo7GvSjHzfdwFu6+phDvxeHxcgrXxGbE+pw/ZaaSYV yyQs0MBtFUumL8CcFTMVGwvk4lp7Fk/f7rjbVH1mY7Hm2pXcT5eNbn09Fl+aE7q0hTVZ myb/2OEe64HM/KcFi4Tb0slY8MNKNZgsb9MjUD7ZHDvmduQ0/XRFi2Soy3Axhz5VeTv0 wPhzgefF6Qzn5b2ABgAdPxdyYmu9yHvWgz/7G+Z1nRq8feeAy4saofZmDa1hQ8ZhPeIG Lzjk9Q3Ofccg6cTMfKL8IHeml+PbhEbFlDEQEcJxsyeQX5x01aM4xpQ+d1TmZ6Utq/V9 9A/A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=AyDnpDCQN+1ievb+MvTo4z6wkDzkkPXZHF76c2WWpU8=; b=Wo0nVyxMyWSc84FzMQwJh6IWY3iP+lOpjX2E3NlPYo1VnBptyRzBCWb1ILbG/Q9IMY lIpllfbqw3194HQ4gv1Ezqhtkop2vRgkkP/2Qy4FYE4BsUxPtSKI5yZF09VpDD1zWZ2F HyQpZFqBsadK9zzDbU9XZsXvM7eM0m1xFigxatsPz2VOlALb8SriqaShj9izfk0/s9em oGy0t9C6wpsodQwi0UJjNchwkyuoyVq0fbQiTT0KZF7Eb3Kh3JNctXeLprazm2wZENlx Zgheh2v9vKMhD+peA9IXhN88U8JP0sg5LqcFrbkuVkikQu/KfUgGOZnZca6fyeOqHy4i 9qVQ== X-Gm-Message-State: APjAAAX8y53Mxt/XVBBTz+ef3HuDeY/TPyTuX+XVXF4Tj0OB2uWwbuqY wnkgfsRPUZRLju1iQnL6nabqviV5rRIqskDjsAWtNkZdbAgTjA== X-Google-Smtp-Source: APXvYqyG2ZJsQi/nwzyncaIcaclbAY13VwtayodffKlSjRoRb4y39Jw2q1vG8Id/2dGVaCQLKW1fUWVNWAzQ679+/jQ= X-Received: by 2002:a2e:87d0:: with SMTP id v16mr3271191ljj.24.1561653529618; Thu, 27 Jun 2019 09:38:49 -0700 (PDT) X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2a00:1450:4864:20::234 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.org gmane.lisp.guile.devel:19988 Archived-At: --000000000000e52efa058c50cba3 Content-Type: text/plain; charset="UTF-8" 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 --000000000000e52efa058c50cba3 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello,

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

The q= sort example in the documentation [1] seems safe because the qsort C functi= on 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.=C2=A0 Ther= e is one call to configure the event callback, in which the function and da= ta pointers are lent to the loop; and then there is the main loop or the si= ngle iteration call.

The way I see it, suppose I a= dd a timer.=C2=A0 I call one C function passing a (proceudre->pointer) a= nd an (scm->pointer).=C2=A0 In a future time, those pointers will be use= d by the C event loop.=C2=A0 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.=C2=A0 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 "brea= k" it, in order to learn the limitations of C calling Scheme within Gu= ile.=C2=A0 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/gu= ile/manual/html_node/Dynamic-FFI.html#index-procedure_002d_003epointer<= /div>[2] http://git.savannah.gnu.org/cgit/guile.git= /tree/libguile/foreign.c?h=3Dstable-2.2#n185

--
Isaac Jurado

"T= he noblest pleasure is the joy of understanding"
Leonardo da Vinci<= /div>
--000000000000e52efa058c50cba3--