| 1 |
;; |
| 2 |
;; Test for threads |
| 3 |
;; |
| 4 |
|
| 5 |
(use gauche.test) |
| 6 |
(use gauche.sequence) |
| 7 |
(use gauche.time) |
| 8 |
(use srfi.19) |
| 9 |
(add-load-path ".") |
| 10 |
|
| 11 |
(test-start "threads") |
| 12 |
|
| 13 |
(use gauche.threads) |
| 14 |
(test-module 'gauche.threads) |
| 15 |
|
| 16 |
;;--------------------------------------------------------------------- |
| 17 |
(test-section "basic thread API") |
| 18 |
|
| 19 |
(test* "current-thread" #t |
| 20 |
(eq? (current-thread) (current-thread))) |
| 21 |
(test* "thread?" '(#t #f) |
| 22 |
(list (thread? (current-thread)) |
| 23 |
(thread? 'foo))) |
| 24 |
(test* "make-thread" #t |
| 25 |
(thread? (make-thread (^[] #f)))) |
| 26 |
(test* "thread-name" 'foo |
| 27 |
(thread-name (make-thread (^[] #f) 'foo))) |
| 28 |
(test* "thread-specific" "hello" |
| 29 |
(begin |
| 30 |
(thread-specific-set! (current-thread) "hello") |
| 31 |
(thread-specific (current-thread)))) |
| 32 |
(test* "thread-start!" "hello" |
| 33 |
(call-with-output-string |
| 34 |
(^p (let1 t (thread-start! (make-thread (^[] (display "hello" p)))) |
| 35 |
(thread-join! t))))) |
| 36 |
|
| 37 |
;; calculate fibonacchi in awful way |
| 38 |
(define (mt-fib n) |
| 39 |
(let1 threads (make-vector n) |
| 40 |
(dotimes [i n] |
| 41 |
(set! (ref threads i) |
| 42 |
(make-thread |
| 43 |
(case i |
| 44 |
[(0) (^[] 1)] |
| 45 |
[(1) (^[] 1)] |
| 46 |
[else (^[] (+ (thread-join! (ref threads (- i 1))) |
| 47 |
(thread-join! (ref threads (- i 2)))))]) |
| 48 |
i))) |
| 49 |
(dotimes [i n] |
| 50 |
(thread-start! (ref threads (- n i 1)))) |
| 51 |
(thread-join! (ref threads (- n 1))))) |
| 52 |
(test* "thread-join!" 1346269 (mt-fib 31)) |
| 53 |
|
| 54 |
(let () |
| 55 |
(define (thread-sleep-test x) |
| 56 |
(test* (format "thread-sleep! ~s" x) #t |
| 57 |
(let1 c (make <real-time-counter>) |
| 58 |
(with-time-counter c (thread-sleep! x)) |
| 59 |
(> (time-counter-value c) |
| 60 |
(- (if (is-a? x <time>) |
| 61 |
(time->seconds (time-difference (current-time) x)) |
| 62 |
x) |
| 63 |
0.01))))) |
| 64 |
|
| 65 |
(thread-sleep-test 0.02) |
| 66 |
(thread-sleep-test 1/50) |
| 67 |
(thread-sleep-test (add-duration (current-time) |
| 68 |
(make-time time-duration #e2e7 0))) |
| 69 |
) |
| 70 |
|
| 71 |
;; thread stop and cont |
| 72 |
(let1 t1 (make-thread (^[] (while #t (sys-nanosleep #e5e8)))) |
| 73 |
(test* "thread-status" 'new (thread-state t1)) |
| 74 |
(thread-start! t1) |
| 75 |
(test* "thread-status" 'runnable (thread-state t1)) |
| 76 |
(thread-stop! t1) |
| 77 |
(test* "thread-status" 'stopped (thread-state t1)) |
| 78 |
(thread-stop! t1) ; duplicate stop test |
| 79 |
(test* "thread-status" 'stopped (thread-state t1)) |
| 80 |
(thread-cont! t1) |
| 81 |
(test* "thread-status" 'runnable (thread-state t1)) |
| 82 |
(thread-terminate! t1) |
| 83 |
(test* "thread-status" 'terminated |
| 84 |
(guard (e [(<terminated-thread-exception> e) |
| 85 |
(thread-state t1)]) |
| 86 |
(thread-join! t1)))) |
| 87 |
|
| 88 |
;; thread termination via stopRequest |
| 89 |
(let1 t1 (thread-start! (make-thread (^[] (let loop () (loop))))) ; busy loop |
| 90 |
(test* "thread termination via stopRequest" 'terminated |
| 91 |
(guard (e [(<terminated-thread-exception> e) |
| 92 |
(thread-state t1)]) |
| 93 |
(thread-terminate! t1) |
| 94 |
(thread-join! t1)))) |
| 95 |
|
| 96 |
;; this SEGVs on 0.9.3.3. test code from @cryks. |
| 97 |
(test* "thread termination before running" 'terminated |
| 98 |
(let1 t1 (make-thread (^[] #f)) |
| 99 |
(thread-terminate! t1) |
| 100 |
(thread-state t1))) |
| 101 |
|
| 102 |
(test* "thread termination while being stopped" 'terminated |
| 103 |
(let1 t1 (thread-start! (make-thread (^[] (let loop () (loop))))) |
| 104 |
(thread-stop! t1) |
| 105 |
(thread-terminate! t1) |
| 106 |
(thread-state t1))) |
| 107 |
|
| 108 |
;;--------------------------------------------------------------------- |
| 109 |
(test-section "thread and error") |
| 110 |
|
| 111 |
(test* "uncaught-exception" #t |
| 112 |
(let1 t (make-thread (^[] (error "foo"))) |
| 113 |
(thread-start! t) |
| 114 |
(with-error-handler |
| 115 |
(^e (and (uncaught-exception? e) |
| 116 |
(is-a? (uncaught-exception-reason e) <error>))) |
| 117 |
(^[] (thread-join! t))))) |
| 118 |
|
| 119 |
(test* "uncaught-exception" #t |
| 120 |
(let1 t (make-thread (^[] (raise 4))) |
| 121 |
(thread-start! t) |
| 122 |
(with-error-handler |
| 123 |
(^e (and (uncaught-exception? e) |
| 124 |
(eqv? (uncaught-exception-reason e) 4))) |
| 125 |
(^[] (thread-join! t))))) |
| 126 |
|
| 127 |
(test* "uncaught-exception" #t |
| 128 |
(let1 t (make-thread (^[] (with-error-handler identity |
| 129 |
(^[] (error "foo"))))) |
| 130 |
(thread-start! t) |
| 131 |
(with-error-handler identity |
| 132 |
(^[] (is-a? (thread-join! t) <error>))))) |
| 133 |
|
| 134 |
;;--------------------------------------------------------------------- |
| 135 |
(test-section "basic mutex API") |
| 136 |
|
| 137 |
(test* "make-mutex" #t (mutex? (make-mutex))) |
| 138 |
(test* "mutex-name" 'foo (mutex-name (make-mutex 'foo))) |
| 139 |
|
| 140 |
(test* "mutex-specific" "hoge" |
| 141 |
(let1 m (make-mutex 'bar) |
| 142 |
(mutex-specific-set! m "hoge") |
| 143 |
(mutex-specific m))) |
| 144 |
|
| 145 |
(test* "lock and unlock - no blocking" #t |
| 146 |
(let1 m (make-mutex) |
| 147 |
(mutex-lock! m) |
| 148 |
(mutex-unlock! m))) |
| 149 |
|
| 150 |
(test* "mutex-state" |
| 151 |
(list 'not-abandoned (current-thread) 'not-owned 'not-abandoned) |
| 152 |
(let ([m (make-mutex)] |
| 153 |
[r '()]) |
| 154 |
(push! r (mutex-state m)) |
| 155 |
(mutex-lock! m) |
| 156 |
(push! r (mutex-state m)) |
| 157 |
(mutex-unlock! m) |
| 158 |
(mutex-lock! m #f #f) |
| 159 |
(push! r (mutex-state m)) |
| 160 |
(mutex-unlock! m) |
| 161 |
(push! r (mutex-state m)) |
| 162 |
(reverse r))) |
| 163 |
|
| 164 |
;; This test uses simple-minded spin lock, without using mutex timeouts |
| 165 |
;; nor condition variables. Not recommended way for real code. |
| 166 |
(test* "lock and unlock - blocking (simple spin-lock)" |
| 167 |
'((put a) (get a) (put b) (get b) (put c) (get c)) |
| 168 |
(let ([log '()] |
| 169 |
[cell #f] |
| 170 |
[m (make-mutex)]) |
| 171 |
(define (put! msg) |
| 172 |
(mutex-lock! m) |
| 173 |
(if cell |
| 174 |
(begin (mutex-unlock! m) (put! msg)) |
| 175 |
(begin (set! cell msg) |
| 176 |
(push! log `(put ,msg)) |
| 177 |
(mutex-unlock! m)))) |
| 178 |
(define (get!) |
| 179 |
(mutex-lock! m) |
| 180 |
(if cell |
| 181 |
(rlet1 r cell |
| 182 |
(set! cell #f) |
| 183 |
(push! log `(get ,r)) |
| 184 |
(mutex-unlock! m)) |
| 185 |
(begin (mutex-unlock! m) (get!)))) |
| 186 |
(define (producer) |
| 187 |
(put! 'a) |
| 188 |
(put! 'b) |
| 189 |
(put! 'c)) |
| 190 |
(define (consumer) |
| 191 |
(get!) |
| 192 |
(get!) |
| 193 |
(get!)) |
| 194 |
(let ([tp (thread-start! (make-thread producer 'producer))] |
| 195 |
[tc (thread-start! (make-thread consumer 'consumer))]) |
| 196 |
(thread-join! tp) |
| 197 |
(thread-join! tc) |
| 198 |
(reverse log)))) |
| 199 |
|
| 200 |
(test* "lock with timeout" |
| 201 |
'(#t #f #f #f #f #t #t) |
| 202 |
(let1 m (make-mutex) |
| 203 |
(let* ([r0 (mutex-lock! m)] |
| 204 |
[r1 (mutex-lock! m 0)] |
| 205 |
[r2 (mutex-lock! m 0.05)] |
| 206 |
[r3 (mutex-lock! m (seconds->time (+ (time->seconds (current-time)) 0.05)))] |
| 207 |
[r4 (mutex-lock! m (seconds->time (- (time->seconds (current-time)) 0.05)))] |
| 208 |
[r5 (mutex-unlock! m)] |
| 209 |
[r6 (mutex-lock! m 0)]) |
| 210 |
(mutex-unlock! m) |
| 211 |
(list r0 r1 r2 r3 r4 r5 r6)))) |
| 212 |
|
| 213 |
;; recursive mutex code taken from an example in SRFI-18 |
| 214 |
(test "recursive mutex" |
| 215 |
(list (current-thread) 0 'not-abandoned) |
| 216 |
(^[] |
| 217 |
(define (mutex-lock-recursively! mutex) |
| 218 |
(if (eq? (mutex-state mutex) (current-thread)) |
| 219 |
(let1 n (mutex-specific mutex) |
| 220 |
(mutex-specific-set! mutex (+ n 1))) |
| 221 |
(begin |
| 222 |
(mutex-lock! mutex) |
| 223 |
(mutex-specific-set! mutex 0)))) |
| 224 |
(define (mutex-unlock-recursively! mutex) |
| 225 |
(let1 n (mutex-specific mutex) |
| 226 |
(if (= n 0) |
| 227 |
(mutex-unlock! mutex) |
| 228 |
(mutex-specific-set! mutex (- n 1))))) |
| 229 |
(let1 m (make-mutex) |
| 230 |
(mutex-specific-set! m 0) |
| 231 |
(mutex-lock-recursively! m) |
| 232 |
(mutex-lock-recursively! m) |
| 233 |
(mutex-lock-recursively! m) |
| 234 |
(let1 r0 (mutex-state m) |
| 235 |
(mutex-unlock-recursively! m) |
| 236 |
(mutex-unlock-recursively! m) |
| 237 |
(let1 r1 (mutex-specific m) |
| 238 |
(mutex-unlock-recursively! m) |
| 239 |
(list r0 r1 (mutex-state m))))) |
| 240 |
)) |
| 241 |
|
| 242 |
;;--------------------------------------------------------------------- |
| 243 |
(test-section "condition variables") |
| 244 |
|
| 245 |
(test* "make-condition-variable" #t |
| 246 |
(condition-variable? (make-condition-variable))) |
| 247 |
|
| 248 |
(test* "condition-variable-name" 'foo |
| 249 |
(condition-variable-name (make-condition-variable 'foo))) |
| 250 |
|
| 251 |
(test* "condition-variable-specific" "hello" |
| 252 |
(let1 c (make-condition-variable 'foo) |
| 253 |
(condition-variable-specific-set! c "hello") |
| 254 |
(condition-variable-specific c))) |
| 255 |
|
| 256 |
;; Producer-consumer model using condition variable. |
| 257 |
(test* "condition-variable-signal!" |
| 258 |
'((put a) (get a) (put b) (get b) (put c) (get c)) |
| 259 |
(let ([log '()] |
| 260 |
[cell #f] |
| 261 |
[m (make-mutex)] |
| 262 |
[put-cv (make-condition-variable)] |
| 263 |
[get-cv (make-condition-variable)]) |
| 264 |
(define (put! msg) |
| 265 |
(mutex-lock! m) |
| 266 |
(if cell |
| 267 |
(begin (mutex-unlock! m put-cv) (put! msg)) |
| 268 |
(begin (set! cell msg) |
| 269 |
(push! log `(put ,msg)) |
| 270 |
(condition-variable-signal! get-cv) |
| 271 |
(mutex-unlock! m)))) |
| 272 |
(define (get!) |
| 273 |
(mutex-lock! m) |
| 274 |
(if cell |
| 275 |
(rlet1 r cell |
| 276 |
(set! cell #f) |
| 277 |
(push! log `(get ,r)) |
| 278 |
(condition-variable-signal! put-cv) |
| 279 |
(mutex-unlock! m)) |
| 280 |
(begin |
| 281 |
(mutex-unlock! m get-cv) (get!)))) |
| 282 |
(define (producer) |
| 283 |
(put! 'a) |
| 284 |
(put! 'b) |
| 285 |
(put! 'c)) |
| 286 |
(define (consumer) |
| 287 |
(get!) |
| 288 |
(get!) |
| 289 |
(get!)) |
| 290 |
(let ([tp (thread-start! (make-thread producer 'producer))] |
| 291 |
[tc (thread-start! (make-thread consumer 'consumer))]) |
| 292 |
(thread-join! tp) |
| 293 |
(thread-join! tc) |
| 294 |
(reverse log)))) |
| 295 |
|
| 296 |
;;--------------------------------------------------------------------- |
| 297 |
(test-section "port access serialization") |
| 298 |
|
| 299 |
(use scheme.list) |
| 300 |
|
| 301 |
(define (port-test-chunk-generator nchars c) |
| 302 |
(^[] (make-string nchars c))) |
| 303 |
|
| 304 |
(define (port-test-read-string nchars port) |
| 305 |
(let loop ([i 1] [c (read-char port)] [r '()]) |
| 306 |
(cond [(eof-object? c) |
| 307 |
(if (null? r) c (list->string (reverse r)))] |
| 308 |
[(= i nchars) (list->string (reverse (cons c r)))] |
| 309 |
[else (loop (+ i 1) (read-char port) (cons c r))]))) |
| 310 |
|
| 311 |
(define (port-test-testers nchars nthread nrepeat line?) |
| 312 |
(let* ([strgen (map (^i ($ port-test-chunk-generator nchars |
| 313 |
$ integer->char (+ (char->integer #\a) i))) |
| 314 |
(iota nthread))] |
| 315 |
[generators (map (^[gen] |
| 316 |
(let ((i 0)) |
| 317 |
(^[] (if (= i nrepeat) |
| 318 |
#f |
| 319 |
(begin |
| 320 |
(inc! i) |
| 321 |
(if line? |
| 322 |
(string-append (gen) "\n") |
| 323 |
(gen))))))) |
| 324 |
strgen)] |
| 325 |
[getter (if line? |
| 326 |
read-line |
| 327 |
(cut port-test-read-string nchars <>))] |
| 328 |
[confirmer (^[inp] |
| 329 |
(let1 strs (map (cut <>) strgen) |
| 330 |
(let loop ([chunk (getter inp)]) |
| 331 |
(cond [(eof-object? chunk) #t] |
| 332 |
[(member chunk strs) (loop (getter inp))] |
| 333 |
[else #f]))))]) |
| 334 |
(values confirmer generators))) |
| 335 |
|
| 336 |
(define (port-test-kick-threads generators outp) |
| 337 |
(let* ([thunks (map (^[gen] (^[] (let loop ([s (gen)]) |
| 338 |
(when s |
| 339 |
(display s outp) |
| 340 |
(thread-sleep! 0.001) |
| 341 |
(loop (gen)))))) |
| 342 |
generators)] |
| 343 |
[threads (map make-thread thunks)]) |
| 344 |
(for-each thread-start! threads) |
| 345 |
(for-each thread-join! threads))) |
| 346 |
|
| 347 |
(sys-system "rm -rf test.out") |
| 348 |
|
| 349 |
(test* "write to file, buffered" #t |
| 350 |
(receive (confirmer generators) |
| 351 |
(port-test-testers 160 8 20 #f) |
| 352 |
(call-with-output-file "test.out" |
| 353 |
(^[outp] (port-test-kick-threads generators outp))) |
| 354 |
(call-with-input-file "test.out" confirmer))) |
| 355 |
|
| 356 |
(sys-system "rm -rf test.out") |
| 357 |
|
| 358 |
(test* "write to file, line-buffered" #t |
| 359 |
(receive (confirmer generators) |
| 360 |
(port-test-testers 160 8 20 #t) |
| 361 |
(call-with-output-file "test.out" |
| 362 |
(^[outp] (port-test-kick-threads generators outp)) |
| 363 |
:buffering :line) |
| 364 |
(call-with-input-file "test.out" confirmer))) |
| 365 |
|
| 366 |
|
| 367 |
(sys-system "rm -rf test.out") |
| 368 |
|
| 369 |
(test* "write to string" #t |
| 370 |
(receive (confirmer generators) |
| 371 |
(port-test-testers 160 8 20 #f) |
| 372 |
(let1 s (call-with-output-string |
| 373 |
(^[outp] (port-test-kick-threads generators outp))) |
| 374 |
(call-with-input-string s confirmer)))) |
| 375 |
|
| 376 |
;; Check if port is properly unlocked when an error is signalled |
| 377 |
;; inside the port processing routine. |
| 378 |
|
| 379 |
(define *port-test-error* #f) |
| 380 |
|
| 381 |
(define (make-error-test-port outp flush?) |
| 382 |
(open-output-buffered-port |
| 383 |
(^[str] |
| 384 |
(cond [(not str) (flush outp)] |
| 385 |
[(string-scan str "Z" 'before) |
| 386 |
=> (^s (display s outp) |
| 387 |
(if flush? (flush outp)) |
| 388 |
(unless *port-test-error* |
| 389 |
(set! *port-test-error* #t) |
| 390 |
(error "error")))] |
| 391 |
[else (display str outp) (if flush? (flush outp))])) |
| 392 |
5)) |
| 393 |
|
| 394 |
(define (port-test-on-error port use-flush?) |
| 395 |
(set! *port-test-error* #f) |
| 396 |
(let* ([p (make-error-test-port port use-flush?)] |
| 397 |
[th1 (make-thread |
| 398 |
(^[] (with-error-handler (^e #f) |
| 399 |
(^[] (display "aaaaaAAAZAa" p)))) |
| 400 |
'th1)] |
| 401 |
[th2 (make-thread |
| 402 |
(^[] (display "bbbbbbbb" p)) |
| 403 |
'th2)]) |
| 404 |
(thread-start! th1) |
| 405 |
(thread-join! th1) |
| 406 |
(thread-start! th2) |
| 407 |
(thread-join! th2) |
| 408 |
(close-output-port p))) |
| 409 |
|
| 410 |
(test* "check if port is unlocked on error" "aaaaaAAAAAAbbbbbbbb" |
| 411 |
(call-with-output-string (cut port-test-on-error <> #f))) |
| 412 |
(test* "check if port is unlocked on error" "aaaaaAAAAAAbbbbbbbb" |
| 413 |
(call-with-output-string (cut port-test-on-error <> #t))) |
| 414 |
|
| 415 |
(sys-system "rm -f test.out") |
| 416 |
(test* "check if port is unlocked on error (use file)" "aaaaaAAAAAAbbbbbbbb" |
| 417 |
(begin |
| 418 |
(call-with-output-file "test.out" |
| 419 |
(cut port-test-on-error <> #f)) |
| 420 |
(call-with-input-file "test.out" port->string))) |
| 421 |
|
| 422 |
(sys-system "rm -f test.out") |
| 423 |
(test* "check if port is unlocked on error (use file)" "aaaaaAAAAAAbbbbbbbb" |
| 424 |
(begin |
| 425 |
(call-with-output-file "test.out" |
| 426 |
(cut port-test-on-error <> #t)) |
| 427 |
(call-with-input-file "test.out" port->string))) |
| 428 |
|
| 429 |
;;--------------------------------------------------------------------- |
| 430 |
;(test-section "thread and signal") |
| 431 |
|
| 432 |
;(test "catching signal by primordial thread" (make-list 10 'int) |
| 433 |
|
| 434 |
;;--------------------------------------------------------------------- |
| 435 |
(test-section "thread-locals") |
| 436 |
|
| 437 |
(define tl1 (make-thread-local 0)) |
| 438 |
|
| 439 |
(test* "thread-local?" #t (thread-local? tl1)) |
| 440 |
(test* "thread-local (default value)" 0 (tlref tl1)) |
| 441 |
|
| 442 |
(test* "thread local" '(1 2 0) |
| 443 |
(let1 handshake #f |
| 444 |
(let ([t1 (make-thread (^[] |
| 445 |
(tlset! tl1 1) |
| 446 |
(until handshake (sys-nanosleep 500000)) |
| 447 |
(tlref tl1)))] |
| 448 |
[t2 (make-thread (^[] |
| 449 |
(set! (tlref tl1) 2) |
| 450 |
(until handshake (sys-nanosleep 500000)) |
| 451 |
(tlref tl1)))] |
| 452 |
[t3 (make-thread (^[] |
| 453 |
(until handshake (sys-nanosleep 500000)) |
| 454 |
(tlref tl1)))]) |
| 455 |
(thread-start! t1) |
| 456 |
(thread-start! t2) |
| 457 |
(thread-start! t3) |
| 458 |
(set! handshake #t) |
| 459 |
(list (thread-join! t1) |
| 460 |
(thread-join! t2) |
| 461 |
(thread-join! t3))))) |
| 462 |
|
| 463 |
(define tl2 (make-thread-local 0 #t)) |
| 464 |
|
| 465 |
(test* "thread local (inheritable)" '(200 100) |
| 466 |
(let1 handshake #f |
| 467 |
(let* ([t2 #f] |
| 468 |
[t1 (make-thread (^[] |
| 469 |
(tlset! tl2 100) |
| 470 |
(set! t2 (make-thread |
| 471 |
(^[] |
| 472 |
(until handshake |
| 473 |
(sys-nanosleep 500000)) |
| 474 |
(tlref tl2)))) |
| 475 |
(tlset! tl2 200) |
| 476 |
(thread-start! t2) |
| 477 |
(until handshake (sys-nanosleep 500000)) |
| 478 |
(tlref tl2)))]) |
| 479 |
(thread-start! t1) |
| 480 |
(set! handshake #t) |
| 481 |
(list (thread-join! t1) |
| 482 |
(thread-join! t2))))) |
| 483 |
|
| 484 |
;;--------------------------------------------------------------------- |
| 485 |
(test-section "error handler and threads") |
| 486 |
|
| 487 |
(let () |
| 488 |
(test* "reraising in thread" 'foo |
| 489 |
(let ([th (make-thread (^[] |
| 490 |
(with-error-handler (^e (raise 'foo)) |
| 491 |
(^[] (error 'bar)))))]) |
| 492 |
(thread-start! th) |
| 493 |
(guard (e [(uncaught-exception? e) |
| 494 |
(uncaught-exception-reason e)]) |
| 495 |
(thread-join! th)))) |
| 496 |
|
| 497 |
(test* "reraising in thread w/guard" 'bar |
| 498 |
(let ([th (make-thread (^[] |
| 499 |
(guard (e ((error? e) 'huh?)) |
| 500 |
(raise 'bar))))]) |
| 501 |
(thread-start! th) |
| 502 |
(guard (e [(uncaught-exception? e) |
| 503 |
(uncaught-exception-reason e)]) |
| 504 |
(thread-join! th)))) |
| 505 |
) |
| 506 |
|
| 507 |
;;--------------------------------------------------------------------- |
| 508 |
(test-section "parameters and threads") |
| 509 |
|
| 510 |
(let () |
| 511 |
(define *thr1-val* #f) |
| 512 |
(define *thr2-val* #f) |
| 513 |
(define pl (make-thread-parameter 3)) |
| 514 |
(define ps (make-shared-parameter 5)) |
| 515 |
|
| 516 |
(test* "thread parameter: check locality of parameters" '(3 4 5) |
| 517 |
(let ([th1 (make-thread (^[] (pl 4) (set! *thr1-val* (pl))))] |
| 518 |
[th2 (make-thread (^[] (pl 5) (set! *thr2-val* (pl))))]) |
| 519 |
(thread-start! th1) |
| 520 |
(thread-start! th2) |
| 521 |
(thread-join! th1) |
| 522 |
(thread-join! th2) |
| 523 |
(list (pl) *thr1-val* *thr2-val*))) |
| 524 |
|
| 525 |
(test* "shared parameter (global)" |
| 526 |
'((-5 6) |
| 527 |
(5 6) |
| 528 |
6) |
| 529 |
(let1 b (make-barrier 2) |
| 530 |
(let ([th1 (make-thread (^[] |
| 531 |
(barrier-await b) |
| 532 |
(let1 v1 (ps 6) |
| 533 |
(barrier-await b) |
| 534 |
(list v1 (ps)))))] |
| 535 |
[th2 (make-thread (^[] |
| 536 |
(let1 v0 (ps -5) |
| 537 |
(barrier-await b) |
| 538 |
(barrier-await b) |
| 539 |
(list v0 (ps)))))]) |
| 540 |
(thread-start! th1) |
| 541 |
(thread-start! th2) |
| 542 |
(list (thread-join! th1) |
| 543 |
(thread-join! th2) |
| 544 |
(ps))))) |
| 545 |
|
| 546 |
(test* "shared parameter (dynamic)" |
| 547 |
'((-1 -1) |
| 548 |
(99 198) |
| 549 |
(99 198) |
| 550 |
-1) |
| 551 |
(let ([b (make-barrier 3)] |
| 552 |
[g (make-latch 1)]) |
| 553 |
(let1 th1 (make-thread (^[] |
| 554 |
(latch-await g) |
| 555 |
(let1 v1 (ps) |
| 556 |
(barrier-await b) |
| 557 |
(barrier-await b) |
| 558 |
(list v1 (ps))))) |
| 559 |
(receive (th2 th3) |
| 560 |
(parameterize ((ps 99)) |
| 561 |
(values (make-thread (^[] |
| 562 |
(latch-await g) |
| 563 |
(let1 v1 (ps) |
| 564 |
(barrier-await b) |
| 565 |
(ps 198) |
| 566 |
(barrier-await b) |
| 567 |
(list v1 (ps))))) |
| 568 |
(make-thread (^[] |
| 569 |
(latch-await g) |
| 570 |
(let1 v1 (ps) |
| 571 |
(barrier-await b) |
| 572 |
(barrier-await b) |
| 573 |
(list v1 (ps))))))) |
| 574 |
(thread-start! th1) |
| 575 |
(thread-start! th2) |
| 576 |
(thread-start! th3) |
| 577 |
(ps -1) |
| 578 |
(latch-clear! g) |
| 579 |
|
| 580 |
(list (thread-join! th1) |
| 581 |
(thread-join! th2) |
| 582 |
(thread-join! th3) |
| 583 |
(ps)))))) |
| 584 |
) |
| 585 |
|
| 586 |
;; Parameters that are created in a different thread |
| 587 |
;; We used to prohibit accessing parameters that are created by different |
| 588 |
;; threads (up to 0.9.2). But it caused annoyance when a module creating |
| 589 |
;; parameters is loaded after some threads are already started. |
| 590 |
|
| 591 |
(test* "thread parameter: parameters created by different thread" '(2 1 3) |
| 592 |
(let ([p #f] |
| 593 |
[handshake #f] |
| 594 |
[val1 #f] |
| 595 |
[val2 #f] |
| 596 |
[val3 #f]) |
| 597 |
(let ([th1 (make-thread (^[] |
| 598 |
(set! p (make-parameter 1)) |
| 599 |
(p 2) |
| 600 |
(until handshake (sys-nanosleep 10000)) |
| 601 |
(set! val1 (p))))] |
| 602 |
[th2 (make-thread (^[] |
| 603 |
(until p (sys-nanosleep 10000)) |
| 604 |
(set! val2 (p))))] |
| 605 |
[th3 (make-thread (^[] |
| 606 |
(until p (sys-nanosleep 10000)) |
| 607 |
(p 3) |
| 608 |
(set! handshake #t) |
| 609 |
(set! val3 (p))))]) |
| 610 |
(thread-start! th1) |
| 611 |
(thread-start! th2) |
| 612 |
(thread-start! th3) |
| 613 |
(thread-join! th1) |
| 614 |
(thread-join! th2) |
| 615 |
(thread-join! th3) |
| 616 |
(list val1 val2 val3)))) |
| 617 |
|
| 618 |
;;--------------------------------------------------------------------- |
| 619 |
(test-section "run-once") |
| 620 |
|
| 621 |
(let ((v 0)) |
| 622 |
(define (refv) |
| 623 |
(sys-nanosleep 1000) |
| 624 |
(begin0 v (set! v (+ v 1)))) |
| 625 |
(test* "run-once" |
| 626 |
(make-list 10 0) |
| 627 |
(let1 thrs (map (^_ (make-thread |
| 628 |
(^[] (run-once (refv))))) |
| 629 |
(iota 10)) |
| 630 |
(for-each thread-start! thrs) |
| 631 |
(map thread-join! thrs))) |
| 632 |
) |
| 633 |
|
| 634 |
;;--------------------------------------------------------------------- |
| 635 |
(test-section "atoms") |
| 636 |
|
| 637 |
(test* "atom" #t (atom? (atom 0 1 2))) |
| 638 |
|
| 639 |
(test* "atomic counting" 300 |
| 640 |
(let ([a (atom 0)] [ts '()]) |
| 641 |
(dotimes [n 30] |
| 642 |
(push! ts |
| 643 |
(thread-start! (make-thread |
| 644 |
(^[] (dotimes [m 10] |
| 645 |
(atomic-update! a (pa$ + 1)))))))) |
| 646 |
(for-each thread-join! ts) |
| 647 |
(atom-ref a))) |
| 648 |
|
| 649 |
(test* "atomic-update! extra values" '(2 3 4) |
| 650 |
(let ([a (atom 1 2)]) |
| 651 |
(values->list |
| 652 |
(atomic-update! a |
| 653 |
(^[x y] (values (+ x 1) (+ y 1) (+ x y 1))))))) |
| 654 |
|
| 655 |
;;--------------------------------------------------------------------- |
| 656 |
(test-section "semaphore") |
| 657 |
|
| 658 |
(test* "semaphore" #t (semaphore? (make-semaphore 1))) |
| 659 |
|
| 660 |
(test* "semaphore wait" '(2 3 6) |
| 661 |
(let* ([d (atom 0)] |
| 662 |
[s (make-semaphore 2)] |
| 663 |
[ts (map (^i (make-thread (^[] |
| 664 |
(semaphore-acquire! s) |
| 665 |
(atomic-update! d (cut + <> 1))))) |
| 666 |
(iota 6))]) |
| 667 |
(for-each thread-start! ts) |
| 668 |
(let loop ([d1 (atom-ref d)]) |
| 669 |
(cond |
| 670 |
[(< d1 2) (sys-nanosleep 500) (loop (atom-ref d))] |
| 671 |
[else |
| 672 |
(semaphore-release! s) |
| 673 |
(let loop ([d2 (atom-ref d)]) |
| 674 |
(cond |
| 675 |
[(= d1 d2) (sys-nanosleep 500) (loop (atom-ref d))] |
| 676 |
[else |
| 677 |
(semaphore-release! s 3) |
| 678 |
(for-each thread-join! ts) |
| 679 |
(list d1 d2 (atom-ref d 0))]))])))) |
| 680 |
|
| 681 |
;;--------------------------------------------------------------------- |
| 682 |
(test-section "latch") |
| 683 |
|
| 684 |
(test* "latch" #t (latch? (make-latch 2))) |
| 685 |
|
| 686 |
(test* "latch operation" '(0 0 5 6) |
| 687 |
(let* ([d (atom 0)] |
| 688 |
[l (make-latch 2)] |
| 689 |
[l2 (make-latch 5)] |
| 690 |
[ts (map (^i (make-thread (^[] |
| 691 |
(latch-await l) |
| 692 |
(atomic-update! d (cut + <> 1)) |
| 693 |
(latch-dec! l2)))) |
| 694 |
(iota 6))]) |
| 695 |
(for-each thread-start! (cdr ts)) |
| 696 |
(sys-nanosleep 1000) |
| 697 |
(let ([d1 (atom-ref d 0)]) |
| 698 |
(latch-dec! l) |
| 699 |
(sys-nanosleep 1000) |
| 700 |
(let ([d2 (atom-ref d 0)]) |
| 701 |
(latch-dec! l) |
| 702 |
(latch-await l2) |
| 703 |
(let ([d3 (atom-ref d 0)]) |
| 704 |
(thread-start! (car ts)) |
| 705 |
(for-each thread-join! ts) |
| 706 |
(list d1 d2 d3 (atom-ref d 0))))))) |
| 707 |
|
| 708 |
;;--------------------------------------------------------------------- |
| 709 |
(test-section "barrier") |
| 710 |
|
| 711 |
(test* "barrier" #t (barrier? (make-barrier 3))) |
| 712 |
|
| 713 |
(let* ([pre (atom 0)] |
| 714 |
[post (atom 0)] |
| 715 |
[b (make-barrier 3)] |
| 716 |
[l0 (make-latch 2)] |
| 717 |
[l1 (make-latch 3)] |
| 718 |
[ts (map (^i (make-thread (^[] |
| 719 |
(atomic-update! pre (cut + <> 1)) |
| 720 |
(latch-dec! l0) |
| 721 |
(barrier-await b) |
| 722 |
(atomic-update! post (cut + <> 1)) |
| 723 |
(latch-dec! l1)))) |
| 724 |
(iota 3))]) |
| 725 |
(for-each thread-start! (cdr ts)) |
| 726 |
(latch-await l0) |
| 727 |
(test* "simple barrier pre" '(2 0) |
| 728 |
(list (atom-ref pre 0) (atom-ref post 0))) |
| 729 |
(thread-start! (car ts)) |
| 730 |
(latch-await l1) |
| 731 |
(test* "simple barrier post" '(3 3) |
| 732 |
(list (atom-ref pre 0) (atom-ref post 0))) |
| 733 |
(for-each thread-join! ts)) |
| 734 |
|
| 735 |
(let* ([b (make-barrier 4)] |
| 736 |
[t0 (make-thread (^[] (barrier-await b #f 'timeout0)))] |
| 737 |
[t1 (make-thread (^[] (barrier-await b 1e-3 'timeout1)))] |
| 738 |
[t2 (make-thread (^[] (barrier-await b #f 'timeout2)))]) |
| 739 |
(thread-start! t0) |
| 740 |
(test* "barrier broken?" #f |
| 741 |
(barrier-broken? b)) |
| 742 |
(test* "barrier timeout" 'timeout1 |
| 743 |
(thread-join! (thread-start! t1))) |
| 744 |
(test* "barrier broken?" #t |
| 745 |
(barrier-broken? b)) |
| 746 |
(test* "broken barrier pass through" 'timeout2 |
| 747 |
(thread-join! (thread-start! t2))) |
| 748 |
(test* "barrier reset" #f |
| 749 |
(begin (barrier-reset! b) |
| 750 |
(barrier-broken? b))) |
| 751 |
) |
| 752 |
|
| 753 |
(let* ([b (make-barrier 3)] |
| 754 |
[x (atom '())] |
| 755 |
[ts (map (^t (make-thread (^[] |
| 756 |
(dotimes [i 3] |
| 757 |
(sys-nanosleep (* t 1000.0)) |
| 758 |
(atomic-update! x (cut cons i <>)) |
| 759 |
(barrier-await b))))) |
| 760 |
(iota 3))]) |
| 761 |
(test* "barrier cycle" '(2 2 2 1 1 1 0 0 0) |
| 762 |
(begin |
| 763 |
(for-each thread-start! ts) |
| 764 |
(for-each thread-join! ts) |
| 765 |
(atom-ref x)))) |
| 766 |
|
| 767 |
(let* ([x (atom 0)] |
| 768 |
[b (make-barrier 3 (^[] (atomic-update! x (cut + <> 1))))] |
| 769 |
[ts (map (^t (make-thread (^[] |
| 770 |
(sys-nanosleep (* t 100.0)) |
| 771 |
(barrier-await b)))) |
| 772 |
(iota 3))]) |
| 773 |
(test* "barrier action once" 1 |
| 774 |
(begin (for-each thread-start! ts) |
| 775 |
(for-each thread-join! ts) |
| 776 |
(atom-ref x)))) |
| 777 |
|
| 778 |
(let* ([b (make-barrier 3 (^[] (raise 'oops)))] |
| 779 |
[ts (map (^t (make-thread (^[] |
| 780 |
(sys-nanosleep (* t 100.0)) |
| 781 |
(guard (e [else e]) |
| 782 |
(barrier-await b #f t))))) |
| 783 |
(iota 3))]) |
| 784 |
(for-each thread-start! ts) |
| 785 |
(test* "barrier action once" (test-one-of '(#t 0 1 oops) |
| 786 |
'(#t 0 oops 2) |
| 787 |
'(#t oops 1 2)) |
| 788 |
(let1 rs (map thread-join! ts) |
| 789 |
(cons (barrier-broken? b) rs)))) |
| 790 |
|
| 791 |
;;--------------------------------------------------------------------- |
| 792 |
(test-section "threads and promise") |
| 793 |
|
| 794 |
(use scheme.list) |
| 795 |
|
| 796 |
(letrec ([z (delay (begin (sys-nanosleep 100000) |
| 797 |
(values-ref (sys-gettimeofday) 1)))]) |
| 798 |
(test* "concurrent forcing" #t |
| 799 |
(let1 ts (map (^_ (make-thread (cut force z))) (iota 10)) |
| 800 |
(for-each thread-start! ts) |
| 801 |
(let1 rs (map thread-join! ts) |
| 802 |
(every (cut = (car rs) <>) (cdr rs)))))) |
| 803 |
|
| 804 |
(letrec ([r 0] |
| 805 |
[count (make-thread-local 0)] |
| 806 |
[z (delay (if (<= (tlref count) 5) |
| 807 |
(begin |
| 808 |
(thread-yield!) |
| 809 |
(tlset! count (+ (tlref count) 1)) |
| 810 |
(force z)) |
| 811 |
(tlref count)))]) |
| 812 |
(test* "concurrent forcing w/ recursive force" 6 |
| 813 |
(let1 ts (map (^_ (make-thread (^[] (set! r (force z))))) |
| 814 |
(iota 10)) |
| 815 |
(for-each thread-start! ts) |
| 816 |
(for-each thread-join! ts) |
| 817 |
r))) |
| 818 |
|
| 819 |
;;--------------------------------------------------------------------- |
| 820 |
(test-section "threads and lazy sequences") |
| 821 |
|
| 822 |
(let1 k 0 |
| 823 |
(define (weird-generator) |
| 824 |
(inc! k) |
| 825 |
(case k |
| 826 |
[(1) 0] |
| 827 |
[(2) (sys-nanosleep #e1e7) (raise "boo")] |
| 828 |
[(3) 1] |
| 829 |
[else (eof-object)])) |
| 830 |
(define (task seq) |
| 831 |
(guard (e [else e]) |
| 832 |
(let1 k (length seq) seq))) |
| 833 |
(let* ([seq (generator->lseq weird-generator)] |
| 834 |
[t0 (make-thread (^[] (task seq)))] |
| 835 |
[t1 (make-thread (^[] (task seq)))]) |
| 836 |
(test* "accessing the same lazy sequence" |
| 837 |
(test-one-of '("boo" (0 1)) '((0 1) "boo")) |
| 838 |
(begin (thread-start! t0) |
| 839 |
(thread-start! t1) |
| 840 |
(list (thread-join! t0) (thread-join! t1)))))) |
| 841 |
|
| 842 |
;;--------------------------------------------------------------------- |
| 843 |
(test-section "synchrnization by queues") |
| 844 |
|
| 845 |
;; These are actually for testing mtqueue, but put here since they |
| 846 |
;; require threads to work. |
| 847 |
|
| 848 |
(use data.queue) |
| 849 |
|
| 850 |
(define (test-producer-consumer name qs ndata nthreads) |
| 851 |
(define qr (make-mtqueue)) |
| 852 |
(define data (iota ndata)) |
| 853 |
(define (producer) |
| 854 |
(dolist [n data] (enqueue/wait! qs n)) |
| 855 |
(dotimes [k nthreads] (enqueue/wait! qs #f))) |
| 856 |
(define (consumer) |
| 857 |
(let loop ([x (dequeue/wait! qs)]) |
| 858 |
(when x |
| 859 |
(enqueue! qr x) |
| 860 |
(sys-nanosleep #e1e7) |
| 861 |
(loop (dequeue/wait! qs))))) |
| 862 |
|
| 863 |
(test* #"synchronized queue ~name" data |
| 864 |
(let* ([cs (map (^_ (thread-start! (make-thread consumer))) |
| 865 |
(iota nthreads))] |
| 866 |
[p1 (make-thread producer)]) |
| 867 |
(sys-nanosleep #e1e8) |
| 868 |
(thread-start! p1) |
| 869 |
(for-each thread-join! cs) |
| 870 |
(thread-join! p1) |
| 871 |
(queue->list qr)) |
| 872 |
(cut lset= eqv? <> <>))) |
| 873 |
|
| 874 |
(test-producer-consumer "(unbound queue length)" |
| 875 |
(make-mtqueue) |
| 876 |
100 3) |
| 877 |
|
| 878 |
(test-producer-consumer "(bound queue length)" |
| 879 |
(make-mtqueue :max-length 5) |
| 880 |
100 3) |
| 881 |
|
| 882 |
(test-producer-consumer "(zero-length queue)" |
| 883 |
(make-mtqueue :max-length 0) |
| 884 |
100 3) |
| 885 |
|
| 886 |
(test* "dequeue/wait! timeout" "timed out!" |
| 887 |
(dequeue/wait! (make-mtqueue) 0.01 "timed out!")) |
| 888 |
(test* "enqueue/wait! timeout" "timed out!" |
| 889 |
(let1 q (make-mtqueue :max-length 1) |
| 890 |
(enqueue! q 'a) |
| 891 |
(enqueue/wait! q 'b 0.01 "timed out!"))) |
| 892 |
(test* "queue-push/wait! timeout" "timed out!" |
| 893 |
(let1 q (make-mtqueue :max-length 1) |
| 894 |
(enqueue! q 'a) |
| 895 |
(queue-push/wait! q 'b 0.01 "timed out!"))) |
| 896 |
|
| 897 |
(test* "zero-length-queue handshaking" '(5 4 3 2 1 0) |
| 898 |
(let ([r '()] |
| 899 |
[q0 (make-mtqueue :max-length 0)] |
| 900 |
[q1 (make-mtqueue :max-length 0)]) |
| 901 |
(let1 t (thread-start! |
| 902 |
(make-thread (^[] |
| 903 |
(push! r 0) |
| 904 |
(dequeue/wait! q0) |
| 905 |
(enqueue/wait! q1 'b) |
| 906 |
(push! r 2) |
| 907 |
(dequeue/wait! q0) |
| 908 |
(enqueue/wait! q1 'd) |
| 909 |
(push! r 4) |
| 910 |
(dequeue/wait! q0)))) |
| 911 |
(enqueue/wait! q0 'a) |
| 912 |
(push! r 1) |
| 913 |
(dequeue/wait! q1) |
| 914 |
(enqueue/wait! q0 'c) |
| 915 |
(push! r 3) |
| 916 |
(dequeue/wait! q1) |
| 917 |
(enqueue/wait! q0 'e) |
| 918 |
(push! r 5)) |
| 919 |
r)) |
| 920 |
|
| 921 |
(test* "zero-length-queue multiple reader" '(a a b b) |
| 922 |
(let ([r0 #f] [r1 #f] |
| 923 |
[q (make-mtqueue :max-length 0)] |
| 924 |
[qq (make-mtqueue :max-length 0)]) |
| 925 |
(let ([t0 (thread-start! |
| 926 |
(make-thread (^[] (enqueue/wait! qq #t) |
| 927 |
(set! r0 (dequeue/wait! q)) |
| 928 |
(enqueue/wait! qq 'b))))] |
| 929 |
[t1 (thread-start! |
| 930 |
(make-thread (^[] (enqueue/wait! qq #t) |
| 931 |
(set! r1 (dequeue/wait! q)) |
| 932 |
(enqueue/wait! qq 'b))))]) |
| 933 |
(dequeue/wait! qq) |
| 934 |
(dequeue/wait! qq) |
| 935 |
(enqueue/wait! q 'a) |
| 936 |
(enqueue/wait! q 'a) |
| 937 |
(let1 r (list (dequeue/wait! qq) (dequeue/wait! qq)) |
| 938 |
(list* r0 r1 r))))) |
| 939 |
|
| 940 |
(let () |
| 941 |
(test* "mtqueue-closed?" '(#f #t) |
| 942 |
(let* ([q (make-mtqueue)] |
| 943 |
[a (~ q'closed)] |
| 944 |
[_ (dequeue/wait! q 0 #f #t)] |
| 945 |
[b (~ q'closed)]) |
| 946 |
(list a b))) |
| 947 |
(test* "closed mtqueue rejects enqueue" |
| 948 |
(test-error <error> #/queue is closed/) |
| 949 |
(let1 q (make-mtqueue) |
| 950 |
(dequeue/wait! q 0 #f #t) |
| 951 |
(enqueue! q 'a))) |
| 952 |
(test* "closed mtqueue rejects enqueue/wait" |
| 953 |
(test-error <error> #/queue is closed/) |
| 954 |
(let1 q (make-mtqueue) |
| 955 |
(dequeue/wait! q 0 #f #t) |
| 956 |
(enqueue/wait! q 'a))) |
| 957 |
(test* "enqueue with closing" #t |
| 958 |
(let1 q (make-mtqueue) |
| 959 |
(enqueue/wait! q 'a #f #f #t) |
| 960 |
(~ q'closed))) |
| 961 |
) |
| 962 |
|
| 963 |
;;--------------------------------------------------------------------- |
| 964 |
(test-section "memo tables") |
| 965 |
|
| 966 |
;; This actually tests built-in memoization table primitives, but |
| 967 |
;; here we stress it with MT-access. |
| 968 |
|
| 969 |
(let ([make-memo-table (with-module gauche.internal make-memo-table)] |
| 970 |
[memo-table-put! (with-module gauche.internal memo-table-put!)] |
| 971 |
[memo-table-get2 (with-module gauche.internal memo-table-get2)] |
| 972 |
[memo-table-putv! (with-module gauche.internal memo-table-putv!)] |
| 973 |
[memo-table-getv2 (with-module gauche.internal memo-table-getv2)]) |
| 974 |
|
| 975 |
(define fib |
| 976 |
(let1 tab (make-memo-table 2 1) |
| 977 |
(lambda (n) |
| 978 |
(if (<= n 1) |
| 979 |
1 |
| 980 |
(let1 key (vector n) |
| 981 |
(receive (val exists?) (memo-table-getv2 tab key) |
| 982 |
(if exists? |
| 983 |
val |
| 984 |
(rlet1 v (+ (fib (- n 1)) (fib (- n 2))) |
| 985 |
(memo-table-putv! tab key v))))))))) |
| 986 |
|
| 987 |
(define string-hash-tab (make-memo-table 3 2)) |
| 988 |
|
| 989 |
(define (string-hash/memo s) |
| 990 |
(let1 key (list s (box s)) |
| 991 |
(receive (val exists?) (memo-table-get2 string-hash-tab key) |
| 992 |
(if exists? |
| 993 |
val |
| 994 |
(rlet1 n (string-hash s) |
| 995 |
(memo-table-put! string-hash-tab key n)))))) |
| 996 |
|
| 997 |
;; avoid using data.random, gauche.generator etc. |
| 998 |
(define (random-integer n) |
| 999 |
(modulo (sys-random) n)) ;; not a uniform distribution, but ok here |
| 1000 |
|
| 1001 |
(define string-pool |
| 1002 |
(rlet1 vec (make-vector 256) |
| 1003 |
(dotimes [n (vector-length vec)] |
| 1004 |
($ vector-set! vec n |
| 1005 |
$ list->string |
| 1006 |
$ map (^_ (integer->char (+ (random-integer 95) 32))) (iota 5))))) |
| 1007 |
|
| 1008 |
(define (string-hash-test) |
| 1009 |
(dotimes [n (ash (vector-length string-pool) -1)] |
| 1010 |
($ string-hash/memo |
| 1011 |
$ vector-ref string-pool |
| 1012 |
$ random-integer (vector-length string-pool)))) |
| 1013 |
|
| 1014 |
(test* "memoizing fib" (make-list 100 20365011074) |
| 1015 |
(map thread-join! |
| 1016 |
(map (^_ (thread-start! (make-thread (^[] (fib 50))))) |
| 1017 |
(iota 100)))) |
| 1018 |
|
| 1019 |
(sys-srandom 42) |
| 1020 |
|
| 1021 |
(test* "memoizing string hash" #t |
| 1022 |
(let/cc return |
| 1023 |
($ for-each thread-join! |
| 1024 |
$ map thread-start! |
| 1025 |
$ map (^_ (make-thread string-hash-test)) (iota 100)) |
| 1026 |
(positive? |
| 1027 |
(count (^s (receive (val exists?) |
| 1028 |
(memo-table-get2 string-hash-tab (list s (box s))) |
| 1029 |
(and exists? |
| 1030 |
(or (= val (string-hash s)) |
| 1031 |
(return val))))) |
| 1032 |
(vector->list string-pool))))) |
| 1033 |
|
| 1034 |
;;((with-module gauche.internal memo-table-dump) string-hash-tab) |
| 1035 |
) |
| 1036 |
|
| 1037 |
(test-end) |