From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxime Devos Newsgroups: gmane.lisp.guile.user Subject: Re: foreign objects and the garbage collector Date: Sun, 19 Sep 2021 20:23:07 +0200 Message-ID: <5461c5ead3388c6e678eb8028b1f5f1d268520bd.camel@telenet.be> References: <42b93d9ba50eceff320f3dde53ffd6ad77ab6070.camel@planete-kraus.eu> <87czp4h1un.fsf@laura> Mime-Version: 1.0 Content-Type: multipart/signed; micalg="pgp-sha512"; protocol="application/pgp-signature"; boundary="=-DTR+rpWN50JPoS8wZCx2" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="4318"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Evolution 3.34.2 To: Olivier Dion , adriano , guile-user Original-X-From: guile-user-bounces+guile-user=m.gmane-mx.org@gnu.org Sun Sep 19 20:25:04 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 1mS1Uu-0000yT-Q9 for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 20:25:04 +0200 Original-Received: from localhost ([::1]:50278 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mS1Us-0007kA-W6 for guile-user@m.gmane-mx.org; Sun, 19 Sep 2021 14:25:03 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:57890) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mS1TC-0005d6-7y for guile-user@gnu.org; Sun, 19 Sep 2021 14:23:18 -0400 Original-Received: from baptiste.telenet-ops.be ([2a02:1800:120:4::f00:13]:58078) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mS1T9-0004A5-RG for guile-user@gnu.org; Sun, 19 Sep 2021 14:23:17 -0400 Original-Received: from ptr-bvsjgyjmffd7q9timvx.18120a2.ip6.access.telenet.be ([IPv6:2a02:1811:8c09:9d00:aaf1:9810:a0b8:a55d]) by baptiste.telenet-ops.be with bizsmtp id vuPC250060mfAB401uPCRa; Sun, 19 Sep 2021 20:23:12 +0200 In-Reply-To: <87czp4h1un.fsf@laura> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r21; t=1632075792; bh=gE5fY8TnxMSqo/zapYF4Wk58Ppg0vo3WseNTUDchgIc=; h=Subject:From:To:Date:In-Reply-To:References; b=gt1qM14pd7TnPQg4T1xOMSuKDXAuZ10PcxxIDn7SPKrdtvO1TCOx6xWXknsGRuC79 TDHa7on19NDE0Or19rL1NooBZt2K4JcxXdeNdVeKACH8NdZszqmmNOk6ct76Xccj7A m8foZ0won0aKReUPSRY609F+7MLwaNh9olE9ayYw7+sYtmeHRxfa36PQld4JLaGq+w ulV1AvIdkpiyKfS/dz6dLF2w/XAw1YSnI4nlzka14/0Wx4/Biw7Fk9B6XothKO8Zi4 ZqWS2PKpP5O0ESIdJWen0K0UZF9uZe5kWQZYJNSNDCHHMQxuO8GSu7im+fq24zcdwh rgD0Wsj5GzbQA== Received-SPF: pass client-ip=2a02:1800:120:4::f00:13; envelope-from=maximedevos@telenet.be; helo=baptiste.telenet-ops.be 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, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=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:17761 Archived-At: --=-DTR+rpWN50JPoS8wZCx2 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Olivier Dion via General Guile related discussions schreef op zo 19-09-2021= om 14:11 [-0400]: > On Sun, 19 Sep 2021, adriano wrote: > > > If you want to avoid the problem, you should explicitely bind and > > > call the gps-close function and not rely on the garbage collector to > > > do it for you. You can use dynamic-wind to open and close > > > resources as needed. > >=20 > > It'd be so nice to have an example >=20 > (define (with-my-resource token proc) > (let ((resource #f)) > (dynamic-wind > (lambda () > (set! resource (open-my-resource% token))) >=20 > (proc resource) >=20 > (lambda () > (when resource > (close-my-resource% resource)))))) >=20 > (with-my-resource "some-internal-token" (lambda ())) FWIW, this doesn't work well with continuations, e.g. if you use guile-fibe= rs for concurrency. I'm not familiar with 'libgps', so I'm not sure if it wou= ld work for you, but another method for implementing with-my-resource could be= to use the exception handling mechanism to call 'close-my-resource' on both normal exits and abnormal exits. Note that this alternative method does _not_ close the resource when callin= g an escape continuation (let/ec (with-my-resource .. (lambda (r) (ec) (unre= achable)))), so you might want to use something like guardians and after-gc-hook to even= tually free the resource. (Here, close-my-resource is like close-my-resource% except it doesn't do an= ything if the resource is already closed.) Greetings, Maxime. --=-DTR+rpWN50JPoS8wZCx2 Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- iI0EABYKADUWIQTB8z7iDFKP233XAR9J4+4iGRcl7gUCYUeACxccbWF4aW1lZGV2 b3NAdGVsZW5ldC5iZQAKCRBJ4+4iGRcl7qL9AP4nkcWvP7Nx41jE5oeTqF1V+kzS KHGX0pBpXbh2DzZakgEA3P+xetLh1JomBdiiprafvxvWqZ4sU5+/+QCrHz1RgQk= =eVqw -----END PGP SIGNATURE----- --=-DTR+rpWN50JPoS8wZCx2--