;; ;; Test for threads ;; (use gauche.test) (use gauche.sequence) (use gauche.time) (use srfi.19) (add-load-path ".") (test-start "threads") (use gauche.threads) (test-module 'gauche.threads) ;;--------------------------------------------------------------------- (test-section "basic thread API") (test* "current-thread" #t (eq? (current-thread) (current-thread))) (test* "thread?" '(#t #f) (list (thread? (current-thread)) (thread? 'foo))) (test* "make-thread" #t (thread? (make-thread (^[] #f)))) (test* "thread-name" 'foo (thread-name (make-thread (^[] #f) 'foo))) (test* "thread-specific" "hello" (begin (thread-specific-set! (current-thread) "hello") (thread-specific (current-thread)))) (test* "thread-start!" "hello" (call-with-output-string (^p (let1 t (thread-start! (make-thread (^[] (display "hello" p)))) (thread-join! t))))) ;; calculate fibonacchi in awful way (define (mt-fib n) (let1 threads (make-vector n) (dotimes [i n] (set! (ref threads i) (make-thread (case i [(0) (^[] 1)] [(1) (^[] 1)] [else (^[] (+ (thread-join! (ref threads (- i 1))) (thread-join! (ref threads (- i 2)))))]) i))) (dotimes [i n] (thread-start! (ref threads (- n i 1)))) (thread-join! (ref threads (- n 1))))) (test* "thread-join!" 1346269 (mt-fib 31)) (let () (define (thread-sleep-test x) (test* (format "thread-sleep! ~s" x) #t (let1 c (make ) (with-time-counter c (thread-sleep! x)) (> (time-counter-value c) (- (if (is-a? x