unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* foreign objects and the garbage collector
@ 2021-09-04 12:41 Tim Meehan
  2021-09-04 13:35 ` Vivien Kraus via General Guile related discussions
  0 siblings, 1 reply; 3+ messages in thread
From: Tim Meehan @ 2021-09-04 12:41 UTC (permalink / raw)
  To: guile-user

I was looking at writing something to interact with gpsd.
There is a shared memory interface, and a Unix socket interface. The
protocol spoken by gpsd is JSON, so I figured that I would just open up a
socket and start talking ... however ... really early on in the man page,
it says "it is a bad idea for clients to speak the protocol directly" (
https://gpsd.gitlab.io/gpsd/gpsd_json.html) presumably since the protocol
was likely to change? They didn't elaborate ...

There is a C interface described at (https://gpsd.gitlab.io/gpsd/libgps.html),
which has the familiar "open-and-get-opaque-thing" and
"close-opaque-thing-when-done" design.

I'd rather not compile anything in C, and just use the tools in Guile to
interact with libgps. Is there a way to get Guile's garbage collector to
call "gps_close" on the opaque structure returned by "gps_open"? Or is this
something that would be more handily implemented in a C extension?

Right now, because I'm just not concerned about sloppiness, I've just been
speaking the wire protocol, but I do feel bad about being sloppy and wanted
to clean it up for others to use perhaps.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: foreign objects and the garbage collector
  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
  0 siblings, 1 reply; 3+ messages in thread
From: Vivien Kraus via General Guile related discussions @ 2021-09-04 13:35 UTC (permalink / raw)
  To: Tim Meehan, guile-user

Hello,

Le samedi 04 septembre 2021 à 07:41 -0500, Tim Meehan a écrit :
> I'd rather not compile anything in C, and just use the tools in Guile to
> interact with libgps. Is there a way to get Guile's garbage collector to
> call "gps_close" on the opaque structure returned by "gps_open"?
I think it would be best to do it yourself. Maybe the garbage collector will not run immediately (if at all), and if each creation uses "precious" resources (such as file descriptors), you might run
into a shortage before the garbage collector is triggered. This is not considered by people writing the C API of course, because they assume you would close the thing as soon as possible. (Also, if
calling the close function is mandatory, for instance to run code that’s not just freeing resources, and the garbage collector does not have a chance to run, then it’s another problem. However, I
doubt your library does something like that). 

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.

That being said, make-pointer (from (system foreign-library)) is probably what you are expecting. It should work with gps_close.

Best regards,

Vivien




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: foreign objects and the garbage collector
  2021-09-04 13:35 ` Vivien Kraus via General Guile related discussions
@ 2021-09-05  0:58   ` Tim Meehan
  0 siblings, 0 replies; 3+ messages in thread
From: Tim Meehan @ 2021-09-05  0:58 UTC (permalink / raw)
  Cc: guile-user

Thanks Vivien, I had not considered dynamic-wind. I'll look into that a bit
more.

Cheers,
Tim

On Sat, Sep 4, 2021 at 8:35 AM Vivien Kraus <vivien@planete-kraus.eu> wrote:

> Hello,
>
> Le samedi 04 septembre 2021 à 07:41 -0500, Tim Meehan a écrit :
> > I'd rather not compile anything in C, and just use the tools in Guile to
> > interact with libgps. Is there a way to get Guile's garbage collector to
> > call "gps_close" on the opaque structure returned by "gps_open"?
> I think it would be best to do it yourself. Maybe the garbage collector
> will not run immediately (if at all), and if each creation uses "precious"
> resources (such as file descriptors), you might run
> into a shortage before the garbage collector is triggered. This is not
> considered by people writing the C API of course, because they assume you
> would close the thing as soon as possible. (Also, if
> calling the close function is mandatory, for instance to run code that’s
> not just freeing resources, and the garbage collector does not have a
> chance to run, then it’s another problem. However, I
> doubt your library does something like that).
>
> 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.
>
> That being said, make-pointer (from (system foreign-library)) is probably
> what you are expecting. It should work with gps_close.
>
> Best regards,
>
> Vivien
>
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-09-05  0:58 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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

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 NNTP newsgroup(s).