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 (defun exercise-syscall (fn reference-errno
)
778 (let ((errno (sb-unix::get-errno
)))
779 (sleep (random 0.1d0
))
780 (unless (eql errno reference-errno
)
781 (format t
"Got errno: ~A (~A) instead of ~A~%"
788 ;; (nanosleep -1 0) does not fail on FreeBSD
789 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
791 (nanosleep-errno (progn
792 (sb-unix:nanosleep -
1 0)
793 (sb-unix::get-errno
)))
796 :if-does-not-exist nil
)
797 (sb-unix::get-errno
)))
801 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
802 (exercise-syscall (lambda () (open "no-such-file"
803 :if-does-not-exist nil
))
805 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
807 (princ "terminating threads")
808 (dolist (thread threads
)
809 (sb-thread:terminate-thread thread
))))
811 (format t
"~&errno test done~%")
813 (with-test (:name
:all-threads-have-abort-restart
)
815 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
816 (sb-thread:interrupt-thread
819 (assert (find-restart 'abort
)))))))
823 (format t
"~&thread startup sigmask test done~%")
825 ;; expose thread creation races by exiting quickly
826 (with-test (:name
(:no-thread-creation-race
:light
))
827 (make-join-thread (lambda ())))
829 (with-test (:name
(:no-thread-creation-race
:heavy
))
832 (loop for i below
100 collect
833 (sb-thread:make-thread
(lambda ()))))))
835 (format t
"~&creation test done~%")
837 ;; interrupt handlers are per-thread with pthreads, make sure the
838 ;; handler installed in one thread is global
839 (with-test (:name
(:global-interrupt-handler
))
842 (sb-ext:run-program
"sleep" '("1") :search t
:wait nil
))))
844 ;;;; Binding stack safety
846 (defparameter *x
* nil
)
847 (defparameter *n-gcs-requested
* 0)
848 (defparameter *n-gcs-done
* 0)
851 (defun make-something-big ()
852 (let ((x (make-string 32000)))
854 (let ((counter counter
))
855 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
858 (defmacro wait-for-gc
()
860 (incf *n-gcs-requested
*)
861 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
864 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
870 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
871 (defun exercise-binding ()
873 (let ((*x
* (make-something-big)))
875 ;; at this point the binding stack looks like this:
876 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
879 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
880 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
881 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
882 ;; unbinding but values are not).
884 (binding-pointer-delta (ash 2 (- sb-vm
:word-shift sb-vm
:n-fixnum-tag-bits
))))
885 ;; bump bsp as if a BIND had just started
886 (incf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
)
888 (decf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
))))
890 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
891 (with-test (:name
(:binding-stack-gc-safety
))
895 (push (make-kill-thread #'exercise-binding
) threads
)
896 (push (make-kill-thread (lambda ()
902 (mapc #'sb-thread
:terminate-thread threads
))))
904 (with-test (:name
:test-%thread-local-references
)
905 (let ((mysym (gensym))
908 (progv (list mysym
) '(nil)
909 (let* ((i (get-lisp-obj-address (sb-kernel:symbol-tls-index mysym
)))
910 (j (+ i sb-vm
:n-word-bytes
)))
911 (assert (eql (sap-ref-word (current-thread-sap) j
)
912 sb-vm
:no-tls-value-marker-widetag
))
913 (setf (sap-ref-lispobj (current-thread-sap) i
) fool1
914 (sap-ref-lispobj (current-thread-sap) j
) fool2
)
915 ;; assert that my pointer arithmetic worked as expected
916 (assert (eq (symbol-value mysym
) fool1
))
917 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
918 (let ((list (%thread-local-references
)))
919 (assert (memq fool1 list
))
920 (assert (not (memq fool2 list
))))
921 ;; repair the TLS entry that was corrupted by the test
922 (setf (sap-ref-word (current-thread-sap) j
)
923 sb-vm
:no-tls-value-marker-widetag
)))))
925 (format t
"~&binding test done~%")
929 (defvar *errors
* nil
)
933 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
934 (sb-debug:print-backtrace
)
937 (with-test (:name
(:unsynchronized-hash-table
)
938 ;; FIXME: This test occasionally eats out craploads
939 ;; of heap instead of expected error early. Not 100%
940 ;; sure if it would finish as expected, but since it
941 ;; hits swap on my system I'm not likely to find out
942 ;; soon. Disabling for now. -- nikodemus
944 ;; We expect a (probable) error here: parellel readers and writers
945 ;; on a hash-table are not expected to work -- but we also don't
946 ;; expect this to corrupt the image.
947 (let* ((hash (make-hash-table))
949 (threads (list (make-kill-thread
952 (handler-bind ((serious-condition 'oops
))
954 ;;(princ "1") (force-output)
955 (setf (gethash (random 100) hash
) 'h
)))))
960 (handler-bind ((serious-condition 'oops
))
962 ;;(princ "2") (force-output)
963 (remhash (random 100) hash
)))))
968 (handler-bind ((serious-condition 'oops
))
971 (sb-ext:gc
:full t
)))))
972 :name
"collector"))))
975 (mapc #'sb-thread
:terminate-thread threads
))))
977 (format t
"~&unsynchronized hash table test done~%")
979 (with-test (:name
(:synchronized-hash-table
))
980 (let* ((hash (make-hash-table :synchronized t
))
982 (threads (list (make-join-thread
985 (handler-bind ((serious-condition 'oops
))
987 ;;(princ "1") (force-output)
988 (setf (gethash (random 100) hash
) 'h
)))))
993 (handler-bind ((serious-condition 'oops
))
995 ;;(princ "2") (force-output)
996 (remhash (random 100) hash
)))))
1001 (handler-bind ((serious-condition 'oops
))
1003 (sleep (random 1.0))
1004 (sb-ext:gc
:full t
)))))
1005 :name
"collector"))))
1008 (mapc #'sb-thread
:terminate-thread threads
))
1009 (assert (not *errors
*))))
1011 (format t
"~&synchronized hash table test done~%")
1013 (with-test (:name
(:hash-table-parallel-readers
))
1014 (let ((hash (make-hash-table))
1017 do
(setf (gethash (random 100) hash
) 'xxx
))
1018 (let ((threads (list (make-kill-thread
1021 (handler-bind ((serious-condition 'oops
))
1023 until
(eq t
(gethash (random 100) hash
))))))
1028 (handler-bind ((serious-condition 'oops
))
1030 until
(eq t
(gethash (random 100) hash
))))))
1035 (handler-bind ((serious-condition 'oops
))
1037 until
(eq t
(gethash (random 100) hash
))))))
1042 (handler-bind ((serious-condition 'oops
))
1044 (sleep (random 1.0))
1045 (sb-ext:gc
:full t
)))))
1046 :name
"collector"))))
1049 (mapc #'sb-thread
:terminate-thread threads
))
1050 (assert (not *errors
*)))))
1052 (format t
"~&multiple reader hash table test done~%")
1054 (with-test (:name
(:hash-table-single-accessor-parallel-gc
))
1055 (let ((hash (make-hash-table))
1057 (let ((threads (list (make-kill-thread
1059 (handler-bind ((serious-condition 'oops
))
1061 (let ((n (random 100)))
1062 (if (gethash n hash
)
1064 (setf (gethash n hash
) 'h
))))))
1068 (handler-bind ((serious-condition 'oops
))
1070 (sleep (random 1.0))
1071 (sb-ext:gc
:full t
))))
1072 :name
"collector"))))
1075 (mapc #'sb-thread
:terminate-thread threads
))
1076 (assert (not *errors
*)))))
1078 (format t
"~&single accessor hash table test~%")
1080 #|
;; a cll post from eric marsden
1082 |
(setq *debugger-hook
*
1083 |
(lambda (condition old-debugger-hook
)
1084 |
(debug:backtrace
10)
1085 |
(unix:unix-exit
2)))
1086 |
#+live-dangerously
1087 |
(mp::start-sigalrm-yield
)
1088 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
1089 |
(mp:make-process
#'roomy
)
1090 |
(mp:make-process
#'roomy
)))
1093 (with-test (:name
(:condition-variable
:notify-multiple
))
1094 (flet ((tester (notify-fun)
1095 (let ((queue (make-waitqueue :name
"queue"))
1096 (lock (make-mutex :name
"lock"))
1101 (format t
"condition-wait ~a~%" x
)
1103 (condition-wait queue lock
)
1104 (format t
"woke up ~a~%" x
)
1107 (let ((threads (loop for x from
1 to
10
1110 (make-kill-thread (lambda ()
1114 (funcall notify-fun queue
))
1116 (mapcar #'terminate-thread threads
)
1117 ;; Check that all threads woke up at least once
1118 (assert (= (length (remove-duplicates data
)) 10)))))))
1119 (tester (lambda (queue)
1120 (format t
"~&(condition-notify queue 10)~%")
1122 (condition-notify queue
10)))
1123 (tester (lambda (queue)
1124 (format t
"~&(condition-broadcast queue)~%")
1126 (condition-broadcast queue
)))))
1128 (format t
"waitqueue wakeup tests done~%")
1130 ;;; Make sure that a deadline handler is not invoked twice in a row in
1131 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1133 (with-test (:name
(:condition-wait
:deadlines
:LP-512914
))
1134 (let ((n 2) ; was empirically enough to trigger the bug
1135 (mutex (sb-thread:make-mutex
))
1136 (waitq (sb-thread:make-waitqueue
))
1138 (deadline-handler-run-twice? nil
))
1141 (sb-thread:make-thread
1144 ((sb-sys:deadline-timeout
1145 (let ((already? nil
))
1148 (setq deadline-handler-run-twice? t
))
1151 (sb-thread:condition-broadcast waitq
)
1152 (sb-sys:defer-deadline
10.0 c
)))))
1153 (sb-sys:with-deadline
(:seconds
0.1)
1154 (sb-thread:with-mutex
(mutex)
1155 (sb-thread:condition-wait waitq mutex
))))))))
1156 (push child threads
)))
1157 (mapc #'sb-thread
:join-thread threads
)
1158 (assert (not deadline-handler-run-twice?
))))
1160 (with-test (:name
(:condition-wait
:signal-deadline-with-interrupts-enabled
))
1161 (let ((mutex (sb-thread:make-mutex
))
1162 (waitq (sb-thread:make-waitqueue
))
1165 (A-interrupts-enabled?
:unknown
)
1166 (B-interrupts-enabled?
:unknown
)
1169 ;; W.L.O.G., we assume that A is executed first...
1170 (setq A
(sb-thread:make-thread
1173 ((sb-sys:deadline-timeout
1175 ;; We came here through the call to DECODE-TIMEOUT
1176 ;; in CONDITION-WAIT; hence both here are supposed
1177 ;; to evaluate to T.
1178 (setq A-holds?
(sb-thread:holding-mutex-p mutex
))
1179 (setq A-interrupts-enabled?
1180 sb-sys
:*interrupts-enabled
*)
1182 (sb-thread:condition-broadcast waitq
)
1183 (sb-sys:defer-deadline
10.0 c
))))
1184 (sb-sys:with-deadline
(:seconds
0.1)
1185 (sb-thread:with-mutex
(mutex)
1186 (sb-thread:condition-wait waitq mutex
)))))
1188 (setq B
(sb-thread:make-thread
1192 ((sb-sys:deadline-timeout
1194 ;; We came here through the call to DECODE-TIMEOUT
1195 ;; in CONDITION-WAIT (contended case of
1196 ;; reaquiring the mutex) - so the former will
1197 ;; be NIL, but interrupts should still be enabled.
1198 (setq B-holds?
(sb-thread:holding-mutex-p mutex
))
1199 (setq B-interrupts-enabled?
1200 sb-sys
:*interrupts-enabled
*)
1202 (sb-thread:condition-broadcast waitq
)
1203 (sb-sys:defer-deadline
10.0 c
))))
1204 (sb-sys:with-deadline
(:seconds
0.1)
1205 (sb-thread:with-mutex
(mutex)
1206 (sb-thread:condition-wait waitq mutex
)))))
1208 (sb-thread:join-thread A
)
1209 (sb-thread:join-thread B
)
1210 (let ((A-result (list A-holds? A-interrupts-enabled?
))
1211 (B-result (list B-holds? B-interrupts-enabled?
)))
1212 ;; We also check some subtle behaviour w.r.t. whether a deadline
1213 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1214 ;; probably very internal behaviour (so user should not depend
1215 ;; on it) -- I added the testing here just to manifest current
1217 (cond ((equal A-result
'(t t
)) (assert (equal B-result
'(nil t
))))
1218 ((equal B-result
'(t t
)) (assert (equal A-result
'(nil t
))))
1220 (error "Failure: fell through wit A: ~S, B: ~S"
1224 (with-test (:name
(:mutex
:finalization
))
1227 (setf a
(make-mutex)))))
1229 (format t
"mutex finalization test done~%")
1231 ;; You have to shoehorn this arbitrary sexpr into a feature expression
1232 ;; to have the test summary show that a test was disabled.
1234 (unless (eql (extern-alien "verify_gens" int
)
1235 (1+ sb-vm
:+highest-normal-generation
+))
1236 (pushnew :verify-gens
*features
*))
1238 (with-test (:name
:backtrace
:skipped-on
':verify-gens
)
1239 ;; Printing backtraces from several threads at once used to hang the
1240 ;; whole SBCL process (discovered by accident due to a timer.impure
1241 ;; test misbehaving). The cause was that packages weren't even
1242 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1243 ;; backtraces a loot of symbol lookups need to be done due to
1245 (let* ((threads (loop repeat
10
1246 collect
(sb-thread:make-thread
1249 (with-output-to-string (*debug-io
*)
1250 (sb-debug:print-backtrace
:count
10))))))))
1251 (wait-for-threads threads
)))
1253 (format t
"backtrace test done~%")
1255 (format t
"~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1257 (with-test (:name
:gc-deadlock
)
1258 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1259 ;; GC due to *all-threads-lock* and session lock. On earlier
1260 ;; versions and at least on one specific box this test is good enough
1261 ;; to catch that typically well before the 1500th iteration.
1268 (when (zerop (mod i
100))
1275 (sleep (random 0.001)))
1276 :name
(format nil
"SLEEP-~D" i
))
1279 ;; KLUDGE: what we are doing here is explicit,
1280 ;; but the same can happen because of a regular
1281 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1282 ;; session functions.
1283 (sb-thread::with-all-threads-lock
1284 (sb-thread::with-session-lock
(sb-thread::*session
*)
1286 :name
(format nil
"GC-~D" i
)))
1288 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
1292 (format t
"~&gc deadlock test done~%")
1294 (let ((count (make-array 8 :initial-element
0)))
1295 (defun closure-one ()
1296 (declare (optimize safety
))
1297 (values (incf (aref count
0)) (incf (aref count
1))
1298 (incf (aref count
2)) (incf (aref count
3))
1299 (incf (aref count
4)) (incf (aref count
5))
1300 (incf (aref count
6)) (incf (aref count
7))))
1301 (defun no-optimizing-away-closure-one ()
1302 (setf count
(make-array 8 :initial-element
0))))
1307 (let ((one (make-box))
1310 (defun closure-two ()
1311 (declare (optimize safety
))
1312 (values (incf (box-count one
)) (incf (box-count two
)) (incf (box-count three
))))
1313 (defun no-optimizing-away-closure-two ()
1314 (setf one
(make-box)
1318 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1319 ;;; sometimes run out of memory) in the following test. For developers
1320 ;;; interested in debugging this combination of features, it might be
1321 ;;; fruitful to concentrate their efforts around this test...
1323 (with-test (:name
(:funcallable-instances
)
1324 :skipped-on
'(and :sb-safepoint
1325 (not :c-stack-is-control-stack
)))
1326 ;; the funcallable-instance implementation used not to be threadsafe
1327 ;; against setting the funcallable-instance function to a closure
1328 ;; (because the code and lexenv were set separately).
1329 (let ((fun (sb-kernel:%make-funcallable-instance
0))
1331 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1333 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1334 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1336 (handler-case (loop (funcall fun
))
1337 (serious-condition (c) (setf condition c
)))))
1338 (let ((changer (make-thread #'changer
))
1339 (test (make-thread #'test
)))
1342 ;; The two closures above are fairly carefully crafted
1343 ;; so that if given the wrong lexenv they will tend to
1344 ;; do some serious damage, but it is of course difficult
1345 ;; to predict where the various bits and pieces will be
1346 ;; allocated. Five seconds failed fairly reliably on
1347 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1348 (sb-ext:with-timeout
5
1349 (wait-for-threads (list test
)))
1350 (error "~@<test thread got condition:~2I~_~A~@:>" condition
))
1352 (terminate-thread changer
)
1353 (terminate-thread test
)
1354 (wait-for-threads (list changer test
))))))))
1356 (format t
"~&funcallable-instance test done~%")
1358 (defun random-type (n)
1359 `(integer ,(random n
) ,(+ n
(random n
))))
1361 (defun subtypep-hash-cache-test ()
1363 (let ((type1 (random-type 500))
1364 (type2 (random-type 500)))
1365 (let ((a (subtypep type1 type2
)))
1367 (assert (eq (subtypep type1 type2
) a
))))))
1371 (with-test (:name
(:hash-cache
:subtypep
))
1373 ;; this didn't "reliably fail" with a small number of threads.
1374 ;; 30 is a compromise between running time and confidence in the result.
1376 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
)))
1379 ;;;; BLACK BOX TESTS
1381 (in-package :cl-user
)
1382 (use-package :test-util
)
1383 (use-package "ASSERTOID")
1385 (format t
"parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1386 (with-test (:name
:parallel-defclass
)
1387 (defclass test-1
() ((a :initform
:orig-a
)))
1388 (defclass test-2
() ((b :initform
:orig-b
)))
1389 (defclass test-3
(test-1 test-2
) ((c :initform
:orig-c
)))
1390 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1391 ;; calls disabled in the folloving code. (As seen on a Server 2012
1392 ;; installation.) Clearly, this sort of workaround in a test is
1393 ;; cheating, and might be hiding the underlying bug that the test is
1394 ;; exposing. Let's review this later.
1396 (d1 (sb-thread:make-thread
(lambda ()
1398 do
(defclass test-1
() ((a :initform
:new-a
)))
1400 #-win32
(force-output)))
1402 (d2 (sb-thread:make-thread
(lambda ()
1404 do
(defclass test-2
() ((b :initform
:new-b
)))
1406 #-win32
(force-output)))
1408 (d3 (sb-thread:make-thread
(lambda ()
1410 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1412 #-win32
(force-output)))
1414 (i (sb-thread:make-thread
(lambda ()
1416 do
(let ((i (make-instance 'test-3
)))
1417 (assert (member (slot-value i
'a
) '(:orig-a
:new-a
)))
1418 (assert (member (slot-value i
'b
) '(:orig-b
:new-b
)))
1419 (assert (member (slot-value i
'c
) '(:orig-c
:new-c
))))
1421 #-win32
(force-output)))
1423 (format t
"~%sleeping!~%")
1425 (format t
"~%stopping!~%")
1428 (sb-thread:join-thread th
)
1429 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
1432 (format t
"parallel defclass test done~%")
1434 (with-test (:name
(:deadlock-detection
:interrupts
) :fails-on
:win32
)
1435 #+win32
;be more explicit than just :skipped-on
1436 (error "not attempting, because of deadlock error in background thread")
1437 (let* ((m1 (sb-thread:make-mutex
:name
"M1"))
1438 (m2 (sb-thread:make-mutex
:name
"M2"))
1439 (t1-can-go (sb-thread:make-semaphore
:name
"T1 can go"))
1440 (t2-can-go (sb-thread:make-semaphore
:name
"T2 can go"))
1441 (t1 (sb-thread:make-thread
1443 (sb-thread:with-mutex
(m1)
1444 (sb-thread:wait-on-semaphore t1-can-go
)
1447 (t2 (sb-thread:make-thread
1449 (sb-ext:wait-for
(eq t1
(sb-thread:mutex-owner m1
)))
1450 (sb-thread:with-mutex
(m1 :wait-p t
)
1451 (sb-thread:wait-on-semaphore t2-can-go
)
1454 (sb-ext:wait-for
(eq m1
(sb-thread::thread-waiting-for t2
)))
1455 (sb-thread:interrupt-thread t2
(lambda ()
1456 (sb-thread:with-mutex
(m2 :wait-p t
)
1458 (eq m2
(sb-thread::thread-waiting-for t1
)))
1459 (sb-thread:signal-semaphore t2-can-go
))))
1460 (sb-ext:wait-for
(eq t2
(sb-thread:mutex-owner m2
)))
1461 (sb-thread:interrupt-thread t1
(lambda ()
1462 (sb-thread:with-mutex
(m2 :wait-p t
)
1463 (sb-thread:signal-semaphore t1-can-go
))))
1464 ;; both threads should finish without a deadlock or deadlock
1466 (let ((res (list (sb-thread:join-thread t1
)
1467 (sb-thread:join-thread t2
))))
1468 (assert (equal '(:ok1
:ok2
) res
)))))
1470 (with-test (:name
(:deadlock-detection
:gc
))
1471 ;; To semi-reliably trigger the error (in SBCL's where)
1472 ;; it was present you had to run this for > 30 seconds,
1473 ;; but that's a bit long for a single test.
1474 (let* ((stop (+ 5 (get-universal-time)))
1475 (m1 (sb-thread:make-mutex
:name
"m1"))
1476 (t1 (sb-thread:make-thread
1478 (loop until
(> (get-universal-time) stop
)
1479 do
(sb-thread:with-mutex
(m1)
1480 (eval `(make-array 24))))
1482 (t2 (sb-thread:make-thread
1484 (loop until
(> (get-universal-time) stop
)
1485 do
(sb-thread:with-mutex
(m1)
1486 (eval `(make-array 24))))
1488 (let ((res (list (sb-thread:join-thread t1
)
1489 (sb-thread:join-thread t2
))))
1490 (assert (equal '(:ok
:ok
) res
)))))
1492 (with-test (:name
:spinlock-api
)
1493 (handler-bind ((warning #'error
))
1494 (destructuring-bind (with make get release
)
1496 (list (compile nil
`(lambda (lock)
1497 (sb-thread::with-spinlock
(lock)
1499 (compile nil
`(lambda ()
1500 (sb-thread::make-spinlock
:name
"foo")))
1501 (compile nil
`(lambda (lock)
1502 (sb-thread::get-spinlock lock
)))
1503 (compile nil
`(lambda (lock)
1504 (sb-thread::release-spinlock lock
))))
1505 early-deprecation-warning
4)
1506 (let ((lock (funcall make
)))
1508 (funcall release lock
)
1509 (assert (eq t
(funcall with lock
)))))))
1511 (with-test (:name
:interrupt-io-unnamed-pipe
)
1515 (let ((stream (sb-sys:make-fd-stream fd
1516 :element-type
:default
1517 :serve-events nil
)))
1519 (let ((ok (handler-case
1530 (format *trace-output
* "~&=> ~A~%" ok
)
1531 (force-output *trace-output
*))))
1533 (ignore-errors (close stream
))))
1536 (multiple-value-bind (read write
)
1538 (let* ((reader (sb-thread:make-thread
(lambda () (reader read
))))
1539 (stream (sb-sys:make-fd-stream write
1541 :element-type
:default
1545 (sb-thread:interrupt-thread reader
(lambda ()
1551 (write-char #\x stream
)
1553 (sb-thread:join-thread reader
)))))
1555 (assert (eq result
:ok
))))
1557 (with-test (:name
:thread-alloca
)
1558 (sb-ext:run-program
"sh"
1559 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1560 "alloca.c" "-o" "alloca.so")
1562 (load-shared-object (truename "alloca.so"))
1564 (alien-funcall (extern-alien "alloca_test" (function void
)))
1565 (sb-thread:join-thread
1566 (sb-thread:make-thread
1568 (alien-funcall (extern-alien "alloca_test" (function void
)))))))
1570 (with-test (:name
:fp-mode-inheritance-threads
)
1572 (let* ((reserved-bits #+x86
(ash #b1110000011000000
16)
1574 (fp-mode (logandc2 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))
1577 (sb-thread:join-thread
1578 (sb-thread:make-thread
1580 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
)))))))
1581 (assert (= fp-mode thread-fp-mode
)))))
1583 (sb-int:with-float-traps-masked
(:divide-by-zero
)
1585 (setf (sb-vm:floating-point-modes
)
1586 (dpb sb-vm
:float-divide-by-zero-trap-bit
1587 sb-vm
::float-traps-byte
1588 (sb-vm:floating-point-modes
)))