From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Greg Troxel Newsgroups: gmane.lisp.guile.devel Subject: Re: C calling Scheme and garbage collection Date: Thu, 27 Jun 2019 15:52:24 -0400 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="191908"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (berkeley-unix) Cc: guile-devel@gnu.org To: Isaac Jurado Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Thu Jun 27 21:53:12 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 1hgaSD-000nct-Bv for guile-devel@m.gmane.org; Thu, 27 Jun 2019 21:53:09 +0200 Original-Received: from localhost ([::1]:54008 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgaSC-0005jV-0I for guile-devel@m.gmane.org; Thu, 27 Jun 2019 15:53:08 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37768) by lists.gnu.org with esmtp (Exim 4.86_2) (envelope-from ) id 1hgaRh-0005jC-1B for guile-devel@gnu.org; Thu, 27 Jun 2019 15:52:38 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hgaRf-00008B-76 for guile-devel@gnu.org; Thu, 27 Jun 2019 15:52:36 -0400 Original-Received: from s1.lexort.com ([71.19.148.97]:49241) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hgaRd-0008Vg-4E for guile-devel@gnu.org; Thu, 27 Jun 2019 15:52:34 -0400 Original-Received: by s1.lexort.com (Postfix, from userid 10853) id E364F410C29; Thu, 27 Jun 2019 15:52:24 -0400 (EDT) OpenPGP: id=098ED60E In-Reply-To: (Isaac Jurado's message of "Thu, 27 Jun 2019 18:38:38 +0200") X-detected-operating-system: by eggs.gnu.org: FreeBSD 8.x [fuzzy] X-Received-From: 71.19.148.97 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:19989 Archived-At: Isaac Jurado 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.