chicken-users
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Chicken-users] continuing callback confusion


From: john
Subject: Re: [Chicken-users] continuing callback confusion
Date: Tue, 16 Jan 2007 19:15:18 +0000

You don't need easyffi in your example. A call to print out the value
of string-length could be added.

A more simplified example:

x2.scm

(define-external (foo (c-string x)) int (print (string-length x)))

(return-to-host)

----

y2.c

#include "chicken.h"
extern int foo(char* blah);
int main()
{
C_word k = CHICKEN_run(C_toplevel);
char x[6];
x[0] = 'h';
x[1] = 'o';
x[2] = 'w';
x[3] = 'd';
x[4] = 'y';
x[5] = '\0';

foo(x);

return 0;
}

On 16/01/07, Elliot Cuzzillo <address@hidden> wrote:
I've been trying for a while now to get a main C program to get
compiled with a Scheme program and call a Scheme callback. My current
attempt follows; I just modified one of the embedding examples, so I
wouldn't get the embedding part wrong.

x2.scm:
-------------
(require-extension srfi-18 easyffi)

(define-external (foo (c-string x)) int (string-length x))
(define (x) (+ 1 2));

(define m (make-mutex))

(define (t)
  (mutex-lock! m)
  (thread-sleep! 1)
  (print (thread-name (current-thread)))
  (mutex-unlock! m)
  (t) )

(thread-start! (make-thread t 'PING!))
(thread-start! (make-thread t 'PONG!))

(let loop ()
  (return-to-host)
  (thread-yield!)
  (loop) )


y2.c:
--------------
#include "chicken.h"
extern int foo(char* blah);
int main()
{
  C_word k = CHICKEN_run(C_toplevel);
  char x[6];
  x[0] = 'h';
  x[1] = 'o';
  x[2] = 'w';
  x[3] = 'd';
  x[4] = 'y';
  x[5] = 0;

  for(;;) {
    k = CHICKEN_continue(k);
    foo(x);
  }

  return 0;
}

-----------------
To compile:
csc x2.scm y2.c -embedded

-------------------
It segfaults whenever I have a call to foo(x) anywhere; I've tried
doing it outside the for loop, before the call to CHICKEN_continue, as
well as where it is now. It segfaults before printing PING!, which
seems odd.
I also tried it without the extern keyword.

I would very much like to know what I'm doing wrong, so that I can
finally call a Scheme function from C. I also think it would be good
to add an example of this to the wiki, because, unless I am being very
stupid, it is not completely obvious how to do this.

Thanks,
Elliot


_______________________________________________
Chicken-users mailing list
address@hidden
http://lists.nongnu.org/mailman/listinfo/chicken-users





reply via email to

[Prev in Thread] Current Thread [Next in Thread]