1 ;;;; miscellaneous tests of thread stuff
3 ;;;; This software is part of the SBCL system. See the README file for
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
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.
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)))
41 (sb-alien:define-alien-routine
"check_deferrables_blocked_or_lose"
43 (where sb-alien
:unsigned-long
))
44 (sb-alien:define-alien-routine
"check_deferrables_unblocked_or_lose"
46 (where sb-alien
:unsigned-long
))
48 (with-test (:name
(:interrupt-thread
:basics
:no-unwinding
))
50 (interrupt-thread *current-thread
* (lambda () (setq a
1)))
53 (with-test (:name
(:interrupt-thread
:deferrables-blocked
))
54 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
56 (check-deferrables-blocked-or-lose 0))))
58 (with-test (:name
(:interrupt-thread
:deferrables-unblocked
))
59 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
62 (check-deferrables-unblocked-or-lose 0)))))
64 (with-test (:name
(:interrupt-thread
:nlx
))
66 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
68 (check-deferrables-blocked-or-lose 0)
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)))
79 (assert (null (join-thread (make-thread
81 (with-mutex (m :timeout
0.1)
83 (assert (join-thread (make-thread
85 (with-mutex (m :timeout
0.1)
88 (with-test (:name
(:interrupt-thread
:deferrables-unblocked-by-lock
))
89 (let ((lock (sb-thread::make-mutex
))
90 (thread (make-join-thread (lambda ()
92 (sb-thread::grab-mutex lock
)
93 (sb-thread:interrupt-thread thread
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
)))
100 (sb-thread::release-mutex lock
)))
104 (defmacro defincf
(name accessor
&rest args
)
106 (let* ((old (,accessor x
,@args
))
108 (loop until
(eq old
(sb-ext:compare-and-swap
(,accessor x
,@args
) old new
))
109 do
(setf old
(,accessor x
,@args
)
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
)
130 collect
(sb-thread:make-thread
133 do
(sb-thread:thread-yield
))
134 (loop repeat n do
(,incf x
)))))))
137 (sb-thread:join-thread th
))
138 (assert (= (,op x
) (* 10 n
))))))
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.
))
147 incf-symbol-value symbol-value
)
148 (def-test-cas test-cas-svref
/0 (vector 0 nil
) incf-svref
/0 (lambda (x)
150 (def-test-cas test-cas-svref
/1 (vector nil
0) incf-svref
/1 (lambda (x)
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
*))
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
)))
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))))
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)
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
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 ()
207 (sb-thread:condition-wait queue mutex
))
208 (sb-thread:abort-thread
))))
209 (let ((start-time (get-internal-run-time)))
211 (prog1 (- (get-internal-run-time) start-time
)
212 (sb-thread:condition-broadcast queue
)))))
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
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)))
264 (assert (null (join-thread (make-thread
266 (with-recursive-lock (m :wait-p nil
)
268 (assert (join-thread (make-thread
270 (with-recursive-lock (m :wait-p nil
)
273 (with-test (:name
(:with-recursive-lock
:wait-p
:recursive
))
274 (let ((m (make-mutex)))
275 (assert (join-thread (make-thread
277 (with-recursive-lock (m :wait-p nil
)
278 (with-recursive-lock (m :wait-p nil
)
281 (with-test (:name
(:with-recursive-lock
:timeout
))
282 (let ((m (make-mutex)))
284 (assert (null (join-thread (make-thread
286 (with-recursive-lock (m :timeout
0.1)
288 (assert (join-thread (make-thread
290 (with-recursive-lock (m :timeout
0.1)
293 (with-test (:name
(:with-recursive-lock
:timeout
:recursive
))
294 (let ((m (make-mutex)))
295 (assert (join-thread (make-thread
297 (with-recursive-lock (m :timeout
0.1)
298 (with-recursive-lock (m :timeout
0.1)
301 (with-test (:name
(:mutex
:nesting-mutex-and-recursive-lock
))
302 (let ((l (make-mutex :name
"a mutex")))
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
)))
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"))
319 (labels ((in-new-thread ()
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
*))
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
))
336 (condition-notify queue
))
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
))
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
))
362 (with-test (:name
(:mutex
:contention
))
363 (let ((mutex (make-mutex :name
"contended")))
365 (let ((me *current-thread
*))
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
))))))
379 (with-test (:name
(:grab-mutex
:waitp nil
))
380 (let ((m (make-mutex)))
382 (assert (null (join-thread (make-thread
384 (grab-mutex m
:waitp nil
)))))))))
386 (with-test (:name
(:grab-mutex
:timeout
:acquisition-fail
))
387 (let ((m (make-mutex))
388 (w (make-semaphore)))
390 (let ((th (make-thread
393 (grab-mutex m
:timeout
0.1)
394 (signal-semaphore w
))))))
395 ;; Wait for it to -- otherwise the detect the deadlock chain
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))
404 (setq child
(make-thread #'(lambda () (grab-mutex m
:timeout
1.0))))
406 (assert (eq (join-thread child
) 't
))))
408 (with-test (:name
(:grab-mutex
:timeout
+deadline
))
409 (let ((m (make-mutex))
410 (w (make-semaphore)))
412 (let ((th (make-thread #'(lambda ()
413 (sb-sys:with-deadline
(:seconds
0.0)
415 (grab-mutex m
:timeout
0.0)
416 (sb-sys:deadline-timeout
()
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)))
425 (assert (eq (join-thread
426 (make-thread #'(lambda ()
427 (sb-sys:with-deadline
(:seconds
0.0)
429 (grab-mutex m
:waitp nil
)
430 (sb-sys:deadline-timeout
()
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))
454 (make-join-thread (lambda ()
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))
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
)))
479 (assert (= 15 (count-live-threads)))
480 (signal-semaphore sem
10)
482 (assert (= 5 (count-live-threads)))
483 (signal-semaphore sem
3)
485 (assert (= 2 (count-live-threads)))
486 (signal-semaphore sem
4)
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))))))
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
)))))
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
))
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))
557 (thread (make-thread (lambda ()
558 (let ((mutex (semaphore-mutex semaphore
))
559 (queue (semaphore-queue semaphore
)))
560 (loop :until done
:do
562 (condition-notify queue
))))))))
563 (assert (eq nil
(wait-on-semaphore semaphore
:timeout
.5)))
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)
573 (format t
"interrupting child ~A~%" child
)
574 (interrupt-thread child
576 (format t
"child pid ~A~%" *current-thread
*)
577 (when quit-p
(abort-thread))))
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
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.
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"))
603 (setf child
(test-interrupt
606 (assert (eql (mutex-value lock
) *current-thread
*)))
607 (assert (not (eql (mutex-value lock
) *current-thread
*)))
609 ;;hold onto lock for long enough that child can't get it immediately
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))))))
623 (loop repeat
4 collect
624 (sb-thread:make-thread
627 (sleep (random 0.1d0
))
630 (sb-thread:interrupt-thread thread
(lambda ()))))))))
631 (wait-for-threads killers
)
632 (sb-thread:terminate-thread thread
)
633 (wait-for-threads (list thread
))))
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
644 (sleep (random 0.1d0
))
647 (princ ".") (force-output)
648 (assert (thread-alive-p *current-thread
*))
650 (not (logbitp 0 SB-KERNEL
:*PSEUDO-ATOMIC-BITS
*))))))
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
669 (handler-bind ((error #'(lambda (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 ()
678 (sb-ext:atomic-incf
(counter-n *interrupt-counter
*)))))
679 (setf (counter-n *interrupt-counter
*) 0)
681 (sleep (random 0.1d0
))
682 (interrupt-thread c func
))
683 (loop until
(= (counter-n *interrupt-counter
*) 100) do
(sleep 0.1))
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
696 (declare (special runningp
))
698 (sb-thread:interrupt-thread thread
700 (let ((*runningp
* t
))
703 (sb-thread:interrupt-thread thread
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
713 (declare (special runningp
))
715 (sb-thread:interrupt-thread thread
717 (let ((*runningp
* t
))
718 (sb-sys:with-interrupts
721 (sb-thread:interrupt-thread thread
723 (throw 'xxx
*runningp
*)))
724 (assert (sb-thread:join-thread thread
))))
726 (with-test (:name
(:two-threads-running-gc
))
728 (make-join-thread (lambda ()
730 (sb-ext:gc
) (princ "\\") (force-output))
732 (make-join-thread (lambda ()
735 (princ "/") (force-output))
738 (when (and a-done b-done
) (return))
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
758 (defparameter *aaa
* nil
)
759 (with-test (:name
(:one-thread-runs-gc-while-other-conses
:again
))
760 (loop for i below
100 do
765 (let ((*aaa
* (waste)))
767 (let ((*aaa
* (waste)))
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
)
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~%"
792 ;; (nanosleep -1 0) does not fail on FreeBSD
793 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
795 (nanosleep-errno (progn
796 (sb-unix:nanosleep -
1 0)
797 (sb-unix::get-errno
)))
800 :if-does-not-exist nil
)
801 (sb-unix::get-errno
)))
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
))
809 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
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
)
819 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
820 (sb-thread:interrupt-thread
823 (assert (find-restart 'abort
)))))))
827 (format t
"~&thread startup sigmask test done~%")
829 (with-test (:name
(:debugger-no-hang-on-session-lock-if-interrupted
)
831 #+win32
(error "user would have to touch a key interactively to proceed")
832 (sb-debug::enable-debugger
)
833 (let* ((main-thread *current-thread
*)
835 (make-thread (lambda ()
837 (interrupt-thread main-thread
842 (interrupt-thread main-thread
#'continue
))
843 :name
"interruptor")))
844 (with-session-lock (*session
*)
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
))
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
))
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)
876 (defun make-something-big ()
877 (let ((x (make-string 32000)))
879 (let ((counter counter
))
880 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
883 (defmacro wait-for-gc
()
885 (incf *n-gcs-requested
*)
886 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
889 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
895 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
896 (defun exercise-binding ()
898 (let ((*x
* (make-something-big)))
900 ;; at this point the binding stack looks like this:
901 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
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).
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
)
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
))
920 (push (make-kill-thread #'exercise-binding
) threads
)
921 (push (make-kill-thread (lambda ()
927 (mapc #'sb-thread
:terminate-thread threads
))))
929 (with-test (:name
:test-%thread-local-references
)
930 (let ((mysym (gensym))
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~%")
954 (defvar *errors
* nil
)
958 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
959 (sb-debug:print-backtrace
)
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
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))
974 (threads (list (make-kill-thread
977 (handler-bind ((serious-condition 'oops
))
979 ;;(princ "1") (force-output)
980 (setf (gethash (random 100) hash
) 'h
)))))
985 (handler-bind ((serious-condition 'oops
))
987 ;;(princ "2") (force-output)
988 (remhash (random 100) hash
)))))
993 (handler-bind ((serious-condition 'oops
))
996 (sb-ext:gc
:full t
)))))
997 :name
"collector"))))
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
))
1007 (threads (list (make-join-thread
1010 (handler-bind ((serious-condition 'oops
))
1012 ;;(princ "1") (force-output)
1013 (setf (gethash (random 100) hash
) 'h
)))))
1018 (handler-bind ((serious-condition 'oops
))
1020 ;;(princ "2") (force-output)
1021 (remhash (random 100) hash
)))))
1026 (handler-bind ((serious-condition 'oops
))
1028 (sleep (random 1.0))
1029 (sb-ext:gc
:full t
)))))
1030 :name
"collector"))))
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))
1042 do
(setf (gethash (random 100) hash
) 'xxx
))
1043 (let ((threads (list (make-kill-thread
1046 (handler-bind ((serious-condition 'oops
))
1048 until
(eq t
(gethash (random 100) hash
))))))
1053 (handler-bind ((serious-condition 'oops
))
1055 until
(eq t
(gethash (random 100) hash
))))))
1060 (handler-bind ((serious-condition 'oops
))
1062 until
(eq t
(gethash (random 100) hash
))))))
1067 (handler-bind ((serious-condition 'oops
))
1069 (sleep (random 1.0))
1070 (sb-ext:gc
:full t
)))))
1071 :name
"collector"))))
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))
1082 (let ((threads (list (make-kill-thread
1084 (handler-bind ((serious-condition 'oops
))
1086 (let ((n (random 100)))
1087 (if (gethash n hash
)
1089 (setf (gethash n hash
) 'h
))))))
1093 (handler-bind ((serious-condition 'oops
))
1095 (sleep (random 1.0))
1096 (sb-ext:gc
:full t
))))
1097 :name
"collector"))))
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
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"))
1126 (format t
"condition-wait ~a~%" x
)
1128 (condition-wait queue lock
)
1129 (format t
"woke up ~a~%" x
)
1132 (let ((threads (loop for x from
1 to
10
1135 (make-kill-thread (lambda ()
1139 (funcall notify-fun queue
))
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)~%")
1147 (condition-notify queue
10)))
1148 (tester (lambda (queue)
1149 (format t
"~&(condition-broadcast queue)~%")
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
))
1163 (deadline-handler-run-twice? nil
))
1166 (sb-thread:make-thread
1169 ((sb-sys:deadline-timeout
1170 (let ((already? nil
))
1173 (setq deadline-handler-run-twice? t
))
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
))
1190 (A-interrupts-enabled?
:unknown
)
1191 (B-interrupts-enabled?
:unknown
)
1194 ;; W.L.O.G., we assume that A is executed first...
1195 (setq A
(sb-thread:make-thread
1198 ((sb-sys:deadline-timeout
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
*)
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
)))))
1213 (setq B
(sb-thread:make-thread
1217 ((sb-sys:deadline-timeout
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
*)
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
)))))
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
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"
1249 (with-test (:name
(:mutex
:finalization
))
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.
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
1270 (let* ((threads (loop repeat
10
1271 collect
(sb-thread:make-thread
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.
1293 (when (zerop (mod i
100))
1300 (sleep (random 0.001)))
1301 :name
(format nil
"SLEEP-~D" i
))
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
*)
1311 :name
(format nil
"GC-~D" i
)))
1313 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
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))))
1332 (let ((one (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)
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))
1356 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1358 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1359 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1361 (handler-case (loop (funcall fun
))
1362 (serious-condition (c) (setf condition c
)))))
1363 (let ((changer (make-thread #'changer
))
1364 (test (make-thread #'test
)))
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
))
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 ()
1388 (let ((type1 (random-type 500))
1389 (type2 (random-type 500)))
1390 (let ((a (subtypep type1 type2
)))
1392 (assert (eq (subtypep type1 type2
) a
))))))
1396 (with-test (:name
(:hash-cache
:subtypep
))
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.
1401 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
)))
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.
1421 (d1 (sb-thread:make-thread
(lambda ()
1423 do
(defclass test-1
() ((a :initform
:new-a
)))
1425 #-win32
(force-output)))
1427 (d2 (sb-thread:make-thread
(lambda ()
1429 do
(defclass test-2
() ((b :initform
:new-b
)))
1431 #-win32
(force-output)))
1433 (d3 (sb-thread:make-thread
(lambda ()
1435 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1437 #-win32
(force-output)))
1439 (i (sb-thread:make-thread
(lambda ()
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
))))
1446 #-win32
(force-output)))
1448 (format t
"~%sleeping!~%")
1450 (format t
"~%stopping!~%")
1453 (sb-thread:join-thread th
)
1454 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
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
1468 (sb-thread:with-mutex
(m1)
1469 (sb-thread:wait-on-semaphore t1-can-go
)
1472 (t2 (sb-thread:make-thread
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
)
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
)
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
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
1503 (loop until
(> (get-universal-time) stop
)
1504 do
(sb-thread:with-mutex
(m1)
1505 (eval `(make-array 24))))
1507 (t2 (sb-thread:make-thread
1509 (loop until
(> (get-universal-time) stop
)
1510 do
(sb-thread:with-mutex
(m1)
1511 (eval `(make-array 24))))
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
)
1521 (list (compile nil
`(lambda (lock)
1522 (sb-thread::with-spinlock
(lock)
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
)))
1533 (funcall release lock
)
1534 (assert (eq t
(funcall with lock
)))))))
1536 (with-test (:name
:interrupt-io-unnamed-pipe
)
1540 (let ((stream (sb-sys:make-fd-stream fd
1541 :element-type
:default
1542 :serve-events nil
)))
1544 (let ((ok (handler-case
1555 (format *trace-output
* "~&=> ~A~%" ok
)
1556 (force-output *trace-output
*))))
1558 (ignore-errors (close stream
))))
1561 (multiple-value-bind (read write
)
1563 (let* ((reader (sb-thread:make-thread
(lambda () (reader read
))))
1564 (stream (sb-sys:make-fd-stream write
1566 :element-type
:default
1570 (sb-thread:interrupt-thread reader
(lambda ()
1576 (write-char #\x stream
)
1578 (sb-thread:join-thread reader
)))))
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")
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
1593 (alien-funcall (extern-alien "alloca_test" (function void
)))))))
1595 (with-test (:name
:fp-mode-inheritance-threads
)
1597 (let* ((reserved-bits #+x86
(ash #b1110000011000000
16)
1599 (fp-mode (logandc2 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))
1602 (sb-thread:join-thread
1603 (sb-thread:make-thread
1605 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
)))))))
1606 (assert (= fp-mode thread-fp-mode
)))))
1608 (sb-int:with-float-traps-masked
(:divide-by-zero
)
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
)))