From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: adriano Newsgroups: gmane.lisp.guile.user Subject: Re: foreign objects and the garbage collector Date: Tue, 21 Sep 2021 16:51:30 +0200 Message-ID: References: <42b93d9ba50eceff320f3dde53ffd6ad77ab6070.camel@planete-kraus.eu> <87czp4h1un.fsf@laura> <5daca356c077a108d530b7a88d2361b4a0d63ce1.camel@riseup.net> <87czp2t384.fsf@laura> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="40109"; mail-complaints-to="usenet@ciao.gmane.io" To: Olivier Dion , guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Tue Sep 21 16:51:52 2021 Return-path: Envelope-to: guile-user@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mSh7f-000AC2-DA for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 16:51:51 +0200 Original-Received: from localhost ([::1]:53020 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mSh7e-0001ck-Bt for guile-user@m.gmane-mx.org; Tue, 21 Sep 2021 10:51:50 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:52060) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSh7R-0001cM-SG for guile-user@gnu.org; Tue, 21 Sep 2021 10:51:37 -0400 Original-Received: from mx1.riseup.net ([198.252.153.129]:46380) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mSh7P-0007Si-L7 for guile-user@gnu.org; Tue, 21 Sep 2021 10:51:37 -0400 Original-Received: from fews2.riseup.net (fews2-pn.riseup.net [10.0.1.84]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256 client-signature RSA-PSS (2048 bits) client-digest SHA256) (Client CN "fews2.riseup.net", Issuer "R3" (not verified)) by mx1.riseup.net (Postfix) with ESMTPS id 4HDPWp1YmBzF5c3; Tue, 21 Sep 2021 07:51:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1632235894; bh=lVE99yUlqKb9zCXI6fhaopFQ44XQJ/lLeYzdXrgdm4c=; h=Subject:From:To:Date:In-Reply-To:References:From; b=PIetfj6vx6CYxlxIdg2I1Pbcz47+eobTiztv0Z2ohgkErI9opT4vJvgrXe+DmGulB SBiSgIXx7WnEadrrUX/7w/RKV3GNYlJt1OdrmeamVL3+le7pge63M/9Bqml/d7pJV2 JYOgKHaB3i9+PyvJutwkU43xa7LAMcclzbMgnN/g= X-Riseup-User-ID: 8464B7769429FF6309C09E512593577205C926596A0DD60C1ED822B33F082B6F Original-Received: from [127.0.0.1] (localhost [127.0.0.1]) by fews2.riseup.net (Postfix) with ESMTPSA id 4HDPWn05Rqz1yQm; Tue, 21 Sep 2021 07:51:32 -0700 (PDT) In-Reply-To: <87czp2t384.fsf@laura> Received-SPF: pass client-ip=198.252.153.129; envelope-from=randomlooser@riseup.net; helo=mx1.riseup.net X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Original-Sender: "guile-user" Xref: news.gmane.io gmane.lisp.guile.user:17772 Archived-At: Il giorno mar, 21/09/2021 alle 10.25 -0400, Olivier Dion ha scritto: > On Tue, 21 Sep 2021, adriano wrote: > > Hi Olivier, > > > > thank you very much for your reply > > > > Il giorno dom, 19/09/2021 alle 14.11 -0400, Olivier Dion ha > > scritto: > > > On Sun, 19 Sep 2021, adriano wrote: > > > > > > > > > > > > > > > > > > > > It'd be so nice to have an example > > > > > > (define (with-my-resource token proc) > > >   (let ((resource #f)) > > >     (dynamic-wind > > >       (lambda () > > >         (set! resource (open-my-resource% token))) > > > > > >       (proc resource) > > > > > >       (lambda () > > >         (when resource > > >           (close-my-resource% resource)))))) > > > > > > (with-my-resource "some-internal-token" (lambda ())) > > > > Oh my, thank you for this ! > > > > This should be included in the manual ! > > > > The example that's there currently is totally indequate, in my > > opinion > > > > > > > > > > > > Says you have `open_my_resource()` and `close_my_resource()` in C > > > in > > > library "libfoo.so" where open_my_resource takes a C string and > > > returns > > > an integer for the resource while close_my_resource takes the > > > integer > > > of > > > the resource: > > > > > > (define open-my-resource% > > >   (eval-when (eval load compile) > > >     (let ((this-lib (load-foreign-library "libfoo"))) > > >       (foreign-library-function this-lib "open_my_resource" > > >                                 #:return-type int > > >                                 #:arg-types (list '*)))) > > > > > > (define open-my-resource% > > >   (eval-when (eval load compile) > > >     (let ((this-lib (load-foreign-library "libfoo"))) > > >       (foreign-library-function this-lib "open_my_resource" > > >                                 #:return-type int > > >                                 #:arg-types (list int))))) > > > > Uhmm... I see 2 versions of open-my-resource% > > The only slight difference I see is in the #:arg-types > > > > The first one has  > > > > (list '*)  > > > > and the second one has > > > > (list int) > > > > > > Maybe you you got confused while editing ? > > You're right.  I copy paste the form two times and forget to edit the > second > binding name.  I meant `close-my-resource%` for the second define. > > > > > Thank you again > > Just make sure to read what Maxime says about escape continuation. > Dynamic wind are not bullet proof. > > As the manual says: > >         If, any time during the execution of THUNK, the dynamic > extent of >         the ‘dynamic-wind’ expression is escaped non-locally, > OUT_GUARD is >         called.  If the dynamic extent of the dynamic-wind is re- > entered, >         IN_GUARD is called.  Thus IN_GUARD and OUT_GUARD may be > called any >         number of times. > > Thus, you could also do a `(set! resource #f)` after freeing it, so > that > the overall dynamic-wind is reentrant and you don't end up with > double > free of resource. > After seeing your example of usage of dinamic-wind, I understand what IN_GUARD and OUT_GUARD are supposed to be Before that it was quite nebulous Thanks again