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