Reduce pinned object table size, part 2 of 2.
[sbcl.git] / tests / threads.impure.lisp
blob3b53ed2f6e8832f9403d2e1e9182f4ae26185205
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 ;; this used to deadlock on session-lock
774 (with-test (:name (:no-session-deadlock))
775 (make-join-thread (lambda () (sb-ext:gc))))
777 (defun exercise-syscall (fn reference-errno)
778 (make-kill-thread
779 (lambda ()
780 (loop do
781 (funcall fn)
782 (let ((errno (sb-unix::get-errno)))
783 (sleep (random 0.1d0))
784 (unless (eql errno reference-errno)
785 (format t "Got errno: ~A (~A) instead of ~A~%"
786 errno
787 (sb-unix::strerror)
788 reference-errno)
789 (force-output)
790 (abort-thread)))))))
792 ;; (nanosleep -1 0) does not fail on FreeBSD
793 (with-test (:name (:exercising-concurrent-syscalls) :fails-on :win32)
794 (let* (#-freebsd
795 (nanosleep-errno (progn
796 (sb-unix:nanosleep -1 0)
797 (sb-unix::get-errno)))
798 (open-errno (progn
799 (open "no-such-file"
800 :if-does-not-exist nil)
801 (sb-unix::get-errno)))
802 (threads
803 (list
804 #-freebsd
805 (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
806 (exercise-syscall (lambda () (open "no-such-file"
807 :if-does-not-exist nil))
808 open-errno)
809 (make-join-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
810 (sleep 10)
811 (princ "terminating threads")
812 (dolist (thread threads)
813 (sb-thread:terminate-thread thread))))
815 (format t "~&errno test done~%")
817 (with-test (:name :all-threads-have-abort-restart)
818 (loop repeat 100 do
819 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
820 (sb-thread:interrupt-thread
821 thread
822 (lambda ()
823 (assert (find-restart 'abort)))))))
825 (sb-ext:gc :full t)
827 (format t "~&thread startup sigmask test done~%")
829 (with-test (:name (:debugger-no-hang-on-session-lock-if-interrupted)
830 :fails-on :win32)
831 #+win32 (error "user would have to touch a key interactively to proceed")
832 (sb-debug::enable-debugger)
833 (let* ((main-thread *current-thread*)
834 (interruptor-thread
835 (make-thread (lambda ()
836 (sleep 2)
837 (interrupt-thread main-thread
838 (lambda ()
839 (with-interrupts
840 (break))))
841 (sleep 2)
842 (interrupt-thread main-thread #'continue))
843 :name "interruptor")))
844 (with-session-lock (*session*)
845 (sleep 3))
846 (loop while (thread-alive-p interruptor-thread))))
848 (format t "~&session lock test done~%")
850 ;; expose thread creation races by exiting quickly
851 (with-test (:name (:no-thread-creation-race :light))
852 (make-join-thread (lambda ())))
854 (with-test (:name (:no-thread-creation-race :heavy))
855 (loop repeat 20 do
856 (wait-for-threads
857 (loop for i below 100 collect
858 (sb-thread:make-thread (lambda ()))))))
860 (format t "~&creation test done~%")
862 ;; interrupt handlers are per-thread with pthreads, make sure the
863 ;; handler installed in one thread is global
864 (with-test (:name (:global-interrupt-handler))
865 (make-join-thread
866 (lambda ()
867 (sb-ext:run-program "sleep" '("1") :search t :wait nil))))
869 ;;;; Binding stack safety
871 (defparameter *x* nil)
872 (defparameter *n-gcs-requested* 0)
873 (defparameter *n-gcs-done* 0)
875 (let ((counter 0))
876 (defun make-something-big ()
877 (let ((x (make-string 32000)))
878 (incf counter)
879 (let ((counter counter))
880 (sb-ext:finalize x (lambda () (format t " ~S" counter)
881 (force-output)))))))
883 (defmacro wait-for-gc ()
884 `(progn
885 (incf *n-gcs-requested*)
886 (loop while (< *n-gcs-done* *n-gcs-requested*))))
888 (defun send-gc ()
889 (loop until (< *n-gcs-done* *n-gcs-requested*))
890 (format t "G")
891 (force-output)
892 (sb-ext:gc)
893 (incf *n-gcs-done*))
895 #+(or x86 x86-64) ;the only platforms with a *binding-stack-pointer* variable
896 (defun exercise-binding ()
897 (loop
898 (let ((*x* (make-something-big)))
899 (let ((*x* 42))
900 ;; at this point the binding stack looks like this:
901 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
903 (wait-for-gc)
904 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
905 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
906 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
907 ;; unbinding but values are not).
908 (let ((*x* nil)
909 (binding-pointer-delta (ash 2 (- sb-vm:word-shift sb-vm:n-fixnum-tag-bits))))
910 ;; bump bsp as if a BIND had just started
911 (incf sb-vm::*binding-stack-pointer* binding-pointer-delta)
912 (wait-for-gc)
913 (decf sb-vm::*binding-stack-pointer* binding-pointer-delta))))
915 #+(or x86 x86-64) ;the only platforms with a *binding-stack-pointer* variable
916 (with-test (:name (:binding-stack-gc-safety))
917 (let (threads)
918 (unwind-protect
919 (progn
920 (push (make-kill-thread #'exercise-binding) threads)
921 (push (make-kill-thread (lambda ()
922 (loop
923 (sleep 0.1)
924 (send-gc))))
925 threads)
926 (sleep 4))
927 (mapc #'sb-thread:terminate-thread threads))))
929 (with-test (:name :test-%thread-local-references)
930 (let ((mysym (gensym))
931 (fool1 (cons 1 2))
932 (fool2 (cons 2 3)))
933 (progv (list mysym) '(nil)
934 (let* ((i (get-lisp-obj-address (sb-kernel:symbol-tls-index mysym)))
935 (j (+ i sb-vm:n-word-bytes)))
936 (assert (eql (sap-ref-word (current-thread-sap) j)
937 sb-vm:no-tls-value-marker-widetag))
938 (setf (sap-ref-lispobj (current-thread-sap) i) fool1
939 (sap-ref-lispobj (current-thread-sap) j) fool2)
940 ;; assert that my pointer arithmetic worked as expected
941 (assert (eq (symbol-value mysym) fool1))
942 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
943 (let ((list (%thread-local-references)))
944 (assert (memq fool1 list))
945 (assert (not (memq fool2 list))))
946 ;; repair the TLS entry that was corrupted by the test
947 (setf (sap-ref-word (current-thread-sap) j)
948 sb-vm:no-tls-value-marker-widetag)))))
950 (format t "~&binding test done~%")
952 ;;; HASH TABLES
954 (defvar *errors* nil)
956 (defun oops (e)
957 (setf *errors* e)
958 (format t "~&oops: ~A in ~S~%" e *current-thread*)
959 (sb-debug:print-backtrace)
960 (catch 'done))
962 (with-test (:name (:unsynchronized-hash-table)
963 ;; FIXME: This test occasionally eats out craploads
964 ;; of heap instead of expected error early. Not 100%
965 ;; sure if it would finish as expected, but since it
966 ;; hits swap on my system I'm not likely to find out
967 ;; soon. Disabling for now. -- nikodemus
968 :skipped-on :sbcl)
969 ;; We expect a (probable) error here: parellel readers and writers
970 ;; on a hash-table are not expected to work -- but we also don't
971 ;; expect this to corrupt the image.
972 (let* ((hash (make-hash-table))
973 (*errors* nil)
974 (threads (list (make-kill-thread
975 (lambda ()
976 (catch 'done
977 (handler-bind ((serious-condition 'oops))
978 (loop
979 ;;(princ "1") (force-output)
980 (setf (gethash (random 100) hash) 'h)))))
981 :name "writer")
982 (make-kill-thread
983 (lambda ()
984 (catch 'done
985 (handler-bind ((serious-condition 'oops))
986 (loop
987 ;;(princ "2") (force-output)
988 (remhash (random 100) hash)))))
989 :name "reader")
990 (make-kill-thread
991 (lambda ()
992 (catch 'done
993 (handler-bind ((serious-condition 'oops))
994 (loop
995 (sleep (random 1.0))
996 (sb-ext:gc :full t)))))
997 :name "collector"))))
998 (unwind-protect
999 (sleep 10)
1000 (mapc #'sb-thread:terminate-thread threads))))
1002 (format t "~&unsynchronized hash table test done~%")
1004 (with-test (:name (:synchronized-hash-table))
1005 (let* ((hash (make-hash-table :synchronized t))
1006 (*errors* nil)
1007 (threads (list (make-join-thread
1008 (lambda ()
1009 (catch 'done
1010 (handler-bind ((serious-condition 'oops))
1011 (loop
1012 ;;(princ "1") (force-output)
1013 (setf (gethash (random 100) hash) 'h)))))
1014 :name "writer")
1015 (make-join-thread
1016 (lambda ()
1017 (catch 'done
1018 (handler-bind ((serious-condition 'oops))
1019 (loop
1020 ;;(princ "2") (force-output)
1021 (remhash (random 100) hash)))))
1022 :name "reader")
1023 (make-join-thread
1024 (lambda ()
1025 (catch 'done
1026 (handler-bind ((serious-condition 'oops))
1027 (loop
1028 (sleep (random 1.0))
1029 (sb-ext:gc :full t)))))
1030 :name "collector"))))
1031 (unwind-protect
1032 (sleep 10)
1033 (mapc #'sb-thread:terminate-thread threads))
1034 (assert (not *errors*))))
1036 (format t "~&synchronized hash table test done~%")
1038 (with-test (:name (:hash-table-parallel-readers))
1039 (let ((hash (make-hash-table))
1040 (*errors* nil))
1041 (loop repeat 50
1042 do (setf (gethash (random 100) hash) 'xxx))
1043 (let ((threads (list (make-kill-thread
1044 (lambda ()
1045 (catch 'done
1046 (handler-bind ((serious-condition 'oops))
1047 (loop
1048 until (eq t (gethash (random 100) hash))))))
1049 :name "reader 1")
1050 (make-kill-thread
1051 (lambda ()
1052 (catch 'done
1053 (handler-bind ((serious-condition 'oops))
1054 (loop
1055 until (eq t (gethash (random 100) hash))))))
1056 :name "reader 2")
1057 (make-kill-thread
1058 (lambda ()
1059 (catch 'done
1060 (handler-bind ((serious-condition 'oops))
1061 (loop
1062 until (eq t (gethash (random 100) hash))))))
1063 :name "reader 3")
1064 (make-kill-thread
1065 (lambda ()
1066 (catch 'done
1067 (handler-bind ((serious-condition 'oops))
1068 (loop
1069 (sleep (random 1.0))
1070 (sb-ext:gc :full t)))))
1071 :name "collector"))))
1072 (unwind-protect
1073 (sleep 10)
1074 (mapc #'sb-thread:terminate-thread threads))
1075 (assert (not *errors*)))))
1077 (format t "~&multiple reader hash table test done~%")
1079 (with-test (:name (:hash-table-single-accessor-parallel-gc))
1080 (let ((hash (make-hash-table))
1081 (*errors* nil))
1082 (let ((threads (list (make-kill-thread
1083 (lambda ()
1084 (handler-bind ((serious-condition 'oops))
1085 (loop
1086 (let ((n (random 100)))
1087 (if (gethash n hash)
1088 (remhash n hash)
1089 (setf (gethash n hash) 'h))))))
1090 :name "accessor")
1091 (make-kill-thread
1092 (lambda ()
1093 (handler-bind ((serious-condition 'oops))
1094 (loop
1095 (sleep (random 1.0))
1096 (sb-ext:gc :full t))))
1097 :name "collector"))))
1098 (unwind-protect
1099 (sleep 10)
1100 (mapc #'sb-thread:terminate-thread threads))
1101 (assert (not *errors*)))))
1103 (format t "~&single accessor hash table test~%")
1105 #| ;; a cll post from eric marsden
1106 | (defun crash ()
1107 | (setq *debugger-hook*
1108 | (lambda (condition old-debugger-hook)
1109 | (debug:backtrace 10)
1110 | (unix:unix-exit 2)))
1111 | #+live-dangerously
1112 | (mp::start-sigalrm-yield)
1113 | (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
1114 | (mp:make-process #'roomy)
1115 | (mp:make-process #'roomy)))
1118 (with-test (:name (:condition-variable :notify-multiple))
1119 (flet ((tester (notify-fun)
1120 (let ((queue (make-waitqueue :name "queue"))
1121 (lock (make-mutex :name "lock"))
1122 (data nil))
1123 (labels ((test (x)
1124 (loop
1125 (with-mutex (lock)
1126 (format t "condition-wait ~a~%" x)
1127 (force-output)
1128 (condition-wait queue lock)
1129 (format t "woke up ~a~%" x)
1130 (force-output)
1131 (push x data)))))
1132 (let ((threads (loop for x from 1 to 10
1133 collect
1134 (let ((x x))
1135 (make-kill-thread (lambda ()
1136 (test x)))))))
1137 (sleep 5)
1138 (with-mutex (lock)
1139 (funcall notify-fun queue))
1140 (sleep 5)
1141 (mapcar #'terminate-thread threads)
1142 ;; Check that all threads woke up at least once
1143 (assert (= (length (remove-duplicates data)) 10)))))))
1144 (tester (lambda (queue)
1145 (format t "~&(condition-notify queue 10)~%")
1146 (force-output)
1147 (condition-notify queue 10)))
1148 (tester (lambda (queue)
1149 (format t "~&(condition-broadcast queue)~%")
1150 (force-output)
1151 (condition-broadcast queue)))))
1153 (format t "waitqueue wakeup tests done~%")
1155 ;;; Make sure that a deadline handler is not invoked twice in a row in
1156 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1158 (with-test (:name (:condition-wait :deadlines :LP-512914))
1159 (let ((n 2) ; was empirically enough to trigger the bug
1160 (mutex (sb-thread:make-mutex))
1161 (waitq (sb-thread:make-waitqueue))
1162 (threads nil)
1163 (deadline-handler-run-twice? nil))
1164 (dotimes (i n)
1165 (let ((child
1166 (sb-thread:make-thread
1167 #'(lambda ()
1168 (handler-bind
1169 ((sb-sys:deadline-timeout
1170 (let ((already? nil))
1171 #'(lambda (c)
1172 (when already?
1173 (setq deadline-handler-run-twice? t))
1174 (setq already? t)
1175 (sleep 0.2)
1176 (sb-thread:condition-broadcast waitq)
1177 (sb-sys:defer-deadline 10.0 c)))))
1178 (sb-sys:with-deadline (:seconds 0.1)
1179 (sb-thread:with-mutex (mutex)
1180 (sb-thread:condition-wait waitq mutex))))))))
1181 (push child threads)))
1182 (mapc #'sb-thread:join-thread threads)
1183 (assert (not deadline-handler-run-twice?))))
1185 (with-test (:name (:condition-wait :signal-deadline-with-interrupts-enabled))
1186 (let ((mutex (sb-thread:make-mutex))
1187 (waitq (sb-thread:make-waitqueue))
1188 (A-holds? :unknown)
1189 (B-holds? :unknown)
1190 (A-interrupts-enabled? :unknown)
1191 (B-interrupts-enabled? :unknown)
1193 (B))
1194 ;; W.L.O.G., we assume that A is executed first...
1195 (setq A (sb-thread:make-thread
1196 #'(lambda ()
1197 (handler-bind
1198 ((sb-sys:deadline-timeout
1199 #'(lambda (c)
1200 ;; We came here through the call to DECODE-TIMEOUT
1201 ;; in CONDITION-WAIT; hence both here are supposed
1202 ;; to evaluate to T.
1203 (setq A-holds? (sb-thread:holding-mutex-p mutex))
1204 (setq A-interrupts-enabled?
1205 sb-sys:*interrupts-enabled*)
1206 (sleep 0.2)
1207 (sb-thread:condition-broadcast waitq)
1208 (sb-sys:defer-deadline 10.0 c))))
1209 (sb-sys:with-deadline (:seconds 0.1)
1210 (sb-thread:with-mutex (mutex)
1211 (sb-thread:condition-wait waitq mutex)))))
1212 :name "A"))
1213 (setq B (sb-thread:make-thread
1214 #'(lambda ()
1215 (thread-yield)
1216 (handler-bind
1217 ((sb-sys:deadline-timeout
1218 #'(lambda (c)
1219 ;; We came here through the call to DECODE-TIMEOUT
1220 ;; in CONDITION-WAIT (contended case of
1221 ;; reaquiring the mutex) - so the former will
1222 ;; be NIL, but interrupts should still be enabled.
1223 (setq B-holds? (sb-thread:holding-mutex-p mutex))
1224 (setq B-interrupts-enabled?
1225 sb-sys:*interrupts-enabled*)
1226 (sleep 0.2)
1227 (sb-thread:condition-broadcast waitq)
1228 (sb-sys:defer-deadline 10.0 c))))
1229 (sb-sys:with-deadline (:seconds 0.1)
1230 (sb-thread:with-mutex (mutex)
1231 (sb-thread:condition-wait waitq mutex)))))
1232 :name "B"))
1233 (sb-thread:join-thread A)
1234 (sb-thread:join-thread B)
1235 (let ((A-result (list A-holds? A-interrupts-enabled?))
1236 (B-result (list B-holds? B-interrupts-enabled?)))
1237 ;; We also check some subtle behaviour w.r.t. whether a deadline
1238 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1239 ;; probably very internal behaviour (so user should not depend
1240 ;; on it) -- I added the testing here just to manifest current
1241 ;; behaviour.
1242 (cond ((equal A-result '(t t)) (assert (equal B-result '(nil t))))
1243 ((equal B-result '(t t)) (assert (equal A-result '(nil t))))
1245 (error "Failure: fell through wit A: ~S, B: ~S"
1246 A-result
1247 B-result))))))
1249 (with-test (:name (:mutex :finalization))
1250 (let ((a nil))
1251 (dotimes (i 500000)
1252 (setf a (make-mutex)))))
1254 (format t "mutex finalization test done~%")
1256 ;; You have to shoehorn this arbitrary sexpr into a feature expression
1257 ;; to have the test summary show that a test was disabled.
1258 #+gencgc
1259 (unless (eql (extern-alien "verify_gens" int)
1260 (1+ sb-vm:+highest-normal-generation+))
1261 (pushnew :verify-gens *features*))
1263 (with-test (:name :backtrace :skipped-on ':verify-gens)
1264 ;; Printing backtraces from several threads at once used to hang the
1265 ;; whole SBCL process (discovered by accident due to a timer.impure
1266 ;; test misbehaving). The cause was that packages weren't even
1267 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1268 ;; backtraces a loot of symbol lookups need to be done due to
1269 ;; *PRINT-ESCAPE*.
1270 (let* ((threads (loop repeat 10
1271 collect (sb-thread:make-thread
1272 (lambda ()
1273 (dotimes (i 1000)
1274 (with-output-to-string (*debug-io*)
1275 (sb-debug:print-backtrace :count 10))))))))
1276 (wait-for-threads threads)))
1278 (format t "backtrace test done~%")
1280 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1282 (with-test (:name :gc-deadlock)
1283 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1284 ;; GC due to *all-threads-lock* and session lock. On earlier
1285 ;; versions and at least on one specific box this test is good enough
1286 ;; to catch that typically well before the 1500th iteration.
1287 (loop
1288 with i = 0
1289 with n = 3000
1290 while (< i n)
1292 (incf i)
1293 (when (zerop (mod i 100))
1294 (write-char #\.)
1295 (force-output))
1296 (handler-case
1297 (if (oddp i)
1298 (make-join-thread
1299 (lambda ()
1300 (sleep (random 0.001)))
1301 :name (format nil "SLEEP-~D" i))
1302 (make-join-thread
1303 (lambda ()
1304 ;; KLUDGE: what we are doing here is explicit,
1305 ;; but the same can happen because of a regular
1306 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1307 ;; session functions.
1308 (sb-thread::with-all-threads-lock
1309 (sb-thread::with-session-lock (sb-thread::*session*)
1310 (sb-ext:gc))))
1311 :name (format nil "GC-~D" i)))
1312 (error (e)
1313 (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
1314 (sleep 0.1)
1315 (incf i)))))
1317 (format t "~&gc deadlock test done~%")
1319 (let ((count (make-array 8 :initial-element 0)))
1320 (defun closure-one ()
1321 (declare (optimize safety))
1322 (values (incf (aref count 0)) (incf (aref count 1))
1323 (incf (aref count 2)) (incf (aref count 3))
1324 (incf (aref count 4)) (incf (aref count 5))
1325 (incf (aref count 6)) (incf (aref count 7))))
1326 (defun no-optimizing-away-closure-one ()
1327 (setf count (make-array 8 :initial-element 0))))
1329 (defstruct box
1330 (count 0))
1332 (let ((one (make-box))
1333 (two (make-box))
1334 (three (make-box)))
1335 (defun closure-two ()
1336 (declare (optimize safety))
1337 (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
1338 (defun no-optimizing-away-closure-two ()
1339 (setf one (make-box)
1340 two (make-box)
1341 three (make-box))))
1343 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1344 ;;; sometimes run out of memory) in the following test. For developers
1345 ;;; interested in debugging this combination of features, it might be
1346 ;;; fruitful to concentrate their efforts around this test...
1348 (with-test (:name (:funcallable-instances)
1349 :skipped-on '(and :sb-safepoint
1350 (not :c-stack-is-control-stack)))
1351 ;; the funcallable-instance implementation used not to be threadsafe
1352 ;; against setting the funcallable-instance function to a closure
1353 ;; (because the code and lexenv were set separately).
1354 (let ((fun (sb-kernel:%make-funcallable-instance 0))
1355 (condition nil))
1356 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1357 (flet ((changer ()
1358 (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1359 (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
1360 (test ()
1361 (handler-case (loop (funcall fun))
1362 (serious-condition (c) (setf condition c)))))
1363 (let ((changer (make-thread #'changer))
1364 (test (make-thread #'test)))
1365 (handler-case
1366 (progn
1367 ;; The two closures above are fairly carefully crafted
1368 ;; so that if given the wrong lexenv they will tend to
1369 ;; do some serious damage, but it is of course difficult
1370 ;; to predict where the various bits and pieces will be
1371 ;; allocated. Five seconds failed fairly reliably on
1372 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1373 (sb-ext:with-timeout 5
1374 (wait-for-threads (list test)))
1375 (error "~@<test thread got condition:~2I~_~A~@:>" condition))
1376 (sb-ext:timeout ()
1377 (terminate-thread changer)
1378 (terminate-thread test)
1379 (wait-for-threads (list changer test))))))))
1381 (format t "~&funcallable-instance test done~%")
1383 (defun random-type (n)
1384 `(integer ,(random n) ,(+ n (random n))))
1386 (defun subtypep-hash-cache-test ()
1387 (dotimes (i 10000)
1388 (let ((type1 (random-type 500))
1389 (type2 (random-type 500)))
1390 (let ((a (subtypep type1 type2)))
1391 (dotimes (i 100)
1392 (assert (eq (subtypep type1 type2) a))))))
1393 (write-char #\.)
1394 (force-output))
1396 (with-test (:name (:hash-cache :subtypep))
1397 (mapc #'join-thread
1398 ;; this didn't "reliably fail" with a small number of threads.
1399 ;; 30 is a compromise between running time and confidence in the result.
1400 (loop repeat 30
1401 collect (sb-thread:make-thread #'subtypep-hash-cache-test)))
1402 (terpri))
1404 ;;;; BLACK BOX TESTS
1406 (in-package :cl-user)
1407 (use-package :test-util)
1408 (use-package "ASSERTOID")
1410 (format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1411 (with-test (:name :parallel-defclass)
1412 (defclass test-1 () ((a :initform :orig-a)))
1413 (defclass test-2 () ((b :initform :orig-b)))
1414 (defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
1415 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1416 ;; calls disabled in the folloving code. (As seen on a Server 2012
1417 ;; installation.) Clearly, this sort of workaround in a test is
1418 ;; cheating, and might be hiding the underlying bug that the test is
1419 ;; exposing. Let's review this later.
1420 (let* ((run t)
1421 (d1 (sb-thread:make-thread (lambda ()
1422 (loop while run
1423 do (defclass test-1 () ((a :initform :new-a)))
1424 (write-char #\1)
1425 #-win32 (force-output)))
1426 :name "d1"))
1427 (d2 (sb-thread:make-thread (lambda ()
1428 (loop while run
1429 do (defclass test-2 () ((b :initform :new-b)))
1430 (write-char #\2)
1431 #-win32 (force-output)))
1432 :name "d2"))
1433 (d3 (sb-thread:make-thread (lambda ()
1434 (loop while run
1435 do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
1436 (write-char #\3)
1437 #-win32 (force-output)))
1438 :name "d3"))
1439 (i (sb-thread:make-thread (lambda ()
1440 (loop while run
1441 do (let ((i (make-instance 'test-3)))
1442 (assert (member (slot-value i 'a) '(:orig-a :new-a)))
1443 (assert (member (slot-value i 'b) '(:orig-b :new-b)))
1444 (assert (member (slot-value i 'c) '(:orig-c :new-c))))
1445 (write-char #\i)
1446 #-win32 (force-output)))
1447 :name "i")))
1448 (format t "~%sleeping!~%")
1449 (sleep 2.0)
1450 (format t "~%stopping!~%")
1451 (setf run nil)
1452 (mapc (lambda (th)
1453 (sb-thread:join-thread th)
1454 (format t "~%joined ~S~%" (sb-thread:thread-name th)))
1455 (list d1 d2 d3 i))
1456 (force-output)))
1457 (format t "parallel defclass test done~%")
1459 (with-test (:name (:deadlock-detection :interrupts) :fails-on :win32)
1460 #+win32 ;be more explicit than just :skipped-on
1461 (error "not attempting, because of deadlock error in background thread")
1462 (let* ((m1 (sb-thread:make-mutex :name "M1"))
1463 (m2 (sb-thread:make-mutex :name "M2"))
1464 (t1-can-go (sb-thread:make-semaphore :name "T1 can go"))
1465 (t2-can-go (sb-thread:make-semaphore :name "T2 can go"))
1466 (t1 (sb-thread:make-thread
1467 (lambda ()
1468 (sb-thread:with-mutex (m1)
1469 (sb-thread:wait-on-semaphore t1-can-go)
1470 :ok1))
1471 :name "T1"))
1472 (t2 (sb-thread:make-thread
1473 (lambda ()
1474 (sb-ext:wait-for (eq t1 (sb-thread:mutex-owner m1)))
1475 (sb-thread:with-mutex (m1 :wait-p t)
1476 (sb-thread:wait-on-semaphore t2-can-go)
1477 :ok2))
1478 :name "T2")))
1479 (sb-ext:wait-for (eq m1 (sb-thread::thread-waiting-for t2)))
1480 (sb-thread:interrupt-thread t2 (lambda ()
1481 (sb-thread:with-mutex (m2 :wait-p t)
1482 (sb-ext:wait-for
1483 (eq m2 (sb-thread::thread-waiting-for t1)))
1484 (sb-thread:signal-semaphore t2-can-go))))
1485 (sb-ext:wait-for (eq t2 (sb-thread:mutex-owner m2)))
1486 (sb-thread:interrupt-thread t1 (lambda ()
1487 (sb-thread:with-mutex (m2 :wait-p t)
1488 (sb-thread:signal-semaphore t1-can-go))))
1489 ;; both threads should finish without a deadlock or deadlock
1490 ;; detection error
1491 (let ((res (list (sb-thread:join-thread t1)
1492 (sb-thread:join-thread t2))))
1493 (assert (equal '(:ok1 :ok2) res)))))
1495 (with-test (:name (:deadlock-detection :gc))
1496 ;; To semi-reliably trigger the error (in SBCL's where)
1497 ;; it was present you had to run this for > 30 seconds,
1498 ;; but that's a bit long for a single test.
1499 (let* ((stop (+ 5 (get-universal-time)))
1500 (m1 (sb-thread:make-mutex :name "m1"))
1501 (t1 (sb-thread:make-thread
1502 (lambda ()
1503 (loop until (> (get-universal-time) stop)
1504 do (sb-thread:with-mutex (m1)
1505 (eval `(make-array 24))))
1506 :ok)))
1507 (t2 (sb-thread:make-thread
1508 (lambda ()
1509 (loop until (> (get-universal-time) stop)
1510 do (sb-thread:with-mutex (m1)
1511 (eval `(make-array 24))))
1512 :ok))))
1513 (let ((res (list (sb-thread:join-thread t1)
1514 (sb-thread:join-thread t2))))
1515 (assert (equal '(:ok :ok) res)))))
1517 (with-test (:name :spinlock-api)
1518 (handler-bind ((warning #'error))
1519 (destructuring-bind (with make get release)
1520 (assert-signal
1521 (list (compile nil `(lambda (lock)
1522 (sb-thread::with-spinlock (lock)
1523 t)))
1524 (compile nil `(lambda ()
1525 (sb-thread::make-spinlock :name "foo")))
1526 (compile nil `(lambda (lock)
1527 (sb-thread::get-spinlock lock)))
1528 (compile nil `(lambda (lock)
1529 (sb-thread::release-spinlock lock))))
1530 early-deprecation-warning 4)
1531 (let ((lock (funcall make)))
1532 (funcall get lock)
1533 (funcall release lock)
1534 (assert (eq t (funcall with lock)))))))
1536 (with-test (:name :interrupt-io-unnamed-pipe)
1537 (let (result)
1538 (labels
1539 ((reader (fd)
1540 (let ((stream (sb-sys:make-fd-stream fd
1541 :element-type :default
1542 :serve-events nil)))
1543 (time
1544 (let ((ok (handler-case
1545 (catch 'stop
1546 (progn
1547 (read-char stream)
1548 (sleep 0.1)
1549 (sleep 0.1)
1550 (sleep 0.1)))
1551 (error (c)
1552 c))))
1553 (setf result ok)
1554 (progn
1555 (format *trace-output* "~&=> ~A~%" ok)
1556 (force-output *trace-output*))))
1557 (sleep 2)
1558 (ignore-errors (close stream))))
1560 (writer ()
1561 (multiple-value-bind (read write)
1562 (sb-unix:unix-pipe)
1563 (let* ((reader (sb-thread:make-thread (lambda () (reader read))))
1564 (stream (sb-sys:make-fd-stream write
1565 :output t
1566 :element-type :default
1567 :serve-events nil))
1568 (ok :ok))
1569 (sleep 1)
1570 (sb-thread:interrupt-thread reader (lambda ()
1571 (print :throwing)
1572 (force-output)
1573 (throw 'stop ok)))
1574 (sleep 1)
1575 (setf ok :not-ok)
1576 (write-char #\x stream)
1577 (close stream)
1578 (sb-thread:join-thread reader)))))
1579 (writer))
1580 (assert (eq result :ok))))
1582 (with-test (:name :thread-alloca)
1583 (sb-ext:run-program "sh"
1584 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1585 "alloca.c" "-o" "alloca.so")
1586 :search t)
1587 (load-shared-object (truename "alloca.so"))
1589 (alien-funcall (extern-alien "alloca_test" (function void)))
1590 (sb-thread:join-thread
1591 (sb-thread:make-thread
1592 (lambda ()
1593 (alien-funcall (extern-alien "alloca_test" (function void)))))))
1595 (with-test (:name :fp-mode-inheritance-threads)
1596 (flet ((test ()
1597 (let* ((reserved-bits #+x86 (ash #b1110000011000000 16)
1598 #-x86 0)
1599 (fp-mode (logandc2 (dpb 0 sb-vm::float-sticky-bits (sb-vm:floating-point-modes))
1600 reserved-bits))
1601 (thread-fp-mode
1602 (sb-thread:join-thread
1603 (sb-thread:make-thread
1604 (lambda ()
1605 (dpb 0 sb-vm::float-sticky-bits (sb-vm:floating-point-modes)))))))
1606 (assert (= fp-mode thread-fp-mode)))))
1607 (test)
1608 (sb-int:with-float-traps-masked (:divide-by-zero)
1609 (test))
1610 (setf (sb-vm:floating-point-modes)
1611 (dpb sb-vm:float-divide-by-zero-trap-bit
1612 sb-vm::float-traps-byte
1613 (sb-vm:floating-point-modes)))
1614 (test)))