unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: Isaac Jurado <diptongo@gmail.com>
To: Greg Troxel <gdt@lexort.com>
Cc: guile-devel@gnu.org
Subject: Re: C calling Scheme and garbage collection
Date: Fri, 28 Jun 2019 13:07:53 +0200	[thread overview]
Message-ID: <CALqPu34W=e-JQ_dHBf21XhOeeqX+w0C-Jgx80JKb+83XcExUjA@mail.gmail.com> (raw)
In-Reply-To: <rmia7e2yfev.fsf@s1.lexort.com>


[-- Attachment #1.1: Type: text/plain, Size: 1822 bytes --]

On Thu, Jun 27, 2019 at 9:52 PM Greg Troxel <gdt@lexort.com> wrote:

>
> 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.
>

That's more or less what I had in mind, although instead of an array I
would use a hash table indexed by a fundamental type (e.g. integer) which
can be converted painlessly between Scheme and C.


> 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.
>

I prepared a minimal case of the kind of C interactions that I'm trying.
I'm attaching the files, the C code has to be compiled with:

gcc -shared -fPIC -o mysalsa.so mysalsa.c

Running the Scheme script yields something like the following:

Captured: Closure without collection
Argument: noitcelloc tuohtiw erusolC
Captured: Closure with garbate collected
Argument:

So primitive values seem to be garbage collected, but closures are treated
slightly differently.  This is very interesting, since working with
closures eliminates the need of those common "void *userdata" extra
arguments.

Testing continuation is going to be interesting too.

Best regards.

-- 
Isaac Jurado

"The noblest pleasure is the joy of understanding"
Leonardo da Vinci

[-- Attachment #1.2: Type: text/html, Size: 2630 bytes --]

[-- Attachment #2: mysalsa.c --]
[-- Type: text/x-csrc, Size: 257 bytes --]

typedef void (*callback_t) (void *);

static callback_t saved_callback;
static void *saved_arg;

void give (callback_t callback, void *arg)
{
        saved_callback = callback;
        saved_arg = arg;
}

void call ()
{
        saved_callback(saved_arg);
}

[-- Attachment #3: mysalsa.scm --]
[-- Type: text/x-scheme, Size: 755 bytes --]

(use-modules (system foreign))

(define lib (dynamic-link (string-append (getcwd) "/mysalsa")))
(define give (pointer->procedure void (dynamic-func "give" lib) '(* *)))
(define call (pointer->procedure void (dynamic-func "call" lib) '()))

(define (simple-closure capture)
  (let ([proc (lambda (arg)
                (display (string-append "Captured: " capture))
                (newline)
                (display (string-append "Argument: " (pointer->string arg)))
                (newline))]
        [arg (string-reverse capture)])
    (give (procedure->pointer void proc '(*)) (string->pointer arg))))


(simple-closure "Closure without collection")
(call)
(gc)

(simple-closure "Closure with garbate collected")
(gc)
(call)


             
  




  

  reply	other threads:[~2019-06-28 11:07 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-06-27 16:38 C calling Scheme and garbage collection Isaac Jurado
2019-06-27 19:52 ` Greg Troxel
2019-06-28 11:07   ` Isaac Jurado [this message]
2019-06-29 17:44     ` Greg Troxel
2019-06-30 12:17       ` David Pirotte
2019-06-30 20:05       ` Isaac Jurado
2019-06-30 12:51   ` Hans Åberg
2019-07-01  1:26 ` Mark H Weaver

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='CALqPu34W=e-JQ_dHBf21XhOeeqX+w0C-Jgx80JKb+83XcExUjA@mail.gmail.com' \
    --to=diptongo@gmail.com \
    --cc=gdt@lexort.com \
    --cc=guile-devel@gnu.org \
    /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).