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-select* :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 (assert (eql 1 (length (list-all-threads))))
28 (assert (eq *current-thread*
29 (find (thread-name *current-thread*) (list-all-threads)
30 :key #'thread-name :test #'equal)))
32 (assert (thread-alive-p *current-thread*))
35 (interrupt-thread *current-thread* (lambda () (setq a 1)))
38 (let ((spinlock (make-spinlock)))
39 (with-spinlock (spinlock)))
41 (let ((mutex (make-mutex)))
45 (sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
47 (sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
50 (with-test (:name (:interrupt-thread :deferrables-blocked))
51 (sb-thread:interrupt-thread sb-thread:*current-thread*
53 (check-deferrables-blocked-or-lose))))
55 (with-test (:name (:interrupt-thread :deferrables-unblocked))
56 (sb-thread:interrupt-thread sb-thread:*current-thread*
59 (check-deferrables-unblocked-or-lose)))))
61 (with-test (:name (:interrupt-thread :nlx))
63 (sb-thread:interrupt-thread sb-thread:*current-thread*
65 (check-deferrables-blocked-or-lose)
67 (check-deferrables-unblocked-or-lose))
69 #-sb-thread (sb-ext:quit :unix-status 104)
71 (with-test (:name (:interrupt-thread :deferrables-unblocked-by-spinlock))
72 (let ((spinlock (sb-thread::make-spinlock))
73 (thread (sb-thread:make-thread (lambda ()
75 (sb-thread::get-spinlock spinlock)
76 (sb-thread:interrupt-thread thread
78 (check-deferrables-blocked-or-lose)
79 (sb-thread::get-spinlock spinlock)
80 (check-deferrables-unblocked-or-lose)
83 (sb-thread::release-spinlock spinlock)))
87 (defmacro defincf (name accessor &rest args)
89 (let* ((old (,accessor x ,@args))
91 (loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
92 do (setf old (,accessor x ,@args)
96 (defstruct cas-struct (slot 0))
98 (defincf incf-car car)
99 (defincf incf-cdr cdr)
100 (defincf incf-slot cas-struct-slot)
101 (defincf incf-symbol-value symbol-value)
102 (defincf incf-svref/1 svref 1)
103 (defincf incf-svref/0 svref 0)
105 (defmacro def-test-cas (name init incf op)
113 collect (sb-thread:make-thread
116 do (sb-thread:thread-yield))
117 (loop repeat n do (,incf x)))))))
120 (sb-thread:join-thread th))
121 (assert (= (,op x) (* 10 n)))))
124 (def-test-cas test-cas-car (cons 0 nil) incf-car car)
125 (def-test-cas test-cas-cdr (cons nil 0) incf-cdr cdr)
126 (def-test-cas test-cas-slot (make-cas-struct) incf-slot cas-struct-slot)
127 (def-test-cas test-cas-value (let ((x '.x.))
130 incf-symbol-value symbol-value)
131 (def-test-cas test-cas-svref/0 (vector 0 nil) incf-svref/0 (lambda (x)
133 (def-test-cas test-cas-svref/1 (vector nil 0) incf-svref/1 (lambda (x)
135 (format t "~&compare-and-swap tests done~%")
137 (let ((old-threads (list-all-threads))
138 (thread (make-thread (lambda ()
139 (assert (find *current-thread* *all-threads*))
141 (new-threads (list-all-threads)))
142 (assert (thread-alive-p thread))
143 (assert (eq thread (first new-threads)))
144 (assert (= (1+ (length old-threads)) (length new-threads)))
146 (assert (not (thread-alive-p thread))))
148 (with-test (:name '(:join-thread :nlx :default))
149 (let ((sym (gensym)))
150 (assert (eq sym (join-thread (make-thread (lambda () (sb-ext:quit)))
153 (with-test (:name '(:join-thread :nlx :error))
154 (raises-error? (join-thread (make-thread (lambda () (sb-ext:quit))))
157 (with-test (:name '(:join-thread :multiple-values))
158 (assert (equal '(1 2 3)
160 (join-thread (make-thread (lambda () (values 1 2 3))))))))
162 ;;; We had appalling scaling properties for a while. Make sure they
164 (defun scaling-test (function &optional (nthreads 5))
165 "Execute FUNCTION with NTHREADS lurking to slow it down."
166 (let ((queue (sb-thread:make-waitqueue))
167 (mutex (sb-thread:make-mutex)))
168 ;; Start NTHREADS idle threads.
169 (dotimes (i nthreads)
170 (sb-thread:make-thread (lambda ()
172 (sb-thread:condition-wait queue mutex))
174 (let ((start-time (get-internal-run-time)))
176 (prog1 (- (get-internal-run-time) start-time)
177 (sb-thread:condition-broadcast queue)))))
179 "A function that does work with the CPU."
180 (if (zerop n) 1 (* n (fact (1- n)))))
181 (let ((work (lambda () (fact 15000))))
182 (let ((zero (scaling-test work 0))
183 (four (scaling-test work 4)))
184 ;; a slightly weak assertion, but good enough for starters.
185 (assert (< four (* 1.5 zero)))))
187 ;;; For one of the interupt-thread tests, we want a foreign function
188 ;;; that does not make syscalls
190 (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
191 (format o "void loop_forever() { while(1) ; }~%"))
193 #-sunos "cc" #+sunos "gcc"
194 (or #+(or linux freebsd sunos) '(#+x86-64 "-fPIC"
195 "-shared" "-o" "threads-foreign.so" "threads-foreign.c")
196 #+darwin '(#+x86-64 "-arch" #+x86-64 "x86_64"
197 "-dynamiclib" "-o" "threads-foreign.so" "threads-foreign.c")
198 (error "Missing shared library compilation options for this platform"))
200 (sb-alien:load-shared-object (truename "threads-foreign.so"))
201 (sb-alien:define-alien-routine loop-forever sb-alien:void)
202 (delete-file "threads-foreign.c")
204 ;;; elementary "can we get a lock and release it again"
205 (let ((l (make-mutex :name "foo"))
206 (p *current-thread*))
207 (assert (eql (mutex-value l) nil) nil "1")
208 (sb-thread:get-mutex l)
209 (assert (eql (mutex-value l) p) nil "3")
210 (sb-thread:release-mutex l)
211 (assert (eql (mutex-value l) nil) nil "5"))
213 (labels ((ours-p (value)
214 (eq *current-thread* value)))
215 (let ((l (make-mutex :name "rec")))
216 (assert (eql (mutex-value l) nil) nil "1")
217 (sb-thread:with-recursive-lock (l)
218 (assert (ours-p (mutex-value l)) nil "3")
219 (sb-thread:with-recursive-lock (l)
220 (assert (ours-p (mutex-value l)) nil "4"))
221 (assert (ours-p (mutex-value l)) nil "5"))
222 (assert (eql (mutex-value l) nil) nil "6")))
224 (labels ((ours-p (value)
225 (eq *current-thread* value)))
226 (let ((l (make-spinlock :name "rec")))
227 (assert (eql (spinlock-value l) nil) nil "1")
228 (with-recursive-spinlock (l)
229 (assert (ours-p (spinlock-value l)) nil "3")
230 (with-recursive-spinlock (l)
231 (assert (ours-p (spinlock-value l)) nil "4"))
232 (assert (ours-p (spinlock-value l)) nil "5"))
233 (assert (eql (spinlock-value l) nil) nil "6")))
235 (with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
236 (let ((l (make-mutex :name "a mutex")))
238 (with-recursive-lock (l)))))
240 (with-test (:name (:spinlock :nesting-spinlock-and-recursive-spinlock))
241 (let ((l (make-spinlock :name "a spinlock")))
243 (with-recursive-spinlock (l)))))
245 (let ((l (make-spinlock :name "spinlock")))
246 (assert (eql (spinlock-value l) nil) ((spinlock-value l))
247 "spinlock not free (1)")
249 (assert (eql (spinlock-value l) *current-thread*) ((spinlock-value l))
250 "spinlock not taken"))
251 (assert (eql (spinlock-value l) nil) ((spinlock-value l))
252 "spinlock not free (2)"))
254 ;; test that SLEEP actually sleeps for at least the given time, even
255 ;; if interrupted by another thread exiting/a gc/anything
256 (let ((start-time (get-universal-time)))
257 (make-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
259 (assert (>= (get-universal-time) (+ 5 start-time))))
262 (let ((queue (make-waitqueue :name "queue"))
263 (lock (make-mutex :name "lock"))
265 (labels ((in-new-thread ()
267 (assert (eql (mutex-value lock) *current-thread*))
268 (format t "~A got mutex~%" *current-thread*)
269 ;; now drop it and sleep
270 (condition-wait queue lock)
271 ;; after waking we should have the lock again
272 (assert (eql (mutex-value lock) *current-thread*))
275 (make-thread #'in-new-thread)
276 (sleep 2) ; give it a chance to start
277 ;; check the lock is free while it's asleep
278 (format t "parent thread ~A~%" *current-thread*)
279 (assert (eql (mutex-value lock) nil))
282 (condition-notify queue))
285 (let ((queue (make-waitqueue :name "queue"))
286 (lock (make-mutex :name "lock")))
287 (labels ((ours-p (value)
288 (eq *current-thread* value))
290 (with-recursive-lock (lock)
291 (assert (ours-p (mutex-value lock)))
292 (format t "~A got mutex~%" (mutex-value lock))
293 ;; now drop it and sleep
294 (condition-wait queue lock)
295 ;; after waking we should have the lock again
296 (format t "woken, ~A got mutex~%" (mutex-value lock))
297 (assert (ours-p (mutex-value lock))))))
298 (make-thread #'in-new-thread)
299 (sleep 2) ; give it a chance to start
300 ;; check the lock is free while it's asleep
301 (format t "parent thread ~A~%" *current-thread*)
302 (assert (eql (mutex-value lock) nil))
303 (with-recursive-lock (lock)
304 (condition-notify queue))
307 (let ((mutex (make-mutex :name "contended")))
309 (let ((me *current-thread*))
313 (assert (eql (mutex-value mutex) me)))
314 (assert (not (eql (mutex-value mutex) me))))
315 (format t "done ~A~%" *current-thread*))))
316 (let ((kid1 (make-thread #'run))
317 (kid2 (make-thread #'run)))
318 (format t "contention ~A ~A~%" kid1 kid2)
319 (wait-for-threads (list kid1 kid2)))))
323 (defmacro raises-timeout-p (&body body)
324 `(handler-case (progn (progn ,@body) nil)
325 (sb-ext:timeout () t)))
327 (with-test (:name (:semaphore :wait-forever))
328 (let ((sem (make-semaphore :count 0)))
329 (assert (raises-timeout-p
330 (sb-ext:with-timeout 0.1
331 (wait-on-semaphore sem))))))
333 (with-test (:name (:semaphore :initial-count))
334 (let ((sem (make-semaphore :count 1)))
335 (sb-ext:with-timeout 0.1
336 (wait-on-semaphore sem))))
338 (with-test (:name (:semaphore :wait-then-signal))
339 (let ((sem (make-semaphore))
341 (make-thread (lambda ()
344 (signal-semaphore sem)))
345 (wait-on-semaphore sem)
346 (assert signalled-p)))
348 (with-test (:name (:semaphore :signal-then-wait))
349 (let ((sem (make-semaphore))
351 (make-thread (lambda ()
352 (signal-semaphore sem)
353 (setq signalled-p t)))
354 (loop until signalled-p)
355 (wait-on-semaphore sem)
356 (assert signalled-p)))
358 (with-test (:name (:semaphore :multiple-signals))
359 (let* ((sem (make-semaphore :count 5))
360 (threads (loop repeat 20
361 collect (make-thread (lambda ()
362 (wait-on-semaphore sem))))))
363 (flet ((count-live-threads ()
364 (count-if #'thread-alive-p threads)))
366 (assert (= 15 (count-live-threads)))
367 (signal-semaphore sem 10)
369 (assert (= 5 (count-live-threads)))
370 (signal-semaphore sem 3)
372 (assert (= 2 (count-live-threads)))
373 (signal-semaphore sem 4)
375 (assert (= 0 (count-live-threads))))))
377 (format t "~&semaphore tests done~%")
379 (defun test-interrupt (function-to-interrupt &optional quit-p)
380 (let ((child (make-thread function-to-interrupt)))
381 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
383 (format t "interrupting child ~A~%" child)
384 (interrupt-thread child
386 (format t "child pid ~A~%" *current-thread*)
387 (when quit-p (sb-ext:quit))))
391 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
392 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
395 (let ((child (test-interrupt (lambda () (loop))))) (terminate-thread child))
397 (test-interrupt #'loop-forever :quit)
399 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
400 (terminate-thread child)
401 (wait-for-threads (list child)))
403 (let ((lock (make-mutex :name "loctite"))
406 (setf child (test-interrupt
409 (assert (eql (mutex-value lock) *current-thread*)))
410 (assert (not (eql (mutex-value lock) *current-thread*)))
412 ;;hold onto lock for long enough that child can't get it immediately
414 (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
415 (format t "parent releasing lock~%"))
416 (terminate-thread child)
417 (wait-for-threads (list child)))
419 (format t "~&locking test done~%")
421 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
424 (let ((thread (sb-thread:make-thread (lambda () (loop (alloc-stuff))))))
426 (loop repeat 4 collect
427 (sb-thread:make-thread
430 (sleep (random 0.1d0))
433 (sb-thread:interrupt-thread thread (lambda ()))))))))
434 (wait-for-threads killers)
435 (sb-thread:terminate-thread thread)
436 (wait-for-threads (list thread))))
439 (format t "~&multi interrupt test done~%")
441 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
442 ;; NB this only works on x86: other ports don't have a symbol for
443 ;; pseudo-atomic atomicity
445 (sleep (random 0.1d0))
448 (princ ".") (force-output)
449 (assert (thread-alive-p *current-thread*))
451 (not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
453 (wait-for-threads (list c)))
455 (format t "~&interrupt test done~%")
457 (defparameter *interrupt-count* 0)
459 (declaim (notinline check-interrupt-count))
460 (defun check-interrupt-count (i)
461 (declare (optimize (debug 1) (speed 1)))
462 ;; This used to lose if eflags were not restored after an interrupt.
463 (unless (typep i 'fixnum)
464 (error "!!!!!!!!!!!")))
466 (let ((c (make-thread
468 (handler-bind ((error #'(lambda (cond)
471 most-positive-fixnum))))
472 (loop (check-interrupt-count *interrupt-count*)))))))
473 (let ((func (lambda ()
476 (sb-impl::atomic-incf/symbol *interrupt-count*))))
477 (setq *interrupt-count* 0)
479 (sleep (random 0.1d0))
480 (interrupt-thread c func))
481 (loop until (= *interrupt-count* 100) do (sleep 0.1))
483 (wait-for-threads (list c))))
485 (format t "~&interrupt count test done~%")
487 (defvar *runningp* nil)
489 (with-test (:name (:interrupt-thread :no-nesting))
490 (let ((thread (sb-thread:make-thread
494 (declare (special runningp))
496 (sb-thread:interrupt-thread thread
498 (let ((*runningp* t))
501 (sb-thread:interrupt-thread thread
503 (throw 'xxx *runningp*)))
504 (assert (not (sb-thread:join-thread thread)))))
506 (with-test (:name (:interrupt-thread :nesting))
507 (let ((thread (sb-thread:make-thread
511 (declare (special runningp))
513 (sb-thread:interrupt-thread thread
515 (let ((*runningp* t))
516 (sb-sys:with-interrupts
519 (sb-thread:interrupt-thread thread
521 (throw 'xxx *runningp*)))
522 (assert (sb-thread:join-thread thread))))
525 (make-thread (lambda ()
527 (sb-ext:gc) (princ "\\") (force-output))
529 (make-thread (lambda ()
532 (princ "/") (force-output))
535 (when (and a-done b-done) (return))
540 (defun waste (&optional (n 100000))
541 (loop repeat n do (make-string 16384)))
543 (loop for i below 100 do
546 (sb-thread:make-thread
554 (defparameter *aaa* nil)
555 (loop for i below 100 do
558 (sb-thread:make-thread
560 (let ((*aaa* (waste)))
562 (let ((*aaa* (waste)))
566 (format t "~&gc test done~%")
568 ;; this used to deadlock on session-lock
569 (sb-thread:make-thread (lambda () (sb-ext:gc)))
570 ;; expose thread creation races by exiting quickly
571 (sb-thread:make-thread (lambda ()))
573 (defun exercise-syscall (fn reference-errno)
574 (sb-thread:make-thread
578 (let ((errno (sb-unix::get-errno)))
579 (sleep (random 0.1d0))
580 (unless (eql errno reference-errno)
581 (format t "Got errno: ~A (~A) instead of ~A~%"
586 (sb-ext:quit :unix-status 1)))))))
588 ;; (nanosleep -1 0) does not fail on FreeBSD
590 (nanosleep-errno (progn
591 (sb-unix:nanosleep -1 0)
592 (sb-unix::get-errno)))
595 :if-does-not-exist nil)
596 (sb-unix::get-errno)))
600 (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
601 (exercise-syscall (lambda () (open "no-such-file"
602 :if-does-not-exist nil))
604 (sb-thread:make-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
606 (princ "terminating threads")
607 (dolist (thread threads)
608 (sb-thread:terminate-thread thread)))
610 (format t "~&errno test done~%")
613 (let ((thread (sb-thread:make-thread (lambda () (sleep 0.1)))))
614 (sb-thread:interrupt-thread
617 (assert (find-restart 'sb-thread:terminate-thread))))))
621 (format t "~&thread startup sigmask test done~%")
623 ;; FIXME: What is this supposed to test?
624 (sb-debug::enable-debugger)
625 (let* ((main-thread *current-thread*)
627 (make-thread (lambda ()
629 (interrupt-thread main-thread
634 (interrupt-thread main-thread #'continue))
635 :name "interruptor")))
636 (with-session-lock (*session*)
638 (loop while (thread-alive-p interruptor-thread)))
640 (format t "~&session lock test done~%")
644 (loop for i below 100 collect
645 (sb-thread:make-thread (lambda ())))))
647 (format t "~&creation test done~%")
649 ;; interrupt handlers are per-thread with pthreads, make sure the
650 ;; handler installed in one thread is global
651 (sb-thread:make-thread
653 (sb-ext:run-program "sleep" '("1") :search t :wait nil)))
655 ;;;; Binding stack safety
657 (defparameter *x* nil)
658 (defparameter *n-gcs-requested* 0)
659 (defparameter *n-gcs-done* 0)
662 (defun make-something-big ()
663 (let ((x (make-string 32000)))
665 (let ((counter counter))
666 (sb-ext:finalize x (lambda () (format t " ~S" counter)
669 (defmacro wait-for-gc ()
671 (incf *n-gcs-requested*)
672 (loop while (< *n-gcs-done* *n-gcs-requested*))))
675 (loop until (< *n-gcs-done* *n-gcs-requested*))
681 (defun exercise-binding ()
683 (let ((*x* (make-something-big)))
685 ;; at this point the binding stack looks like this:
686 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
689 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
690 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
691 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
692 ;; unbinding but values are not).
694 ;; bump bsp as if a BIND had just started
695 (incf sb-vm::*binding-stack-pointer* 2)
697 (decf sb-vm::*binding-stack-pointer* 2))))
699 (with-test (:name (:binding-stack-gc-safety))
703 (push (sb-thread:make-thread #'exercise-binding) threads)
704 (push (sb-thread:make-thread (lambda ()
710 (mapc #'sb-thread:terminate-thread threads))))
712 (format t "~&binding test done~%")
716 (defvar *errors* nil)
720 (format t "~&oops: ~A in ~S~%" e *current-thread*)
724 (with-test (:name (:unsynchronized-hash-table))
725 ;; We expect a (probable) error here: parellel readers and writers
726 ;; on a hash-table are not expected to work -- but we also don't
727 ;; expect this to corrupt the image.
728 (let* ((hash (make-hash-table))
730 (threads (list (sb-thread:make-thread
733 (handler-bind ((serious-condition 'oops))
735 ;;(princ "1") (force-output)
736 (setf (gethash (random 100) hash) 'h)))))
738 (sb-thread:make-thread
741 (handler-bind ((serious-condition 'oops))
743 ;;(princ "2") (force-output)
744 (remhash (random 100) hash)))))
746 (sb-thread:make-thread
749 (handler-bind ((serious-condition 'oops))
752 (sb-ext:gc :full t)))))
753 :name "collector"))))
756 (mapc #'sb-thread:terminate-thread threads))))
758 (format t "~&unsynchronized hash table test done~%")
760 (with-test (:name (:synchronized-hash-table))
761 (let* ((hash (make-hash-table :synchronized t))
763 (threads (list (sb-thread:make-thread
766 (handler-bind ((serious-condition 'oops))
768 ;;(princ "1") (force-output)
769 (setf (gethash (random 100) hash) 'h)))))
771 (sb-thread:make-thread
774 (handler-bind ((serious-condition 'oops))
776 ;;(princ "2") (force-output)
777 (remhash (random 100) hash)))))
779 (sb-thread:make-thread
782 (handler-bind ((serious-condition 'oops))
785 (sb-ext:gc :full t)))))
786 :name "collector"))))
789 (mapc #'sb-thread:terminate-thread threads))
790 (assert (not *errors*))))
792 (format t "~&synchronized hash table test done~%")
794 (with-test (:name (:hash-table-parallel-readers))
795 (let ((hash (make-hash-table))
798 do (setf (gethash (random 100) hash) 'xxx))
799 (let ((threads (list (sb-thread:make-thread
802 (handler-bind ((serious-condition 'oops))
804 until (eq t (gethash (random 100) hash))))))
806 (sb-thread:make-thread
809 (handler-bind ((serious-condition 'oops))
811 until (eq t (gethash (random 100) hash))))))
813 (sb-thread:make-thread
816 (handler-bind ((serious-condition 'oops))
818 until (eq t (gethash (random 100) hash))))))
820 (sb-thread:make-thread
823 (handler-bind ((serious-condition 'oops))
826 (sb-ext:gc :full t)))))
827 :name "collector"))))
830 (mapc #'sb-thread:terminate-thread threads))
831 (assert (not *errors*)))))
833 (format t "~&multiple reader hash table test done~%")
835 (with-test (:name (:hash-table-single-accessor-parallel-gc))
836 (let ((hash (make-hash-table))
838 (let ((threads (list (sb-thread:make-thread
840 (handler-bind ((serious-condition 'oops))
842 (let ((n (random 100)))
845 (setf (gethash n hash) 'h))))))
847 (sb-thread:make-thread
849 (handler-bind ((serious-condition 'oops))
852 (sb-ext:gc :full t))))
853 :name "collector"))))
856 (mapc #'sb-thread:terminate-thread threads))
857 (assert (not *errors*)))))
859 (format t "~&single accessor hash table test~%")
861 #| ;; a cll post from eric marsden
863 | (setq *debugger-hook*
864 | (lambda (condition old-debugger-hook)
865 | (debug:backtrace 10)
866 | (unix:unix-exit 2)))
868 | (mp::start-sigalrm-yield)
869 | (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
870 | (mp:make-process #'roomy)
871 | (mp:make-process #'roomy)))
874 (with-test (:name (:condition-variable :wait-multiple))
876 (let ((waitqueue (sb-thread:make-waitqueue :name "Q"))
877 (mutex (sb-thread:make-mutex :name "M"))
881 (let ((threads (loop repeat 200
883 (sb-thread:make-thread
886 (sb-sys:with-deadline (:seconds 0.01)
887 (sb-thread:with-mutex (mutex)
888 (sb-thread:condition-wait waitqueue
891 (sb-sys:deadline-timeout (c)
892 (declare (ignore c)))))))))
893 (mapc #'sb-thread:join-thread threads)
894 (assert (not failedp))))))
896 (with-test (:name (:condition-variable :notify-multiple))
897 (flet ((tester (notify-fun)
898 (let ((queue (make-waitqueue :name "queue"))
899 (lock (make-mutex :name "lock"))
904 (format t "condition-wait ~a~%" x)
906 (condition-wait queue lock)
907 (format t "woke up ~a~%" x)
910 (let ((threads (loop for x from 1 to 10
913 (sb-thread:make-thread (lambda ()
917 (funcall notify-fun queue))
919 (mapcar #'terminate-thread threads)
920 ;; Check that all threads woke up at least once
921 (assert (= (length (remove-duplicates data)) 10)))))))
922 (tester (lambda (queue)
923 (format t "~&(condition-notify queue 10)~%")
925 (condition-notify queue 10)))
926 (tester (lambda (queue)
927 (format t "~&(condition-broadcast queue)~%")
929 (condition-broadcast queue)))))
931 (format t "waitqueue wakeup tests done~%")
933 (with-test (:name (:mutex :finalization))
936 (setf a (make-mutex)))))
938 (format t "mutex finalization test done~%")
940 ;;; Check that INFO is thread-safe, at least when we're just doing reads.
942 (let* ((symbols (loop repeat 10000 collect (gensym)))
943 (functions (loop for (symbol . rest) on symbols
944 for next = (car rest)
945 for fun = (let ((next next))
948 (funcall next (1- n))
950 do (setf (symbol-function symbol) fun)
952 (defun infodb-test ()
953 (funcall (car functions) 9999)))
955 (with-test (:name (:infodb :read))
957 (threads (loop for i from 0 to 10
958 collect (sb-thread:make-thread
963 (let ((n (infodb-test)))
966 (format t "N != 0 (~A)~%" n)
967 (sb-ext:quit)))))))))
968 (wait-for-threads threads)
971 (format t "infodb test done~%")
973 (with-test (:name (:backtrace))
974 ;; Printing backtraces from several threads at once used to hang the
975 ;; whole SBCL process (discovered by accident due to a timer.impure
976 ;; test misbehaving). The cause was that packages weren't even
977 ;; thread-safe for only doing FIND-SYMBOL, and while printing
978 ;; backtraces a loot of symbol lookups need to be done due to
980 (let* ((threads (loop repeat 10
981 collect (sb-thread:make-thread
984 (with-output-to-string (*debug-io*)
985 (sb-debug::backtrace 10))))))))
986 (wait-for-threads threads)))
988 (format t "backtrace test done~%")
990 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
992 (with-test (:name (:gc-deadlock))
993 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
994 ;; GC due to *all-threads-lock* and session lock. On earlier
995 ;; versions and at least on one specific box this test is good enough
996 ;; to catch that typically well before the 1500th iteration.
1003 (when (zerop (mod i 100))
1008 (sb-thread:make-thread
1010 (sleep (random 0.001)))
1011 :name (list :sleep i))
1012 (sb-thread:make-thread
1014 ;; KLUDGE: what we are doing here is explicit,
1015 ;; but the same can happen because of a regular
1016 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1017 ;; session functions.
1018 (sb-thread::with-all-threads-lock
1019 (sb-thread::with-session-lock (sb-thread::*session*)
1021 :name (list :gc i)))
1023 (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
1027 (format t "~&gc deadlock test done~%")
1029 (let ((count (make-array 8 :initial-element 0)))
1030 (defun closure-one ()
1031 (declare (optimize safety))
1032 (values (incf (aref count 0)) (incf (aref count 1))
1033 (incf (aref count 2)) (incf (aref count 3))
1034 (incf (aref count 4)) (incf (aref count 5))
1035 (incf (aref count 6)) (incf (aref count 7))))
1036 (defun no-optimizing-away-closure-one ()
1037 (setf count (make-array 8 :initial-element 0))))
1042 (let ((one (make-box))
1045 (defun closure-two ()
1046 (declare (optimize safety))
1047 (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
1048 (defun no-optimizing-away-closure-two ()
1049 (setf one (make-box)
1053 (with-test (:name (:funcallable-instances))
1054 ;; the funcallable-instance implementation used not to be threadsafe
1055 ;; against setting the funcallable-instance function to a closure
1056 ;; (because the code and lexenv were set separately).
1057 (let ((fun (sb-kernel:%make-funcallable-instance 0))
1059 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1061 (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1062 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
1064 (handler-case (loop (funcall fun))
1065 (serious-condition (c) (setf condition c)))))
1066 (let ((changer (make-thread #'changer))
1067 (test (make-thread #'test)))
1070 ;; The two closures above are fairly carefully crafted
1071 ;; so that if given the wrong lexenv they will tend to
1072 ;; do some serious damage, but it is of course difficult
1073 ;; to predict where the various bits and pieces will be
1074 ;; allocated. Five seconds failed fairly reliably on
1075 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1076 (sb-ext:with-timeout 5
1077 (wait-for-threads (list test)))
1078 (error "~@<test thread got condition:~2I~_~A~@:>" condition))
1080 (terminate-thread changer)
1081 (terminate-thread test)
1082 (wait-for-threads (list changer test))))))))
1084 (format t "~&funcallable-instance test done~%")
1086 (defun random-type (n)
1087 `(integer ,(random n) ,(+ n (random n))))
1089 (defun subtypep-hash-cache-test ()
1091 (let ((type1 (random-type 500))
1092 (type2 (random-type 500)))
1093 (let ((a (subtypep type1 type2)))
1095 (assert (eq (subtypep type1 type2) a))))))
1099 (with-test (:name '(:hash-cache :subtypep))
1101 (sb-thread:make-thread #'subtypep-hash-cache-test)))
1102 (format t "hash-cache tests done~%")
1104 ;;;; BLACK BOX TESTS
1106 (in-package :cl-user)
1107 (use-package :test-util)
1108 (use-package "ASSERTOID")
1110 (format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1111 (with-test (:name :parallel-defclass)
1112 (defclass test-1 () ((a :initform :orig-a)))
1113 (defclass test-2 () ((b :initform :orig-b)))
1114 (defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
1116 (d1 (sb-thread:make-thread (lambda ()
1118 do (defclass test-1 () ((a :initform :new-a)))
1122 (d2 (sb-thread:make-thread (lambda ()
1124 do (defclass test-2 () ((b :initform :new-b)))
1128 (d3 (sb-thread:make-thread (lambda ()
1130 do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
1134 (i (sb-thread:make-thread (lambda ()
1136 do (let ((i (make-instance 'test-3)))
1137 (assert (member (slot-value i 'a) '(:orig-a :new-a)))
1138 (assert (member (slot-value i 'b) '(:orig-b :new-b)))
1139 (assert (member (slot-value i 'c) '(:orig-c :new-c))))
1143 (format t "~%sleeping!~%")
1145 (format t "~%stopping!~%")
1148 (sb-thread:join-thread th)
1149 (format t "~%joined ~S~%" (sb-thread:thread-name th)))
1150 (list d1 d2 d3 i))))
1151 (format t "parallel defclass test done~%")