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 (in-package "SB-THREAD")
17 (use-package :test-util)
18 (use-package "ASSERTOID")
20 (setf sb-unix::*on-dangerous-wait* :error)
22 (defun wait-for-threads (threads)
23 (mapc (lambda (thread) (sb-thread:join-thread thread :default nil)) threads)
24 (assert (not (some #'sb-thread:thread-alive-p threads))))
26 (with-test (:name (:threads :trivia))
27 (assert (eql 1 (length (list-all-threads))))
29 (assert (eq *current-thread*
30 (find (thread-name *current-thread*) (list-all-threads)
31 :key #'thread-name :test #'equal)))
33 (assert (thread-alive-p *current-thread*)))
35 (with-test (:name (:with-mutex :basics))
36 (let ((mutex (make-mutex)))
40 (sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
42 (where sb-alien:unsigned-long))
43 (sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
45 (where sb-alien:unsigned-long))
47 (with-test (:name (:interrupt-thread :basics :no-unwinding))
49 (interrupt-thread *current-thread* (lambda () (setq a 1)))
52 (with-test (:name (:interrupt-thread :deferrables-blocked))
53 (sb-thread:interrupt-thread sb-thread:*current-thread*
55 (check-deferrables-blocked-or-lose 0))))
57 (with-test (:name (:interrupt-thread :deferrables-unblocked))
58 (sb-thread:interrupt-thread sb-thread:*current-thread*
61 (check-deferrables-unblocked-or-lose 0)))))
63 (with-test (:name (:interrupt-thread :nlx))
65 (sb-thread:interrupt-thread sb-thread:*current-thread*
67 (check-deferrables-blocked-or-lose 0)
69 (check-deferrables-unblocked-or-lose 0))
71 #-sb-thread (sb-ext:exit :code 104)
73 ;;;; Now the real tests...
75 (with-test (:name (:interrupt-thread :deferrables-unblocked-by-lock))
76 (let ((lock (sb-thread::make-mutex))
77 (thread (sb-thread:make-thread (lambda ()
79 (sb-thread::grab-mutex lock)
80 (sb-thread:interrupt-thread thread
82 (check-deferrables-blocked-or-lose 0)
83 (sb-thread::grab-mutex lock)
84 (check-deferrables-unblocked-or-lose 0)
85 (sb-thread:abort-thread)))
87 (sb-thread::release-mutex lock)))
91 (defmacro defincf (name accessor &rest args)
93 (let* ((old (,accessor x ,@args))
95 (loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
96 do (setf old (,accessor x ,@args)
100 (defstruct cas-struct (slot 0))
102 (defincf incf-car car)
103 (defincf incf-cdr cdr)
104 (defincf incf-slot cas-struct-slot)
105 (defincf incf-symbol-value symbol-value)
106 (defincf incf-svref/1 svref 1)
107 (defincf incf-svref/0 svref 0)
109 (defmacro def-test-cas (name init incf op)
110 `(with-test (:name ,name)
117 collect (sb-thread:make-thread
120 do (sb-thread:thread-yield))
121 (loop repeat n do (,incf x)))))))
124 (sb-thread:join-thread th))
125 (assert (= (,op x) (* 10 n))))))
128 (def-test-cas test-cas-car (cons 0 nil) incf-car car)
129 (def-test-cas test-cas-cdr (cons nil 0) incf-cdr cdr)
130 (def-test-cas test-cas-slot (make-cas-struct) incf-slot cas-struct-slot)
131 (def-test-cas test-cas-value (let ((x '.x.))
134 incf-symbol-value symbol-value)
135 (def-test-cas test-cas-svref/0 (vector 0 nil) incf-svref/0 (lambda (x)
137 (def-test-cas test-cas-svref/1 (vector nil 0) incf-svref/1 (lambda (x)
139 (format t "~&compare-and-swap tests done~%")
141 (with-test (:name (:threads :more-trivia)))
142 (let ((old-threads (list-all-threads))
143 (thread (make-thread (lambda ()
144 (assert (find *current-thread* *all-threads*))
146 (new-threads (list-all-threads)))
147 (assert (thread-alive-p thread))
148 (assert (eq thread (first new-threads)))
149 (assert (= (1+ (length old-threads)) (length new-threads)))
151 (assert (not (thread-alive-p thread))))
153 (with-test (:name (:join-thread :nlx :default))
154 (let ((sym (gensym)))
155 (assert (eq sym (join-thread (make-thread (lambda () (sb-thread:abort-thread)))
158 (with-test (:name (:join-thread :nlx :error))
159 (raises-error? (join-thread (make-thread (lambda () (sb-thread:abort-thread))))
162 (with-test (:name (:join-thread :multiple-values))
163 (assert (equal '(1 2 3)
165 (join-thread (make-thread (lambda () (values 1 2 3))))))))
167 ;;; We had appalling scaling properties for a while. Make sure they
169 (defun scaling-test (function &optional (nthreads 5))
170 "Execute FUNCTION with NTHREADS lurking to slow it down."
171 (let ((queue (sb-thread:make-waitqueue))
172 (mutex (sb-thread:make-mutex)))
173 ;; Start NTHREADS idle threads.
174 (dotimes (i nthreads)
175 (sb-thread:make-thread (lambda ()
177 (sb-thread:condition-wait queue mutex))
178 (sb-thread:abort-thread))))
179 (let ((start-time (get-internal-run-time)))
181 (prog1 (- (get-internal-run-time) start-time)
182 (sb-thread:condition-broadcast queue)))))
184 "A function that does work with the CPU."
185 (if (zerop n) 1 (* n (fact (1- n)))))
186 (let ((work (lambda () (fact 15000))))
187 (let ((zero (scaling-test work 0))
188 (four (scaling-test work 4)))
189 ;; a slightly weak assertion, but good enough for starters.
190 (assert (< four (* 1.5 zero)))))
192 ;;; For one of the interupt-thread tests, we want a foreign function
193 ;;; that does not make syscalls
195 (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
196 (format o "void loop_forever() { while(1) ; }~%"))
197 (sb-ext:run-program "/bin/sh"
198 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
199 "-o" "threads-foreign.so" "threads-foreign.c")
200 :environment (test-util::test-env))
201 (sb-alien:load-shared-object (truename "threads-foreign.so"))
202 (sb-alien:define-alien-routine loop-forever sb-alien:void)
203 (delete-file "threads-foreign.c")
206 ;;; elementary "can we get a lock and release it again"
207 (with-test (:name (:mutex :basics))
208 (let ((l (make-mutex :name "foo"))
209 (p *current-thread*))
210 (assert (eql (mutex-value l) nil) nil "1")
211 (sb-thread:get-mutex l)
212 (assert (eql (mutex-value l) p) nil "3")
213 (sb-thread:release-mutex l)
214 (assert (eql (mutex-value l) nil) nil "5")))
216 (with-test (:name (:with-recursive-lock :basics))
217 (labels ((ours-p (value)
218 (eq *current-thread* value)))
219 (let ((l (make-mutex :name "rec")))
220 (assert (eql (mutex-value l) nil) nil "1")
221 (sb-thread:with-recursive-lock (l)
222 (assert (ours-p (mutex-value l)) nil "3")
223 (sb-thread:with-recursive-lock (l)
224 (assert (ours-p (mutex-value l)) nil "4"))
225 (assert (ours-p (mutex-value l)) nil "5"))
226 (assert (eql (mutex-value l) nil) nil "6"))))
228 (with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
229 (let ((l (make-mutex :name "a mutex")))
231 (with-recursive-lock (l)))))
233 ;; test that SLEEP actually sleeps for at least the given time, even
234 ;; if interrupted by another thread exiting/a gc/anything
235 (with-test (:name (:sleep :continue-sleeping-after-interrupt))
236 (let ((start-time (get-universal-time)))
237 (make-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
239 (assert (>= (get-universal-time) (+ 5 start-time)))))
242 (with-test (:name (:condition-wait :basics-1))
243 (let ((queue (make-waitqueue :name "queue"))
244 (lock (make-mutex :name "lock"))
246 (labels ((in-new-thread ()
248 (assert (eql (mutex-value lock) *current-thread*))
249 (format t "~A got mutex~%" *current-thread*)
250 ;; now drop it and sleep
251 (condition-wait queue lock)
252 ;; after waking we should have the lock again
253 (assert (eql (mutex-value lock) *current-thread*))
256 (make-thread #'in-new-thread)
257 (sleep 2) ; give it a chance to start
258 ;; check the lock is free while it's asleep
259 (format t "parent thread ~A~%" *current-thread*)
260 (assert (eql (mutex-value lock) nil))
263 (condition-notify queue))
266 (with-test (:name (:condition-wait :basics-2))
267 (let ((queue (make-waitqueue :name "queue"))
268 (lock (make-mutex :name "lock")))
269 (labels ((ours-p (value)
270 (eq *current-thread* value))
272 (with-recursive-lock (lock)
273 (assert (ours-p (mutex-value lock)))
274 (format t "~A got mutex~%" (mutex-value lock))
275 ;; now drop it and sleep
276 (condition-wait queue lock)
277 ;; after waking we should have the lock again
278 (format t "woken, ~A got mutex~%" (mutex-value lock))
279 (assert (ours-p (mutex-value lock))))))
280 (make-thread #'in-new-thread)
281 (sleep 2) ; give it a chance to start
282 ;; check the lock is free while it's asleep
283 (format t "parent thread ~A~%" *current-thread*)
284 (assert (eql (mutex-value lock) nil))
285 (with-recursive-lock (lock)
286 (condition-notify queue))
289 (with-test (:name (:mutex :contention))
290 (let ((mutex (make-mutex :name "contended")))
292 (let ((me *current-thread*))
296 (assert (eql (mutex-value mutex) me)))
297 (assert (not (eql (mutex-value mutex) me))))
298 (format t "done ~A~%" *current-thread*))))
299 (let ((kid1 (make-thread #'run))
300 (kid2 (make-thread #'run)))
301 (format t "contention ~A ~A~%" kid1 kid2)
302 (wait-for-threads (list kid1 kid2))))))
306 (with-test (:name (:grab-mutex :waitp nil))
307 (let ((m (make-mutex)))
309 (assert (null (join-thread (make-thread
311 (grab-mutex m :waitp nil)))))))))
313 (with-test (:name (:grab-mutex :timeout :acquisition-fail))
314 (let ((m (make-mutex))
315 (w (make-semaphore)))
317 (let ((th (make-thread
320 (grab-mutex m :timeout 0.1)
321 (signal-semaphore w))))))
322 ;; Wait for it to -- otherwise the detect the deadlock chain
324 (wait-on-semaphore w)
325 (assert (null (join-thread th)))))))
327 (with-test (:name (:grab-mutex :timeout :acquisition-success))
328 (let ((m (make-mutex))
331 (setq child (make-thread #'(lambda () (grab-mutex m :timeout 1.0))))
333 (assert (eq (join-thread child) 't))))
335 (with-test (:name (:grab-mutex :timeout+deadline))
336 (let ((m (make-mutex))
337 (w (make-semaphore)))
339 (let ((th (make-thread #'(lambda ()
340 (sb-sys:with-deadline (:seconds 0.0)
342 (grab-mutex m :timeout 0.0)
343 (sb-sys:deadline-timeout ()
346 (wait-on-semaphore w)
347 (assert (eq (join-thread th) :deadline))))))
349 (with-test (:name (:grab-mutex :waitp+deadline))
350 (let ((m (make-mutex)))
352 (assert (eq (join-thread
353 (make-thread #'(lambda ()
354 (sb-sys:with-deadline (:seconds 0.0)
356 (grab-mutex m :waitp nil)
357 (sb-sys:deadline-timeout ()
363 (defmacro raises-timeout-p (&body body)
364 `(handler-case (progn (progn ,@body) nil)
365 (sb-ext:timeout () t)))
367 (with-test (:name (:semaphore :wait-forever))
368 (let ((sem (make-semaphore :count 0)))
369 (assert (raises-timeout-p
370 (sb-ext:with-timeout 0.1
371 (wait-on-semaphore sem))))))
373 (with-test (:name (:semaphore :initial-count))
374 (let ((sem (make-semaphore :count 1)))
375 (sb-ext:with-timeout 0.1
376 (wait-on-semaphore sem))))
378 (with-test (:name (:semaphore :wait-then-signal))
379 (let ((sem (make-semaphore))
381 (make-thread (lambda ()
384 (signal-semaphore sem)))
385 (wait-on-semaphore sem)
386 (assert signalled-p)))
388 (with-test (:name (:semaphore :signal-then-wait))
389 (let ((sem (make-semaphore))
391 (make-thread (lambda ()
392 (signal-semaphore sem)
393 (setq signalled-p t)))
394 (loop until signalled-p)
395 (wait-on-semaphore sem)
396 (assert signalled-p)))
398 (defun test-semaphore-multiple-signals (wait-on-semaphore)
399 (let* ((sem (make-semaphore :count 5))
400 (threads (loop repeat 20 collecting
401 (make-thread (lambda ()
402 (funcall wait-on-semaphore sem))))))
403 (flet ((count-live-threads ()
404 (count-if #'thread-alive-p threads)))
406 (assert (= 15 (count-live-threads)))
407 (signal-semaphore sem 10)
409 (assert (= 5 (count-live-threads)))
410 (signal-semaphore sem 3)
412 (assert (= 2 (count-live-threads)))
413 (signal-semaphore sem 4)
415 (assert (= 0 (count-live-threads))))))
417 (with-test (:name (:semaphore :multiple-signals))
418 (test-semaphore-multiple-signals #'wait-on-semaphore))
420 (with-test (:name (:try-semaphore :trivial-fail))
421 (assert (eq (try-semaphore (make-semaphore :count 0)) 'nil)))
423 (with-test (:name (:try-semaphore :trivial-success))
424 (let ((sem (make-semaphore :count 1)))
425 (assert (try-semaphore sem))
426 (assert (zerop (semaphore-count sem)))))
428 (with-test (:name (:try-semaphore :trivial-fail :n>1))
429 (assert (eq (try-semaphore (make-semaphore :count 1) 2) 'nil)))
431 (with-test (:name (:try-semaphore :trivial-success :n>1))
432 (let ((sem (make-semaphore :count 10)))
433 (assert (try-semaphore sem 5))
434 (assert (try-semaphore sem 5))
435 (assert (zerop (semaphore-count sem)))))
437 (with-test (:name (:try-semaphore :emulate-wait-on-semaphore))
438 (flet ((busy-wait-on-semaphore (sem)
439 (loop until (try-semaphore sem) do (sleep 0.001))))
440 (test-semaphore-multiple-signals #'busy-wait-on-semaphore)))
442 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
443 ;;; semaphore in a bad state.
444 (with-test (:name (:try-semaphore :interrupt-safe))
445 (flet ((make-threads (count fn)
446 (loop repeat count collect (make-thread fn)))
447 (kill-thread (thread)
448 (when (thread-alive-p thread)
449 (ignore-errors (terminate-thread thread))))
450 (count-live-threads (threads)
451 (count-if #'thread-alive-p threads)))
452 ;; WAITERS will already be waiting on the semaphore while
453 ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
454 ;; semaphore, and MORE-WAITERS are new threads trying to wait on
455 ;; the semaphore during the interruption-fire.
456 (let* ((sem (make-semaphore :count 100))
457 (waiters (make-threads 20 #'(lambda ()
458 (wait-on-semaphore sem))))
459 (triers (make-threads 40 #'(lambda ()
460 (sleep (random 0.01))
461 (try-semaphore sem (1+ (random 5))))))
464 do (kill-thread (nth (random 40) triers))
465 collect (make-thread #'(lambda () (wait-on-semaphore sem)))
466 do (kill-thread (nth (random 40) triers)))))
468 ;; Now ensure that the waiting threads will all be waked up,
469 ;; i.e. that the semaphore is still working.
470 (loop repeat (+ (count-live-threads waiters)
471 (count-live-threads more-waiters))
472 do (signal-semaphore sem))
474 (assert (zerop (count-live-threads triers)))
475 (assert (zerop (count-live-threads waiters)))
476 (assert (zerop (count-live-threads more-waiters))))))
480 (format t "~&semaphore tests done~%")
482 (defun test-interrupt (function-to-interrupt &optional quit-p)
483 (let ((child (make-thread function-to-interrupt)))
484 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
486 (format t "interrupting child ~A~%" child)
487 (interrupt-thread child
489 (format t "child pid ~A~%" *current-thread*)
490 (when quit-p (abort-thread))))
494 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
495 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
498 (with-test (:name (:interrupt-thread :more-basics))
499 (let ((child (test-interrupt (lambda () (loop)))))
500 (terminate-thread child)))
502 (with-test (:name (:interrupt-thread :interrupt-foreign-loop))
503 (test-interrupt #'loop-forever :quit))
505 (with-test (:name (:interrupt-thread :interrupt-sleep))
506 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
507 (terminate-thread child)
508 (wait-for-threads (list child))))
510 (with-test (:name (:interrupt-thread :interrupt-mutex-acquisition))
511 (let ((lock (make-mutex :name "loctite"))
514 (setf child (test-interrupt
517 (assert (eql (mutex-value lock) *current-thread*)))
518 (assert (not (eql (mutex-value lock) *current-thread*)))
520 ;;hold onto lock for long enough that child can't get it immediately
522 (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
523 (format t "parent releasing lock~%"))
524 (terminate-thread child)
525 (wait-for-threads (list child))))
527 (format t "~&locking test done~%")
529 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
531 (with-test (:name (:interrupt-thread :interrupt-consing-child))
532 (let ((thread (sb-thread:make-thread (lambda () (loop (alloc-stuff))))))
534 (loop repeat 4 collect
535 (sb-thread:make-thread
538 (sleep (random 0.1d0))
541 (sb-thread:interrupt-thread thread (lambda ()))))))))
542 (wait-for-threads killers)
543 (sb-thread:terminate-thread thread)
544 (wait-for-threads (list thread))))
547 (format t "~&multi interrupt test done~%")
549 #+(or x86 x86-64) ;; x86oid-only, see internal commentary.
550 (with-test (:name (:interrupt-thread :interrupt-consing-child :again))
551 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
552 ;; NB this only works on x86: other ports don't have a symbol for
553 ;; pseudo-atomic atomicity
555 (sleep (random 0.1d0))
558 (princ ".") (force-output)
559 (assert (thread-alive-p *current-thread*))
561 (not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
563 (wait-for-threads (list c))))
565 (format t "~&interrupt test done~%")
567 (defstruct counter (n 0 :type sb-vm:word))
568 (defvar *interrupt-counter* (make-counter))
570 (declaim (notinline check-interrupt-count))
571 (defun check-interrupt-count (i)
572 (declare (optimize (debug 1) (speed 1)))
573 ;; This used to lose if eflags were not restored after an interrupt.
574 (unless (typep i 'fixnum)
575 (error "!!!!!!!!!!!")))
577 (with-test (:name (:interrupt-thread :interrupt-ATOMIC-INCF))
578 (let ((c (make-thread
580 (handler-bind ((error #'(lambda (cond)
583 most-positive-fixnum))))
584 (loop (check-interrupt-count
585 (counter-n *interrupt-counter*))))))))
586 (let ((func (lambda ()
589 (sb-ext:atomic-incf (counter-n *interrupt-counter*)))))
590 (setf (counter-n *interrupt-counter*) 0)
592 (sleep (random 0.1d0))
593 (interrupt-thread c func))
594 (loop until (= (counter-n *interrupt-counter*) 100) do (sleep 0.1))
596 (wait-for-threads (list c)))))
598 (format t "~&interrupt count test done~%")
600 (defvar *runningp* nil)
602 (with-test (:name (:interrupt-thread :no-nesting))
603 (let ((thread (sb-thread:make-thread
607 (declare (special runningp))
609 (sb-thread:interrupt-thread thread
611 (let ((*runningp* t))
614 (sb-thread:interrupt-thread thread
616 (throw 'xxx *runningp*)))
617 (assert (not (sb-thread:join-thread thread)))))
619 (with-test (:name (:interrupt-thread :nesting))
620 (let ((thread (sb-thread:make-thread
624 (declare (special runningp))
626 (sb-thread:interrupt-thread thread
628 (let ((*runningp* t))
629 (sb-sys:with-interrupts
632 (sb-thread:interrupt-thread thread
634 (throw 'xxx *runningp*)))
635 (assert (sb-thread:join-thread thread))))
637 (with-test (:name (:two-threads-running-gc))
639 (make-thread (lambda ()
641 (sb-ext:gc) (princ "\\") (force-output))
643 (make-thread (lambda ()
646 (princ "/") (force-output))
649 (when (and a-done b-done) (return))
654 (defun waste (&optional (n 100000))
655 (loop repeat n do (make-string 16384)))
657 (with-test (:name (:one-thread-runs-gc-while-other-conses))
658 (loop for i below 100 do
661 (sb-thread:make-thread
669 (defparameter *aaa* nil)
670 (with-test (:name (:one-thread-runs-gc-while-other-conses :again))
671 (loop for i below 100 do
674 (sb-thread:make-thread
676 (let ((*aaa* (waste)))
678 (let ((*aaa* (waste)))
682 (format t "~&gc test done~%")
684 ;; this used to deadlock on session-lock
685 (with-test (:name (:no-session-deadlock))
686 (sb-thread:make-thread (lambda () (sb-ext:gc))))
688 (defun exercise-syscall (fn reference-errno)
689 (sb-thread:make-thread
693 (let ((errno (sb-unix::get-errno)))
694 (sleep (random 0.1d0))
695 (unless (eql errno reference-errno)
696 (format t "Got errno: ~A (~A) instead of ~A~%"
703 ;; (nanosleep -1 0) does not fail on FreeBSD
704 (with-test (:name (:exercising-concurrent-syscalls))
706 (nanosleep-errno (progn
707 (sb-unix:nanosleep -1 0)
708 (sb-unix::get-errno)))
711 :if-does-not-exist nil)
712 (sb-unix::get-errno)))
716 (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
717 (exercise-syscall (lambda () (open "no-such-file"
718 :if-does-not-exist nil))
720 (sb-thread:make-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
722 (princ "terminating threads")
723 (dolist (thread threads)
724 (sb-thread:terminate-thread thread))))
726 (format t "~&errno test done~%")
728 (with-test (:name :all-threads-have-abort-restart)
730 (let ((thread (sb-thread:make-thread (lambda () (sleep 0.1)))))
731 (sb-thread:interrupt-thread
734 (assert (find-restart 'abort)))))))
738 (format t "~&thread startup sigmask test done~%")
740 (with-test (:name (:debugger-no-hang-on-session-lock-if-interrupted))
741 (sb-debug::enable-debugger)
742 (let* ((main-thread *current-thread*)
744 (make-thread (lambda ()
746 (interrupt-thread main-thread
751 (interrupt-thread main-thread #'continue))
752 :name "interruptor")))
753 (with-session-lock (*session*)
755 (loop while (thread-alive-p interruptor-thread))))
757 (format t "~&session lock test done~%")
759 ;; expose thread creation races by exiting quickly
760 (with-test (:name (:no-thread-creation-race :light))
761 (sb-thread:make-thread (lambda ())))
763 (with-test (:name (:no-thread-creation-race :heavy))
766 (loop for i below 100 collect
767 (sb-thread:make-thread (lambda ()))))))
769 (format t "~&creation test done~%")
771 ;; interrupt handlers are per-thread with pthreads, make sure the
772 ;; handler installed in one thread is global
773 (with-test (:name (:global-interrupt-handler))
774 (sb-thread:make-thread
776 (sb-ext:run-program "sleep" '("1") :search t :wait nil))))
778 ;;;; Binding stack safety
780 (defparameter *x* nil)
781 (defparameter *n-gcs-requested* 0)
782 (defparameter *n-gcs-done* 0)
785 (defun make-something-big ()
786 (let ((x (make-string 32000)))
788 (let ((counter counter))
789 (sb-ext:finalize x (lambda () (format t " ~S" counter)
792 (defmacro wait-for-gc ()
794 (incf *n-gcs-requested*)
795 (loop while (< *n-gcs-done* *n-gcs-requested*))))
798 (loop until (< *n-gcs-done* *n-gcs-requested*))
804 (defun exercise-binding ()
806 (let ((*x* (make-something-big)))
808 ;; at this point the binding stack looks like this:
809 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
812 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
813 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
814 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
815 ;; unbinding but values are not).
817 (binding-pointer-delta (ash 2 (- sb-vm:word-shift sb-vm:n-fixnum-tag-bits))))
818 ;; bump bsp as if a BIND had just started
819 (incf sb-vm::*binding-stack-pointer* binding-pointer-delta)
821 (decf sb-vm::*binding-stack-pointer* binding-pointer-delta))))
823 (with-test (:name (:binding-stack-gc-safety))
827 (push (sb-thread:make-thread #'exercise-binding) threads)
828 (push (sb-thread:make-thread (lambda ()
834 (mapc #'sb-thread:terminate-thread threads))))
836 (format t "~&binding test done~%")
840 (defvar *errors* nil)
844 (format t "~&oops: ~A in ~S~%" e *current-thread*)
848 (with-test (:name (:unsynchronized-hash-table)
849 ;; FIXME: This test occasionally eats out craploads
850 ;; of heap instead of expected error early. Not 100%
851 ;; sure if it would finish as expected, but since it
852 ;; hits swap on my system I'm not likely to find out
853 ;; soon. Disabling for now. -- nikodemus
855 ;; We expect a (probable) error here: parellel readers and writers
856 ;; on a hash-table are not expected to work -- but we also don't
857 ;; expect this to corrupt the image.
858 (let* ((hash (make-hash-table))
860 (threads (list (sb-thread:make-thread
863 (handler-bind ((serious-condition 'oops))
865 ;;(princ "1") (force-output)
866 (setf (gethash (random 100) hash) 'h)))))
868 (sb-thread:make-thread
871 (handler-bind ((serious-condition 'oops))
873 ;;(princ "2") (force-output)
874 (remhash (random 100) hash)))))
876 (sb-thread:make-thread
879 (handler-bind ((serious-condition 'oops))
882 (sb-ext:gc :full t)))))
883 :name "collector"))))
886 (mapc #'sb-thread:terminate-thread threads))))
888 (format t "~&unsynchronized hash table test done~%")
890 (with-test (:name (:synchronized-hash-table))
891 (let* ((hash (make-hash-table :synchronized t))
893 (threads (list (sb-thread:make-thread
896 (handler-bind ((serious-condition 'oops))
898 ;;(princ "1") (force-output)
899 (setf (gethash (random 100) hash) 'h)))))
901 (sb-thread:make-thread
904 (handler-bind ((serious-condition 'oops))
906 ;;(princ "2") (force-output)
907 (remhash (random 100) hash)))))
909 (sb-thread:make-thread
912 (handler-bind ((serious-condition 'oops))
915 (sb-ext:gc :full t)))))
916 :name "collector"))))
919 (mapc #'sb-thread:terminate-thread threads))
920 (assert (not *errors*))))
922 (format t "~&synchronized hash table test done~%")
924 (with-test (:name (:hash-table-parallel-readers))
925 (let ((hash (make-hash-table))
928 do (setf (gethash (random 100) hash) 'xxx))
929 (let ((threads (list (sb-thread:make-thread
932 (handler-bind ((serious-condition 'oops))
934 until (eq t (gethash (random 100) hash))))))
936 (sb-thread:make-thread
939 (handler-bind ((serious-condition 'oops))
941 until (eq t (gethash (random 100) hash))))))
943 (sb-thread:make-thread
946 (handler-bind ((serious-condition 'oops))
948 until (eq t (gethash (random 100) hash))))))
950 (sb-thread:make-thread
953 (handler-bind ((serious-condition 'oops))
956 (sb-ext:gc :full t)))))
957 :name "collector"))))
960 (mapc #'sb-thread:terminate-thread threads))
961 (assert (not *errors*)))))
963 (format t "~&multiple reader hash table test done~%")
965 (with-test (:name (:hash-table-single-accessor-parallel-gc))
966 (let ((hash (make-hash-table))
968 (let ((threads (list (sb-thread:make-thread
970 (handler-bind ((serious-condition 'oops))
972 (let ((n (random 100)))
975 (setf (gethash n hash) 'h))))))
977 (sb-thread:make-thread
979 (handler-bind ((serious-condition 'oops))
982 (sb-ext:gc :full t))))
983 :name "collector"))))
986 (mapc #'sb-thread:terminate-thread threads))
987 (assert (not *errors*)))))
989 (format t "~&single accessor hash table test~%")
991 #| ;; a cll post from eric marsden
993 | (setq *debugger-hook*
994 | (lambda (condition old-debugger-hook)
995 | (debug:backtrace 10)
996 | (unix:unix-exit 2)))
998 | (mp::start-sigalrm-yield)
999 | (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
1000 | (mp:make-process #'roomy)
1001 | (mp:make-process #'roomy)))
1004 (with-test (:name (:condition-variable :notify-multiple))
1005 (flet ((tester (notify-fun)
1006 (let ((queue (make-waitqueue :name "queue"))
1007 (lock (make-mutex :name "lock"))
1012 (format t "condition-wait ~a~%" x)
1014 (condition-wait queue lock)
1015 (format t "woke up ~a~%" x)
1018 (let ((threads (loop for x from 1 to 10
1021 (sb-thread:make-thread (lambda ()
1025 (funcall notify-fun queue))
1027 (mapcar #'terminate-thread threads)
1028 ;; Check that all threads woke up at least once
1029 (assert (= (length (remove-duplicates data)) 10)))))))
1030 (tester (lambda (queue)
1031 (format t "~&(condition-notify queue 10)~%")
1033 (condition-notify queue 10)))
1034 (tester (lambda (queue)
1035 (format t "~&(condition-broadcast queue)~%")
1037 (condition-broadcast queue)))))
1039 (format t "waitqueue wakeup tests done~%")
1041 ;;; Make sure that a deadline handler is not invoked twice in a row in
1042 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1044 (with-test (:name (:condition-wait :deadlines :LP-512914))
1045 (let ((n 2) ; was empirically enough to trigger the bug
1046 (mutex (sb-thread:make-mutex))
1047 (waitq (sb-thread:make-waitqueue))
1049 (deadline-handler-run-twice? nil))
1052 (sb-thread:make-thread
1055 ((sb-sys:deadline-timeout
1056 (let ((already? nil))
1059 (setq deadline-handler-run-twice? t))
1062 (sb-thread:condition-broadcast waitq)
1063 (sb-sys:defer-deadline 10.0 c)))))
1064 (sb-sys:with-deadline (:seconds 0.1)
1065 (sb-thread:with-mutex (mutex)
1066 (sb-thread:condition-wait waitq mutex))))))))
1067 (push child threads)))
1068 (mapc #'sb-thread:join-thread threads)
1069 (assert (not deadline-handler-run-twice?))))
1071 (with-test (:name (:condition-wait :signal-deadline-with-interrupts-enabled))
1072 (let ((mutex (sb-thread:make-mutex))
1073 (waitq (sb-thread:make-waitqueue))
1076 (A-interrupts-enabled? :unknown)
1077 (B-interrupts-enabled? :unknown)
1080 ;; W.L.O.G., we assume that A is executed first...
1081 (setq A (sb-thread:make-thread
1084 ((sb-sys:deadline-timeout
1086 ;; We came here through the call to DECODE-TIMEOUT
1087 ;; in CONDITION-WAIT; hence both here are supposed
1088 ;; to evaluate to T.
1089 (setq A-holds? (sb-thread:holding-mutex-p mutex))
1090 (setq A-interrupts-enabled?
1091 sb-sys:*interrupts-enabled*)
1093 (sb-thread:condition-broadcast waitq)
1094 (sb-sys:defer-deadline 10.0 c))))
1095 (sb-sys:with-deadline (:seconds 0.1)
1096 (sb-thread:with-mutex (mutex)
1097 (sb-thread:condition-wait waitq mutex)))))
1099 (setq B (sb-thread:make-thread
1103 ((sb-sys:deadline-timeout
1105 ;; We came here through the call to GET-MUTEX
1106 ;; in CONDITION-WAIT (contended case of
1107 ;; reaquiring the mutex) - so the former will
1108 ;; be NIL, but interrupts should still be enabled.
1109 (setq B-holds? (sb-thread:holding-mutex-p mutex))
1110 (setq B-interrupts-enabled?
1111 sb-sys:*interrupts-enabled*)
1113 (sb-thread:condition-broadcast waitq)
1114 (sb-sys:defer-deadline 10.0 c))))
1115 (sb-sys:with-deadline (:seconds 0.1)
1116 (sb-thread:with-mutex (mutex)
1117 (sb-thread:condition-wait waitq mutex)))))
1119 (sb-thread:join-thread A)
1120 (sb-thread:join-thread B)
1121 (let ((A-result (list A-holds? A-interrupts-enabled?))
1122 (B-result (list B-holds? B-interrupts-enabled?)))
1123 ;; We also check some subtle behaviour w.r.t. whether a deadline
1124 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1125 ;; probably very internal behaviour (so user should not depend
1126 ;; on it) -- I added the testing here just to manifest current
1128 (cond ((equal A-result '(t t)) (assert (equal B-result '(nil t))))
1129 ((equal B-result '(t t)) (assert (equal A-result '(nil t))))
1131 (error "Failure: fell through wit A: ~S, B: ~S"
1135 (with-test (:name (:mutex :finalization))
1138 (setf a (make-mutex)))))
1140 (format t "mutex finalization test done~%")
1142 ;;; Check that INFO is thread-safe, at least when we're just doing reads.
1144 (let* ((symbols (loop repeat 10000 collect (gensym)))
1145 (functions (loop for (symbol . rest) on symbols
1146 for next = (car rest)
1147 for fun = (let ((next next))
1150 (funcall next (1- n))
1152 do (setf (symbol-function symbol) fun)
1154 (defun infodb-test ()
1155 (funcall (car functions) 9999)))
1157 (with-test (:name (:infodb :read))
1159 (threads (loop for i from 0 to 10
1160 collect (sb-thread:make-thread
1165 (let ((n (infodb-test)))
1168 (format t "N != 0 (~A)~%" n)
1169 (abort-thread)))))))))
1170 (wait-for-threads threads)
1173 (format t "infodb test done~%")
1175 (with-test (:name :backtrace)
1176 ;; Printing backtraces from several threads at once used to hang the
1177 ;; whole SBCL process (discovered by accident due to a timer.impure
1178 ;; test misbehaving). The cause was that packages weren't even
1179 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1180 ;; backtraces a loot of symbol lookups need to be done due to
1182 (let* ((threads (loop repeat 10
1183 collect (sb-thread:make-thread
1186 (with-output-to-string (*debug-io*)
1187 (sb-debug::backtrace 10))))))))
1188 (wait-for-threads threads)))
1190 (format t "backtrace test done~%")
1192 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1194 (with-test (:name :gc-deadlock)
1195 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1196 ;; GC due to *all-threads-lock* and session lock. On earlier
1197 ;; versions and at least on one specific box this test is good enough
1198 ;; to catch that typically well before the 1500th iteration.
1205 (when (zerop (mod i 100))
1210 (sb-thread:make-thread
1212 (sleep (random 0.001)))
1213 :name (format nil "SLEEP-~D" i))
1214 (sb-thread:make-thread
1216 ;; KLUDGE: what we are doing here is explicit,
1217 ;; but the same can happen because of a regular
1218 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1219 ;; session functions.
1220 (sb-thread::with-all-threads-lock
1221 (sb-thread::with-session-lock (sb-thread::*session*)
1223 :name (format nil "GC-~D" i)))
1225 (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
1229 (format t "~&gc deadlock test done~%")
1231 (let ((count (make-array 8 :initial-element 0)))
1232 (defun closure-one ()
1233 (declare (optimize safety))
1234 (values (incf (aref count 0)) (incf (aref count 1))
1235 (incf (aref count 2)) (incf (aref count 3))
1236 (incf (aref count 4)) (incf (aref count 5))
1237 (incf (aref count 6)) (incf (aref count 7))))
1238 (defun no-optimizing-away-closure-one ()
1239 (setf count (make-array 8 :initial-element 0))))
1244 (let ((one (make-box))
1247 (defun closure-two ()
1248 (declare (optimize safety))
1249 (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
1250 (defun no-optimizing-away-closure-two ()
1251 (setf one (make-box)
1255 (with-test (:name (:funcallable-instances))
1256 ;; the funcallable-instance implementation used not to be threadsafe
1257 ;; against setting the funcallable-instance function to a closure
1258 ;; (because the code and lexenv were set separately).
1259 (let ((fun (sb-kernel:%make-funcallable-instance 0))
1261 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1263 (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1264 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
1266 (handler-case (loop (funcall fun))
1267 (serious-condition (c) (setf condition c)))))
1268 (let ((changer (make-thread #'changer))
1269 (test (make-thread #'test)))
1272 ;; The two closures above are fairly carefully crafted
1273 ;; so that if given the wrong lexenv they will tend to
1274 ;; do some serious damage, but it is of course difficult
1275 ;; to predict where the various bits and pieces will be
1276 ;; allocated. Five seconds failed fairly reliably on
1277 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1278 (sb-ext:with-timeout 5
1279 (wait-for-threads (list test)))
1280 (error "~@<test thread got condition:~2I~_~A~@:>" condition))
1282 (terminate-thread changer)
1283 (terminate-thread test)
1284 (wait-for-threads (list changer test))))))))
1286 (format t "~&funcallable-instance test done~%")
1288 (defun random-type (n)
1289 `(integer ,(random n) ,(+ n (random n))))
1291 (defun subtypep-hash-cache-test ()
1293 (let ((type1 (random-type 500))
1294 (type2 (random-type 500)))
1295 (let ((a (subtypep type1 type2)))
1297 (assert (eq (subtypep type1 type2) a))))))
1301 (with-test (:name (:hash-cache :subtypep))
1303 (sb-thread:make-thread #'subtypep-hash-cache-test)))
1304 (format t "hash-cache tests done~%")
1306 ;;;; BLACK BOX TESTS
1308 (in-package :cl-user)
1309 (use-package :test-util)
1310 (use-package "ASSERTOID")
1312 (format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1313 (with-test (:name :parallel-defclass)
1314 (defclass test-1 () ((a :initform :orig-a)))
1315 (defclass test-2 () ((b :initform :orig-b)))
1316 (defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
1318 (d1 (sb-thread:make-thread (lambda ()
1320 do (defclass test-1 () ((a :initform :new-a)))
1324 (d2 (sb-thread:make-thread (lambda ()
1326 do (defclass test-2 () ((b :initform :new-b)))
1330 (d3 (sb-thread:make-thread (lambda ()
1332 do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
1336 (i (sb-thread:make-thread (lambda ()
1338 do (let ((i (make-instance 'test-3)))
1339 (assert (member (slot-value i 'a) '(:orig-a :new-a)))
1340 (assert (member (slot-value i 'b) '(:orig-b :new-b)))
1341 (assert (member (slot-value i 'c) '(:orig-c :new-c))))
1345 (format t "~%sleeping!~%")
1347 (format t "~%stopping!~%")
1350 (sb-thread:join-thread th)
1351 (format t "~%joined ~S~%" (sb-thread:thread-name th)))
1352 (list d1 d2 d3 i))))
1353 (format t "parallel defclass test done~%")
1355 (with-test (:name (:deadlock-detection :interrupts))
1356 (let* ((m1 (sb-thread:make-mutex :name "M1"))
1357 (m2 (sb-thread:make-mutex :name "M2"))
1358 (t1-can-go (sb-thread:make-semaphore :name "T1 can go"))
1359 (t2-can-go (sb-thread:make-semaphore :name "T2 can go"))
1360 (t1 (sb-thread:make-thread
1362 (sb-thread:with-mutex (m1)
1363 (sb-thread:wait-on-semaphore t1-can-go)
1366 (t2 (sb-thread:make-thread
1368 (sb-ext:wait-for (eq t1 (sb-thread:mutex-owner m1)))
1369 (sb-thread:with-mutex (m1 :wait-p t)
1370 (sb-thread:wait-on-semaphore t2-can-go)
1373 (sb-ext:wait-for (eq m1 (sb-thread::thread-waiting-for t2)))
1374 (sb-thread:interrupt-thread t2 (lambda ()
1375 (sb-thread:with-mutex (m2 :wait-p t)
1377 (eq m2 (sb-thread::thread-waiting-for t1)))
1378 (sb-thread:signal-semaphore t2-can-go))))
1379 (sb-ext:wait-for (eq t2 (sb-thread:mutex-owner m2)))
1380 (sb-thread:interrupt-thread t1 (lambda ()
1381 (sb-thread:with-mutex (m2 :wait-p t)
1382 (sb-thread:signal-semaphore t1-can-go))))
1383 ;; both threads should finish without a deadlock or deadlock
1385 (let ((res (list (sb-thread:join-thread t1)
1386 (sb-thread:join-thread t2))))
1387 (assert (equal '(:ok1 :ok2) res)))))
1389 (with-test (:name (:deadlock-detection :gc))
1390 ;; To semi-reliably trigger the error (in SBCL's where)
1391 ;; it was present you had to run this for > 30 seconds,
1392 ;; but that's a bit long for a single test.
1393 (let* ((stop (+ 5 (get-universal-time)))
1394 (m1 (sb-thread:make-mutex :name "m1"))
1395 (t1 (sb-thread:make-thread
1397 (loop until (> (get-universal-time) stop)
1398 do (sb-thread:with-mutex (m1)
1399 (eval `(make-array 24))))
1401 (t2 (sb-thread:make-thread
1403 (loop until (> (get-universal-time) stop)
1404 do (sb-thread:with-mutex (m1)
1405 (eval `(make-array 24))))
1407 (let ((res (list (sb-thread:join-thread t1)
1408 (sb-thread:join-thread t2))))
1409 (assert (equal '(:ok :ok) res)))))
1411 (with-test (:name :spinlock-api)
1414 (handler-bind ((sb-int:early-deprecation-warning (lambda (_)
1415 (declare (ignore _))
1417 (list (compile nil `(lambda (lock)
1418 (sb-thread::with-spinlock (lock)
1420 (compile nil `(lambda ()
1421 (sb-thread::make-spinlock :name "foo")))
1422 (compile nil `(lambda (lock)
1423 (sb-thread::get-spinlock lock)))
1424 (compile nil `(lambda (lock)
1425 (sb-thread::release-spinlock lock)))))))
1426 (assert (eql 4 warned))
1427 (handler-bind ((warning #'error))
1428 (destructuring-bind (with make get release) funs
1429 (let ((lock (funcall make)))
1431 (funcall release lock)
1432 (assert (eq t (funcall with lock))))))))