1.0.1.18:
[sbcl/simd.git] / tests / threads.impure.lisp
blob0d5453b2fc817b3ab01da3afe7790001595e33f7
1 ;;;; miscellaneous tests of thread stuff
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
8 ;;;; from CMU CL.
9 ;;;
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.
14 (in-package "SB-THREAD") ; this is white-box testing, really
16 (use-package :test-util)
18 (defun wait-for-threads (threads)
19 (loop while (some #'sb-thread:thread-alive-p threads) do (sleep 0.01)))
21 (assert (eql 1 (length (list-all-threads))))
23 (assert (eq *current-thread*
24 (find (thread-name *current-thread*) (list-all-threads)
25 :key #'thread-name :test #'equal)))
27 (assert (thread-alive-p *current-thread*))
29 (let ((a 0))
30 (interrupt-thread *current-thread* (lambda () (setq a 1)))
31 (assert (eql a 1)))
33 (let ((spinlock (make-spinlock)))
34 (with-spinlock (spinlock)))
36 (let ((mutex (make-mutex)))
37 (with-mutex (mutex)
38 mutex))
40 #-sb-thread (sb-ext:quit :unix-status 104)
42 (let ((old-threads (list-all-threads))
43 (thread (make-thread (lambda ()
44 (assert (find *current-thread* *all-threads*))
45 (sleep 2))))
46 (new-threads (list-all-threads)))
47 (assert (thread-alive-p thread))
48 (assert (eq thread (first new-threads)))
49 (assert (= (1+ (length old-threads)) (length new-threads)))
50 (sleep 3)
51 (assert (not (thread-alive-p thread))))
53 ;;; We had appalling scaling properties for a while. Make sure they
54 ;;; don't reappear.
55 (defun scaling-test (function &optional (nthreads 5))
56 "Execute FUNCTION with NTHREADS lurking to slow it down."
57 (let ((queue (sb-thread:make-waitqueue))
58 (mutex (sb-thread:make-mutex)))
59 ;; Start NTHREADS idle threads.
60 (dotimes (i nthreads)
61 (sb-thread:make-thread (lambda ()
62 (with-mutex (mutex)
63 (sb-thread:condition-wait queue mutex))
64 (sb-ext:quit))))
65 (let ((start-time (get-internal-run-time)))
66 (funcall function)
67 (prog1 (- (get-internal-run-time) start-time)
68 (sb-thread:condition-broadcast queue)))))
69 (defun fact (n)
70 "A function that does work with the CPU."
71 (if (zerop n) 1 (* n (fact (1- n)))))
72 (let ((work (lambda () (fact 15000))))
73 (let ((zero (scaling-test work 0))
74 (four (scaling-test work 4)))
75 ;; a slightly weak assertion, but good enough for starters.
76 (assert (< four (* 1.5 zero)))))
78 ;;; For one of the interupt-thread tests, we want a foreign function
79 ;;; that does not make syscalls
81 (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
82 (format o "void loop_forever() { while(1) ; }~%"))
83 (sb-ext:run-program
84 #-sunos "cc" #+sunos "gcc"
85 (or #+(or linux freebsd sunos) '(#+x86-64 "-fPIC"
86 "-shared" "-o" "threads-foreign.so" "threads-foreign.c")
87 #+darwin '("-dynamiclib" "-o" "threads-foreign.so" "threads-foreign.c")
88 (error "Missing shared library compilation options for this platform"))
89 :search t)
90 (sb-alien:load-shared-object "threads-foreign.so")
91 (sb-alien:define-alien-routine loop-forever sb-alien:void)
94 ;;; elementary "can we get a lock and release it again"
95 (let ((l (make-mutex :name "foo"))
96 (p *current-thread*))
97 (assert (eql (mutex-value l) nil) nil "1")
98 (sb-thread:get-mutex l)
99 (assert (eql (mutex-value l) p) nil "3")
100 (sb-thread:release-mutex l)
101 (assert (eql (mutex-value l) nil) nil "5"))
103 (labels ((ours-p (value)
104 (eq *current-thread* value)))
105 (let ((l (make-mutex :name "rec")))
106 (assert (eql (mutex-value l) nil) nil "1")
107 (sb-thread:with-recursive-lock (l)
108 (assert (ours-p (mutex-value l)) nil "3")
109 (sb-thread:with-recursive-lock (l)
110 (assert (ours-p (mutex-value l)) nil "4"))
111 (assert (ours-p (mutex-value l)) nil "5"))
112 (assert (eql (mutex-value l) nil) nil "6")))
114 (with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
115 (let ((l (make-mutex :name "a mutex")))
116 (with-mutex (l)
117 (with-recursive-lock (l)))))
119 (let ((l (make-spinlock :name "spinlock")))
120 (assert (eql (spinlock-value l) 0) nil "1")
121 (with-spinlock (l)
122 (assert (eql (spinlock-value l) 1) nil "2"))
123 (assert (eql (spinlock-value l) 0) nil "3"))
125 ;; test that SLEEP actually sleeps for at least the given time, even
126 ;; if interrupted by another thread exiting/a gc/anything
127 (let ((start-time (get-universal-time)))
128 (make-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
129 (sleep 5)
130 (assert (>= (get-universal-time) (+ 5 start-time))))
133 (let ((queue (make-waitqueue :name "queue"))
134 (lock (make-mutex :name "lock"))
135 (n 0))
136 (labels ((in-new-thread ()
137 (with-mutex (lock)
138 (assert (eql (mutex-value lock) *current-thread*))
139 (format t "~A got mutex~%" *current-thread*)
140 ;; now drop it and sleep
141 (condition-wait queue lock)
142 ;; after waking we should have the lock again
143 (assert (eql (mutex-value lock) *current-thread*))
144 (assert (eql n 1))
145 (decf n))))
146 (make-thread #'in-new-thread)
147 (sleep 2) ; give it a chance to start
148 ;; check the lock is free while it's asleep
149 (format t "parent thread ~A~%" *current-thread*)
150 (assert (eql (mutex-value lock) nil))
151 (with-mutex (lock)
152 (incf n)
153 (condition-notify queue))
154 (sleep 1)))
156 (let ((queue (make-waitqueue :name "queue"))
157 (lock (make-mutex :name "lock")))
158 (labels ((ours-p (value)
159 (eq *current-thread* value))
160 (in-new-thread ()
161 (with-recursive-lock (lock)
162 (assert (ours-p (mutex-value lock)))
163 (format t "~A got mutex~%" (mutex-value lock))
164 ;; now drop it and sleep
165 (condition-wait queue lock)
166 ;; after waking we should have the lock again
167 (format t "woken, ~A got mutex~%" (mutex-value lock))
168 (assert (ours-p (mutex-value lock))))))
169 (make-thread #'in-new-thread)
170 (sleep 2) ; give it a chance to start
171 ;; check the lock is free while it's asleep
172 (format t "parent thread ~A~%" *current-thread*)
173 (assert (eql (mutex-value lock) nil))
174 (with-recursive-lock (lock)
175 (condition-notify queue))
176 (sleep 1)))
178 (let ((mutex (make-mutex :name "contended")))
179 (labels ((run ()
180 (let ((me *current-thread*))
181 (dotimes (i 100)
182 (with-mutex (mutex)
183 (sleep .03)
184 (assert (eql (mutex-value mutex) me)))
185 (assert (not (eql (mutex-value mutex) me))))
186 (format t "done ~A~%" *current-thread*))))
187 (let ((kid1 (make-thread #'run))
188 (kid2 (make-thread #'run)))
189 (format t "contention ~A ~A~%" kid1 kid2)
190 (wait-for-threads (list kid1 kid2)))))
192 ;;; semaphores
194 (defmacro raises-timeout-p (&body body)
195 `(handler-case (progn (progn ,@body) nil)
196 (sb-ext:timeout () t)))
198 (with-test (:name (:semaphore :wait-forever))
199 (let ((sem (make-semaphore :count 0)))
200 (assert (raises-timeout-p
201 (sb-ext:with-timeout 0.1
202 (wait-on-semaphore sem))))))
204 (with-test (:name (:semaphore :initial-count))
205 (let ((sem (make-semaphore :count 1)))
206 (sb-ext:with-timeout 0.1
207 (wait-on-semaphore sem))))
209 (with-test (:name (:semaphore :wait-then-signal))
210 (let ((sem (make-semaphore))
211 (signalled-p nil))
212 (make-thread (lambda ()
213 (sleep 0.1)
214 (setq signalled-p t)
215 (signal-semaphore sem)))
216 (wait-on-semaphore sem)
217 (assert signalled-p)))
219 (with-test (:name (:semaphore :signal-then-wait))
220 (let ((sem (make-semaphore))
221 (signalled-p nil))
222 (make-thread (lambda ()
223 (signal-semaphore sem)
224 (setq signalled-p t)))
225 (loop until signalled-p)
226 (wait-on-semaphore sem)
227 (assert signalled-p)))
229 (with-test (:name (:semaphore :multiple-signals))
230 (let* ((sem (make-semaphore :count 5))
231 (threads (loop repeat 20
232 collect (make-thread (lambda ()
233 (wait-on-semaphore sem))))))
234 (flet ((count-live-threads ()
235 (count-if #'thread-alive-p threads)))
236 (sleep 0.5)
237 (assert (= 15 (count-live-threads)))
238 (signal-semaphore sem 10)
239 (sleep 0.5)
240 (assert (= 5 (count-live-threads)))
241 (signal-semaphore sem 3)
242 (sleep 0.5)
243 (assert (= 2 (count-live-threads)))
244 (signal-semaphore sem 4)
245 (sleep 0.5)
246 (assert (= 0 (count-live-threads))))))
248 (format t "~&semaphore tests done~%")
250 (defun test-interrupt (function-to-interrupt &optional quit-p)
251 (let ((child (make-thread function-to-interrupt)))
252 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
253 (sleep 2)
254 (format t "interrupting child ~A~%" child)
255 (interrupt-thread child
256 (lambda ()
257 (format t "child pid ~A~%" *current-thread*)
258 (when quit-p (sb-ext:quit))))
259 (sleep 1)
260 child))
262 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
263 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
264 ;; in pseudo-atomic
266 (let ((child (test-interrupt (lambda () (loop))))) (terminate-thread child))
268 (test-interrupt #'loop-forever :quit)
270 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
271 (terminate-thread child)
272 (wait-for-threads (list child)))
274 (let ((lock (make-mutex :name "loctite"))
275 child)
276 (with-mutex (lock)
277 (setf child (test-interrupt
278 (lambda ()
279 (with-mutex (lock)
280 (assert (eql (mutex-value lock) *current-thread*)))
281 (assert (not (eql (mutex-value lock) *current-thread*)))
282 (sleep 10))))
283 ;;hold onto lock for long enough that child can't get it immediately
284 (sleep 5)
285 (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
286 (format t "parent releasing lock~%"))
287 (terminate-thread child)
288 (wait-for-threads (list child)))
290 (format t "~&locking test done~%")
292 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
294 (progn
295 (let ((thread (sb-thread:make-thread (lambda () (loop (alloc-stuff))))))
296 (let ((killers
297 (loop repeat 4 collect
298 (sb-thread:make-thread
299 (lambda ()
300 (loop repeat 25 do
301 (sleep (random 0.1d0))
302 (princ ".")
303 (force-output)
304 (sb-thread:interrupt-thread thread (lambda ()))))))))
305 (wait-for-threads killers)
306 (sb-thread:terminate-thread thread)
307 (wait-for-threads (list thread))))
308 (sb-ext:gc :full t))
310 (format t "~&multi interrupt test done~%")
312 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
313 ;; NB this only works on x86: other ports don't have a symbol for
314 ;; pseudo-atomic atomicity
315 (dotimes (i 100)
316 (sleep (random 0.1d0))
317 (interrupt-thread c
318 (lambda ()
319 (princ ".") (force-output)
320 (assert (thread-alive-p *current-thread*))
321 (assert
322 (not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
323 (terminate-thread c)
324 (wait-for-threads (list c)))
326 (format t "~&interrupt test done~%")
328 (defparameter *interrupt-count* 0)
330 (declaim (notinline check-interrupt-count))
331 (defun check-interrupt-count (i)
332 (declare (optimize (debug 1) (speed 1)))
333 ;; This used to lose if eflags were not restored after an interrupt.
334 (unless (typep i 'fixnum)
335 (error "!!!!!!!!!!!")))
337 (let ((c (make-thread
338 (lambda ()
339 (handler-bind ((error #'(lambda (cond)
340 (princ cond)
341 (sb-debug:backtrace
342 most-positive-fixnum))))
343 (loop (check-interrupt-count *interrupt-count*)))))))
344 (let ((func (lambda ()
345 (princ ".")
346 (force-output)
347 (sb-impl::atomic-incf/symbol *interrupt-count*))))
348 (setq *interrupt-count* 0)
349 (dotimes (i 100)
350 (sleep (random 0.1d0))
351 (interrupt-thread c func))
352 (loop until (= *interrupt-count* 100) do (sleep 0.1))
353 (terminate-thread c)
354 (wait-for-threads (list c))))
356 (format t "~&interrupt count test done~%")
358 (let (a-done b-done)
359 (make-thread (lambda ()
360 (dotimes (i 100)
361 (sb-ext:gc) (princ "\\") (force-output))
362 (setf a-done t)))
363 (make-thread (lambda ()
364 (dotimes (i 25)
365 (sb-ext:gc :full t)
366 (princ "/") (force-output))
367 (setf b-done t)))
368 (loop
369 (when (and a-done b-done) (return))
370 (sleep 1)))
372 (terpri)
374 (defun waste (&optional (n 100000))
375 (loop repeat n do (make-string 16384)))
377 (loop for i below 100 do
378 (princ "!")
379 (force-output)
380 (sb-thread:make-thread
381 #'(lambda ()
382 (waste)))
383 (waste)
384 (sb-ext:gc))
386 (terpri)
388 (defparameter *aaa* nil)
389 (loop for i below 100 do
390 (princ "!")
391 (force-output)
392 (sb-thread:make-thread
393 #'(lambda ()
394 (let ((*aaa* (waste)))
395 (waste))))
396 (let ((*aaa* (waste)))
397 (waste))
398 (sb-ext:gc))
400 (format t "~&gc test done~%")
402 ;; this used to deadlock on session-lock
403 (sb-thread:make-thread (lambda () (sb-ext:gc)))
404 ;; expose thread creation races by exiting quickly
405 (sb-thread:make-thread (lambda ()))
407 (defun exercise-syscall (fn reference-errno)
408 (sb-thread:make-thread
409 (lambda ()
410 (loop do
411 (funcall fn)
412 (let ((errno (sb-unix::get-errno)))
413 (sleep (random 0.1d0))
414 (unless (eql errno reference-errno)
415 (format t "Got errno: ~A (~A) instead of ~A~%"
416 errno
417 (sb-unix::strerror)
418 reference-errno)
419 (force-output)
420 (sb-ext:quit :unix-status 1)))))))
422 ;; (nanosleep -1 0) does not fail on FreeBSD
423 (let* (#-freebsd
424 (nanosleep-errno (progn
425 (sb-unix:nanosleep -1 0)
426 (sb-unix::get-errno)))
427 (open-errno (progn
428 (open "no-such-file"
429 :if-does-not-exist nil)
430 (sb-unix::get-errno)))
431 (threads
432 (list
433 #-freebsd
434 (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
435 (exercise-syscall (lambda () (open "no-such-file"
436 :if-does-not-exist nil))
437 open-errno)
438 (sb-thread:make-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
439 (sleep 10)
440 (princ "terminating threads")
441 (dolist (thread threads)
442 (sb-thread:terminate-thread thread)))
444 (format t "~&errno test done~%")
446 (loop repeat 100 do
447 (let ((thread (sb-thread:make-thread (lambda () (sleep 0.1)))))
448 (sb-thread:interrupt-thread
449 thread
450 (lambda ()
451 (assert (find-restart 'sb-thread:terminate-thread))))))
453 (sb-ext:gc :full t)
455 (format t "~&thread startup sigmask test done~%")
457 (sb-debug::enable-debugger)
458 (let* ((main-thread *current-thread*)
459 (interruptor-thread
460 (make-thread (lambda ()
461 (sleep 2)
462 (interrupt-thread main-thread #'break)
463 (sleep 2)
464 (interrupt-thread main-thread #'continue)))))
465 (with-session-lock (*session*)
466 (sleep 3))
467 (loop while (thread-alive-p interruptor-thread)))
469 (format t "~&session lock test done~%")
471 (loop repeat 20 do
472 (wait-for-threads
473 (loop for i below 100 collect
474 (sb-thread:make-thread (lambda ())))))
476 (format t "~&creation test done~%")
478 ;; interrupt handlers are per-thread with pthreads, make sure the
479 ;; handler installed in one thread is global
480 (sb-thread:make-thread
481 (lambda ()
482 (sb-ext:run-program "sleep" '("1") :search t :wait nil)))
484 ;;;; Binding stack safety
486 (defparameter *x* nil)
487 (defparameter *n-gcs-requested* 0)
488 (defparameter *n-gcs-done* 0)
490 (let ((counter 0))
491 (defun make-something-big ()
492 (let ((x (make-string 32000)))
493 (incf counter)
494 (let ((counter counter))
495 (sb-ext:finalize x (lambda () (format t " ~S" counter)
496 (force-output)))))))
498 (defmacro wait-for-gc ()
499 `(progn
500 (incf *n-gcs-requested*)
501 (loop while (< *n-gcs-done* *n-gcs-requested*))))
503 (defun send-gc ()
504 (loop until (< *n-gcs-done* *n-gcs-requested*))
505 (format t "G")
506 (force-output)
507 (sb-ext:gc)
508 (incf *n-gcs-done*))
510 (defun exercise-binding ()
511 (loop
512 (let ((*x* (make-something-big)))
513 (let ((*x* 42))
514 ;; at this point the binding stack looks like this:
515 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
517 (wait-for-gc)
518 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
519 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
520 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
521 ;; unbinding but values are not).
522 (let ((*x* nil))
523 ;; bump bsp as if a BIND had just started
524 (incf sb-vm::*binding-stack-pointer* 2)
525 (wait-for-gc)
526 (decf sb-vm::*binding-stack-pointer* 2))))
528 (with-test (:name (:binding-stack-gc-safety))
529 (let (threads)
530 (unwind-protect
531 (progn
532 (push (sb-thread:make-thread #'exercise-binding) threads)
533 (push (sb-thread:make-thread (lambda ()
534 (loop
535 (sleep 0.1)
536 (send-gc))))
537 threads)
538 (sleep 4))
539 (mapc #'sb-thread:terminate-thread threads))))
541 (format t "~&binding test done~%")
543 ;; Try to corrupt the NEXT-VECTOR. Operations on a hash table with a
544 ;; cyclic NEXT-VECTOR can loop endlessly in a WITHOUT-GCING form
545 ;; causing the next gc hang SBCL.
546 (with-test (:name (:hash-table-thread-safety))
547 (let* ((hash (make-hash-table))
548 (threads (list (sb-thread:make-thread
549 (lambda ()
550 (loop
551 ;;(princ "1") (force-output)
552 (setf (gethash (random 100) hash) 'h))))
553 (sb-thread:make-thread
554 (lambda ()
555 (loop
556 ;;(princ "2") (force-output)
557 (remhash (random 100) hash))))
558 (sb-thread:make-thread
559 (lambda ()
560 (loop
561 (sleep (random 1.0))
562 (sb-ext:gc :full t)))))))
563 (unwind-protect
564 (sleep 5)
565 (mapc #'sb-thread:terminate-thread threads))))
567 (format t "~&hash table test done~%")
568 #| ;; a cll post from eric marsden
569 | (defun crash ()
570 | (setq *debugger-hook*
571 | (lambda (condition old-debugger-hook)
572 | (debug:backtrace 10)
573 | (unix:unix-exit 2)))
574 | #+live-dangerously
575 | (mp::start-sigalrm-yield)
576 | (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
577 | (mp:make-process #'roomy)
578 | (mp:make-process #'roomy)))
581 (with-test (:name (:condition-variable :notify-multiple))
582 (flet ((tester (notify-fun)
583 (let ((queue (make-waitqueue :name "queue"))
584 (lock (make-mutex :name "lock"))
585 (data nil))
586 (labels ((test (x)
587 (loop
588 (with-mutex (lock)
589 (format t "condition-wait ~a~%" x)
590 (force-output)
591 (condition-wait queue lock)
592 (format t "woke up ~a~%" x)
593 (force-output)
594 (push x data)))))
595 (let ((threads (loop for x from 1 to 10
596 collect
597 (let ((x x))
598 (sb-thread:make-thread (lambda ()
599 (test x)))))))
600 (sleep 5)
601 (with-mutex (lock)
602 (funcall notify-fun queue))
603 (sleep 5)
604 (mapcar #'terminate-thread threads)
605 ;; Check that all threads woke up at least once
606 (assert (= (length (remove-duplicates data)) 10)))))))
607 (tester (lambda (queue)
608 (format t "~&(condition-notify queue 10)~%")
609 (force-output)
610 (condition-notify queue 10)))
611 (tester (lambda (queue)
612 (format t "~&(condition-broadcast queue)~%")
613 (force-output)
614 (condition-broadcast queue)))))
616 (format t "waitqueue wakeup tests done~%")
618 (with-test (:name (:mutex :finalization))
619 (let ((a nil))
620 (dotimes (i 500000)
621 (setf a (make-mutex)))))
623 (format t "mutex finalization test done~%")
625 ;;; Check that INFO is thread-safe, at least when we're just doing reads.
627 (let* ((symbols (loop repeat 10000 collect (gensym)))
628 (functions (loop for (symbol . rest) on symbols
629 for next = (car rest)
630 for fun = (let ((next next))
631 (lambda (n)
632 (if next
633 (funcall next (1- n))
634 n)))
635 do (setf (symbol-function symbol) fun)
636 collect fun)))
637 (defun infodb-test ()
638 (funcall (car functions) 9999)))
640 (with-test (:name (:infodb :read))
641 (let* ((ok t)
642 (threads (loop for i from 0 to 10
643 collect (sb-thread:make-thread
644 (let ((i i))
645 (lambda ()
646 (dotimes (j 100)
647 (write-char #\-)
648 (finish-output)
649 (let ((n (infodb-test)))
650 (unless (zerop n)
651 (setf ok nil)
652 (format t "N != 0 (~A)~%" n)
653 (quit))))))))))
654 (wait-for-threads threads)
655 (assert ok)))
657 (format t "infodb test done~%")
659 (with-test (:name (:backtrace))
660 ;; Printing backtraces from several threads at once used to hang the
661 ;; whole SBCL process (discovered by accident due to a timer.impure
662 ;; test misbehaving). The cause was that packages weren't even
663 ;; thread-safe for only doing FIND-SYMBOL, and while printing
664 ;; backtraces a loot of symbol lookups need to be done due to
665 ;; *PRINT-ESCAPE*.
666 (let* ((threads (loop repeat 10
667 collect (sb-thread:make-thread
668 (lambda ()
669 (dotimes (i 1000)
670 (with-output-to-string (*debug-io*)
671 (sb-debug::backtrace 10))))))))
672 (wait-for-threads threads)))
674 (format t "backtrace test done~%")
676 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
678 (with-test (:name (:gc-deadlock))
679 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
680 ;; GC due to *all-threads-lock* and session lock. On earlier
681 ;; versions and at least on one specific box this test is good enough
682 ;; to catch that typically well before the 1500th iteration.
683 (loop
684 with i = 0
685 with n = 3000
686 while (< i n)
688 (incf i)
689 (when (zerop (mod i 100))
690 (write-char #\.)
691 (force-output))
692 (handler-case
693 (if (oddp i)
694 (sb-thread:make-thread
695 (lambda ()
696 (sleep (random 0.001)))
697 :name (list :sleep i))
698 (sb-thread:make-thread
699 (lambda ()
700 ;; KLUDGE: what we are doing here is explicit,
701 ;; but the same can happen because of a regular
702 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
703 ;; session functions.
704 (sb-thread:with-mutex (sb-thread::*all-threads-lock*)
705 (sb-thread::with-session-lock (sb-thread::*session*)
706 (sb-ext:gc))))
707 :name (list :gc i)))
708 (error (e)
709 (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
710 (sleep 0.1)
711 (incf i)))))
713 (format t "~&gc deadlock test done~%")
715 (let ((count (make-array 8 :initial-element 0)))
716 (defun closure-one ()
717 (declare (optimize safety))
718 (values (incf (aref count 0)) (incf (aref count 1))
719 (incf (aref count 2)) (incf (aref count 3))
720 (incf (aref count 4)) (incf (aref count 5))
721 (incf (aref count 6)) (incf (aref count 7))))
722 (defun no-optimizing-away-closure-one ()
723 (setf count (make-array 8 :initial-element 0))))
725 (defstruct box
726 (count 0))
728 (let ((one (make-box))
729 (two (make-box))
730 (three (make-box)))
731 (defun closure-two ()
732 (declare (optimize safety))
733 (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
734 (defun no-optimizing-away-closure-two ()
735 (setf one (make-box)
736 two (make-box)
737 three (make-box))))
739 (with-test (:name (:funcallable-instances))
740 ;; the funcallable-instance implementation used not to be threadsafe
741 ;; against setting the funcallable-instance function to a closure
742 ;; (because the code and lexenv were set separately).
743 (let ((fun (sb-kernel:%make-funcallable-instance 0))
744 (condition nil))
745 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
746 (flet ((changer ()
747 (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
748 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
749 (test ()
750 (handler-case (loop (funcall fun))
751 (serious-condition (c) (setf condition c)))))
752 (let ((changer (make-thread #'changer))
753 (test (make-thread #'test)))
754 (handler-case
755 (progn
756 ;; The two closures above are fairly carefully crafted
757 ;; so that if given the wrong lexenv they will tend to
758 ;; do some serious damage, but it is of course difficult
759 ;; to predict where the various bits and pieces will be
760 ;; allocated. Five seconds failed fairly reliably on
761 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
762 (sb-ext:with-timeout 5
763 (wait-for-threads (list test)))
764 (error "~@<test thread got condition:~2I~_~A~@:>" condition))
765 (sb-ext:timeout ()
766 (terminate-thread changer)
767 (terminate-thread test)
768 (wait-for-threads (list changer test))))))))
770 (format t "~&funcallable-instance test done~%")
772 (defun random-type (n)
773 `(integer ,(random n) ,(+ n (random n))))
775 (defun subtypep-hash-cache-test ()
776 (dotimes (i 10000)
777 (let ((type1 (random-type 500))
778 (type2 (random-type 500)))
779 (let ((a (subtypep type1 type2)))
780 (dotimes (i 100)
781 (assert (eq (subtypep type1 type2) a))))))
782 (format t "ok~%")
783 (force-output))
785 (with-test (:name '(:hash-cache :subtypep))
786 (dotimes (i 10)
787 (sb-thread:make-thread #'subtypep-hash-cache-test)))
789 (format t "hash-cache tests done~%")