Skip to content

Commit 12a2629

Browse files
committed
add thread support
1 parent b2c8699 commit 12a2629

3 files changed

Lines changed: 475 additions & 9 deletions

File tree

packages/cffi/cffi.ss

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@
4848
cffi-alloc
4949
cffi-free
5050
cffi-log
51-
51+
cffi-thread
5252
)
53-
(import (scheme) (utils libutil) (utils macro) )
53+
(import (scheme) (utils libutil) (utils macro) (thread scm-ffi) )
5454

5555
(define FFI_DEFAULT_ABI 2)
5656

@@ -84,9 +84,13 @@
8484

8585

8686
(define cffi-enable-log #f)
87+
(define cffi-enable-thread (threaded?))
8788
(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+
9094

9195

9296
(define lib-name
@@ -116,7 +120,7 @@
116120
;;(define $ffi-free (lambda (addr)
117121
;;(display (format "addr=~x\n" addr))))
118122

119-
123+
120124
(define (ffi-cif-alloc $ffi-alloc-list)
121125
(let ((m ($ffi-cif-alloc)))
122126
(set! $ffi-alloc-list(append! $ffi-alloc-list (list m) ))
@@ -250,6 +254,7 @@
250254

251255

252256

257+
253258
(define ffi-dlsym (foreign-procedure "ffi_dlsym" (void* string) void*))
254259
(define ffi-dlopen (foreign-procedure "ffi_dlopen" (string int ) void*))
255260
(define ffi-dlerror (foreign-procedure "ffi_dlerror" ( ) string ))
@@ -332,8 +337,6 @@
332337
))
333338
)
334339
)
335-
336-
337340

338341
;;(name1 name2 name3)
339342
;;(type1 type2 type3)
@@ -714,6 +717,10 @@
714717

715718
)
716719

720+
721+
722+
723+
717724
;;ffi call
718725
(define (cffi-call sym arg-type ret-type args )
719726
(if cffi-enable-log
@@ -739,23 +746,30 @@
739746
;;(display (format "ffi-prep-cif len=~a cret-type=~a carg-type=~a\n" (length arg-type) cret-type carg-type) )
740747
;;(display (test-float cif FFI_DEFAULT_ABI cret-type carg-type cargs) )
741748
;;init cif
749+
742750
(if (= FFI_OK (ffi-prep-cif cif FFI_DEFAULT_ABI (length arg-type) cret-type carg-type ) )
743751
(begin
744752
;;(printf "fptr=~a \n" fptr)
745753
(if (> fptr 0)
746-
(begin
754+
(begin
755+
(if cffi-enable-thread
756+
(deactivate-thread))
747757
;;(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))
749761
)
750762
(display (format "cannot find symbol ~a\n" sym ))
751763
))
752764
(error 'cffi (format "ffi-prep-cif return error\n"))
753765
)
766+
754767
;;(display (format "cret=~x\n" cret))
755768
(if (procedure? call-ret)
756769
(set! ret-val (call-ret cret))
757770
)
758771
(ffi-free-all alloc-list)
772+
759773
(if cffi-enable-log
760774
(display (format "ffi-call ret=~x\n" ret-val)))
761775
ret-val

packages/thread/scm-ffi.ss

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2+
;;作者:evilbinary on 2017-06-10 23:49:57.
3+
;;邮箱:rootdebug@163.com
4+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5+
(library (thread scm-ffi)
6+
(export
7+
activate-thread
8+
deactivate-thread
9+
slock-object
10+
sunlock-object
11+
)
12+
13+
(import (scheme) (utils libutil) )
14+
15+
(define lib-name
16+
(case (machine-type)
17+
((arm32le) "libscm.so")
18+
((a6nt i3nt ta6nt ti3nt) "libscm.dll")
19+
((a6osx i3osx ta6osx ti3osx) "libscm.so")
20+
((a6le i3le ta6le ti3le) "libscm.so")))
21+
(define lib (load-lib lib-name ))
22+
23+
;; (def-function activate-thread "Sactivate_thread"
24+
;; (void) int)
25+
26+
;; (def-function deactivate-thread "Sdeactivate_thread"
27+
;; (void) void)
28+
29+
30+
(define activate-thread
31+
(foreign-procedure "Sactivate_thread"
32+
() int))
33+
34+
(define deactivate-thread
35+
(foreign-procedure "Sdeactivate_thread"
36+
() void))
37+
38+
(define slock-object
39+
(foreign-procedure "Slock_object"
40+
() void))
41+
42+
(define sunlock-object
43+
(foreign-procedure"Sunlock_object"
44+
() void))
45+
46+
47+
48+
49+
)

0 commit comments

Comments
 (0)