|
48 | 48 | cffi-alloc |
49 | 49 | cffi-free |
50 | 50 | cffi-log |
51 | | - |
| 51 | + cffi-thread |
52 | 52 | ) |
53 | | - (import (scheme) (utils libutil) (utils macro) ) |
| 53 | + (import (scheme) (utils libutil) (utils macro) (thread scm-ffi) ) |
54 | 54 |
|
55 | 55 | (define FFI_DEFAULT_ABI 2) |
56 | 56 |
|
|
84 | 84 |
|
85 | 85 |
|
86 | 86 | (define cffi-enable-log #f) |
| 87 | + (define cffi-enable-thread (threaded?)) |
87 | 88 | (define (cffi-log t) |
88 | | - (set! cffi-enable-log t) |
89 | | - ) |
| 89 | + (set! cffi-enable-log t)) |
| 90 | + |
| 91 | + (define (cffi-thread t) |
| 92 | + (set! cffi-enable-thread t)) |
| 93 | + |
90 | 94 |
|
91 | 95 |
|
92 | 96 | (define lib-name |
|
116 | 120 | ;;(define $ffi-free (lambda (addr) |
117 | 121 | ;;(display (format "addr=~x\n" addr)))) |
118 | 122 |
|
119 | | - |
| 123 | + |
120 | 124 | (define (ffi-cif-alloc $ffi-alloc-list) |
121 | 125 | (let ((m ($ffi-cif-alloc))) |
122 | 126 | (set! $ffi-alloc-list(append! $ffi-alloc-list (list m) )) |
|
250 | 254 |
|
251 | 255 |
|
252 | 256 |
|
| 257 | + |
253 | 258 | (define ffi-dlsym (foreign-procedure "ffi_dlsym" (void* string) void*)) |
254 | 259 | (define ffi-dlopen (foreign-procedure "ffi_dlopen" (string int ) void*)) |
255 | 260 | (define ffi-dlerror (foreign-procedure "ffi_dlerror" ( ) string )) |
|
332 | 337 | )) |
333 | 338 | ) |
334 | 339 | ) |
335 | | - |
336 | | - |
337 | 340 |
|
338 | 341 | ;;(name1 name2 name3) |
339 | 342 | ;;(type1 type2 type3) |
|
714 | 717 |
|
715 | 718 | ) |
716 | 719 |
|
| 720 | + |
| 721 | + |
| 722 | + |
| 723 | + |
717 | 724 | ;;ffi call |
718 | 725 | (define (cffi-call sym arg-type ret-type args ) |
719 | 726 | (if cffi-enable-log |
|
739 | 746 | ;;(display (format "ffi-prep-cif len=~a cret-type=~a carg-type=~a\n" (length arg-type) cret-type carg-type) ) |
740 | 747 | ;;(display (test-float cif FFI_DEFAULT_ABI cret-type carg-type cargs) ) |
741 | 748 | ;;init cif |
| 749 | + |
742 | 750 | (if (= FFI_OK (ffi-prep-cif cif FFI_DEFAULT_ABI (length arg-type) cret-type carg-type ) ) |
743 | 751 | (begin |
744 | 752 | ;;(printf "fptr=~a \n" fptr) |
745 | 753 | (if (> fptr 0) |
746 | | - (begin |
| 754 | + (begin |
| 755 | + (if cffi-enable-thread |
| 756 | + (deactivate-thread)) |
747 | 757 | ;;(display (format "ffi-call cret=~x cargs=~x\n" cret cargs )) |
748 | | - (ffi-call cif fptr cret cargs) |
| 758 | + (ffi-call cif fptr cret cargs) |
| 759 | + (if cffi-enable-thread |
| 760 | + (activate-thread)) |
749 | 761 | ) |
750 | 762 | (display (format "cannot find symbol ~a\n" sym )) |
751 | 763 | )) |
752 | 764 | (error 'cffi (format "ffi-prep-cif return error\n")) |
753 | 765 | ) |
| 766 | + |
754 | 767 | ;;(display (format "cret=~x\n" cret)) |
755 | 768 | (if (procedure? call-ret) |
756 | 769 | (set! ret-val (call-ret cret)) |
757 | 770 | ) |
758 | 771 | (ffi-free-all alloc-list) |
| 772 | + |
759 | 773 | (if cffi-enable-log |
760 | 774 | (display (format "ffi-call ret=~x\n" ret-val))) |
761 | 775 | ret-val |
|
0 commit comments