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 ;; At some point %DECREMENT-SEMAPHORE did not adjust the remaining
548 ;; timeout after spurious wakeups, potentially leading to
549 ;; longer/infinite waiting despite the specified timeout.
550 (with-test (:name
(:semaphore
:timeout
:spurious-wakeup
))
551 (let* ((semaphore (make-semaphore))
553 (thread (make-thread (lambda ()
554 (let ((mutex (semaphore-mutex semaphore
))
555 (queue (semaphore-queue semaphore
)))
556 (loop :until done
:do
558 (condition-notify queue
))))))))
559 (assert (eq nil
(wait-on-semaphore semaphore
:timeout
.5)))
561 (join-thread thread
)))
563 (format t
"~&semaphore tests done~%")
565 (defun test-interrupt (function-to-interrupt &optional quit-p
)
566 (let ((child (make-kill-thread function-to-interrupt
)))
567 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
569 (format t
"interrupting child ~A~%" child
)
570 (interrupt-thread child
572 (format t
"child pid ~A~%" *current-thread
*)
573 (when quit-p
(abort-thread))))
577 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
578 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
581 (with-test (:name
(:interrupt-thread
:more-basics
))
582 (let ((child (test-interrupt (lambda () (loop)))))
583 (terminate-thread child
)))
585 (with-test (:name
(:interrupt-thread
:interrupt-foreign-loop
)
586 ;; This feature is explicitly unsupported on Win32.
588 (test-interrupt #'loop-forever
:quit
))
590 (with-test (:name
(:interrupt-thread
:interrupt-sleep
))
591 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
592 (terminate-thread child
)
593 (wait-for-threads (list child
))))
595 (with-test (:name
(:interrupt-thread
:interrupt-mutex-acquisition
))
596 (let ((lock (make-mutex :name
"loctite"))
599 (setf child
(test-interrupt
602 (assert (eql (mutex-value lock
) *current-thread
*)))
603 (assert (not (eql (mutex-value lock
) *current-thread
*)))
605 ;;hold onto lock for long enough that child can't get it immediately
607 (interrupt-thread child
(lambda () (format t
"l ~A~%" (mutex-value lock
))))
608 (format t
"parent releasing lock~%"))
609 (terminate-thread child
)
610 (wait-for-threads (list child
))))
612 (format t
"~&locking test done~%")
614 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
616 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
))
617 (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
619 (loop repeat
4 collect
620 (sb-thread:make-thread
623 (sleep (random 0.1d0
))
626 (sb-thread:interrupt-thread thread
(lambda ()))))))))
627 (wait-for-threads killers
)
628 (sb-thread:terminate-thread thread
)
629 (wait-for-threads (list thread
))))
632 (format t
"~&multi interrupt test done~%")
634 #+(or x86 x86-64
) ;; x86oid-only, see internal commentary.
635 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
:again
))
636 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
637 ;; NB this only works on x86: other ports don't have a symbol for
638 ;; pseudo-atomic atomicity
640 (sleep (random 0.1d0
))
643 (princ ".") (force-output)
644 (assert (thread-alive-p *current-thread
*))
646 (not (logbitp 0 SB-KERNEL
:*PSEUDO-ATOMIC-BITS
*))))))
648 (wait-for-threads (list c
))))
650 (format t
"~&interrupt test done~%")
652 (defstruct counter
(n 0 :type sb-vm
:word
))
653 (defvar *interrupt-counter
* (make-counter))
655 (declaim (notinline check-interrupt-count
))
656 (defun check-interrupt-count (i)
657 (declare (optimize (debug 1) (speed 1)))
658 ;; This used to lose if eflags were not restored after an interrupt.
659 (unless (typep i
'fixnum
)
660 (error "!!!!!!!!!!!")))
662 (with-test (:name
(:interrupt-thread
:interrupt-ATOMIC-INCF
))
663 (let ((c (make-thread
665 (handler-bind ((error #'(lambda (cond)
667 (sb-debug:print-backtrace
668 :count most-positive-fixnum
))))
669 (loop (check-interrupt-count
670 (counter-n *interrupt-counter
*))))))))
671 (let ((func (lambda ()
674 (sb-ext:atomic-incf
(counter-n *interrupt-counter
*)))))
675 (setf (counter-n *interrupt-counter
*) 0)
677 (sleep (random 0.1d0
))
678 (interrupt-thread c func
))
679 (loop until
(= (counter-n *interrupt-counter
*) 100) do
(sleep 0.1))
681 (wait-for-threads (list c
)))))
683 (format t
"~&interrupt count test done~%")
685 (defvar *runningp
* nil
)
687 (with-test (:name
(:interrupt-thread
:no-nesting
))
688 (let ((thread (sb-thread:make-thread
692 (declare (special runningp
))
694 (sb-thread:interrupt-thread thread
696 (let ((*runningp
* t
))
699 (sb-thread:interrupt-thread thread
701 (throw 'xxx
*runningp
*)))
702 (assert (not (sb-thread:join-thread thread
)))))
704 (with-test (:name
(:interrupt-thread
:nesting
))
705 (let ((thread (sb-thread:make-thread
709 (declare (special runningp
))
711 (sb-thread:interrupt-thread thread
713 (let ((*runningp
* t
))
714 (sb-sys:with-interrupts
717 (sb-thread:interrupt-thread thread
719 (throw 'xxx
*runningp
*)))
720 (assert (sb-thread:join-thread thread
))))
722 (with-test (:name
(:two-threads-running-gc
))
724 (make-join-thread (lambda ()
726 (sb-ext:gc
) (princ "\\") (force-output))
728 (make-join-thread (lambda ()
731 (princ "/") (force-output))
734 (when (and a-done b-done
) (return))
739 (defun waste (&optional
(n 100000))
740 (loop repeat n do
(make-string 16384)))
742 (with-test (:name
(:one-thread-runs-gc-while-other-conses
))
743 (loop for i below
100 do
754 (defparameter *aaa
* nil
)
755 (with-test (:name
(:one-thread-runs-gc-while-other-conses
:again
))
756 (loop for i below
100 do
761 (let ((*aaa
* (waste)))
763 (let ((*aaa
* (waste)))
767 (format t
"~&gc test done~%")
769 ;; this used to deadlock on session-lock
770 (with-test (:name
(:no-session-deadlock
))
771 (make-join-thread (lambda () (sb-ext:gc
))))
773 (defun exercise-syscall (fn reference-errno
)
778 (let ((errno (sb-unix::get-errno
)))
779 (sleep (random 0.1d0
))
780 (unless (eql errno reference-errno
)
781 (format t
"Got errno: ~A (~A) instead of ~A~%"
788 ;; (nanosleep -1 0) does not fail on FreeBSD
789 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
791 (nanosleep-errno (progn
792 (sb-unix:nanosleep -
1 0)
793 (sb-unix::get-errno
)))
796 :if-does-not-exist nil
)
797 (sb-unix::get-errno
)))
801 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
802 (exercise-syscall (lambda () (open "no-such-file"
803 :if-does-not-exist nil
))
805 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
807 (princ "terminating threads")
808 (dolist (thread threads
)
809 (sb-thread:terminate-thread thread
))))
811 (format t
"~&errno test done~%")
813 (with-test (:name
:all-threads-have-abort-restart
)
815 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
816 (sb-thread:interrupt-thread
819 (assert (find-restart 'abort
)))))))
823 (format t
"~&thread startup sigmask test done~%")
825 (with-test (:name
(:debugger-no-hang-on-session-lock-if-interrupted
)
827 #+win32
(error "user would have to touch a key interactively to proceed")
828 (sb-debug::enable-debugger
)
829 (let* ((main-thread *current-thread
*)
831 (make-thread (lambda ()
833 (interrupt-thread main-thread
838 (interrupt-thread main-thread
#'continue
))
839 :name
"interruptor")))
840 (with-session-lock (*session
*)
842 (loop while
(thread-alive-p interruptor-thread
))))
844 (format t
"~&session lock test done~%")
846 ;; expose thread creation races by exiting quickly
847 (with-test (:name
(:no-thread-creation-race
:light
))
848 (make-join-thread (lambda ())))
850 (with-test (:name
(:no-thread-creation-race
:heavy
))
853 (loop for i below
100 collect
854 (sb-thread:make-thread
(lambda ()))))))
856 (format t
"~&creation test done~%")
858 ;; interrupt handlers are per-thread with pthreads, make sure the
859 ;; handler installed in one thread is global
860 (with-test (:name
(:global-interrupt-handler
))
863 (sb-ext:run-program
"sleep" '("1") :search t
:wait nil
))))
865 ;;;; Binding stack safety
867 (defparameter *x
* nil
)
868 (defparameter *n-gcs-requested
* 0)
869 (defparameter *n-gcs-done
* 0)
872 (defun make-something-big ()
873 (let ((x (make-string 32000)))
875 (let ((counter counter
))
876 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
879 (defmacro wait-for-gc
()
881 (incf *n-gcs-requested
*)
882 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
885 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
891 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
892 (defun exercise-binding ()
894 (let ((*x
* (make-something-big)))
896 ;; at this point the binding stack looks like this:
897 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
900 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
901 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
902 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
903 ;; unbinding but values are not).
905 (binding-pointer-delta (ash 2 (- sb-vm
:word-shift sb-vm
:n-fixnum-tag-bits
))))
906 ;; bump bsp as if a BIND had just started
907 (incf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
)
909 (decf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
))))
911 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
912 (with-test (:name
(:binding-stack-gc-safety
))
916 (push (make-kill-thread #'exercise-binding
) threads
)
917 (push (make-kill-thread (lambda ()
923 (mapc #'sb-thread
:terminate-thread threads
))))
925 (with-test (:name
:test-%thread-local-references
)
926 (let ((mysym (gensym))
929 (progv (list mysym
) '(nil)
930 (let* ((i (get-lisp-obj-address (sb-kernel:symbol-tls-index mysym
)))
931 (j (+ i sb-vm
:n-word-bytes
)))
932 (assert (eql (sap-ref-word (current-thread-sap) j
)
933 sb-vm
:no-tls-value-marker-widetag
))
934 (setf (sap-ref-lispobj (current-thread-sap) i
) fool1
935 (sap-ref-lispobj (current-thread-sap) j
) fool2
)
936 ;; assert that my pointer arithmetic worked as expected
937 (assert (eq (symbol-value mysym
) fool1
))
938 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
939 (let ((list (%thread-local-references
)))
940 (assert (memq fool1 list
))
941 (assert (not (memq fool2 list
))))
942 ;; repair the TLS entry that was corrupted by the test
943 (setf (sap-ref-word (current-thread-sap) j
)
944 sb-vm
:no-tls-value-marker-widetag
)))))
946 (format t
"~&binding test done~%")
950 (defvar *errors
* nil
)
954 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
955 (sb-debug:print-backtrace
)
958 (with-test (:name
(:unsynchronized-hash-table
)
959 ;; FIXME: This test occasionally eats out craploads
960 ;; of heap instead of expected error early. Not 100%
961 ;; sure if it would finish as expected, but since it
962 ;; hits swap on my system I'm not likely to find out
963 ;; soon. Disabling for now. -- nikodemus
965 ;; We expect a (probable) error here: parellel readers and writers
966 ;; on a hash-table are not expected to work -- but we also don't
967 ;; expect this to corrupt the image.
968 (let* ((hash (make-hash-table))
970 (threads (list (make-kill-thread
973 (handler-bind ((serious-condition 'oops
))
975 ;;(princ "1") (force-output)
976 (setf (gethash (random 100) hash
) 'h
)))))
981 (handler-bind ((serious-condition 'oops
))
983 ;;(princ "2") (force-output)
984 (remhash (random 100) hash
)))))
989 (handler-bind ((serious-condition 'oops
))
992 (sb-ext:gc
:full t
)))))
993 :name
"collector"))))
996 (mapc #'sb-thread
:terminate-thread threads
))))
998 (format t
"~&unsynchronized hash table test done~%")
1000 (with-test (:name
(:synchronized-hash-table
))
1001 (let* ((hash (make-hash-table :synchronized t
))
1003 (threads (list (make-join-thread
1006 (handler-bind ((serious-condition 'oops
))
1008 ;;(princ "1") (force-output)
1009 (setf (gethash (random 100) hash
) 'h
)))))
1014 (handler-bind ((serious-condition 'oops
))
1016 ;;(princ "2") (force-output)
1017 (remhash (random 100) hash
)))))
1022 (handler-bind ((serious-condition 'oops
))
1024 (sleep (random 1.0))
1025 (sb-ext:gc
:full t
)))))
1026 :name
"collector"))))
1029 (mapc #'sb-thread
:terminate-thread threads
))
1030 (assert (not *errors
*))))
1032 (format t
"~&synchronized hash table test done~%")
1034 (with-test (:name
(:hash-table-parallel-readers
))
1035 (let ((hash (make-hash-table))
1038 do
(setf (gethash (random 100) hash
) 'xxx
))
1039 (let ((threads (list (make-kill-thread
1042 (handler-bind ((serious-condition 'oops
))
1044 until
(eq t
(gethash (random 100) hash
))))))
1049 (handler-bind ((serious-condition 'oops
))
1051 until
(eq t
(gethash (random 100) hash
))))))
1056 (handler-bind ((serious-condition 'oops
))
1058 until
(eq t
(gethash (random 100) hash
))))))
1063 (handler-bind ((serious-condition 'oops
))
1065 (sleep (random 1.0))
1066 (sb-ext:gc
:full t
)))))
1067 :name
"collector"))))
1070 (mapc #'sb-thread
:terminate-thread threads
))
1071 (assert (not *errors
*)))))
1073 (format t
"~&multiple reader hash table test done~%")
1075 (with-test (:name
(:hash-table-single-accessor-parallel-gc
))
1076 (let ((hash (make-hash-table))
1078 (let ((threads (list (make-kill-thread
1080 (handler-bind ((serious-condition 'oops
))
1082 (let ((n (random 100)))
1083 (if (gethash n hash
)
1085 (setf (gethash n hash
) 'h
))))))
1089 (handler-bind ((serious-condition 'oops
))
1091 (sleep (random 1.0))
1092 (sb-ext:gc
:full t
))))
1093 :name
"collector"))))
1096 (mapc #'sb-thread
:terminate-thread threads
))
1097 (assert (not *errors
*)))))
1099 (format t
"~&single accessor hash table test~%")
1101 #|
;; a cll post from eric marsden
1103 |
(setq *debugger-hook
*
1104 |
(lambda (condition old-debugger-hook
)
1105 |
(debug:backtrace
10)
1106 |
(unix:unix-exit
2)))
1107 |
#+live-dangerously
1108 |
(mp::start-sigalrm-yield
)
1109 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
1110 |
(mp:make-process
#'roomy
)
1111 |
(mp:make-process
#'roomy
)))
1114 (with-test (:name
(:condition-variable
:notify-multiple
))
1115 (flet ((tester (notify-fun)
1116 (let ((queue (make-waitqueue :name
"queue"))
1117 (lock (make-mutex :name
"lock"))
1122 (format t
"condition-wait ~a~%" x
)
1124 (condition-wait queue lock
)
1125 (format t
"woke up ~a~%" x
)
1128 (let ((threads (loop for x from
1 to
10
1131 (make-kill-thread (lambda ()
1135 (funcall notify-fun queue
))
1137 (mapcar #'terminate-thread threads
)
1138 ;; Check that all threads woke up at least once
1139 (assert (= (length (remove-duplicates data
)) 10)))))))
1140 (tester (lambda (queue)
1141 (format t
"~&(condition-notify queue 10)~%")
1143 (condition-notify queue
10)))
1144 (tester (lambda (queue)
1145 (format t
"~&(condition-broadcast queue)~%")
1147 (condition-broadcast queue
)))))
1149 (format t
"waitqueue wakeup tests done~%")
1151 ;;; Make sure that a deadline handler is not invoked twice in a row in
1152 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1154 (with-test (:name
(:condition-wait
:deadlines
:LP-512914
))
1155 (let ((n 2) ; was empirically enough to trigger the bug
1156 (mutex (sb-thread:make-mutex
))
1157 (waitq (sb-thread:make-waitqueue
))
1159 (deadline-handler-run-twice? nil
))
1162 (sb-thread:make-thread
1165 ((sb-sys:deadline-timeout
1166 (let ((already? nil
))
1169 (setq deadline-handler-run-twice? t
))
1172 (sb-thread:condition-broadcast waitq
)
1173 (sb-sys:defer-deadline
10.0 c
)))))
1174 (sb-sys:with-deadline
(:seconds
0.1)
1175 (sb-thread:with-mutex
(mutex)
1176 (sb-thread:condition-wait waitq mutex
))))))))
1177 (push child threads
)))
1178 (mapc #'sb-thread
:join-thread threads
)
1179 (assert (not deadline-handler-run-twice?
))))
1181 (with-test (:name
(:condition-wait
:signal-deadline-with-interrupts-enabled
))
1182 (let ((mutex (sb-thread:make-mutex
))
1183 (waitq (sb-thread:make-waitqueue
))
1186 (A-interrupts-enabled?
:unknown
)
1187 (B-interrupts-enabled?
:unknown
)
1190 ;; W.L.O.G., we assume that A is executed first...
1191 (setq A
(sb-thread:make-thread
1194 ((sb-sys:deadline-timeout
1196 ;; We came here through the call to DECODE-TIMEOUT
1197 ;; in CONDITION-WAIT; hence both here are supposed
1198 ;; to evaluate to T.
1199 (setq A-holds?
(sb-thread:holding-mutex-p mutex
))
1200 (setq A-interrupts-enabled?
1201 sb-sys
:*interrupts-enabled
*)
1203 (sb-thread:condition-broadcast waitq
)
1204 (sb-sys:defer-deadline
10.0 c
))))
1205 (sb-sys:with-deadline
(:seconds
0.1)
1206 (sb-thread:with-mutex
(mutex)
1207 (sb-thread:condition-wait waitq mutex
)))))
1209 (setq B
(sb-thread:make-thread
1213 ((sb-sys:deadline-timeout
1215 ;; We came here through the call to DECODE-TIMEOUT
1216 ;; in CONDITION-WAIT (contended case of
1217 ;; reaquiring the mutex) - so the former will
1218 ;; be NIL, but interrupts should still be enabled.
1219 (setq B-holds?
(sb-thread:holding-mutex-p mutex
))
1220 (setq B-interrupts-enabled?
1221 sb-sys
:*interrupts-enabled
*)
1223 (sb-thread:condition-broadcast waitq
)
1224 (sb-sys:defer-deadline
10.0 c
))))
1225 (sb-sys:with-deadline
(:seconds
0.1)
1226 (sb-thread:with-mutex
(mutex)
1227 (sb-thread:condition-wait waitq mutex
)))))
1229 (sb-thread:join-thread A
)
1230 (sb-thread:join-thread B
)
1231 (let ((A-result (list A-holds? A-interrupts-enabled?
))
1232 (B-result (list B-holds? B-interrupts-enabled?
)))
1233 ;; We also check some subtle behaviour w.r.t. whether a deadline
1234 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1235 ;; probably very internal behaviour (so user should not depend
1236 ;; on it) -- I added the testing here just to manifest current
1238 (cond ((equal A-result
'(t t
)) (assert (equal B-result
'(nil t
))))
1239 ((equal B-result
'(t t
)) (assert (equal A-result
'(nil t
))))
1241 (error "Failure: fell through wit A: ~S, B: ~S"
1245 (with-test (:name
(:mutex
:finalization
))
1248 (setf a
(make-mutex)))))
1250 (format t
"mutex finalization test done~%")
1252 ;; You have to shoehorn this arbitrary sexpr into a feature expression
1253 ;; to have the test summary show that a test was disabled.
1255 (unless (eql (extern-alien "verify_gens" int
)
1256 (1+ sb-vm
:+highest-normal-generation
+))
1257 (pushnew :verify-gens
*features
*))
1259 (with-test (:name
:backtrace
:skipped-on
':verify-gens
)
1260 ;; Printing backtraces from several threads at once used to hang the
1261 ;; whole SBCL process (discovered by accident due to a timer.impure
1262 ;; test misbehaving). The cause was that packages weren't even
1263 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1264 ;; backtraces a loot of symbol lookups need to be done due to
1266 (let* ((threads (loop repeat
10
1267 collect
(sb-thread:make-thread
1270 (with-output-to-string (*debug-io
*)
1271 (sb-debug:print-backtrace
:count
10))))))))
1272 (wait-for-threads threads
)))
1274 (format t
"backtrace test done~%")
1276 (format t
"~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1278 (with-test (:name
:gc-deadlock
)
1279 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1280 ;; GC due to *all-threads-lock* and session lock. On earlier
1281 ;; versions and at least on one specific box this test is good enough
1282 ;; to catch that typically well before the 1500th iteration.
1289 (when (zerop (mod i
100))
1296 (sleep (random 0.001)))
1297 :name
(format nil
"SLEEP-~D" i
))
1300 ;; KLUDGE: what we are doing here is explicit,
1301 ;; but the same can happen because of a regular
1302 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1303 ;; session functions.
1304 (sb-thread::with-all-threads-lock
1305 (sb-thread::with-session-lock
(sb-thread::*session
*)
1307 :name
(format nil
"GC-~D" i
)))
1309 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
1313 (format t
"~&gc deadlock test done~%")
1315 (let ((count (make-array 8 :initial-element
0)))
1316 (defun closure-one ()
1317 (declare (optimize safety
))
1318 (values (incf (aref count
0)) (incf (aref count
1))
1319 (incf (aref count
2)) (incf (aref count
3))
1320 (incf (aref count
4)) (incf (aref count
5))
1321 (incf (aref count
6)) (incf (aref count
7))))
1322 (defun no-optimizing-away-closure-one ()
1323 (setf count
(make-array 8 :initial-element
0))))
1328 (let ((one (make-box))
1331 (defun closure-two ()
1332 (declare (optimize safety
))
1333 (values (incf (box-count one
)) (incf (box-count two
)) (incf (box-count three
))))
1334 (defun no-optimizing-away-closure-two ()
1335 (setf one
(make-box)
1339 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1340 ;;; sometimes run out of memory) in the following test. For developers
1341 ;;; interested in debugging this combination of features, it might be
1342 ;;; fruitful to concentrate their efforts around this test...
1344 (with-test (:name
(:funcallable-instances
)
1345 :skipped-on
'(and :sb-safepoint
1346 (not :c-stack-is-control-stack
)))
1347 ;; the funcallable-instance implementation used not to be threadsafe
1348 ;; against setting the funcallable-instance function to a closure
1349 ;; (because the code and lexenv were set separately).
1350 (let ((fun (sb-kernel:%make-funcallable-instance
0))
1352 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1354 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1355 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1357 (handler-case (loop (funcall fun
))
1358 (serious-condition (c) (setf condition c
)))))
1359 (let ((changer (make-thread #'changer
))
1360 (test (make-thread #'test
)))
1363 ;; The two closures above are fairly carefully crafted
1364 ;; so that if given the wrong lexenv they will tend to
1365 ;; do some serious damage, but it is of course difficult
1366 ;; to predict where the various bits and pieces will be
1367 ;; allocated. Five seconds failed fairly reliably on
1368 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1369 (sb-ext:with-timeout
5
1370 (wait-for-threads (list test
)))
1371 (error "~@<test thread got condition:~2I~_~A~@:>" condition
))
1373 (terminate-thread changer
)
1374 (terminate-thread test
)
1375 (wait-for-threads (list changer test
))))))))
1377 (format t
"~&funcallable-instance test done~%")
1379 (defun random-type (n)
1380 `(integer ,(random n
) ,(+ n
(random n
))))
1382 (defun subtypep-hash-cache-test ()
1384 (let ((type1 (random-type 500))
1385 (type2 (random-type 500)))
1386 (let ((a (subtypep type1 type2
)))
1388 (assert (eq (subtypep type1 type2
) a
))))))
1392 (with-test (:name
(:hash-cache
:subtypep
))
1394 ;; this didn't "reliably fail" with a small number of threads.
1395 ;; 30 is a compromise between running time and confidence in the result.
1397 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
)))
1400 ;;;; BLACK BOX TESTS
1402 (in-package :cl-user
)
1403 (use-package :test-util
)
1404 (use-package "ASSERTOID")
1406 (format t
"parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1407 (with-test (:name
:parallel-defclass
)
1408 (defclass test-1
() ((a :initform
:orig-a
)))
1409 (defclass test-2
() ((b :initform
:orig-b
)))
1410 (defclass test-3
(test-1 test-2
) ((c :initform
:orig-c
)))
1411 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1412 ;; calls disabled in the folloving code. (As seen on a Server 2012
1413 ;; installation.) Clearly, this sort of workaround in a test is
1414 ;; cheating, and might be hiding the underlying bug that the test is
1415 ;; exposing. Let's review this later.
1417 (d1 (sb-thread:make-thread
(lambda ()
1419 do
(defclass test-1
() ((a :initform
:new-a
)))
1421 #-win32
(force-output)))
1423 (d2 (sb-thread:make-thread
(lambda ()
1425 do
(defclass test-2
() ((b :initform
:new-b
)))
1427 #-win32
(force-output)))
1429 (d3 (sb-thread:make-thread
(lambda ()
1431 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1433 #-win32
(force-output)))
1435 (i (sb-thread:make-thread
(lambda ()
1437 do
(let ((i (make-instance 'test-3
)))
1438 (assert (member (slot-value i
'a
) '(:orig-a
:new-a
)))
1439 (assert (member (slot-value i
'b
) '(:orig-b
:new-b
)))
1440 (assert (member (slot-value i
'c
) '(:orig-c
:new-c
))))
1442 #-win32
(force-output)))
1444 (format t
"~%sleeping!~%")
1446 (format t
"~%stopping!~%")
1449 (sb-thread:join-thread th
)
1450 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
1453 (format t
"parallel defclass test done~%")
1455 (with-test (:name
(:deadlock-detection
:interrupts
) :fails-on
:win32
)
1456 #+win32
;be more explicit than just :skipped-on
1457 (error "not attempting, because of deadlock error in background thread")
1458 (let* ((m1 (sb-thread:make-mutex
:name
"M1"))
1459 (m2 (sb-thread:make-mutex
:name
"M2"))
1460 (t1-can-go (sb-thread:make-semaphore
:name
"T1 can go"))
1461 (t2-can-go (sb-thread:make-semaphore
:name
"T2 can go"))
1462 (t1 (sb-thread:make-thread
1464 (sb-thread:with-mutex
(m1)
1465 (sb-thread:wait-on-semaphore t1-can-go
)
1468 (t2 (sb-thread:make-thread
1470 (sb-ext:wait-for
(eq t1
(sb-thread:mutex-owner m1
)))
1471 (sb-thread:with-mutex
(m1 :wait-p t
)
1472 (sb-thread:wait-on-semaphore t2-can-go
)
1475 (sb-ext:wait-for
(eq m1
(sb-thread::thread-waiting-for t2
)))
1476 (sb-thread:interrupt-thread t2
(lambda ()
1477 (sb-thread:with-mutex
(m2 :wait-p t
)
1479 (eq m2
(sb-thread::thread-waiting-for t1
)))
1480 (sb-thread:signal-semaphore t2-can-go
))))
1481 (sb-ext:wait-for
(eq t2
(sb-thread:mutex-owner m2
)))
1482 (sb-thread:interrupt-thread t1
(lambda ()
1483 (sb-thread:with-mutex
(m2 :wait-p t
)
1484 (sb-thread:signal-semaphore t1-can-go
))))
1485 ;; both threads should finish without a deadlock or deadlock
1487 (let ((res (list (sb-thread:join-thread t1
)
1488 (sb-thread:join-thread t2
))))
1489 (assert (equal '(:ok1
:ok2
) res
)))))
1491 (with-test (:name
(:deadlock-detection
:gc
))
1492 ;; To semi-reliably trigger the error (in SBCL's where)
1493 ;; it was present you had to run this for > 30 seconds,
1494 ;; but that's a bit long for a single test.
1495 (let* ((stop (+ 5 (get-universal-time)))
1496 (m1 (sb-thread:make-mutex
:name
"m1"))
1497 (t1 (sb-thread:make-thread
1499 (loop until
(> (get-universal-time) stop
)
1500 do
(sb-thread:with-mutex
(m1)
1501 (eval `(make-array 24))))
1503 (t2 (sb-thread:make-thread
1505 (loop until
(> (get-universal-time) stop
)
1506 do
(sb-thread:with-mutex
(m1)
1507 (eval `(make-array 24))))
1509 (let ((res (list (sb-thread:join-thread t1
)
1510 (sb-thread:join-thread t2
))))
1511 (assert (equal '(:ok
:ok
) res
)))))
1513 (with-test (:name
:spinlock-api
)
1514 (handler-bind ((warning #'error
))
1515 (destructuring-bind (with make get release
)
1517 (list (compile nil
`(lambda (lock)
1518 (sb-thread::with-spinlock
(lock)
1520 (compile nil
`(lambda ()
1521 (sb-thread::make-spinlock
:name
"foo")))
1522 (compile nil
`(lambda (lock)
1523 (sb-thread::get-spinlock lock
)))
1524 (compile nil
`(lambda (lock)
1525 (sb-thread::release-spinlock lock
))))
1526 early-deprecation-warning
4)
1527 (let ((lock (funcall make
)))
1529 (funcall release lock
)
1530 (assert (eq t
(funcall with lock
)))))))
1532 (with-test (:name
:interrupt-io-unnamed-pipe
)
1536 (let ((stream (sb-sys:make-fd-stream fd
1537 :element-type
:default
1538 :serve-events nil
)))
1540 (let ((ok (handler-case
1551 (format *trace-output
* "~&=> ~A~%" ok
)
1552 (force-output *trace-output
*))))
1554 (ignore-errors (close stream
))))
1557 (multiple-value-bind (read write
)
1559 (let* ((reader (sb-thread:make-thread
(lambda () (reader read
))))
1560 (stream (sb-sys:make-fd-stream write
1562 :element-type
:default
1566 (sb-thread:interrupt-thread reader
(lambda ()
1572 (write-char #\x stream
)
1574 (sb-thread:join-thread reader
)))))
1576 (assert (eq result
:ok
))))
1578 (with-test (:name
:thread-alloca
)
1579 (sb-ext:run-program
"sh"
1580 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1581 "alloca.c" "-o" "alloca.so")
1583 (load-shared-object (truename "alloca.so"))
1585 (alien-funcall (extern-alien "alloca_test" (function void
)))
1586 (sb-thread:join-thread
1587 (sb-thread:make-thread
1589 (alien-funcall (extern-alien "alloca_test" (function void
)))))))
1591 (with-test (:name
:fp-mode-inheritance-threads
)
1593 (let* ((reserved-bits #+x86
(ash #b1110000011000000
16)
1595 (fp-mode (logandc2 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))
1598 (sb-thread:join-thread
1599 (sb-thread:make-thread
1601 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
)))))))
1602 (assert (= fp-mode thread-fp-mode
)))))
1604 (sb-int:with-float-traps-masked
(:divide-by-zero
)
1606 (setf (sb-vm:floating-point-modes
)
1607 (dpb sb-vm
:float-divide-by-zero-trap-bit
1608 sb-vm
::float-traps-byte
1609 (sb-vm:floating-point-modes
)))