A library binding a blocking native call (accept/recv/connect/...) needs it emitted __collect_safe so the thread deactivates for the call and doesn't pin the stop-the-world collector. foreign-fn / defcfn take an optional trailing :blocking; the backend emits (foreign-procedure __collect_safe ...). Needed for the ring-janet-adapter socket-server port. ffi-binding-test asserts a thread parked in a :blocking call doesn't block (collect).
52 lines
2.5 KiB
Scheme
52 lines
2.5 KiB
Scheme
;; jolt.ffi regression: a compile-time-typed foreign binding lowers to a real
|
|
;; Chez foreign-procedure and calls native code. Run:
|
|
;; chez --script test/chez/ffi-binding-test.ss
|
|
;; Binds a few libc functions (process symbols, always present) through the
|
|
;; jolt.ffi/__cfn special form + the host memory primitives — the same path a
|
|
;; library uses to bind its native deps.
|
|
|
|
(import (chezscheme))
|
|
(load "host/chez/rt.ss")
|
|
(set-chez-ns! "clojure.core")
|
|
(load "host/chez/seed/prelude.ss")
|
|
(load "host/chez/post-prelude.ss")
|
|
(set-chez-ns! "user")
|
|
(load "host/chez/host-contract.ss")
|
|
(load "host/chez/seed/image.ss")
|
|
(load "host/chez/compile-eval.ss")
|
|
(load "host/chez/ffi.ss")
|
|
|
|
(define total 0) (define fails 0)
|
|
(define (ok name pred) (set! total (+ total 1)) (unless pred (set! fails (+ fails 1)) (printf "FAIL: ~a\n" name)))
|
|
;; eval one form (string) in `user`, like the loader does form-by-form, so a def
|
|
;; is visible to a later form.
|
|
(define (ev s) (jolt-compile-eval s "user"))
|
|
|
|
;; load libc (process symbols) and bind typed foreign functions
|
|
(ev "(jolt.ffi/load-library)")
|
|
(ev "(def c-strlen (jolt.ffi/__cfn \"strlen\" [:string] :size_t))")
|
|
(ev "(def c-abs (jolt.ffi/__cfn \"abs\" [:int] :int))")
|
|
|
|
(ok "foreign-procedure built for strlen" (procedure? (var-deref "user" "c-strlen")))
|
|
(ok "typed call: strlen(\"hello\") = 5" (= 5 (jnum->exact (ev "(c-strlen \"hello\")"))))
|
|
(ok "typed call: abs(-7) = 7" (= 7 (jnum->exact (ev "(c-abs -7)"))))
|
|
|
|
;; memory: alloc / write / read roundtrip through the host primitives
|
|
(ok "mem int roundtrip"
|
|
(= 4242 (jnum->exact
|
|
(ev "(let [p (jolt.ffi/alloc (jolt.ffi/sizeof :int))]
|
|
(jolt.ffi/write p :int 0 4242)
|
|
(let [v (jolt.ffi/read p :int)] (jolt.ffi/free p) v))"))))
|
|
(ok "sizeof :pointer is a word" (let ((n (jnum->exact (ev "(jolt.ffi/sizeof :pointer)")))) (or (= n 8) (= n 4))))
|
|
|
|
;; a :blocking foreign call is collect-safe: a thread parked in it must not pin
|
|
;; the stop-the-world collector. (collect) here would throw "cannot collect when
|
|
;; multiple threads are active" if usleep weren't emitted __collect_safe.
|
|
(ev "(def c-usleep (jolt.ffi/__cfn \"usleep\" [:uint] :int :blocking))")
|
|
(let ((usleep (var-deref "user" "c-usleep")))
|
|
(fork-thread (lambda () (usleep 2000000))) ; ~2s in a blocking call
|
|
(let loop ((i 0)) (when (fx<? i 30000000) (loop (fx+ i 1)))) ; spin so the thread enters usleep
|
|
(ok "blocking ffi call is collect-safe" (guard (e (#t #f)) (collect) #t)))
|
|
|
|
(printf "~a/~a passed~n" (- total fails) total)
|
|
(exit (if (zero? fails) 0 1))
|