From 39b006b493530582d32559ecc1f074785f413cce Mon Sep 17 00:00:00 2001 From: Justin Ethier Date: Thu, 13 Aug 2020 18:04:04 -0400 Subject: [PATCH] Make multiple SCM calls --- examples/call-scm-from-c/full-with-gc.scm | 30 ++++++++++++----------- examples/call-scm-from-c/full.c | 12 ++++++--- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/examples/call-scm-from-c/full-with-gc.scm b/examples/call-scm-from-c/full-with-gc.scm index e9dec313..5bce8b42 100644 --- a/examples/call-scm-from-c/full-with-gc.scm +++ b/examples/call-scm-from-c/full-with-gc.scm @@ -7,36 +7,38 @@ (define lock (make-mutex)) (define *done* #f) -(define *dummy signal-done) ;; Hack to prevent optimizing out signal-done +(define *dummy* signal-done) ;; Hack to prevent optimizing out signal-done +(define *dummy2* sum-numbers) ;; Hack to prevent optimizing out signal-done (define-c start-c-thread "(void *data, int argc, closure _, object k)" "start_c_thread(data); return_closcall1(data, k, boolean_t); ") -;; TODO: this does not work right now, it crashes because we are not setup for GC! +(define (sum-numbers) + (let ((result 0)) + (for-each + (lambda (n) + (set! result (+ result n))) + (iota 1000)) + (lambda X + (list result 'result #(result))))) + +;(define (print-result ;; Signal (wait) that it is done, this is called from C (define (signal-done) ; obj) - (let ((result 0) - (obj #t)) - (for-each - (lambda (n) - (set! result (+ result n)) - ) - (iota 1000)) - - (write `(Called from C result is ,result set *done* to ,obj)) + (let ((obj #t)) + (write `(Called from C set *done* to ,obj)) (newline) (mutex-lock! lock) (set! *done* obj) - (mutex-unlock! lock) - (list list result '#()))) + (mutex-unlock! lock))) ;; More efficient to use a condition var here to signal ready, ;; but this is just an example (define (wait) - (thread-sleep! 1) + (thread-sleep! 0.1) (mutex-lock! lock) (define done *done*) (mutex-unlock! lock) diff --git a/examples/call-scm-from-c/full.c b/examples/call-scm-from-c/full.c index a6790ce2..e9607112 100644 --- a/examples/call-scm-from-c/full.c +++ b/examples/call-scm-from-c/full.c @@ -9,6 +9,7 @@ * that we wish to call into. */ extern object __glo_signal_91done; +extern object __glo_sum_91numbers; gc_thread_data local; @@ -23,11 +24,17 @@ void *Cyc_init_thread(object thread_and_thunk); */ void *c_thread(void *parent_thd) { + object obj; printf("Hello from C thread\n"); - sleep(1); printf("C calling into SCM\n"); - object obj = scm_call_with_gc(parent_thd, __glo_signal_91done, boolean_t); + obj = scm_call_with_gc(parent_thd, __glo_sum_91numbers, boolean_t); + + printf("C received: "); + Cyc_write(NULL, obj, stdout); + printf("\n"); + + obj = scm_call_with_gc(parent_thd, __glo_signal_91done, boolean_t); printf("C received: "); Cyc_write(NULL, obj, stdout); @@ -67,7 +74,6 @@ void after_call_scm(gc_thread_data *thd, int argc, object k, object result) int i; printf("after call scm %p result %p\n", &i, result); - mclosure0(clo, cleanup_and_return); object buf[1]; buf[0] = result; GC(thd, &clo, buf, 1);