unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Maxime Devos <maximedevos@telenet.be>
To: Olivier Dion <olivier.dion@polymtl.ca>,
	adriano <randomlooser@riseup.net>,
	 guile-user <guile-user@gnu.org>
Subject: Re: foreign objects and the garbage collector
Date: Sun, 19 Sep 2021 20:23:07 +0200	[thread overview]
Message-ID: <5461c5ead3388c6e678eb8028b1f5f1d268520bd.camel@telenet.be> (raw)
In-Reply-To: <87czp4h1un.fsf@laura>

[-- Attachment #1: Type: text/plain, Size: 1546 bytes --]

Olivier Dion via General Guile related discussions schreef op zo 19-09-2021 om 14:11 [-0400]:
> On Sun, 19 Sep 2021, adriano <randomlooser@riseup.net> 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.
> > 
> > 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 ()))

FWIW, this doesn't work well with continuations, e.g. if you use guile-fibers
for concurrency.  I'm not familiar with 'libgps', so I'm not sure if it would
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 calling
an escape continuation (let/ec (with-my-resource  .. (lambda (r) (ec) (unreachable)))),
so you might want to use something like guardians and after-gc-hook to eventually
free the resource.

(Here, close-my-resource is like close-my-resource% except it doesn't do anything
if the resource is already closed.)

Greetings,
Maxime.

[-- Attachment #2: This is a digitally signed message part --]
[-- Type: application/pgp-signature, Size: 260 bytes --]

  reply	other threads:[~2021-09-19 18:23 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-04 12:41 foreign objects and the garbage collector Tim Meehan
2021-09-04 13:35 ` Vivien Kraus via General Guile related discussions
2021-09-05  0:58   ` Tim Meehan
2021-09-19  6:14   ` adriano
2021-09-19 18:11     ` Olivier Dion via General Guile related discussions
2021-09-19 18:23       ` Maxime Devos [this message]
2021-09-21 14:26         ` adriano
2021-09-21 18:49           ` Maxime Devos
2021-09-21 14:00       ` adriano
2021-09-21 14:25         ` Olivier Dion via General Guile related discussions
2021-09-21 14:51           ` adriano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=5461c5ead3388c6e678eb8028b1f5f1d268520bd.camel@telenet.be \
    --to=maximedevos@telenet.be \
    --cc=guile-user@gnu.org \
    --cc=olivier.dion@polymtl.ca \
    --cc=randomlooser@riseup.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).