Make INFO's compiler-macro more forgiving.
[sbcl.git] / tests / threads.impure.lisp
blob36fac5c8034726aea6b3be6f3f2ee1e2a80d508c
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 ; WHITE-BOX TESTS
16 (in-package "SB-THREAD")
17 (shadowing-import 'assertoid:assert-error)
18 (use-package :test-util)
19 (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)))
38 (with-mutex (mutex)
39 mutex)))
41 (sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
42 void
43 (where sb-alien:unsigned-long))
44 (sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
45 void
46 (where sb-alien:unsigned-long))
48 (with-test (:name (:interrupt-thread :basics :no-unwinding))
49 (let ((a 0))
50 (interrupt-thread *current-thread* (lambda () (setq a 1)))
51 (assert (eql a 1))))
53 (with-test (:name (:interrupt-thread :deferrables-blocked))
54 (sb-thread:interrupt-thread sb-thread:*current-thread*
55 (lambda ()
56 (check-deferrables-blocked-or-lose 0))))
58 (with-test (:name (:interrupt-thread :deferrables-unblocked))
59 (sb-thread:interrupt-thread sb-thread:*current-thread*
60 (lambda ()
61 (with-interrupts
62 (check-deferrables-unblocked-or-lose 0)))))
64 (with-test (:name (:interrupt-thread :nlx))
65 (catch 'xxx
66 (sb-thread:interrupt-thread sb-thread:*current-thread*
67 (lambda ()
68 (check-deferrables-blocked-or-lose 0)
69 (throw 'xxx nil))))
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)))
78 (with-mutex (m)
79 (assert (null (join-thread (make-thread
80 (lambda ()
81 (with-mutex (m :timeout 0.1)
82 t)))))))
83 (assert (join-thread (make-thread
84 (lambda ()
85 (with-mutex (m :timeout 0.1)
86 t)))))))
88 (with-test (:name (:interrupt-thread :deferrables-unblocked-by-lock))
89 (let ((lock (sb-thread::make-mutex))
90 (thread (make-join-thread (lambda ()
91 (loop (sleep 1))))))
92 (sb-thread::grab-mutex lock)
93 (sb-thread:interrupt-thread thread
94 (lambda ()
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)))
99 (sleep 3)
100 (sb-thread::release-mutex lock)))
102 ;;; compare-and-swap
104 (defmacro defincf (name accessor &rest args)
105 `(defun ,name (x)
106 (let* ((old (,accessor x ,@args))
107 (new (1+ old)))
108 (loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
109 do (setf old (,accessor x ,@args)
110 new (1+ old)))
111 new)))
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)
124 (flet ((,name (n)
125 (declare (fixnum n))
126 (let* ((x ,init)
127 (run nil)
128 (threads
129 (loop repeat 10
130 collect (sb-thread:make-thread
131 (lambda ()
132 (loop until run
133 do (sb-thread:thread-yield))
134 (loop repeat n do (,incf x)))))))
135 (setf run t)
136 (dolist (th threads)
137 (sb-thread:join-thread th))
138 (assert (= (,op x) (* 10 n))))))
139 (,name 200000))))
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.))
145 (set x 0)
147 incf-symbol-value symbol-value)
148 (def-test-cas test-cas-svref/0 (vector 0 nil) incf-svref/0 (lambda (x)
149 (svref x 0)))
150 (def-test-cas test-cas-svref/1 (vector nil 0) incf-svref/1 (lambda (x)
151 (svref x 1)))
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*))
158 (sleep 2))))
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)))
163 (sleep 3)
164 (assert (not (thread-alive-p thread))))
166 (with-test (:name (:join-thread :nlx :default))
167 (let ((sym (gensym)))
168 (assert (eq sym (join-thread (make-thread (lambda () (sb-thread:abort-thread)))
169 :default sym)))))
171 (with-test (:name (:join-thread :nlx :error))
172 (assert-error (join-thread (make-thread (lambda () (sb-thread:abort-thread))))
173 join-thread-error))
175 (with-test (:name (:join-thread :multiple-values))
176 (assert (equal '(1 2 3)
177 (multiple-value-list
178 (join-thread (make-thread (lambda () (values 1 2 3))))))))
180 ;;; We had appalling scaling properties for a while. Make sure they
181 ;;; don't reappear.
182 (defun scaling-test (function &optional (nthreads 5))
183 "Execute FUNCTION with NTHREADS lurking to slow it down."
184 (let ((queue (sb-thread:make-waitqueue))
185 (mutex (sb-thread:make-mutex)))
186 ;; Start NTHREADS idle threads.
187 (dotimes (i nthreads)
188 (make-join-thread (lambda ()
189 (with-mutex (mutex)
190 (sb-thread:condition-wait queue mutex))
191 (sb-thread:abort-thread))))
192 (let ((start-time (get-internal-run-time)))
193 (funcall function)
194 (prog1 (- (get-internal-run-time) start-time)
195 (sb-thread:condition-broadcast queue)))))
196 (defun fact (n)
197 "A function that does work with the CPU."
198 (if (zerop n) 1 (* n (fact (1- n)))))
200 (with-test (:name :lurking-threads)
201 (let ((work (lambda () (fact 15000))))
202 (let ((zero (scaling-test work 0))
203 (four (scaling-test work 4)))
204 ;; a slightly weak assertion, but good enough for starters.
205 (assert (< four (* 1.5 zero))))))
207 ;;; For one of the interupt-thread tests, we want a foreign function
208 ;;; that does not make syscalls
210 #-win32
211 (progn
212 (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
213 (format o "void loop_forever() { while(1) ; }~%"))
214 (sb-ext:run-program "/bin/sh"
215 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
216 "-o" "threads-foreign.so" "threads-foreign.c"))
217 (sb-alien:load-shared-object (truename "threads-foreign.so"))
218 (sb-alien:define-alien-routine loop-forever sb-alien:void)
219 (delete-file "threads-foreign.c"))
221 ;;; elementary "can we get a lock and release it again"
222 (with-test (:name (:mutex :basics))
223 (let ((l (make-mutex :name "foo"))
224 (p *current-thread*))
225 (assert (eql (mutex-value l) nil) nil "1")
226 (sb-thread:grab-mutex l)
227 (assert (eql (mutex-value l) p) nil "3")
228 (sb-thread:release-mutex l)
229 (assert (eql (mutex-value l) nil) nil "5")))
231 (with-test (:name (:with-recursive-lock :basics))
232 (labels ((ours-p (value)
233 (eq *current-thread* value)))
234 (let ((l (make-mutex :name "rec")))
235 (assert (eql (mutex-value l) nil) nil "1")
236 (sb-thread:with-recursive-lock (l)
237 (assert (ours-p (mutex-value l)) nil "3")
238 (sb-thread:with-recursive-lock (l)
239 (assert (ours-p (mutex-value l)) nil "4"))
240 (assert (ours-p (mutex-value l)) nil "5"))
241 (assert (eql (mutex-value l) nil) nil "6"))))
243 (with-test (:name (:with-recursive-lock :wait-p))
244 (let ((m (make-mutex)))
245 (with-mutex (m)
246 (assert (null (join-thread (make-thread
247 (lambda ()
248 (with-recursive-lock (m :wait-p nil)
249 t)))))))
250 (assert (join-thread (make-thread
251 (lambda ()
252 (with-recursive-lock (m :wait-p nil)
253 t)))))))
255 (with-test (:name (:with-recursive-lock :wait-p :recursive))
256 (let ((m (make-mutex)))
257 (assert (join-thread (make-thread
258 (lambda ()
259 (with-recursive-lock (m :wait-p nil)
260 (with-recursive-lock (m :wait-p nil)
261 t))))))))
263 (with-test (:name (:with-recursive-lock :timeout))
264 (let ((m (make-mutex)))
265 (with-mutex (m)
266 (assert (null (join-thread (make-thread
267 (lambda ()
268 (with-recursive-lock (m :timeout 0.1)
269 t)))))))
270 (assert (join-thread (make-thread
271 (lambda ()
272 (with-recursive-lock (m :timeout 0.1)
273 t)))))))
275 (with-test (:name (:with-recursive-lock :timeout :recursive))
276 (let ((m (make-mutex)))
277 (assert (join-thread (make-thread
278 (lambda ()
279 (with-recursive-lock (m :timeout 0.1)
280 (with-recursive-lock (m :timeout 0.1)
281 t))))))))
283 (with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
284 (let ((l (make-mutex :name "a mutex")))
285 (with-mutex (l)
286 (with-recursive-lock (l)))))
288 ;; test that SLEEP actually sleeps for at least the given time, even
289 ;; if interrupted by another thread exiting/a gc/anything
290 (with-test (:name (:sleep :continue-sleeping-after-interrupt))
291 (let ((start-time (get-universal-time)))
292 (make-join-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
293 (sleep 5)
294 (assert (>= (get-universal-time) (+ 5 start-time)))))
297 (with-test (:name (:condition-wait :basics-1))
298 (let ((queue (make-waitqueue :name "queue"))
299 (lock (make-mutex :name "lock"))
300 (n 0))
301 (labels ((in-new-thread ()
302 (with-mutex (lock)
303 (assert (eql (mutex-value lock) *current-thread*))
304 (format t "~A got mutex~%" *current-thread*)
305 ;; now drop it and sleep
306 (condition-wait queue lock)
307 ;; after waking we should have the lock again
308 (assert (eql (mutex-value lock) *current-thread*))
309 (assert (eql n 1))
310 (decf n))))
311 (make-join-thread #'in-new-thread)
312 (sleep 2) ; give it a chance to start
313 ;; check the lock is free while it's asleep
314 (format t "parent thread ~A~%" *current-thread*)
315 (assert (eql (mutex-value lock) nil))
316 (with-mutex (lock)
317 (incf n)
318 (condition-notify queue))
319 (sleep 1))))
321 (with-test (:name (:condition-wait :basics-2))
322 (let ((queue (make-waitqueue :name "queue"))
323 (lock (make-mutex :name "lock")))
324 (labels ((ours-p (value)
325 (eq *current-thread* value))
326 (in-new-thread ()
327 (with-recursive-lock (lock)
328 (assert (ours-p (mutex-value lock)))
329 (format t "~A got mutex~%" (mutex-value lock))
330 ;; now drop it and sleep
331 (condition-wait queue lock)
332 ;; after waking we should have the lock again
333 (format t "woken, ~A got mutex~%" (mutex-value lock))
334 (assert (ours-p (mutex-value lock))))))
335 (make-join-thread #'in-new-thread)
336 (sleep 2) ; give it a chance to start
337 ;; check the lock is free while it's asleep
338 (format t "parent thread ~A~%" *current-thread*)
339 (assert (eql (mutex-value lock) nil))
340 (with-recursive-lock (lock)
341 (condition-notify queue))
342 (sleep 1))))
344 (with-test (:name (:mutex :contention))
345 (let ((mutex (make-mutex :name "contended")))
346 (labels ((run ()
347 (let ((me *current-thread*))
348 (dotimes (i 100)
349 (with-mutex (mutex)
350 (sleep .03)
351 (assert (eql (mutex-value mutex) me)))
352 (assert (not (eql (mutex-value mutex) me))))
353 (format t "done ~A~%" *current-thread*))))
354 (let ((kid1 (make-thread #'run))
355 (kid2 (make-thread #'run)))
356 (format t "contention ~A ~A~%" kid1 kid2)
357 (wait-for-threads (list kid1 kid2))))))
359 ;;; GRAB-MUTEX
361 (with-test (:name (:grab-mutex :waitp nil))
362 (let ((m (make-mutex)))
363 (with-mutex (m)
364 (assert (null (join-thread (make-thread
365 #'(lambda ()
366 (grab-mutex m :waitp nil)))))))))
368 (with-test (:name (:grab-mutex :timeout :acquisition-fail))
369 (let ((m (make-mutex))
370 (w (make-semaphore)))
371 (with-mutex (m)
372 (let ((th (make-thread
373 #'(lambda ()
374 (prog1
375 (grab-mutex m :timeout 0.1)
376 (signal-semaphore w))))))
377 ;; Wait for it to -- otherwise the detect the deadlock chain
378 ;; from JOIN-THREAD.
379 (wait-on-semaphore w)
380 (assert (null (join-thread th)))))))
382 (with-test (:name (:grab-mutex :timeout :acquisition-success))
383 (let ((m (make-mutex))
384 (child))
385 (with-mutex (m)
386 (setq child (make-thread #'(lambda () (grab-mutex m :timeout 1.0))))
387 (sleep 0.2))
388 (assert (eq (join-thread child) 't))))
390 (with-test (:name (:grab-mutex :timeout+deadline))
391 (let ((m (make-mutex))
392 (w (make-semaphore)))
393 (with-mutex (m)
394 (let ((th (make-thread #'(lambda ()
395 (sb-sys:with-deadline (:seconds 0.0)
396 (handler-case
397 (grab-mutex m :timeout 0.0)
398 (sb-sys:deadline-timeout ()
399 (signal-semaphore w)
400 :deadline)))))))
401 (wait-on-semaphore w)
402 (assert (eq (join-thread th) :deadline))))))
404 (with-test (:name (:grab-mutex :waitp+deadline))
405 (let ((m (make-mutex)))
406 (with-mutex (m)
407 (assert (eq (join-thread
408 (make-thread #'(lambda ()
409 (sb-sys:with-deadline (:seconds 0.0)
410 (handler-case
411 (grab-mutex m :waitp nil)
412 (sb-sys:deadline-timeout ()
413 :deadline))))))
414 'nil)))))
416 ;;; semaphores
418 (defmacro raises-timeout-p (&body body)
419 `(handler-case (progn (progn ,@body) nil)
420 (sb-ext:timeout () t)))
422 (with-test (:name (:semaphore :wait-forever))
423 (let ((sem (make-semaphore :count 0)))
424 (assert (raises-timeout-p
425 (sb-ext:with-timeout 0.1
426 (wait-on-semaphore sem))))))
428 (with-test (:name (:semaphore :initial-count))
429 (let ((sem (make-semaphore :count 1)))
430 (sb-ext:with-timeout 0.1
431 (wait-on-semaphore sem))))
433 (with-test (:name (:semaphore :wait-then-signal))
434 (let ((sem (make-semaphore))
435 (signalled-p nil))
436 (make-join-thread (lambda ()
437 (sleep 0.1)
438 (setq signalled-p t)
439 (signal-semaphore sem)))
440 (wait-on-semaphore sem)
441 (assert signalled-p)))
443 (with-test (:name (:semaphore :signal-then-wait))
444 (let ((sem (make-semaphore))
445 (signalled-p nil))
446 (make-join-thread (lambda ()
447 (signal-semaphore sem)
448 (setq signalled-p t)))
449 (loop until signalled-p)
450 (wait-on-semaphore sem)
451 (assert signalled-p)))
453 (defun test-semaphore-multiple-signals (wait-on-semaphore)
454 (let* ((sem (make-semaphore :count 5))
455 (threads (loop repeat 20 collecting
456 (make-join-thread (lambda ()
457 (funcall wait-on-semaphore sem))))))
458 (flet ((count-live-threads ()
459 (count-if #'thread-alive-p threads)))
460 (sleep 0.5)
461 (assert (= 15 (count-live-threads)))
462 (signal-semaphore sem 10)
463 (sleep 0.5)
464 (assert (= 5 (count-live-threads)))
465 (signal-semaphore sem 3)
466 (sleep 0.5)
467 (assert (= 2 (count-live-threads)))
468 (signal-semaphore sem 4)
469 (sleep 0.5)
470 (assert (= 0 (count-live-threads))))))
472 (with-test (:name (:semaphore :multiple-signals))
473 (test-semaphore-multiple-signals #'wait-on-semaphore))
475 (with-test (:name (:try-semaphore :trivial-fail))
476 (assert (eq (try-semaphore (make-semaphore :count 0)) 'nil)))
478 (with-test (:name (:try-semaphore :trivial-success))
479 (let ((sem (make-semaphore :count 1)))
480 (assert (try-semaphore sem))
481 (assert (zerop (semaphore-count sem)))))
483 (with-test (:name (:try-semaphore :trivial-fail :n>1))
484 (assert (eq (try-semaphore (make-semaphore :count 1) 2) 'nil)))
486 (with-test (:name (:try-semaphore :trivial-success :n>1))
487 (let ((sem (make-semaphore :count 10)))
488 (assert (try-semaphore sem 5))
489 (assert (try-semaphore sem 5))
490 (assert (zerop (semaphore-count sem)))))
492 (with-test (:name (:try-semaphore :emulate-wait-on-semaphore))
493 (flet ((busy-wait-on-semaphore (sem)
494 (loop until (try-semaphore sem) do (sleep 0.001))))
495 (test-semaphore-multiple-signals #'busy-wait-on-semaphore)))
497 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
498 ;;; semaphore in a bad state.
499 (with-test (:name (:try-semaphore :interrupt-safe))
500 (flet ((make-threads (count fn)
501 (loop repeat count collect (make-thread fn)))
502 (kill-thread (thread)
503 (when (thread-alive-p thread)
504 (ignore-errors (terminate-thread thread))))
505 (count-live-threads (threads)
506 (count-if #'thread-alive-p threads)))
507 ;; WAITERS will already be waiting on the semaphore while
508 ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
509 ;; semaphore, and MORE-WAITERS are new threads trying to wait on
510 ;; the semaphore during the interruption-fire.
511 (let* ((sem (make-semaphore :count 100))
512 (waiters (make-threads 20 #'(lambda ()
513 (wait-on-semaphore sem))))
514 (triers (make-threads 40 #'(lambda ()
515 (sleep (random 0.01))
516 (try-semaphore sem (1+ (random 5))))))
517 (more-waiters
518 (loop repeat 10
519 do (kill-thread (nth (random 40) triers))
520 collect (make-thread #'(lambda () (wait-on-semaphore sem)))
521 do (kill-thread (nth (random 40) triers)))))
522 (sleep 0.5)
523 ;; Now ensure that the waiting threads will all be waked up,
524 ;; i.e. that the semaphore is still working.
525 (loop repeat (+ (count-live-threads waiters)
526 (count-live-threads more-waiters))
527 do (signal-semaphore sem))
528 (sleep 0.5)
529 (assert (zerop (count-live-threads triers)))
530 (assert (zerop (count-live-threads waiters)))
531 (assert (zerop (count-live-threads more-waiters))))))
535 (format t "~&semaphore tests done~%")
537 (defun test-interrupt (function-to-interrupt &optional quit-p)
538 (let ((child (make-kill-thread function-to-interrupt)))
539 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
540 (sleep 2)
541 (format t "interrupting child ~A~%" child)
542 (interrupt-thread child
543 (lambda ()
544 (format t "child pid ~A~%" *current-thread*)
545 (when quit-p (abort-thread))))
546 (sleep 1)
547 child))
549 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
550 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
551 ;; in pseudo-atomic
553 (with-test (:name (:interrupt-thread :more-basics))
554 (let ((child (test-interrupt (lambda () (loop)))))
555 (terminate-thread child)))
557 (with-test (:name (:interrupt-thread :interrupt-foreign-loop)
558 ;; This feature is explicitly unsupported on Win32.
559 :skipped-on :win32)
560 (test-interrupt #'loop-forever :quit))
562 (with-test (:name (:interrupt-thread :interrupt-sleep))
563 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
564 (terminate-thread child)
565 (wait-for-threads (list child))))
567 (with-test (:name (:interrupt-thread :interrupt-mutex-acquisition))
568 (let ((lock (make-mutex :name "loctite"))
569 child)
570 (with-mutex (lock)
571 (setf child (test-interrupt
572 (lambda ()
573 (with-mutex (lock)
574 (assert (eql (mutex-value lock) *current-thread*)))
575 (assert (not (eql (mutex-value lock) *current-thread*)))
576 (sleep 10))))
577 ;;hold onto lock for long enough that child can't get it immediately
578 (sleep 5)
579 (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
580 (format t "parent releasing lock~%"))
581 (terminate-thread child)
582 (wait-for-threads (list child))))
584 (format t "~&locking test done~%")
586 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
588 (with-test (:name (:interrupt-thread :interrupt-consing-child))
589 (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
590 (let ((killers
591 (loop repeat 4 collect
592 (sb-thread:make-thread
593 (lambda ()
594 (loop repeat 25 do
595 (sleep (random 0.1d0))
596 (princ ".")
597 (force-output)
598 (sb-thread:interrupt-thread thread (lambda ()))))))))
599 (wait-for-threads killers)
600 (sb-thread:terminate-thread thread)
601 (wait-for-threads (list thread))))
602 (sb-ext:gc :full t))
604 (format t "~&multi interrupt test done~%")
606 #+(or x86 x86-64) ;; x86oid-only, see internal commentary.
607 (with-test (:name (:interrupt-thread :interrupt-consing-child :again))
608 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
609 ;; NB this only works on x86: other ports don't have a symbol for
610 ;; pseudo-atomic atomicity
611 (dotimes (i 100)
612 (sleep (random 0.1d0))
613 (interrupt-thread c
614 (lambda ()
615 (princ ".") (force-output)
616 (assert (thread-alive-p *current-thread*))
617 (assert
618 (not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
619 (terminate-thread c)
620 (wait-for-threads (list c))))
622 (format t "~&interrupt test done~%")
624 (defstruct counter (n 0 :type sb-vm:word))
625 (defvar *interrupt-counter* (make-counter))
627 (declaim (notinline check-interrupt-count))
628 (defun check-interrupt-count (i)
629 (declare (optimize (debug 1) (speed 1)))
630 ;; This used to lose if eflags were not restored after an interrupt.
631 (unless (typep i 'fixnum)
632 (error "!!!!!!!!!!!")))
634 (with-test (:name (:interrupt-thread :interrupt-ATOMIC-INCF))
635 (let ((c (make-thread
636 (lambda ()
637 (handler-bind ((error #'(lambda (cond)
638 (princ cond)
639 (sb-debug:backtrace
640 most-positive-fixnum))))
641 (loop (check-interrupt-count
642 (counter-n *interrupt-counter*))))))))
643 (let ((func (lambda ()
644 (princ ".")
645 (force-output)
646 (sb-ext:atomic-incf (counter-n *interrupt-counter*)))))
647 (setf (counter-n *interrupt-counter*) 0)
648 (dotimes (i 100)
649 (sleep (random 0.1d0))
650 (interrupt-thread c func))
651 (loop until (= (counter-n *interrupt-counter*) 100) do (sleep 0.1))
652 (terminate-thread c)
653 (wait-for-threads (list c)))))
655 (format t "~&interrupt count test done~%")
657 (defvar *runningp* nil)
659 (with-test (:name (:interrupt-thread :no-nesting))
660 (let ((thread (sb-thread:make-thread
661 (lambda ()
662 (catch 'xxx
663 (loop))))))
664 (declare (special runningp))
665 (sleep 0.2)
666 (sb-thread:interrupt-thread thread
667 (lambda ()
668 (let ((*runningp* t))
669 (sleep 1))))
670 (sleep 0.2)
671 (sb-thread:interrupt-thread thread
672 (lambda ()
673 (throw 'xxx *runningp*)))
674 (assert (not (sb-thread:join-thread thread)))))
676 (with-test (:name (:interrupt-thread :nesting))
677 (let ((thread (sb-thread:make-thread
678 (lambda ()
679 (catch 'xxx
680 (loop))))))
681 (declare (special runningp))
682 (sleep 0.2)
683 (sb-thread:interrupt-thread thread
684 (lambda ()
685 (let ((*runningp* t))
686 (sb-sys:with-interrupts
687 (sleep 1)))))
688 (sleep 0.2)
689 (sb-thread:interrupt-thread thread
690 (lambda ()
691 (throw 'xxx *runningp*)))
692 (assert (sb-thread:join-thread thread))))
694 (with-test (:name (:two-threads-running-gc))
695 (let (a-done b-done)
696 (make-join-thread (lambda ()
697 (dotimes (i 100)
698 (sb-ext:gc) (princ "\\") (force-output))
699 (setf a-done t)))
700 (make-join-thread (lambda ()
701 (dotimes (i 25)
702 (sb-ext:gc :full t)
703 (princ "/") (force-output))
704 (setf b-done t)))
705 (loop
706 (when (and a-done b-done) (return))
707 (sleep 1))))
709 (terpri)
711 (defun waste (&optional (n 100000))
712 (loop repeat n do (make-string 16384)))
714 (with-test (:name (:one-thread-runs-gc-while-other-conses))
715 (loop for i below 100 do
716 (princ "!")
717 (force-output)
718 (make-join-thread
719 #'(lambda ()
720 (waste)))
721 (waste)
722 (sb-ext:gc)))
724 (terpri)
726 (defparameter *aaa* nil)
727 (with-test (:name (:one-thread-runs-gc-while-other-conses :again))
728 (loop for i below 100 do
729 (princ "!")
730 (force-output)
731 (make-join-thread
732 #'(lambda ()
733 (let ((*aaa* (waste)))
734 (waste))))
735 (let ((*aaa* (waste)))
736 (waste))
737 (sb-ext:gc)))
739 (format t "~&gc test done~%")
741 ;; this used to deadlock on session-lock
742 (with-test (:name (:no-session-deadlock))
743 (make-join-thread (lambda () (sb-ext:gc))))
745 (defun exercise-syscall (fn reference-errno)
746 (make-kill-thread
747 (lambda ()
748 (loop do
749 (funcall fn)
750 (let ((errno (sb-unix::get-errno)))
751 (sleep (random 0.1d0))
752 (unless (eql errno reference-errno)
753 (format t "Got errno: ~A (~A) instead of ~A~%"
754 errno
755 (sb-unix::strerror)
756 reference-errno)
757 (force-output)
758 (abort-thread)))))))
760 ;; (nanosleep -1 0) does not fail on FreeBSD
761 (with-test (:name (:exercising-concurrent-syscalls) :fails-on :win32)
762 (let* (#-freebsd
763 (nanosleep-errno (progn
764 (sb-unix:nanosleep -1 0)
765 (sb-unix::get-errno)))
766 (open-errno (progn
767 (open "no-such-file"
768 :if-does-not-exist nil)
769 (sb-unix::get-errno)))
770 (threads
771 (list
772 #-freebsd
773 (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
774 (exercise-syscall (lambda () (open "no-such-file"
775 :if-does-not-exist nil))
776 open-errno)
777 (make-join-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
778 (sleep 10)
779 (princ "terminating threads")
780 (dolist (thread threads)
781 (sb-thread:terminate-thread thread))))
783 (format t "~&errno test done~%")
785 (with-test (:name :all-threads-have-abort-restart)
786 (loop repeat 100 do
787 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
788 (sb-thread:interrupt-thread
789 thread
790 (lambda ()
791 (assert (find-restart 'abort)))))))
793 (sb-ext:gc :full t)
795 (format t "~&thread startup sigmask test done~%")
797 (with-test (:name (:debugger-no-hang-on-session-lock-if-interrupted)
798 :fails-on :win32)
799 #+win32 (error "user would have to touch a key interactively to proceed")
800 (sb-debug::enable-debugger)
801 (let* ((main-thread *current-thread*)
802 (interruptor-thread
803 (make-thread (lambda ()
804 (sleep 2)
805 (interrupt-thread main-thread
806 (lambda ()
807 (with-interrupts
808 (break))))
809 (sleep 2)
810 (interrupt-thread main-thread #'continue))
811 :name "interruptor")))
812 (with-session-lock (*session*)
813 (sleep 3))
814 (loop while (thread-alive-p interruptor-thread))))
816 (format t "~&session lock test done~%")
818 ;; expose thread creation races by exiting quickly
819 (with-test (:name (:no-thread-creation-race :light))
820 (make-join-thread (lambda ())))
822 (with-test (:name (:no-thread-creation-race :heavy))
823 (loop repeat 20 do
824 (wait-for-threads
825 (loop for i below 100 collect
826 (sb-thread:make-thread (lambda ()))))))
828 (format t "~&creation test done~%")
830 ;; interrupt handlers are per-thread with pthreads, make sure the
831 ;; handler installed in one thread is global
832 (with-test (:name (:global-interrupt-handler))
833 (make-join-thread
834 (lambda ()
835 (sb-ext:run-program "sleep" '("1") :search t :wait nil))))
837 ;;;; Binding stack safety
839 (defparameter *x* nil)
840 (defparameter *n-gcs-requested* 0)
841 (defparameter *n-gcs-done* 0)
843 (let ((counter 0))
844 (defun make-something-big ()
845 (let ((x (make-string 32000)))
846 (incf counter)
847 (let ((counter counter))
848 (sb-ext:finalize x (lambda () (format t " ~S" counter)
849 (force-output)))))))
851 (defmacro wait-for-gc ()
852 `(progn
853 (incf *n-gcs-requested*)
854 (loop while (< *n-gcs-done* *n-gcs-requested*))))
856 (defun send-gc ()
857 (loop until (< *n-gcs-done* *n-gcs-requested*))
858 (format t "G")
859 (force-output)
860 (sb-ext:gc)
861 (incf *n-gcs-done*))
863 #+(or x86 x86-64) ;the only platforms with a *binding-stack-pointer* variable
864 (defun exercise-binding ()
865 (loop
866 (let ((*x* (make-something-big)))
867 (let ((*x* 42))
868 ;; at this point the binding stack looks like this:
869 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
871 (wait-for-gc)
872 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
873 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
874 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
875 ;; unbinding but values are not).
876 (let ((*x* nil)
877 (binding-pointer-delta (ash 2 (- sb-vm:word-shift sb-vm:n-fixnum-tag-bits))))
878 ;; bump bsp as if a BIND had just started
879 (incf sb-vm::*binding-stack-pointer* binding-pointer-delta)
880 (wait-for-gc)
881 (decf sb-vm::*binding-stack-pointer* binding-pointer-delta))))
883 #+(or x86 x86-64) ;the only platforms with a *binding-stack-pointer* variable
884 (with-test (:name (:binding-stack-gc-safety))
885 (let (threads)
886 (unwind-protect
887 (progn
888 (push (make-kill-thread #'exercise-binding) threads)
889 (push (make-kill-thread (lambda ()
890 (loop
891 (sleep 0.1)
892 (send-gc))))
893 threads)
894 (sleep 4))
895 (mapc #'sb-thread:terminate-thread threads))))
897 (with-test (:name :test-%thread-local-references)
898 (let ((mysym (gensym))
899 (fool1 (cons 1 2))
900 (fool2 (cons 2 3)))
901 (progv (list mysym) '(nil)
902 (let* ((i (get-lisp-obj-address (sb-vm:symbol-tls-index mysym)))
903 (j (+ i sb-vm:n-word-bytes)))
904 (assert (eql (sap-ref-word (current-thread-sap) j)
905 sb-vm:no-tls-value-marker-widetag))
906 (setf (sap-ref-lispobj (current-thread-sap) i) fool1
907 (sap-ref-lispobj (current-thread-sap) j) fool2)
908 ;; assert that my pointer arithmetic worked as expected
909 (assert (eq (symbol-value mysym) fool1))
910 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
911 (let ((list (%thread-local-references)))
912 (assert (memq fool1 list))
913 (assert (not (memq fool2 list))))
914 ;; repair the TLS entry that was corrupted by the test
915 (setf (sap-ref-word (current-thread-sap) j)
916 sb-vm:no-tls-value-marker-widetag)))))
918 (format t "~&binding test done~%")
920 ;;; HASH TABLES
922 (defvar *errors* nil)
924 (defun oops (e)
925 (setf *errors* e)
926 (format t "~&oops: ~A in ~S~%" e *current-thread*)
927 (sb-debug:backtrace)
928 (catch 'done))
930 (with-test (:name (:unsynchronized-hash-table)
931 ;; FIXME: This test occasionally eats out craploads
932 ;; of heap instead of expected error early. Not 100%
933 ;; sure if it would finish as expected, but since it
934 ;; hits swap on my system I'm not likely to find out
935 ;; soon. Disabling for now. -- nikodemus
936 :skipped-on :sbcl)
937 ;; We expect a (probable) error here: parellel readers and writers
938 ;; on a hash-table are not expected to work -- but we also don't
939 ;; expect this to corrupt the image.
940 (let* ((hash (make-hash-table))
941 (*errors* nil)
942 (threads (list (make-kill-thread
943 (lambda ()
944 (catch 'done
945 (handler-bind ((serious-condition 'oops))
946 (loop
947 ;;(princ "1") (force-output)
948 (setf (gethash (random 100) hash) 'h)))))
949 :name "writer")
950 (make-kill-thread
951 (lambda ()
952 (catch 'done
953 (handler-bind ((serious-condition 'oops))
954 (loop
955 ;;(princ "2") (force-output)
956 (remhash (random 100) hash)))))
957 :name "reader")
958 (make-kill-thread
959 (lambda ()
960 (catch 'done
961 (handler-bind ((serious-condition 'oops))
962 (loop
963 (sleep (random 1.0))
964 (sb-ext:gc :full t)))))
965 :name "collector"))))
966 (unwind-protect
967 (sleep 10)
968 (mapc #'sb-thread:terminate-thread threads))))
970 (format t "~&unsynchronized hash table test done~%")
972 (with-test (:name (:synchronized-hash-table))
973 (let* ((hash (make-hash-table :synchronized t))
974 (*errors* nil)
975 (threads (list (make-join-thread
976 (lambda ()
977 (catch 'done
978 (handler-bind ((serious-condition 'oops))
979 (loop
980 ;;(princ "1") (force-output)
981 (setf (gethash (random 100) hash) 'h)))))
982 :name "writer")
983 (make-join-thread
984 (lambda ()
985 (catch 'done
986 (handler-bind ((serious-condition 'oops))
987 (loop
988 ;;(princ "2") (force-output)
989 (remhash (random 100) hash)))))
990 :name "reader")
991 (make-join-thread
992 (lambda ()
993 (catch 'done
994 (handler-bind ((serious-condition 'oops))
995 (loop
996 (sleep (random 1.0))
997 (sb-ext:gc :full t)))))
998 :name "collector"))))
999 (unwind-protect
1000 (sleep 10)
1001 (mapc #'sb-thread:terminate-thread threads))
1002 (assert (not *errors*))))
1004 (format t "~&synchronized hash table test done~%")
1006 (with-test (:name (:hash-table-parallel-readers))
1007 (let ((hash (make-hash-table))
1008 (*errors* nil))
1009 (loop repeat 50
1010 do (setf (gethash (random 100) hash) 'xxx))
1011 (let ((threads (list (make-kill-thread
1012 (lambda ()
1013 (catch 'done
1014 (handler-bind ((serious-condition 'oops))
1015 (loop
1016 until (eq t (gethash (random 100) hash))))))
1017 :name "reader 1")
1018 (make-kill-thread
1019 (lambda ()
1020 (catch 'done
1021 (handler-bind ((serious-condition 'oops))
1022 (loop
1023 until (eq t (gethash (random 100) hash))))))
1024 :name "reader 2")
1025 (make-kill-thread
1026 (lambda ()
1027 (catch 'done
1028 (handler-bind ((serious-condition 'oops))
1029 (loop
1030 until (eq t (gethash (random 100) hash))))))
1031 :name "reader 3")
1032 (make-kill-thread
1033 (lambda ()
1034 (catch 'done
1035 (handler-bind ((serious-condition 'oops))
1036 (loop
1037 (sleep (random 1.0))
1038 (sb-ext:gc :full t)))))
1039 :name "collector"))))
1040 (unwind-protect
1041 (sleep 10)
1042 (mapc #'sb-thread:terminate-thread threads))
1043 (assert (not *errors*)))))
1045 (format t "~&multiple reader hash table test done~%")
1047 (with-test (:name (:hash-table-single-accessor-parallel-gc))
1048 (let ((hash (make-hash-table))
1049 (*errors* nil))
1050 (let ((threads (list (make-kill-thread
1051 (lambda ()
1052 (handler-bind ((serious-condition 'oops))
1053 (loop
1054 (let ((n (random 100)))
1055 (if (gethash n hash)
1056 (remhash n hash)
1057 (setf (gethash n hash) 'h))))))
1058 :name "accessor")
1059 (make-kill-thread
1060 (lambda ()
1061 (handler-bind ((serious-condition 'oops))
1062 (loop
1063 (sleep (random 1.0))
1064 (sb-ext:gc :full t))))
1065 :name "collector"))))
1066 (unwind-protect
1067 (sleep 10)
1068 (mapc #'sb-thread:terminate-thread threads))
1069 (assert (not *errors*)))))
1071 (format t "~&single accessor hash table test~%")
1073 #| ;; a cll post from eric marsden
1074 | (defun crash ()
1075 | (setq *debugger-hook*
1076 | (lambda (condition old-debugger-hook)
1077 | (debug:backtrace 10)
1078 | (unix:unix-exit 2)))
1079 | #+live-dangerously
1080 | (mp::start-sigalrm-yield)
1081 | (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
1082 | (mp:make-process #'roomy)
1083 | (mp:make-process #'roomy)))
1086 (with-test (:name (:condition-variable :notify-multiple))
1087 (flet ((tester (notify-fun)
1088 (let ((queue (make-waitqueue :name "queue"))
1089 (lock (make-mutex :name "lock"))
1090 (data nil))
1091 (labels ((test (x)
1092 (loop
1093 (with-mutex (lock)
1094 (format t "condition-wait ~a~%" x)
1095 (force-output)
1096 (condition-wait queue lock)
1097 (format t "woke up ~a~%" x)
1098 (force-output)
1099 (push x data)))))
1100 (let ((threads (loop for x from 1 to 10
1101 collect
1102 (let ((x x))
1103 (make-kill-thread (lambda ()
1104 (test x)))))))
1105 (sleep 5)
1106 (with-mutex (lock)
1107 (funcall notify-fun queue))
1108 (sleep 5)
1109 (mapcar #'terminate-thread threads)
1110 ;; Check that all threads woke up at least once
1111 (assert (= (length (remove-duplicates data)) 10)))))))
1112 (tester (lambda (queue)
1113 (format t "~&(condition-notify queue 10)~%")
1114 (force-output)
1115 (condition-notify queue 10)))
1116 (tester (lambda (queue)
1117 (format t "~&(condition-broadcast queue)~%")
1118 (force-output)
1119 (condition-broadcast queue)))))
1121 (format t "waitqueue wakeup tests done~%")
1123 ;;; Make sure that a deadline handler is not invoked twice in a row in
1124 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1126 (with-test (:name (:condition-wait :deadlines :LP-512914))
1127 (let ((n 2) ; was empirically enough to trigger the bug
1128 (mutex (sb-thread:make-mutex))
1129 (waitq (sb-thread:make-waitqueue))
1130 (threads nil)
1131 (deadline-handler-run-twice? nil))
1132 (dotimes (i n)
1133 (let ((child
1134 (sb-thread:make-thread
1135 #'(lambda ()
1136 (handler-bind
1137 ((sb-sys:deadline-timeout
1138 (let ((already? nil))
1139 #'(lambda (c)
1140 (when already?
1141 (setq deadline-handler-run-twice? t))
1142 (setq already? t)
1143 (sleep 0.2)
1144 (sb-thread:condition-broadcast waitq)
1145 (sb-sys:defer-deadline 10.0 c)))))
1146 (sb-sys:with-deadline (:seconds 0.1)
1147 (sb-thread:with-mutex (mutex)
1148 (sb-thread:condition-wait waitq mutex))))))))
1149 (push child threads)))
1150 (mapc #'sb-thread:join-thread threads)
1151 (assert (not deadline-handler-run-twice?))))
1153 (with-test (:name (:condition-wait :signal-deadline-with-interrupts-enabled))
1154 (let ((mutex (sb-thread:make-mutex))
1155 (waitq (sb-thread:make-waitqueue))
1156 (A-holds? :unknown)
1157 (B-holds? :unknown)
1158 (A-interrupts-enabled? :unknown)
1159 (B-interrupts-enabled? :unknown)
1161 (B))
1162 ;; W.L.O.G., we assume that A is executed first...
1163 (setq A (sb-thread:make-thread
1164 #'(lambda ()
1165 (handler-bind
1166 ((sb-sys:deadline-timeout
1167 #'(lambda (c)
1168 ;; We came here through the call to DECODE-TIMEOUT
1169 ;; in CONDITION-WAIT; hence both here are supposed
1170 ;; to evaluate to T.
1171 (setq A-holds? (sb-thread:holding-mutex-p mutex))
1172 (setq A-interrupts-enabled?
1173 sb-sys:*interrupts-enabled*)
1174 (sleep 0.2)
1175 (sb-thread:condition-broadcast waitq)
1176 (sb-sys:defer-deadline 10.0 c))))
1177 (sb-sys:with-deadline (:seconds 0.1)
1178 (sb-thread:with-mutex (mutex)
1179 (sb-thread:condition-wait waitq mutex)))))
1180 :name "A"))
1181 (setq B (sb-thread:make-thread
1182 #'(lambda ()
1183 (thread-yield)
1184 (handler-bind
1185 ((sb-sys:deadline-timeout
1186 #'(lambda (c)
1187 ;; We came here through the call to DECODE-TIMEOUT
1188 ;; in CONDITION-WAIT (contended case of
1189 ;; reaquiring the mutex) - so the former will
1190 ;; be NIL, but interrupts should still be enabled.
1191 (setq B-holds? (sb-thread:holding-mutex-p mutex))
1192 (setq B-interrupts-enabled?
1193 sb-sys:*interrupts-enabled*)
1194 (sleep 0.2)
1195 (sb-thread:condition-broadcast waitq)
1196 (sb-sys:defer-deadline 10.0 c))))
1197 (sb-sys:with-deadline (:seconds 0.1)
1198 (sb-thread:with-mutex (mutex)
1199 (sb-thread:condition-wait waitq mutex)))))
1200 :name "B"))
1201 (sb-thread:join-thread A)
1202 (sb-thread:join-thread B)
1203 (let ((A-result (list A-holds? A-interrupts-enabled?))
1204 (B-result (list B-holds? B-interrupts-enabled?)))
1205 ;; We also check some subtle behaviour w.r.t. whether a deadline
1206 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1207 ;; probably very internal behaviour (so user should not depend
1208 ;; on it) -- I added the testing here just to manifest current
1209 ;; behaviour.
1210 (cond ((equal A-result '(t t)) (assert (equal B-result '(nil t))))
1211 ((equal B-result '(t t)) (assert (equal A-result '(nil t))))
1213 (error "Failure: fell through wit A: ~S, B: ~S"
1214 A-result
1215 B-result))))))
1217 (with-test (:name (:mutex :finalization))
1218 (let ((a nil))
1219 (dotimes (i 500000)
1220 (setf a (make-mutex)))))
1222 (format t "mutex finalization test done~%")
1224 ;; You have to shoehorn this arbitrary sexpr into a feature expression
1225 ;; to have the test summary show that a test was disabled.
1226 #+gencgc
1227 (unless (eql (extern-alien "verify_gens" int)
1228 (1+ sb-vm:+highest-normal-generation+))
1229 (pushnew :verify-gens *features*))
1231 (with-test (:name :backtrace :skipped-on ':verify-gens)
1232 ;; Printing backtraces from several threads at once used to hang the
1233 ;; whole SBCL process (discovered by accident due to a timer.impure
1234 ;; test misbehaving). The cause was that packages weren't even
1235 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1236 ;; backtraces a loot of symbol lookups need to be done due to
1237 ;; *PRINT-ESCAPE*.
1238 (let* ((threads (loop repeat 10
1239 collect (sb-thread:make-thread
1240 (lambda ()
1241 (dotimes (i 1000)
1242 (with-output-to-string (*debug-io*)
1243 (sb-debug::backtrace 10))))))))
1244 (wait-for-threads threads)))
1246 (format t "backtrace test done~%")
1248 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1250 (with-test (:name :gc-deadlock)
1251 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1252 ;; GC due to *all-threads-lock* and session lock. On earlier
1253 ;; versions and at least on one specific box this test is good enough
1254 ;; to catch that typically well before the 1500th iteration.
1255 (loop
1256 with i = 0
1257 with n = 3000
1258 while (< i n)
1260 (incf i)
1261 (when (zerop (mod i 100))
1262 (write-char #\.)
1263 (force-output))
1264 (handler-case
1265 (if (oddp i)
1266 (make-join-thread
1267 (lambda ()
1268 (sleep (random 0.001)))
1269 :name (format nil "SLEEP-~D" i))
1270 (make-join-thread
1271 (lambda ()
1272 ;; KLUDGE: what we are doing here is explicit,
1273 ;; but the same can happen because of a regular
1274 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1275 ;; session functions.
1276 (sb-thread::with-all-threads-lock
1277 (sb-thread::with-session-lock (sb-thread::*session*)
1278 (sb-ext:gc))))
1279 :name (format nil "GC-~D" i)))
1280 (error (e)
1281 (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
1282 (sleep 0.1)
1283 (incf i)))))
1285 (format t "~&gc deadlock test done~%")
1287 (let ((count (make-array 8 :initial-element 0)))
1288 (defun closure-one ()
1289 (declare (optimize safety))
1290 (values (incf (aref count 0)) (incf (aref count 1))
1291 (incf (aref count 2)) (incf (aref count 3))
1292 (incf (aref count 4)) (incf (aref count 5))
1293 (incf (aref count 6)) (incf (aref count 7))))
1294 (defun no-optimizing-away-closure-one ()
1295 (setf count (make-array 8 :initial-element 0))))
1297 (defstruct box
1298 (count 0))
1300 (let ((one (make-box))
1301 (two (make-box))
1302 (three (make-box)))
1303 (defun closure-two ()
1304 (declare (optimize safety))
1305 (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
1306 (defun no-optimizing-away-closure-two ()
1307 (setf one (make-box)
1308 two (make-box)
1309 three (make-box))))
1311 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1312 ;;; sometimes run out of memory) in the following test. For developers
1313 ;;; interested in debugging this combination of features, it might be
1314 ;;; fruitful to concentrate their efforts around this test...
1316 (with-test (:name (:funcallable-instances)
1317 :skipped-on '(and :sb-safepoint
1318 (not :c-stack-is-control-stack)))
1319 ;; the funcallable-instance implementation used not to be threadsafe
1320 ;; against setting the funcallable-instance function to a closure
1321 ;; (because the code and lexenv were set separately).
1322 (let ((fun (sb-kernel:%make-funcallable-instance 0))
1323 (condition nil))
1324 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1325 (flet ((changer ()
1326 (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1327 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
1328 (test ()
1329 (handler-case (loop (funcall fun))
1330 (serious-condition (c) (setf condition c)))))
1331 (let ((changer (make-thread #'changer))
1332 (test (make-thread #'test)))
1333 (handler-case
1334 (progn
1335 ;; The two closures above are fairly carefully crafted
1336 ;; so that if given the wrong lexenv they will tend to
1337 ;; do some serious damage, but it is of course difficult
1338 ;; to predict where the various bits and pieces will be
1339 ;; allocated. Five seconds failed fairly reliably on
1340 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1341 (sb-ext:with-timeout 5
1342 (wait-for-threads (list test)))
1343 (error "~@<test thread got condition:~2I~_~A~@:>" condition))
1344 (sb-ext:timeout ()
1345 (terminate-thread changer)
1346 (terminate-thread test)
1347 (wait-for-threads (list changer test))))))))
1349 (format t "~&funcallable-instance test done~%")
1351 (defun random-type (n)
1352 `(integer ,(random n) ,(+ n (random n))))
1354 (defun subtypep-hash-cache-test ()
1355 (dotimes (i 10000)
1356 (let ((type1 (random-type 500))
1357 (type2 (random-type 500)))
1358 (let ((a (subtypep type1 type2)))
1359 (dotimes (i 100)
1360 (assert (eq (subtypep type1 type2) a))))))
1361 (write-char #\.)
1362 (force-output))
1364 (with-test (:name (:hash-cache :subtypep))
1365 (mapc #'join-thread
1366 ;; this didn't "reliably fail" with a small number of threads.
1367 ;; 30 is a compromise between running time and confidence in the result.
1368 (loop repeat 30
1369 collect (sb-thread:make-thread #'subtypep-hash-cache-test)))
1370 (terpri))
1372 ;;;; BLACK BOX TESTS
1374 (in-package :cl-user)
1375 (use-package :test-util)
1376 (use-package "ASSERTOID")
1378 (format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1379 (with-test (:name :parallel-defclass)
1380 (defclass test-1 () ((a :initform :orig-a)))
1381 (defclass test-2 () ((b :initform :orig-b)))
1382 (defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
1383 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1384 ;; calls disabled in the folloving code. (As seen on a Server 2012
1385 ;; installation.) Clearly, this sort of workaround in a test is
1386 ;; cheating, and might be hiding the underlying bug that the test is
1387 ;; exposing. Let's review this later.
1388 (let* ((run t)
1389 (d1 (sb-thread:make-thread (lambda ()
1390 (loop while run
1391 do (defclass test-1 () ((a :initform :new-a)))
1392 (write-char #\1)
1393 #-win32 (force-output)))
1394 :name "d1"))
1395 (d2 (sb-thread:make-thread (lambda ()
1396 (loop while run
1397 do (defclass test-2 () ((b :initform :new-b)))
1398 (write-char #\2)
1399 #-win32 (force-output)))
1400 :name "d2"))
1401 (d3 (sb-thread:make-thread (lambda ()
1402 (loop while run
1403 do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
1404 (write-char #\3)
1405 #-win32 (force-output)))
1406 :name "d3"))
1407 (i (sb-thread:make-thread (lambda ()
1408 (loop while run
1409 do (let ((i (make-instance 'test-3)))
1410 (assert (member (slot-value i 'a) '(:orig-a :new-a)))
1411 (assert (member (slot-value i 'b) '(:orig-b :new-b)))
1412 (assert (member (slot-value i 'c) '(:orig-c :new-c))))
1413 (write-char #\i)
1414 #-win32 (force-output)))
1415 :name "i")))
1416 (format t "~%sleeping!~%")
1417 (sleep 2.0)
1418 (format t "~%stopping!~%")
1419 (setf run nil)
1420 (mapc (lambda (th)
1421 (sb-thread:join-thread th)
1422 (format t "~%joined ~S~%" (sb-thread:thread-name th)))
1423 (list d1 d2 d3 i))
1424 (force-output)))
1425 (format t "parallel defclass test done~%")
1427 (with-test (:name (:deadlock-detection :interrupts) :fails-on :win32)
1428 #+win32 ;be more explicit than just :skipped-on
1429 (error "not attempting, because of deadlock error in background thread")
1430 (let* ((m1 (sb-thread:make-mutex :name "M1"))
1431 (m2 (sb-thread:make-mutex :name "M2"))
1432 (t1-can-go (sb-thread:make-semaphore :name "T1 can go"))
1433 (t2-can-go (sb-thread:make-semaphore :name "T2 can go"))
1434 (t1 (sb-thread:make-thread
1435 (lambda ()
1436 (sb-thread:with-mutex (m1)
1437 (sb-thread:wait-on-semaphore t1-can-go)
1438 :ok1))
1439 :name "T1"))
1440 (t2 (sb-thread:make-thread
1441 (lambda ()
1442 (sb-ext:wait-for (eq t1 (sb-thread:mutex-owner m1)))
1443 (sb-thread:with-mutex (m1 :wait-p t)
1444 (sb-thread:wait-on-semaphore t2-can-go)
1445 :ok2))
1446 :name "T2")))
1447 (sb-ext:wait-for (eq m1 (sb-thread::thread-waiting-for t2)))
1448 (sb-thread:interrupt-thread t2 (lambda ()
1449 (sb-thread:with-mutex (m2 :wait-p t)
1450 (sb-ext:wait-for
1451 (eq m2 (sb-thread::thread-waiting-for t1)))
1452 (sb-thread:signal-semaphore t2-can-go))))
1453 (sb-ext:wait-for (eq t2 (sb-thread:mutex-owner m2)))
1454 (sb-thread:interrupt-thread t1 (lambda ()
1455 (sb-thread:with-mutex (m2 :wait-p t)
1456 (sb-thread:signal-semaphore t1-can-go))))
1457 ;; both threads should finish without a deadlock or deadlock
1458 ;; detection error
1459 (let ((res (list (sb-thread:join-thread t1)
1460 (sb-thread:join-thread t2))))
1461 (assert (equal '(:ok1 :ok2) res)))))
1463 (with-test (:name (:deadlock-detection :gc))
1464 ;; To semi-reliably trigger the error (in SBCL's where)
1465 ;; it was present you had to run this for > 30 seconds,
1466 ;; but that's a bit long for a single test.
1467 (let* ((stop (+ 5 (get-universal-time)))
1468 (m1 (sb-thread:make-mutex :name "m1"))
1469 (t1 (sb-thread:make-thread
1470 (lambda ()
1471 (loop until (> (get-universal-time) stop)
1472 do (sb-thread:with-mutex (m1)
1473 (eval `(make-array 24))))
1474 :ok)))
1475 (t2 (sb-thread:make-thread
1476 (lambda ()
1477 (loop until (> (get-universal-time) stop)
1478 do (sb-thread:with-mutex (m1)
1479 (eval `(make-array 24))))
1480 :ok))))
1481 (let ((res (list (sb-thread:join-thread t1)
1482 (sb-thread:join-thread t2))))
1483 (assert (equal '(:ok :ok) res)))))
1485 (with-test (:name :spinlock-api)
1486 (let* ((warned 0)
1487 (funs
1488 (handler-bind ((sb-int:early-deprecation-warning (lambda (_)
1489 (declare (ignore _))
1490 (incf warned))))
1491 (list (compile nil `(lambda (lock)
1492 (sb-thread::with-spinlock (lock)
1493 t)))
1494 (compile nil `(lambda ()
1495 (sb-thread::make-spinlock :name "foo")))
1496 (compile nil `(lambda (lock)
1497 (sb-thread::get-spinlock lock)))
1498 (compile nil `(lambda (lock)
1499 (sb-thread::release-spinlock lock)))))))
1500 (assert (eql 4 warned))
1501 (handler-bind ((warning #'error))
1502 (destructuring-bind (with make get release) funs
1503 (let ((lock (funcall make)))
1504 (funcall get lock)
1505 (funcall release lock)
1506 (assert (eq t (funcall with lock))))))))
1508 (with-test (:name :interrupt-io-unnamed-pipe)
1509 (let (result)
1510 (labels
1511 ((reader (fd)
1512 (let ((stream (sb-sys:make-fd-stream fd
1513 :element-type :default
1514 :serve-events nil)))
1515 (time
1516 (let ((ok (handler-case
1517 (catch 'stop
1518 (progn
1519 (read-char stream)
1520 (sleep 0.1)
1521 (sleep 0.1)
1522 (sleep 0.1)))
1523 (error (c)
1524 c))))
1525 (setf result ok)
1526 (progn
1527 (format *trace-output* "~&=> ~A~%" ok)
1528 (force-output *trace-output*))))
1529 (sleep 2)
1530 (ignore-errors (close stream))))
1532 (writer ()
1533 (multiple-value-bind (read write)
1534 (sb-unix:unix-pipe)
1535 (let* ((reader (sb-thread:make-thread (lambda () (reader read))))
1536 (stream (sb-sys:make-fd-stream write
1537 :output t
1538 :element-type :default
1539 :serve-events nil))
1540 (ok :ok))
1541 (sleep 1)
1542 (sb-thread:interrupt-thread reader (lambda ()
1543 (print :throwing)
1544 (force-output)
1545 (throw 'stop ok)))
1546 (sleep 1)
1547 (setf ok :not-ok)
1548 (write-char #\x stream)
1549 (close stream)
1550 (sb-thread:join-thread reader)))))
1551 (writer))
1552 (assert (eq result :ok))))
1554 (with-test (:name :thread-alloca)
1555 (sb-ext:run-program "sh"
1556 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1557 "alloca.c" "-o" "alloca.so")
1558 :search t)
1559 (load-shared-object (truename "alloca.so"))
1561 (alien-funcall (extern-alien "alloca_test" (function void)))
1562 (sb-thread:join-thread
1563 (sb-thread:make-thread
1564 (lambda ()
1565 (alien-funcall (extern-alien "alloca_test" (function void)))))))
1567 (with-test (:name :fp-mode-inheritance-threads)
1568 (flet ((test ()
1569 (let* ((reserved-bits #+x86 (ash #b1110000011000000 16)
1570 #-x86 0)
1571 (fp-mode (logandc2 (dpb 0 sb-vm::float-sticky-bits (sb-vm:floating-point-modes))
1572 reserved-bits))
1573 (thread-fp-mode
1574 (sb-thread:join-thread
1575 (sb-thread:make-thread
1576 (lambda ()
1577 (dpb 0 sb-vm::float-sticky-bits (sb-vm:floating-point-modes)))))))
1578 (assert (= fp-mode thread-fp-mode)))))
1579 (test)
1580 (sb-int:with-float-traps-masked (:divide-by-zero)
1581 (test))
1582 (setf (sb-vm:floating-point-modes)
1583 (dpb sb-vm:float-divide-by-zero-trap-bit
1584 sb-vm::float-traps-byte
1585 (sb-vm:floating-point-modes)))
1586 (test)))