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