1 ;;;; miscellaneous tests of thread stuff
3 ;;;; This software is part of the SBCL system. See the README file for
6 ;;;; While most of SBCL is derived from the CMU CL system, the test
7 ;;;; files (like this one) were written from scratch after the fork
10 ;;;; This software is in the public domain and is provided with
11 ;;;; absoluely no warranty. See the COPYING and CREDITS files for
12 ;;;; more information.
16 (cl:in-package
#:sb-thread
)
17 (cl:shadowing-import
'assertoid
:assert-error
)
18 (cl:use-package
'#:test-util
)
19 (cl:use-package
'#:assertoid
)
21 (setf sb-unix
::*on-dangerous-wait
* :error
)
23 (defun wait-for-threads (threads)
24 (mapc (lambda (thread) (sb-thread:join-thread thread
:default nil
)) threads
)
25 (assert (not (some #'sb-thread
:thread-alive-p threads
))))
27 (with-test (:name
(:threads
:trivia
))
28 (assert (eql 1 (length (list-all-threads))))
30 (assert (eq *current-thread
*
31 (find (thread-name *current-thread
*) (list-all-threads)
32 :key
#'thread-name
:test
#'equal
)))
34 (assert (thread-alive-p *current-thread
*)))
36 (with-test (:name
(:with-mutex
:basics
))
37 (let ((mutex (make-mutex)))
41 (sb-alien:define-alien-routine
"check_deferrables_blocked_or_lose"
43 (where sb-alien
:unsigned-long
))
44 (sb-alien:define-alien-routine
"check_deferrables_unblocked_or_lose"
46 (where sb-alien
:unsigned-long
))
48 (with-test (:name
(:interrupt-thread
:basics
:no-unwinding
))
50 (interrupt-thread *current-thread
* (lambda () (setq a
1)))
53 (with-test (:name
(:interrupt-thread
:deferrables-blocked
))
54 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
56 (check-deferrables-blocked-or-lose 0))))
58 (with-test (:name
(:interrupt-thread
:deferrables-unblocked
))
59 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
62 (check-deferrables-unblocked-or-lose 0)))))
64 (with-test (:name
(:interrupt-thread
:nlx
))
66 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
68 (check-deferrables-blocked-or-lose 0)
70 (check-deferrables-unblocked-or-lose 0))
72 #-sb-thread
(sb-ext:exit
:code
104)
74 ;;;; Now the real tests...
76 (with-test (:name
(:with-mutex
:timeout
))
77 (let ((m (make-mutex)))
79 (assert (null (join-thread (make-thread
81 (with-mutex (m :timeout
0.1)
83 (assert (join-thread (make-thread
85 (with-mutex (m :timeout
0.1)
88 (with-test (:name
(:interrupt-thread
:deferrables-unblocked-by-lock
))
89 (let ((lock (sb-thread::make-mutex
))
90 (thread (make-join-thread (lambda ()
92 (sb-thread::grab-mutex lock
)
93 (sb-thread:interrupt-thread thread
95 (check-deferrables-blocked-or-lose 0)
96 (sb-thread::grab-mutex lock
)
97 (check-deferrables-unblocked-or-lose 0)
98 (sb-thread:abort-thread
)))
100 (sb-thread::release-mutex lock
)))
104 (defmacro defincf
(name accessor
&rest args
)
106 (let* ((old (,accessor x
,@args
))
108 (loop until
(eq old
(sb-ext:compare-and-swap
(,accessor x
,@args
) old new
))
109 do
(setf old
(,accessor x
,@args
)
113 (defstruct cas-struct
(slot 0))
115 (defincf incf-car car
)
116 (defincf incf-cdr cdr
)
117 (defincf incf-slot cas-struct-slot
)
118 (defincf incf-symbol-value symbol-value
)
119 (defincf incf-svref
/1 svref
1)
120 (defincf incf-svref
/0 svref
0)
122 (defmacro def-test-cas
(name init incf op
)
123 `(with-test (:name
,name
)
130 collect
(sb-thread:make-thread
133 do
(sb-thread:thread-yield
))
134 (loop repeat n do
(,incf x
)))))))
137 (sb-thread:join-thread th
))
138 (assert (= (,op x
) (* 10 n
))))))
141 (def-test-cas test-cas-car
(cons 0 nil
) incf-car car
)
142 (def-test-cas test-cas-cdr
(cons nil
0) incf-cdr cdr
)
143 (def-test-cas test-cas-slot
(make-cas-struct) incf-slot cas-struct-slot
)
144 (def-test-cas test-cas-value
(let ((x '.x.
))
147 incf-symbol-value symbol-value
)
148 (def-test-cas test-cas-svref
/0 (vector 0 nil
) incf-svref
/0 (lambda (x)
150 (def-test-cas test-cas-svref
/1 (vector nil
0) incf-svref
/1 (lambda (x)
152 (format t
"~&compare-and-swap tests done~%")
154 (with-test (:name
(:threads
:more-trivia
))
155 (let ((old-threads (list-all-threads))
156 (thread (make-thread (lambda ()
157 (assert (find *current-thread
* *all-threads
*))
159 (new-threads (list-all-threads)))
160 (assert (thread-alive-p thread
))
161 (assert (eq thread
(first new-threads
)))
162 (assert (= (1+ (length old-threads
)) (length new-threads
)))
164 (assert (not (thread-alive-p thread
)))))
166 (with-test (:name
(join-thread :abort
:default
))
167 (let* ((sym (gensym))
168 (thread (make-thread (lambda () (abort-thread)))))
169 (assert (equal (multiple-value-list
170 (join-thread thread
:default sym
))
171 (list sym
:abort
)))))
173 (with-test (:name
(join-thread :abort
:error
))
174 (assert-error (join-thread (make-thread (lambda () (abort-thread))))
177 (with-test (:name
(join-thread :timeout
:default
))
178 (let* ((sym (gensym))
179 (thread (make-kill-thread (lambda () (sleep most-positive-fixnum
)))))
180 (assert (equal (multiple-value-list
181 (join-thread thread
:timeout
.001 :default sym
))
182 (list sym
:timeout
)))))
184 (with-test (:name
(join-thread :timeout
:error
))
185 (let ((thread (make-kill-thread (lambda () (sleep most-positive-fixnum
)))))
186 (assert-error (join-thread thread
:timeout
.001) join-thread-error
)))
188 (with-test (:name
(join-thread :multiple-values
))
189 (assert (equal '(1 2 3)
191 (join-thread (make-thread (lambda () (values 1 2 3))))))))
193 ;;; We had appalling scaling properties for a while. Make sure they
195 (defun scaling-test (function &optional
(nthreads 5))
196 "Execute FUNCTION with NTHREADS lurking to slow it down."
197 (let ((queue (sb-thread:make-waitqueue
))
198 (mutex (sb-thread:make-mutex
)))
199 ;; Start NTHREADS idle threads.
200 (dotimes (i nthreads
)
201 (make-join-thread (lambda ()
203 (sb-thread:condition-wait queue mutex
))
204 (sb-thread:abort-thread
))))
205 (let ((start-time (get-internal-run-time)))
207 (prog1 (- (get-internal-run-time) start-time
)
208 (sb-thread:condition-broadcast queue
)))))
211 "A function that does work with the CPU."
212 (if (zerop n
) 1 (* n
(fact (1- n
)))))
214 (with-test (:name
:lurking-threads
)
215 (let ((work (lambda () (fact 15000))))
216 (let ((zero (scaling-test work
0))
217 (four (scaling-test work
4)))
218 ;; a slightly weak assertion, but good enough for starters.
219 (assert (< four
(* 1.5 zero
))))))
221 ;;; For one of the interupt-thread tests, we want a foreign function
222 ;;; that does not make syscalls
226 (with-open-file (o "threads-foreign.c" :direction
:output
:if-exists
:supersede
)
227 (format o
"void loop_forever() { while(1) ; }~%"))
228 (sb-ext:run-program
"/bin/sh"
229 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
230 "-o" "threads-foreign.so" "threads-foreign.c"))
231 (sb-alien:load-shared-object
(truename "threads-foreign.so"))
232 (sb-alien:define-alien-routine loop-forever sb-alien
:void
)
233 (delete-file "threads-foreign.c"))
235 ;;; elementary "can we get a lock and release it again"
236 (with-test (:name
(:mutex
:basics
))
237 (let ((l (make-mutex :name
"foo"))
238 (p *current-thread
*))
239 (assert (eql (mutex-value l
) nil
) nil
"1")
240 (sb-thread:grab-mutex l
)
241 (assert (eql (mutex-value l
) p
) nil
"3")
242 (sb-thread:release-mutex l
)
243 (assert (eql (mutex-value l
) nil
) nil
"5")))
245 (with-test (:name
(:with-recursive-lock
:basics
))
246 (labels ((ours-p (value)
247 (eq *current-thread
* value
)))
248 (let ((l (make-mutex :name
"rec")))
249 (assert (eql (mutex-value l
) nil
) nil
"1")
250 (sb-thread:with-recursive-lock
(l)
251 (assert (ours-p (mutex-value l
)) nil
"3")
252 (sb-thread:with-recursive-lock
(l)
253 (assert (ours-p (mutex-value l
)) nil
"4"))
254 (assert (ours-p (mutex-value l
)) nil
"5"))
255 (assert (eql (mutex-value l
) nil
) nil
"6"))))
257 (with-test (:name
(:with-recursive-lock
:wait-p
))
258 (let ((m (make-mutex)))
260 (assert (null (join-thread (make-thread
262 (with-recursive-lock (m :wait-p nil
)
264 (assert (join-thread (make-thread
266 (with-recursive-lock (m :wait-p nil
)
269 (with-test (:name
(:with-recursive-lock
:wait-p
:recursive
))
270 (let ((m (make-mutex)))
271 (assert (join-thread (make-thread
273 (with-recursive-lock (m :wait-p nil
)
274 (with-recursive-lock (m :wait-p nil
)
277 (with-test (:name
(:with-recursive-lock
:timeout
))
278 (let ((m (make-mutex)))
280 (assert (null (join-thread (make-thread
282 (with-recursive-lock (m :timeout
0.1)
284 (assert (join-thread (make-thread
286 (with-recursive-lock (m :timeout
0.1)
289 (with-test (:name
(:with-recursive-lock
:timeout
:recursive
))
290 (let ((m (make-mutex)))
291 (assert (join-thread (make-thread
293 (with-recursive-lock (m :timeout
0.1)
294 (with-recursive-lock (m :timeout
0.1)
297 (with-test (:name
(:mutex
:nesting-mutex-and-recursive-lock
))
298 (let ((l (make-mutex :name
"a mutex")))
300 (with-recursive-lock (l)))))
302 ;; test that SLEEP actually sleeps for at least the given time, even
303 ;; if interrupted by another thread exiting/a gc/anything
304 (with-test (:name
(:sleep
:continue-sleeping-after-interrupt
))
305 (let ((start-time (get-universal-time)))
306 (make-join-thread (lambda () (sleep 1) (sb-ext:gc
:full t
)))
308 (assert (>= (get-universal-time) (+ 5 start-time
)))))
311 (with-test (:name
(:condition-wait
:basics-1
))
312 (let ((queue (make-waitqueue :name
"queue"))
313 (lock (make-mutex :name
"lock"))
315 (labels ((in-new-thread ()
317 (assert (eql (mutex-value lock
) *current-thread
*))
318 (format t
"~A got mutex~%" *current-thread
*)
319 ;; now drop it and sleep
320 (condition-wait queue lock
)
321 ;; after waking we should have the lock again
322 (assert (eql (mutex-value lock
) *current-thread
*))
325 (make-join-thread #'in-new-thread
)
326 (sleep 2) ; give it a chance to start
327 ;; check the lock is free while it's asleep
328 (format t
"parent thread ~A~%" *current-thread
*)
329 (assert (eql (mutex-value lock
) nil
))
332 (condition-notify queue
))
335 (with-test (:name
(:condition-wait
:basics-2
))
336 (let ((queue (make-waitqueue :name
"queue"))
337 (lock (make-mutex :name
"lock")))
338 (labels ((ours-p (value)
339 (eq *current-thread
* value
))
341 (with-recursive-lock (lock)
342 (assert (ours-p (mutex-value lock
)))
343 (format t
"~A got mutex~%" (mutex-value lock
))
344 ;; now drop it and sleep
345 (condition-wait queue lock
)
346 ;; after waking we should have the lock again
347 (format t
"woken, ~A got mutex~%" (mutex-value lock
))
348 (assert (ours-p (mutex-value lock
))))))
349 (make-join-thread #'in-new-thread
)
350 (sleep 2) ; give it a chance to start
351 ;; check the lock is free while it's asleep
352 (format t
"parent thread ~A~%" *current-thread
*)
353 (assert (eql (mutex-value lock
) nil
))
354 (with-recursive-lock (lock)
355 (condition-notify queue
))
358 (with-test (:name
(:mutex
:contention
))
359 (let ((mutex (make-mutex :name
"contended")))
361 (let ((me *current-thread
*))
365 (assert (eql (mutex-value mutex
) me
)))
366 (assert (not (eql (mutex-value mutex
) me
))))
367 (format t
"done ~A~%" *current-thread
*))))
368 (let ((kid1 (make-thread #'run
))
369 (kid2 (make-thread #'run
)))
370 (format t
"contention ~A ~A~%" kid1 kid2
)
371 (wait-for-threads (list kid1 kid2
))))))
375 (with-test (:name
(:grab-mutex
:waitp nil
))
376 (let ((m (make-mutex)))
378 (assert (null (join-thread (make-thread
380 (grab-mutex m
:waitp nil
)))))))))
382 (with-test (:name
(:grab-mutex
:timeout
:acquisition-fail
))
383 (let ((m (make-mutex))
384 (w (make-semaphore)))
386 (let ((th (make-thread
389 (grab-mutex m
:timeout
0.1)
390 (signal-semaphore w
))))))
391 ;; Wait for it to -- otherwise the detect the deadlock chain
393 (wait-on-semaphore w
)
394 (assert (null (join-thread th
)))))))
396 (with-test (:name
(:grab-mutex
:timeout
:acquisition-success
))
397 (let ((m (make-mutex))
400 (setq child
(make-thread #'(lambda () (grab-mutex m
:timeout
1.0))))
402 (assert (eq (join-thread child
) 't
))))
404 (with-test (:name
(:grab-mutex
:timeout
+deadline
))
405 (let ((m (make-mutex))
406 (w (make-semaphore)))
408 (let ((th (make-thread #'(lambda ()
409 (sb-sys:with-deadline
(:seconds
0.0)
411 (grab-mutex m
:timeout
0.0)
412 (sb-sys:deadline-timeout
()
415 (wait-on-semaphore w
)
416 (assert (eq (join-thread th
) :deadline
))))))
418 (with-test (:name
(:grab-mutex
:waitp
+deadline
))
419 (let ((m (make-mutex)))
421 (assert (eq (join-thread
422 (make-thread #'(lambda ()
423 (sb-sys:with-deadline
(:seconds
0.0)
425 (grab-mutex m
:waitp nil
)
426 (sb-sys:deadline-timeout
()
432 (defmacro raises-timeout-p
(&body body
)
433 `(handler-case (progn (progn ,@body
) nil
)
434 (sb-ext:timeout
() t
)))
436 (with-test (:name
(:semaphore
:wait-forever
))
437 (let ((sem (make-semaphore :count
0)))
438 (assert (raises-timeout-p
439 (sb-ext:with-timeout
0.1
440 (wait-on-semaphore sem
))))))
442 (with-test (:name
(:semaphore
:initial-count
))
443 (let ((sem (make-semaphore :count
1)))
444 (sb-ext:with-timeout
0.1
445 (assert (= 0 (wait-on-semaphore sem
))))))
447 (with-test (:name
(:semaphore
:wait-then-signal
))
448 (let ((sem (make-semaphore))
450 (make-join-thread (lambda ()
453 (signal-semaphore sem
)))
454 (assert (= 0 (wait-on-semaphore sem
)))
455 (assert signalled-p
)))
457 (with-test (:name
(:semaphore
:signal-then-wait
))
458 (let ((sem (make-semaphore))
460 (make-join-thread (lambda ()
461 (signal-semaphore sem
)
462 (setq signalled-p t
)))
463 (loop until signalled-p
)
464 (assert (= 0 (wait-on-semaphore sem
)))
465 (assert signalled-p
)))
467 (defun test-semaphore-multiple-signals (wait-on-semaphore)
468 (let* ((sem (make-semaphore :count
5))
469 (threads (loop repeat
20 collecting
470 (make-join-thread (lambda ()
471 (funcall wait-on-semaphore sem
))))))
472 (flet ((count-live-threads ()
473 (count-if #'thread-alive-p threads
)))
475 (assert (= 15 (count-live-threads)))
476 (signal-semaphore sem
10)
478 (assert (= 5 (count-live-threads)))
479 (signal-semaphore sem
3)
481 (assert (= 2 (count-live-threads)))
482 (signal-semaphore sem
4)
484 (assert (= 0 (count-live-threads))))))
486 (with-test (:name
(:semaphore
:multiple-signals
))
487 (test-semaphore-multiple-signals #'wait-on-semaphore
))
489 (with-test (:name
(:try-semaphore
:trivial-fail
))
490 (assert (eq (try-semaphore (make-semaphore :count
0)) 'nil
)))
492 (with-test (:name
(:try-semaphore
:trivial-success
))
493 (let ((sem (make-semaphore :count
1)))
494 (assert (= 0 (try-semaphore sem
)))
495 (assert (zerop (semaphore-count sem
)))))
497 (with-test (:name
(:try-semaphore
:trivial-fail
:n
>1))
498 (assert (eq (try-semaphore (make-semaphore :count
1) 2) 'nil
)))
500 (with-test (:name
(:try-semaphore
:trivial-success
:n
>1))
501 (let ((sem (make-semaphore :count
10)))
502 (assert (= 5 (try-semaphore sem
5)))
503 (assert (= 0 (try-semaphore sem
5)))
504 (assert (zerop (semaphore-count sem
)))))
506 (with-test (:name
(:try-semaphore
:emulate-wait-on-semaphore
))
507 (flet ((busy-wait-on-semaphore (sem)
508 (loop until
(try-semaphore sem
) do
(sleep 0.001))))
509 (test-semaphore-multiple-signals #'busy-wait-on-semaphore
)))
511 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
512 ;;; semaphore in a bad state.
513 (with-test (:name
(:try-semaphore
:interrupt-safe
))
514 (flet ((make-threads (count fn
)
515 (loop repeat count collect
(make-thread fn
)))
516 (kill-thread (thread)
517 (when (thread-alive-p thread
)
518 (ignore-errors (terminate-thread thread
))))
519 (count-live-threads (threads)
520 (count-if #'thread-alive-p threads
)))
521 ;; WAITERS will already be waiting on the semaphore while
522 ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
523 ;; semaphore, and MORE-WAITERS are new threads trying to wait on
524 ;; the semaphore during the interruption-fire.
525 (let* ((sem (make-semaphore :count
100))
526 (waiters (make-threads 20 #'(lambda ()
527 (wait-on-semaphore sem
))))
528 (triers (make-threads 40 #'(lambda ()
529 (sleep (random 0.01))
530 (try-semaphore sem
(1+ (random 5))))))
533 do
(kill-thread (nth (random 40) triers
))
534 collect
(make-thread #'(lambda () (wait-on-semaphore sem
)))
535 do
(kill-thread (nth (random 40) triers
)))))
537 ;; Now ensure that the waiting threads will all be waked up,
538 ;; i.e. that the semaphore is still working.
539 (loop repeat
(+ (count-live-threads waiters
)
540 (count-live-threads more-waiters
))
541 do
(signal-semaphore sem
))
543 (assert (zerop (count-live-threads triers
)))
544 (assert (zerop (count-live-threads waiters
)))
545 (assert (zerop (count-live-threads more-waiters
))))))
547 (format t
"~&semaphore tests done~%")
549 (defun test-interrupt (function-to-interrupt &optional quit-p
)
550 (let ((child (make-kill-thread function-to-interrupt
)))
551 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
553 (format t
"interrupting child ~A~%" child
)
554 (interrupt-thread child
556 (format t
"child pid ~A~%" *current-thread
*)
557 (when quit-p
(abort-thread))))
561 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
562 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
565 (with-test (:name
(:interrupt-thread
:more-basics
))
566 (let ((child (test-interrupt (lambda () (loop)))))
567 (terminate-thread child
)))
569 (with-test (:name
(:interrupt-thread
:interrupt-foreign-loop
)
570 ;; This feature is explicitly unsupported on Win32.
572 (test-interrupt #'loop-forever
:quit
))
574 (with-test (:name
(:interrupt-thread
:interrupt-sleep
))
575 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
576 (terminate-thread child
)
577 (wait-for-threads (list child
))))
579 (with-test (:name
(:interrupt-thread
:interrupt-mutex-acquisition
))
580 (let ((lock (make-mutex :name
"loctite"))
583 (setf child
(test-interrupt
586 (assert (eql (mutex-value lock
) *current-thread
*)))
587 (assert (not (eql (mutex-value lock
) *current-thread
*)))
589 ;;hold onto lock for long enough that child can't get it immediately
591 (interrupt-thread child
(lambda () (format t
"l ~A~%" (mutex-value lock
))))
592 (format t
"parent releasing lock~%"))
593 (terminate-thread child
)
594 (wait-for-threads (list child
))))
596 (format t
"~&locking test done~%")
598 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
600 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
))
601 (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
603 (loop repeat
4 collect
604 (sb-thread:make-thread
607 (sleep (random 0.1d0
))
610 (sb-thread:interrupt-thread thread
(lambda ()))))))))
611 (wait-for-threads killers
)
612 (sb-thread:terminate-thread thread
)
613 (wait-for-threads (list thread
))))
616 (format t
"~&multi interrupt test done~%")
618 #+(or x86 x86-64
) ;; x86oid-only, see internal commentary.
619 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
:again
))
620 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
621 ;; NB this only works on x86: other ports don't have a symbol for
622 ;; pseudo-atomic atomicity
624 (sleep (random 0.1d0
))
627 (princ ".") (force-output)
628 (assert (thread-alive-p *current-thread
*))
630 (not (logbitp 0 SB-KERNEL
:*PSEUDO-ATOMIC-BITS
*))))))
632 (wait-for-threads (list c
))))
634 (format t
"~&interrupt test done~%")
636 (defstruct counter
(n 0 :type sb-vm
:word
))
637 (defvar *interrupt-counter
* (make-counter))
639 (declaim (notinline check-interrupt-count
))
640 (defun check-interrupt-count (i)
641 (declare (optimize (debug 1) (speed 1)))
642 ;; This used to lose if eflags were not restored after an interrupt.
643 (unless (typep i
'fixnum
)
644 (error "!!!!!!!!!!!")))
646 (with-test (:name
(:interrupt-thread
:interrupt-ATOMIC-INCF
))
647 (let ((c (make-thread
649 (handler-bind ((error #'(lambda (cond)
652 most-positive-fixnum
))))
653 (loop (check-interrupt-count
654 (counter-n *interrupt-counter
*))))))))
655 (let ((func (lambda ()
658 (sb-ext:atomic-incf
(counter-n *interrupt-counter
*)))))
659 (setf (counter-n *interrupt-counter
*) 0)
661 (sleep (random 0.1d0
))
662 (interrupt-thread c func
))
663 (loop until
(= (counter-n *interrupt-counter
*) 100) do
(sleep 0.1))
665 (wait-for-threads (list c
)))))
667 (format t
"~&interrupt count test done~%")
669 (defvar *runningp
* nil
)
671 (with-test (:name
(:interrupt-thread
:no-nesting
))
672 (let ((thread (sb-thread:make-thread
676 (declare (special runningp
))
678 (sb-thread:interrupt-thread thread
680 (let ((*runningp
* t
))
683 (sb-thread:interrupt-thread thread
685 (throw 'xxx
*runningp
*)))
686 (assert (not (sb-thread:join-thread thread
)))))
688 (with-test (:name
(:interrupt-thread
:nesting
))
689 (let ((thread (sb-thread:make-thread
693 (declare (special runningp
))
695 (sb-thread:interrupt-thread thread
697 (let ((*runningp
* t
))
698 (sb-sys:with-interrupts
701 (sb-thread:interrupt-thread thread
703 (throw 'xxx
*runningp
*)))
704 (assert (sb-thread:join-thread thread
))))
706 (with-test (:name
(:two-threads-running-gc
))
708 (make-join-thread (lambda ()
710 (sb-ext:gc
) (princ "\\") (force-output))
712 (make-join-thread (lambda ()
715 (princ "/") (force-output))
718 (when (and a-done b-done
) (return))
723 (defun waste (&optional
(n 100000))
724 (loop repeat n do
(make-string 16384)))
726 (with-test (:name
(:one-thread-runs-gc-while-other-conses
))
727 (loop for i below
100 do
738 (defparameter *aaa
* nil
)
739 (with-test (:name
(:one-thread-runs-gc-while-other-conses
:again
))
740 (loop for i below
100 do
745 (let ((*aaa
* (waste)))
747 (let ((*aaa
* (waste)))
751 (format t
"~&gc test done~%")
753 ;; this used to deadlock on session-lock
754 (with-test (:name
(:no-session-deadlock
))
755 (make-join-thread (lambda () (sb-ext:gc
))))
757 (defun exercise-syscall (fn reference-errno
)
762 (let ((errno (sb-unix::get-errno
)))
763 (sleep (random 0.1d0
))
764 (unless (eql errno reference-errno
)
765 (format t
"Got errno: ~A (~A) instead of ~A~%"
772 ;; (nanosleep -1 0) does not fail on FreeBSD
773 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
775 (nanosleep-errno (progn
776 (sb-unix:nanosleep -
1 0)
777 (sb-unix::get-errno
)))
780 :if-does-not-exist nil
)
781 (sb-unix::get-errno
)))
785 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
786 (exercise-syscall (lambda () (open "no-such-file"
787 :if-does-not-exist nil
))
789 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
791 (princ "terminating threads")
792 (dolist (thread threads
)
793 (sb-thread:terminate-thread thread
))))
795 (format t
"~&errno test done~%")
797 (with-test (:name
:all-threads-have-abort-restart
)
799 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
800 (sb-thread:interrupt-thread
803 (assert (find-restart 'abort
)))))))
807 (format t
"~&thread startup sigmask test done~%")
809 (with-test (:name
(:debugger-no-hang-on-session-lock-if-interrupted
)
811 #+win32
(error "user would have to touch a key interactively to proceed")
812 (sb-debug::enable-debugger
)
813 (let* ((main-thread *current-thread
*)
815 (make-thread (lambda ()
817 (interrupt-thread main-thread
822 (interrupt-thread main-thread
#'continue
))
823 :name
"interruptor")))
824 (with-session-lock (*session
*)
826 (loop while
(thread-alive-p interruptor-thread
))))
828 (format t
"~&session lock test done~%")
830 ;; expose thread creation races by exiting quickly
831 (with-test (:name
(:no-thread-creation-race
:light
))
832 (make-join-thread (lambda ())))
834 (with-test (:name
(:no-thread-creation-race
:heavy
))
837 (loop for i below
100 collect
838 (sb-thread:make-thread
(lambda ()))))))
840 (format t
"~&creation test done~%")
842 ;; interrupt handlers are per-thread with pthreads, make sure the
843 ;; handler installed in one thread is global
844 (with-test (:name
(:global-interrupt-handler
))
847 (sb-ext:run-program
"sleep" '("1") :search t
:wait nil
))))
849 ;;;; Binding stack safety
851 (defparameter *x
* nil
)
852 (defparameter *n-gcs-requested
* 0)
853 (defparameter *n-gcs-done
* 0)
856 (defun make-something-big ()
857 (let ((x (make-string 32000)))
859 (let ((counter counter
))
860 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
863 (defmacro wait-for-gc
()
865 (incf *n-gcs-requested
*)
866 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
869 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
875 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
876 (defun exercise-binding ()
878 (let ((*x
* (make-something-big)))
880 ;; at this point the binding stack looks like this:
881 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
884 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
885 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
886 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
887 ;; unbinding but values are not).
889 (binding-pointer-delta (ash 2 (- sb-vm
:word-shift sb-vm
:n-fixnum-tag-bits
))))
890 ;; bump bsp as if a BIND had just started
891 (incf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
)
893 (decf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
))))
895 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
896 (with-test (:name
(:binding-stack-gc-safety
))
900 (push (make-kill-thread #'exercise-binding
) threads
)
901 (push (make-kill-thread (lambda ()
907 (mapc #'sb-thread
:terminate-thread threads
))))
909 (with-test (:name
:test-%thread-local-references
)
910 (let ((mysym (gensym))
913 (progv (list mysym
) '(nil)
914 (let* ((i (get-lisp-obj-address (sb-vm:symbol-tls-index mysym
)))
915 (j (+ i sb-vm
:n-word-bytes
)))
916 (assert (eql (sap-ref-word (current-thread-sap) j
)
917 sb-vm
:no-tls-value-marker-widetag
))
918 (setf (sap-ref-lispobj (current-thread-sap) i
) fool1
919 (sap-ref-lispobj (current-thread-sap) j
) fool2
)
920 ;; assert that my pointer arithmetic worked as expected
921 (assert (eq (symbol-value mysym
) fool1
))
922 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
923 (let ((list (%thread-local-references
)))
924 (assert (memq fool1 list
))
925 (assert (not (memq fool2 list
))))
926 ;; repair the TLS entry that was corrupted by the test
927 (setf (sap-ref-word (current-thread-sap) j
)
928 sb-vm
:no-tls-value-marker-widetag
)))))
930 (format t
"~&binding test done~%")
934 (defvar *errors
* nil
)
938 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
942 (with-test (:name
(:unsynchronized-hash-table
)
943 ;; FIXME: This test occasionally eats out craploads
944 ;; of heap instead of expected error early. Not 100%
945 ;; sure if it would finish as expected, but since it
946 ;; hits swap on my system I'm not likely to find out
947 ;; soon. Disabling for now. -- nikodemus
949 ;; We expect a (probable) error here: parellel readers and writers
950 ;; on a hash-table are not expected to work -- but we also don't
951 ;; expect this to corrupt the image.
952 (let* ((hash (make-hash-table))
954 (threads (list (make-kill-thread
957 (handler-bind ((serious-condition 'oops
))
959 ;;(princ "1") (force-output)
960 (setf (gethash (random 100) hash
) 'h
)))))
965 (handler-bind ((serious-condition 'oops
))
967 ;;(princ "2") (force-output)
968 (remhash (random 100) hash
)))))
973 (handler-bind ((serious-condition 'oops
))
976 (sb-ext:gc
:full t
)))))
977 :name
"collector"))))
980 (mapc #'sb-thread
:terminate-thread threads
))))
982 (format t
"~&unsynchronized hash table test done~%")
984 (with-test (:name
(:synchronized-hash-table
))
985 (let* ((hash (make-hash-table :synchronized t
))
987 (threads (list (make-join-thread
990 (handler-bind ((serious-condition 'oops
))
992 ;;(princ "1") (force-output)
993 (setf (gethash (random 100) hash
) 'h
)))))
998 (handler-bind ((serious-condition 'oops
))
1000 ;;(princ "2") (force-output)
1001 (remhash (random 100) hash
)))))
1006 (handler-bind ((serious-condition 'oops
))
1008 (sleep (random 1.0))
1009 (sb-ext:gc
:full t
)))))
1010 :name
"collector"))))
1013 (mapc #'sb-thread
:terminate-thread threads
))
1014 (assert (not *errors
*))))
1016 (format t
"~&synchronized hash table test done~%")
1018 (with-test (:name
(:hash-table-parallel-readers
))
1019 (let ((hash (make-hash-table))
1022 do
(setf (gethash (random 100) hash
) 'xxx
))
1023 (let ((threads (list (make-kill-thread
1026 (handler-bind ((serious-condition 'oops
))
1028 until
(eq t
(gethash (random 100) hash
))))))
1033 (handler-bind ((serious-condition 'oops
))
1035 until
(eq t
(gethash (random 100) hash
))))))
1040 (handler-bind ((serious-condition 'oops
))
1042 until
(eq t
(gethash (random 100) hash
))))))
1047 (handler-bind ((serious-condition 'oops
))
1049 (sleep (random 1.0))
1050 (sb-ext:gc
:full t
)))))
1051 :name
"collector"))))
1054 (mapc #'sb-thread
:terminate-thread threads
))
1055 (assert (not *errors
*)))))
1057 (format t
"~&multiple reader hash table test done~%")
1059 (with-test (:name
(:hash-table-single-accessor-parallel-gc
))
1060 (let ((hash (make-hash-table))
1062 (let ((threads (list (make-kill-thread
1064 (handler-bind ((serious-condition 'oops
))
1066 (let ((n (random 100)))
1067 (if (gethash n hash
)
1069 (setf (gethash n hash
) 'h
))))))
1073 (handler-bind ((serious-condition 'oops
))
1075 (sleep (random 1.0))
1076 (sb-ext:gc
:full t
))))
1077 :name
"collector"))))
1080 (mapc #'sb-thread
:terminate-thread threads
))
1081 (assert (not *errors
*)))))
1083 (format t
"~&single accessor hash table test~%")
1085 #|
;; a cll post from eric marsden
1087 |
(setq *debugger-hook
*
1088 |
(lambda (condition old-debugger-hook
)
1089 |
(debug:backtrace
10)
1090 |
(unix:unix-exit
2)))
1091 |
#+live-dangerously
1092 |
(mp::start-sigalrm-yield
)
1093 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
1094 |
(mp:make-process
#'roomy
)
1095 |
(mp:make-process
#'roomy
)))
1098 (with-test (:name
(:condition-variable
:notify-multiple
))
1099 (flet ((tester (notify-fun)
1100 (let ((queue (make-waitqueue :name
"queue"))
1101 (lock (make-mutex :name
"lock"))
1106 (format t
"condition-wait ~a~%" x
)
1108 (condition-wait queue lock
)
1109 (format t
"woke up ~a~%" x
)
1112 (let ((threads (loop for x from
1 to
10
1115 (make-kill-thread (lambda ()
1119 (funcall notify-fun queue
))
1121 (mapcar #'terminate-thread threads
)
1122 ;; Check that all threads woke up at least once
1123 (assert (= (length (remove-duplicates data
)) 10)))))))
1124 (tester (lambda (queue)
1125 (format t
"~&(condition-notify queue 10)~%")
1127 (condition-notify queue
10)))
1128 (tester (lambda (queue)
1129 (format t
"~&(condition-broadcast queue)~%")
1131 (condition-broadcast queue
)))))
1133 (format t
"waitqueue wakeup tests done~%")
1135 ;;; Make sure that a deadline handler is not invoked twice in a row in
1136 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1138 (with-test (:name
(:condition-wait
:deadlines
:LP-512914
))
1139 (let ((n 2) ; was empirically enough to trigger the bug
1140 (mutex (sb-thread:make-mutex
))
1141 (waitq (sb-thread:make-waitqueue
))
1143 (deadline-handler-run-twice? nil
))
1146 (sb-thread:make-thread
1149 ((sb-sys:deadline-timeout
1150 (let ((already? nil
))
1153 (setq deadline-handler-run-twice? t
))
1156 (sb-thread:condition-broadcast waitq
)
1157 (sb-sys:defer-deadline
10.0 c
)))))
1158 (sb-sys:with-deadline
(:seconds
0.1)
1159 (sb-thread:with-mutex
(mutex)
1160 (sb-thread:condition-wait waitq mutex
))))))))
1161 (push child threads
)))
1162 (mapc #'sb-thread
:join-thread threads
)
1163 (assert (not deadline-handler-run-twice?
))))
1165 (with-test (:name
(:condition-wait
:signal-deadline-with-interrupts-enabled
))
1166 (let ((mutex (sb-thread:make-mutex
))
1167 (waitq (sb-thread:make-waitqueue
))
1170 (A-interrupts-enabled?
:unknown
)
1171 (B-interrupts-enabled?
:unknown
)
1174 ;; W.L.O.G., we assume that A is executed first...
1175 (setq A
(sb-thread:make-thread
1178 ((sb-sys:deadline-timeout
1180 ;; We came here through the call to DECODE-TIMEOUT
1181 ;; in CONDITION-WAIT; hence both here are supposed
1182 ;; to evaluate to T.
1183 (setq A-holds?
(sb-thread:holding-mutex-p mutex
))
1184 (setq A-interrupts-enabled?
1185 sb-sys
:*interrupts-enabled
*)
1187 (sb-thread:condition-broadcast waitq
)
1188 (sb-sys:defer-deadline
10.0 c
))))
1189 (sb-sys:with-deadline
(:seconds
0.1)
1190 (sb-thread:with-mutex
(mutex)
1191 (sb-thread:condition-wait waitq mutex
)))))
1193 (setq B
(sb-thread:make-thread
1197 ((sb-sys:deadline-timeout
1199 ;; We came here through the call to DECODE-TIMEOUT
1200 ;; in CONDITION-WAIT (contended case of
1201 ;; reaquiring the mutex) - so the former will
1202 ;; be NIL, but interrupts should still be enabled.
1203 (setq B-holds?
(sb-thread:holding-mutex-p mutex
))
1204 (setq B-interrupts-enabled?
1205 sb-sys
:*interrupts-enabled
*)
1207 (sb-thread:condition-broadcast waitq
)
1208 (sb-sys:defer-deadline
10.0 c
))))
1209 (sb-sys:with-deadline
(:seconds
0.1)
1210 (sb-thread:with-mutex
(mutex)
1211 (sb-thread:condition-wait waitq mutex
)))))
1213 (sb-thread:join-thread A
)
1214 (sb-thread:join-thread B
)
1215 (let ((A-result (list A-holds? A-interrupts-enabled?
))
1216 (B-result (list B-holds? B-interrupts-enabled?
)))
1217 ;; We also check some subtle behaviour w.r.t. whether a deadline
1218 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1219 ;; probably very internal behaviour (so user should not depend
1220 ;; on it) -- I added the testing here just to manifest current
1222 (cond ((equal A-result
'(t t
)) (assert (equal B-result
'(nil t
))))
1223 ((equal B-result
'(t t
)) (assert (equal A-result
'(nil t
))))
1225 (error "Failure: fell through wit A: ~S, B: ~S"
1229 (with-test (:name
(:mutex
:finalization
))
1232 (setf a
(make-mutex)))))
1234 (format t
"mutex finalization test done~%")
1236 ;; You have to shoehorn this arbitrary sexpr into a feature expression
1237 ;; to have the test summary show that a test was disabled.
1239 (unless (eql (extern-alien "verify_gens" int
)
1240 (1+ sb-vm
:+highest-normal-generation
+))
1241 (pushnew :verify-gens
*features
*))
1243 (with-test (:name
:backtrace
:skipped-on
':verify-gens
)
1244 ;; Printing backtraces from several threads at once used to hang the
1245 ;; whole SBCL process (discovered by accident due to a timer.impure
1246 ;; test misbehaving). The cause was that packages weren't even
1247 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1248 ;; backtraces a loot of symbol lookups need to be done due to
1250 (let* ((threads (loop repeat
10
1251 collect
(sb-thread:make-thread
1254 (with-output-to-string (*debug-io
*)
1255 (sb-debug::backtrace
10))))))))
1256 (wait-for-threads threads
)))
1258 (format t
"backtrace test done~%")
1260 (format t
"~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1262 (with-test (:name
:gc-deadlock
)
1263 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1264 ;; GC due to *all-threads-lock* and session lock. On earlier
1265 ;; versions and at least on one specific box this test is good enough
1266 ;; to catch that typically well before the 1500th iteration.
1273 (when (zerop (mod i
100))
1280 (sleep (random 0.001)))
1281 :name
(format nil
"SLEEP-~D" i
))
1284 ;; KLUDGE: what we are doing here is explicit,
1285 ;; but the same can happen because of a regular
1286 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1287 ;; session functions.
1288 (sb-thread::with-all-threads-lock
1289 (sb-thread::with-session-lock
(sb-thread::*session
*)
1291 :name
(format nil
"GC-~D" i
)))
1293 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
1297 (format t
"~&gc deadlock test done~%")
1299 (let ((count (make-array 8 :initial-element
0)))
1300 (defun closure-one ()
1301 (declare (optimize safety
))
1302 (values (incf (aref count
0)) (incf (aref count
1))
1303 (incf (aref count
2)) (incf (aref count
3))
1304 (incf (aref count
4)) (incf (aref count
5))
1305 (incf (aref count
6)) (incf (aref count
7))))
1306 (defun no-optimizing-away-closure-one ()
1307 (setf count
(make-array 8 :initial-element
0))))
1312 (let ((one (make-box))
1315 (defun closure-two ()
1316 (declare (optimize safety
))
1317 (values (incf (box-count one
)) (incf (box-count two
)) (incf (box-count three
))))
1318 (defun no-optimizing-away-closure-two ()
1319 (setf one
(make-box)
1323 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1324 ;;; sometimes run out of memory) in the following test. For developers
1325 ;;; interested in debugging this combination of features, it might be
1326 ;;; fruitful to concentrate their efforts around this test...
1328 (with-test (:name
(:funcallable-instances
)
1329 :skipped-on
'(and :sb-safepoint
1330 (not :c-stack-is-control-stack
)))
1331 ;; the funcallable-instance implementation used not to be threadsafe
1332 ;; against setting the funcallable-instance function to a closure
1333 ;; (because the code and lexenv were set separately).
1334 (let ((fun (sb-kernel:%make-funcallable-instance
0))
1336 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1338 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1339 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1341 (handler-case (loop (funcall fun
))
1342 (serious-condition (c) (setf condition c
)))))
1343 (let ((changer (make-thread #'changer
))
1344 (test (make-thread #'test
)))
1347 ;; The two closures above are fairly carefully crafted
1348 ;; so that if given the wrong lexenv they will tend to
1349 ;; do some serious damage, but it is of course difficult
1350 ;; to predict where the various bits and pieces will be
1351 ;; allocated. Five seconds failed fairly reliably on
1352 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1353 (sb-ext:with-timeout
5
1354 (wait-for-threads (list test
)))
1355 (error "~@<test thread got condition:~2I~_~A~@:>" condition
))
1357 (terminate-thread changer
)
1358 (terminate-thread test
)
1359 (wait-for-threads (list changer test
))))))))
1361 (format t
"~&funcallable-instance test done~%")
1363 (defun random-type (n)
1364 `(integer ,(random n
) ,(+ n
(random n
))))
1366 (defun subtypep-hash-cache-test ()
1368 (let ((type1 (random-type 500))
1369 (type2 (random-type 500)))
1370 (let ((a (subtypep type1 type2
)))
1372 (assert (eq (subtypep type1 type2
) a
))))))
1376 (with-test (:name
(:hash-cache
:subtypep
))
1378 ;; this didn't "reliably fail" with a small number of threads.
1379 ;; 30 is a compromise between running time and confidence in the result.
1381 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
)))
1384 ;;;; BLACK BOX TESTS
1386 (in-package :cl-user
)
1387 (use-package :test-util
)
1388 (use-package "ASSERTOID")
1390 (format t
"parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1391 (with-test (:name
:parallel-defclass
)
1392 (defclass test-1
() ((a :initform
:orig-a
)))
1393 (defclass test-2
() ((b :initform
:orig-b
)))
1394 (defclass test-3
(test-1 test-2
) ((c :initform
:orig-c
)))
1395 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1396 ;; calls disabled in the folloving code. (As seen on a Server 2012
1397 ;; installation.) Clearly, this sort of workaround in a test is
1398 ;; cheating, and might be hiding the underlying bug that the test is
1399 ;; exposing. Let's review this later.
1401 (d1 (sb-thread:make-thread
(lambda ()
1403 do
(defclass test-1
() ((a :initform
:new-a
)))
1405 #-win32
(force-output)))
1407 (d2 (sb-thread:make-thread
(lambda ()
1409 do
(defclass test-2
() ((b :initform
:new-b
)))
1411 #-win32
(force-output)))
1413 (d3 (sb-thread:make-thread
(lambda ()
1415 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1417 #-win32
(force-output)))
1419 (i (sb-thread:make-thread
(lambda ()
1421 do
(let ((i (make-instance 'test-3
)))
1422 (assert (member (slot-value i
'a
) '(:orig-a
:new-a
)))
1423 (assert (member (slot-value i
'b
) '(:orig-b
:new-b
)))
1424 (assert (member (slot-value i
'c
) '(:orig-c
:new-c
))))
1426 #-win32
(force-output)))
1428 (format t
"~%sleeping!~%")
1430 (format t
"~%stopping!~%")
1433 (sb-thread:join-thread th
)
1434 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
1437 (format t
"parallel defclass test done~%")
1439 (with-test (:name
(:deadlock-detection
:interrupts
) :fails-on
:win32
)
1440 #+win32
;be more explicit than just :skipped-on
1441 (error "not attempting, because of deadlock error in background thread")
1442 (let* ((m1 (sb-thread:make-mutex
:name
"M1"))
1443 (m2 (sb-thread:make-mutex
:name
"M2"))
1444 (t1-can-go (sb-thread:make-semaphore
:name
"T1 can go"))
1445 (t2-can-go (sb-thread:make-semaphore
:name
"T2 can go"))
1446 (t1 (sb-thread:make-thread
1448 (sb-thread:with-mutex
(m1)
1449 (sb-thread:wait-on-semaphore t1-can-go
)
1452 (t2 (sb-thread:make-thread
1454 (sb-ext:wait-for
(eq t1
(sb-thread:mutex-owner m1
)))
1455 (sb-thread:with-mutex
(m1 :wait-p t
)
1456 (sb-thread:wait-on-semaphore t2-can-go
)
1459 (sb-ext:wait-for
(eq m1
(sb-thread::thread-waiting-for t2
)))
1460 (sb-thread:interrupt-thread t2
(lambda ()
1461 (sb-thread:with-mutex
(m2 :wait-p t
)
1463 (eq m2
(sb-thread::thread-waiting-for t1
)))
1464 (sb-thread:signal-semaphore t2-can-go
))))
1465 (sb-ext:wait-for
(eq t2
(sb-thread:mutex-owner m2
)))
1466 (sb-thread:interrupt-thread t1
(lambda ()
1467 (sb-thread:with-mutex
(m2 :wait-p t
)
1468 (sb-thread:signal-semaphore t1-can-go
))))
1469 ;; both threads should finish without a deadlock or deadlock
1471 (let ((res (list (sb-thread:join-thread t1
)
1472 (sb-thread:join-thread t2
))))
1473 (assert (equal '(:ok1
:ok2
) res
)))))
1475 (with-test (:name
(:deadlock-detection
:gc
))
1476 ;; To semi-reliably trigger the error (in SBCL's where)
1477 ;; it was present you had to run this for > 30 seconds,
1478 ;; but that's a bit long for a single test.
1479 (let* ((stop (+ 5 (get-universal-time)))
1480 (m1 (sb-thread:make-mutex
:name
"m1"))
1481 (t1 (sb-thread:make-thread
1483 (loop until
(> (get-universal-time) stop
)
1484 do
(sb-thread:with-mutex
(m1)
1485 (eval `(make-array 24))))
1487 (t2 (sb-thread:make-thread
1489 (loop until
(> (get-universal-time) stop
)
1490 do
(sb-thread:with-mutex
(m1)
1491 (eval `(make-array 24))))
1493 (let ((res (list (sb-thread:join-thread t1
)
1494 (sb-thread:join-thread t2
))))
1495 (assert (equal '(:ok
:ok
) res
)))))
1497 (with-test (:name
:spinlock-api
)
1498 (handler-bind ((warning #'error
))
1499 (destructuring-bind (with make get release
)
1501 (list (compile nil
`(lambda (lock)
1502 (sb-thread::with-spinlock
(lock)
1504 (compile nil
`(lambda ()
1505 (sb-thread::make-spinlock
:name
"foo")))
1506 (compile nil
`(lambda (lock)
1507 (sb-thread::get-spinlock lock
)))
1508 (compile nil
`(lambda (lock)
1509 (sb-thread::release-spinlock lock
))))
1510 early-deprecation-warning
4)
1511 (let ((lock (funcall make
)))
1513 (funcall release lock
)
1514 (assert (eq t
(funcall with lock
)))))))
1516 (with-test (:name
:interrupt-io-unnamed-pipe
)
1520 (let ((stream (sb-sys:make-fd-stream fd
1521 :element-type
:default
1522 :serve-events nil
)))
1524 (let ((ok (handler-case
1535 (format *trace-output
* "~&=> ~A~%" ok
)
1536 (force-output *trace-output
*))))
1538 (ignore-errors (close stream
))))
1541 (multiple-value-bind (read write
)
1543 (let* ((reader (sb-thread:make-thread
(lambda () (reader read
))))
1544 (stream (sb-sys:make-fd-stream write
1546 :element-type
:default
1550 (sb-thread:interrupt-thread reader
(lambda ()
1556 (write-char #\x stream
)
1558 (sb-thread:join-thread reader
)))))
1560 (assert (eq result
:ok
))))
1562 (with-test (:name
:thread-alloca
)
1563 (sb-ext:run-program
"sh"
1564 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1565 "alloca.c" "-o" "alloca.so")
1567 (load-shared-object (truename "alloca.so"))
1569 (alien-funcall (extern-alien "alloca_test" (function void
)))
1570 (sb-thread:join-thread
1571 (sb-thread:make-thread
1573 (alien-funcall (extern-alien "alloca_test" (function void
)))))))
1575 (with-test (:name
:fp-mode-inheritance-threads
)
1577 (let* ((reserved-bits #+x86
(ash #b1110000011000000
16)
1579 (fp-mode (logandc2 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))
1582 (sb-thread:join-thread
1583 (sb-thread:make-thread
1585 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
)))))))
1586 (assert (= fp-mode thread-fp-mode
)))))
1588 (sb-int:with-float-traps-masked
(:divide-by-zero
)
1590 (setf (sb-vm:floating-point-modes
)
1591 (dpb sb-vm
:float-divide-by-zero-trap-bit
1592 sb-vm
::float-traps-byte
1593 (sb-vm:floating-point-modes
)))