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 (in-package "SB-THREAD")
17 (use-package :test-util
)
18 (use-package "ASSERTOID")
20 (setf sb-unix
::*on-dangerous-wait
* :error
)
22 (defun wait-for-threads (threads)
23 (mapc (lambda (thread) (sb-thread:join-thread thread
:default nil
)) threads
)
24 (assert (not (some #'sb-thread
:thread-alive-p threads
))))
26 (with-test (:name
(:threads
:trivia
))
27 (assert (eql 1 (length (list-all-threads))))
29 (assert (eq *current-thread
*
30 (find (thread-name *current-thread
*) (list-all-threads)
31 :key
#'thread-name
:test
#'equal
)))
33 (assert (thread-alive-p *current-thread
*)))
35 (with-test (:name
(:with-mutex
:basics
))
36 (let ((mutex (make-mutex)))
40 (sb-alien:define-alien-routine
"check_deferrables_blocked_or_lose"
42 (where sb-alien
:unsigned-long
))
43 (sb-alien:define-alien-routine
"check_deferrables_unblocked_or_lose"
45 (where sb-alien
:unsigned-long
))
47 (with-test (:name
(:interrupt-thread
:basics
:no-unwinding
))
49 (interrupt-thread *current-thread
* (lambda () (setq a
1)))
52 (with-test (:name
(:interrupt-thread
:deferrables-blocked
))
53 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
55 (check-deferrables-blocked-or-lose 0))))
57 (with-test (:name
(:interrupt-thread
:deferrables-unblocked
))
58 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
61 (check-deferrables-unblocked-or-lose 0)))))
63 (with-test (:name
(:interrupt-thread
:nlx
))
65 (sb-thread:interrupt-thread sb-thread
:*current-thread
*
67 (check-deferrables-blocked-or-lose 0)
69 (check-deferrables-unblocked-or-lose 0))
71 #-sb-thread
(sb-ext:exit
:code
104)
73 ;;;; Now the real tests...
75 (with-test (:name
(:with-mutex
:timeout
))
76 (let ((m (make-mutex)))
78 (assert (null (join-thread (make-thread
80 (with-mutex (m :timeout
0.1)
82 (assert (join-thread (make-thread
84 (with-mutex (m :timeout
0.1)
87 (with-test (:name
(:interrupt-thread
:deferrables-unblocked-by-lock
))
88 (let ((lock (sb-thread::make-mutex
))
89 (thread (make-join-thread (lambda ()
91 (sb-thread::grab-mutex lock
)
92 (sb-thread:interrupt-thread thread
94 (check-deferrables-blocked-or-lose 0)
95 (sb-thread::grab-mutex lock
)
96 (check-deferrables-unblocked-or-lose 0)
97 (sb-thread:abort-thread
)))
99 (sb-thread::release-mutex lock
)))
103 (defmacro defincf
(name accessor
&rest args
)
105 (let* ((old (,accessor x
,@args
))
107 (loop until
(eq old
(sb-ext:compare-and-swap
(,accessor x
,@args
) old new
))
108 do
(setf old
(,accessor x
,@args
)
112 (defstruct cas-struct
(slot 0))
114 (defincf incf-car car
)
115 (defincf incf-cdr cdr
)
116 (defincf incf-slot cas-struct-slot
)
117 (defincf incf-symbol-value symbol-value
)
118 (defincf incf-svref
/1 svref
1)
119 (defincf incf-svref
/0 svref
0)
121 (defmacro def-test-cas
(name init incf op
)
122 `(with-test (:name
,name
)
129 collect
(sb-thread:make-thread
132 do
(sb-thread:thread-yield
))
133 (loop repeat n do
(,incf x
)))))))
136 (sb-thread:join-thread th
))
137 (assert (= (,op x
) (* 10 n
))))))
140 (def-test-cas test-cas-car
(cons 0 nil
) incf-car car
)
141 (def-test-cas test-cas-cdr
(cons nil
0) incf-cdr cdr
)
142 (def-test-cas test-cas-slot
(make-cas-struct) incf-slot cas-struct-slot
)
143 (def-test-cas test-cas-value
(let ((x '.x.
))
146 incf-symbol-value symbol-value
)
147 (def-test-cas test-cas-svref
/0 (vector 0 nil
) incf-svref
/0 (lambda (x)
149 (def-test-cas test-cas-svref
/1 (vector nil
0) incf-svref
/1 (lambda (x)
151 (format t
"~&compare-and-swap tests done~%")
153 (with-test (:name
(:threads
:more-trivia
)))
154 (let ((old-threads (list-all-threads))
155 (thread (make-thread (lambda ()
156 (assert (find *current-thread
* *all-threads
*))
158 (new-threads (list-all-threads)))
159 (assert (thread-alive-p thread
))
160 (assert (eq thread
(first new-threads
)))
161 (assert (= (1+ (length old-threads
)) (length new-threads
)))
163 (assert (not (thread-alive-p thread
))))
165 (with-test (:name
(:join-thread
:nlx
:default
))
166 (let ((sym (gensym)))
167 (assert (eq sym
(join-thread (make-thread (lambda () (sb-thread:abort-thread
)))
170 (with-test (:name
(:join-thread
:nlx
:error
))
171 (raises-error?
(join-thread (make-thread (lambda () (sb-thread:abort-thread
))))
174 (with-test (:name
(:join-thread
:multiple-values
))
175 (assert (equal '(1 2 3)
177 (join-thread (make-thread (lambda () (values 1 2 3))))))))
179 ;;; We had appalling scaling properties for a while. Make sure they
181 (defun scaling-test (function &optional
(nthreads 5))
182 "Execute FUNCTION with NTHREADS lurking to slow it down."
183 (let ((queue (sb-thread:make-waitqueue
))
184 (mutex (sb-thread:make-mutex
)))
185 ;; Start NTHREADS idle threads.
186 (dotimes (i nthreads
)
187 (make-join-thread (lambda ()
189 (sb-thread:condition-wait queue mutex
))
190 (sb-thread:abort-thread
))))
191 (let ((start-time (get-internal-run-time)))
193 (prog1 (- (get-internal-run-time) start-time
)
194 (sb-thread:condition-broadcast queue
)))))
196 "A function that does work with the CPU."
197 (if (zerop n
) 1 (* n
(fact (1- n
)))))
199 (with-test (:name
:lurking-threads
)
200 (let ((work (lambda () (fact 15000))))
201 (let ((zero (scaling-test work
0))
202 (four (scaling-test work
4)))
203 ;; a slightly weak assertion, but good enough for starters.
204 (assert (< four
(* 1.5 zero
))))))
206 ;;; For one of the interupt-thread tests, we want a foreign function
207 ;;; that does not make syscalls
211 (with-open-file (o "threads-foreign.c" :direction
:output
:if-exists
:supersede
)
212 (format o
"void loop_forever() { while(1) ; }~%"))
213 (sb-ext:run-program
"/bin/sh"
214 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
215 "-o" "threads-foreign.so" "threads-foreign.c"))
216 (sb-alien:load-shared-object
(truename "threads-foreign.so"))
217 (sb-alien:define-alien-routine loop-forever sb-alien
:void
)
218 (delete-file "threads-foreign.c"))
220 ;;; elementary "can we get a lock and release it again"
221 (with-test (:name
(:mutex
:basics
))
222 (let ((l (make-mutex :name
"foo"))
223 (p *current-thread
*))
224 (assert (eql (mutex-value l
) nil
) nil
"1")
225 (sb-thread:grab-mutex l
)
226 (assert (eql (mutex-value l
) p
) nil
"3")
227 (sb-thread:release-mutex l
)
228 (assert (eql (mutex-value l
) nil
) nil
"5")))
230 (with-test (:name
(:with-recursive-lock
:basics
))
231 (labels ((ours-p (value)
232 (eq *current-thread
* value
)))
233 (let ((l (make-mutex :name
"rec")))
234 (assert (eql (mutex-value l
) nil
) nil
"1")
235 (sb-thread:with-recursive-lock
(l)
236 (assert (ours-p (mutex-value l
)) nil
"3")
237 (sb-thread:with-recursive-lock
(l)
238 (assert (ours-p (mutex-value l
)) nil
"4"))
239 (assert (ours-p (mutex-value l
)) nil
"5"))
240 (assert (eql (mutex-value l
) nil
) nil
"6"))))
242 (with-test (:name
(:with-recursive-lock
:wait-p
))
243 (let ((m (make-mutex)))
245 (assert (null (join-thread (make-thread
247 (with-recursive-lock (m :wait-p nil
)
249 (assert (join-thread (make-thread
251 (with-recursive-lock (m :wait-p nil
)
254 (with-test (:name
(:with-recursive-lock
:wait-p
:recursive
))
255 (let ((m (make-mutex)))
256 (assert (join-thread (make-thread
258 (with-recursive-lock (m :wait-p nil
)
259 (with-recursive-lock (m :wait-p nil
)
262 (with-test (:name
(:with-recursive-lock
:timeout
))
263 (let ((m (make-mutex)))
265 (assert (null (join-thread (make-thread
267 (with-recursive-lock (m :timeout
0.1)
269 (assert (join-thread (make-thread
271 (with-recursive-lock (m :timeout
0.1)
274 (with-test (:name
(:with-recursive-lock
:timeout
:recursive
))
275 (let ((m (make-mutex)))
276 (assert (join-thread (make-thread
278 (with-recursive-lock (m :timeout
0.1)
279 (with-recursive-lock (m :timeout
0.1)
282 (with-test (:name
(:mutex
:nesting-mutex-and-recursive-lock
))
283 (let ((l (make-mutex :name
"a mutex")))
285 (with-recursive-lock (l)))))
287 ;; test that SLEEP actually sleeps for at least the given time, even
288 ;; if interrupted by another thread exiting/a gc/anything
289 (with-test (:name
(:sleep
:continue-sleeping-after-interrupt
))
290 (let ((start-time (get-universal-time)))
291 (make-join-thread (lambda () (sleep 1) (sb-ext:gc
:full t
)))
293 (assert (>= (get-universal-time) (+ 5 start-time
)))))
296 (with-test (:name
(:condition-wait
:basics-1
))
297 (let ((queue (make-waitqueue :name
"queue"))
298 (lock (make-mutex :name
"lock"))
300 (labels ((in-new-thread ()
302 (assert (eql (mutex-value lock
) *current-thread
*))
303 (format t
"~A got mutex~%" *current-thread
*)
304 ;; now drop it and sleep
305 (condition-wait queue lock
)
306 ;; after waking we should have the lock again
307 (assert (eql (mutex-value lock
) *current-thread
*))
310 (make-join-thread #'in-new-thread
)
311 (sleep 2) ; give it a chance to start
312 ;; check the lock is free while it's asleep
313 (format t
"parent thread ~A~%" *current-thread
*)
314 (assert (eql (mutex-value lock
) nil
))
317 (condition-notify queue
))
320 (with-test (:name
(:condition-wait
:basics-2
))
321 (let ((queue (make-waitqueue :name
"queue"))
322 (lock (make-mutex :name
"lock")))
323 (labels ((ours-p (value)
324 (eq *current-thread
* value
))
326 (with-recursive-lock (lock)
327 (assert (ours-p (mutex-value lock
)))
328 (format t
"~A got mutex~%" (mutex-value lock
))
329 ;; now drop it and sleep
330 (condition-wait queue lock
)
331 ;; after waking we should have the lock again
332 (format t
"woken, ~A got mutex~%" (mutex-value lock
))
333 (assert (ours-p (mutex-value lock
))))))
334 (make-join-thread #'in-new-thread
)
335 (sleep 2) ; give it a chance to start
336 ;; check the lock is free while it's asleep
337 (format t
"parent thread ~A~%" *current-thread
*)
338 (assert (eql (mutex-value lock
) nil
))
339 (with-recursive-lock (lock)
340 (condition-notify queue
))
343 (with-test (:name
(:mutex
:contention
))
344 (let ((mutex (make-mutex :name
"contended")))
346 (let ((me *current-thread
*))
350 (assert (eql (mutex-value mutex
) me
)))
351 (assert (not (eql (mutex-value mutex
) me
))))
352 (format t
"done ~A~%" *current-thread
*))))
353 (let ((kid1 (make-thread #'run
))
354 (kid2 (make-thread #'run
)))
355 (format t
"contention ~A ~A~%" kid1 kid2
)
356 (wait-for-threads (list kid1 kid2
))))))
360 (with-test (:name
(:grab-mutex
:waitp nil
))
361 (let ((m (make-mutex)))
363 (assert (null (join-thread (make-thread
365 (grab-mutex m
:waitp nil
)))))))))
367 (with-test (:name
(:grab-mutex
:timeout
:acquisition-fail
))
368 (let ((m (make-mutex))
369 (w (make-semaphore)))
371 (let ((th (make-thread
374 (grab-mutex m
:timeout
0.1)
375 (signal-semaphore w
))))))
376 ;; Wait for it to -- otherwise the detect the deadlock chain
378 (wait-on-semaphore w
)
379 (assert (null (join-thread th
)))))))
381 (with-test (:name
(:grab-mutex
:timeout
:acquisition-success
))
382 (let ((m (make-mutex))
385 (setq child
(make-thread #'(lambda () (grab-mutex m
:timeout
1.0))))
387 (assert (eq (join-thread child
) 't
))))
389 (with-test (:name
(:grab-mutex
:timeout
+deadline
))
390 (let ((m (make-mutex))
391 (w (make-semaphore)))
393 (let ((th (make-thread #'(lambda ()
394 (sb-sys:with-deadline
(:seconds
0.0)
396 (grab-mutex m
:timeout
0.0)
397 (sb-sys:deadline-timeout
()
400 (wait-on-semaphore w
)
401 (assert (eq (join-thread th
) :deadline
))))))
403 (with-test (:name
(:grab-mutex
:waitp
+deadline
))
404 (let ((m (make-mutex)))
406 (assert (eq (join-thread
407 (make-thread #'(lambda ()
408 (sb-sys:with-deadline
(:seconds
0.0)
410 (grab-mutex m
:waitp nil
)
411 (sb-sys:deadline-timeout
()
417 (defmacro raises-timeout-p
(&body body
)
418 `(handler-case (progn (progn ,@body
) nil
)
419 (sb-ext:timeout
() t
)))
421 (with-test (:name
(:semaphore
:wait-forever
))
422 (let ((sem (make-semaphore :count
0)))
423 (assert (raises-timeout-p
424 (sb-ext:with-timeout
0.1
425 (wait-on-semaphore sem
))))))
427 (with-test (:name
(:semaphore
:initial-count
))
428 (let ((sem (make-semaphore :count
1)))
429 (sb-ext:with-timeout
0.1
430 (wait-on-semaphore sem
))))
432 (with-test (:name
(:semaphore
:wait-then-signal
))
433 (let ((sem (make-semaphore))
435 (make-join-thread (lambda ()
438 (signal-semaphore sem
)))
439 (wait-on-semaphore sem
)
440 (assert signalled-p
)))
442 (with-test (:name
(:semaphore
:signal-then-wait
))
443 (let ((sem (make-semaphore))
445 (make-join-thread (lambda ()
446 (signal-semaphore sem
)
447 (setq signalled-p t
)))
448 (loop until signalled-p
)
449 (wait-on-semaphore sem
)
450 (assert signalled-p
)))
452 (defun test-semaphore-multiple-signals (wait-on-semaphore)
453 (let* ((sem (make-semaphore :count
5))
454 (threads (loop repeat
20 collecting
455 (make-join-thread (lambda ()
456 (funcall wait-on-semaphore sem
))))))
457 (flet ((count-live-threads ()
458 (count-if #'thread-alive-p threads
)))
460 (assert (= 15 (count-live-threads)))
461 (signal-semaphore sem
10)
463 (assert (= 5 (count-live-threads)))
464 (signal-semaphore sem
3)
466 (assert (= 2 (count-live-threads)))
467 (signal-semaphore sem
4)
469 (assert (= 0 (count-live-threads))))))
471 (with-test (:name
(:semaphore
:multiple-signals
))
472 (test-semaphore-multiple-signals #'wait-on-semaphore
))
474 (with-test (:name
(:try-semaphore
:trivial-fail
))
475 (assert (eq (try-semaphore (make-semaphore :count
0)) 'nil
)))
477 (with-test (:name
(:try-semaphore
:trivial-success
))
478 (let ((sem (make-semaphore :count
1)))
479 (assert (try-semaphore sem
))
480 (assert (zerop (semaphore-count sem
)))))
482 (with-test (:name
(:try-semaphore
:trivial-fail
:n
>1))
483 (assert (eq (try-semaphore (make-semaphore :count
1) 2) 'nil
)))
485 (with-test (:name
(:try-semaphore
:trivial-success
:n
>1))
486 (let ((sem (make-semaphore :count
10)))
487 (assert (try-semaphore sem
5))
488 (assert (try-semaphore sem
5))
489 (assert (zerop (semaphore-count sem
)))))
491 (with-test (:name
(:try-semaphore
:emulate-wait-on-semaphore
))
492 (flet ((busy-wait-on-semaphore (sem)
493 (loop until
(try-semaphore sem
) do
(sleep 0.001))))
494 (test-semaphore-multiple-signals #'busy-wait-on-semaphore
)))
496 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
497 ;;; semaphore in a bad state.
498 (with-test (:name
(:try-semaphore
:interrupt-safe
))
499 (flet ((make-threads (count fn
)
500 (loop repeat count collect
(make-thread fn
)))
501 (kill-thread (thread)
502 (when (thread-alive-p thread
)
503 (ignore-errors (terminate-thread thread
))))
504 (count-live-threads (threads)
505 (count-if #'thread-alive-p threads
)))
506 ;; WAITERS will already be waiting on the semaphore while
507 ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
508 ;; semaphore, and MORE-WAITERS are new threads trying to wait on
509 ;; the semaphore during the interruption-fire.
510 (let* ((sem (make-semaphore :count
100))
511 (waiters (make-threads 20 #'(lambda ()
512 (wait-on-semaphore sem
))))
513 (triers (make-threads 40 #'(lambda ()
514 (sleep (random 0.01))
515 (try-semaphore sem
(1+ (random 5))))))
518 do
(kill-thread (nth (random 40) triers
))
519 collect
(make-thread #'(lambda () (wait-on-semaphore sem
)))
520 do
(kill-thread (nth (random 40) triers
)))))
522 ;; Now ensure that the waiting threads will all be waked up,
523 ;; i.e. that the semaphore is still working.
524 (loop repeat
(+ (count-live-threads waiters
)
525 (count-live-threads more-waiters
))
526 do
(signal-semaphore sem
))
528 (assert (zerop (count-live-threads triers
)))
529 (assert (zerop (count-live-threads waiters
)))
530 (assert (zerop (count-live-threads more-waiters
))))))
534 (format t
"~&semaphore tests done~%")
536 (defun test-interrupt (function-to-interrupt &optional quit-p
)
537 (let ((child (make-kill-thread function-to-interrupt
)))
538 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
540 (format t
"interrupting child ~A~%" child
)
541 (interrupt-thread child
543 (format t
"child pid ~A~%" *current-thread
*)
544 (when quit-p
(abort-thread))))
548 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
549 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
552 (with-test (:name
(:interrupt-thread
:more-basics
))
553 (let ((child (test-interrupt (lambda () (loop)))))
554 (terminate-thread child
)))
556 (with-test (:name
(:interrupt-thread
:interrupt-foreign-loop
)
557 ;; This feature is explicitly unsupported on Win32.
559 (test-interrupt #'loop-forever
:quit
))
561 (with-test (:name
(:interrupt-thread
:interrupt-sleep
))
562 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
563 (terminate-thread child
)
564 (wait-for-threads (list child
))))
566 (with-test (:name
(:interrupt-thread
:interrupt-mutex-acquisition
))
567 (let ((lock (make-mutex :name
"loctite"))
570 (setf child
(test-interrupt
573 (assert (eql (mutex-value lock
) *current-thread
*)))
574 (assert (not (eql (mutex-value lock
) *current-thread
*)))
576 ;;hold onto lock for long enough that child can't get it immediately
578 (interrupt-thread child
(lambda () (format t
"l ~A~%" (mutex-value lock
))))
579 (format t
"parent releasing lock~%"))
580 (terminate-thread child
)
581 (wait-for-threads (list child
))))
583 (format t
"~&locking test done~%")
585 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
587 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
))
588 (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
590 (loop repeat
4 collect
591 (sb-thread:make-thread
594 (sleep (random 0.1d0
))
597 (sb-thread:interrupt-thread thread
(lambda ()))))))))
598 (wait-for-threads killers
)
599 (sb-thread:terminate-thread thread
)
600 (wait-for-threads (list thread
))))
603 (format t
"~&multi interrupt test done~%")
605 #+(or x86 x86-64
) ;; x86oid-only, see internal commentary.
606 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
:again
))
607 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
608 ;; NB this only works on x86: other ports don't have a symbol for
609 ;; pseudo-atomic atomicity
611 (sleep (random 0.1d0
))
614 (princ ".") (force-output)
615 (assert (thread-alive-p *current-thread
*))
617 (not (logbitp 0 SB-KERNEL
:*PSEUDO-ATOMIC-BITS
*))))))
619 (wait-for-threads (list c
))))
621 (format t
"~&interrupt test done~%")
623 (defstruct counter
(n 0 :type sb-vm
:word
))
624 (defvar *interrupt-counter
* (make-counter))
626 (declaim (notinline check-interrupt-count
))
627 (defun check-interrupt-count (i)
628 (declare (optimize (debug 1) (speed 1)))
629 ;; This used to lose if eflags were not restored after an interrupt.
630 (unless (typep i
'fixnum
)
631 (error "!!!!!!!!!!!")))
633 (with-test (:name
(:interrupt-thread
:interrupt-ATOMIC-INCF
))
634 (let ((c (make-thread
636 (handler-bind ((error #'(lambda (cond)
639 most-positive-fixnum
))))
640 (loop (check-interrupt-count
641 (counter-n *interrupt-counter
*))))))))
642 (let ((func (lambda ()
645 (sb-ext:atomic-incf
(counter-n *interrupt-counter
*)))))
646 (setf (counter-n *interrupt-counter
*) 0)
648 (sleep (random 0.1d0
))
649 (interrupt-thread c func
))
650 (loop until
(= (counter-n *interrupt-counter
*) 100) do
(sleep 0.1))
652 (wait-for-threads (list c
)))))
654 (format t
"~&interrupt count test done~%")
656 (defvar *runningp
* nil
)
658 (with-test (:name
(:interrupt-thread
:no-nesting
))
659 (let ((thread (sb-thread:make-thread
663 (declare (special runningp
))
665 (sb-thread:interrupt-thread thread
667 (let ((*runningp
* t
))
670 (sb-thread:interrupt-thread thread
672 (throw 'xxx
*runningp
*)))
673 (assert (not (sb-thread:join-thread thread
)))))
675 (with-test (:name
(:interrupt-thread
:nesting
))
676 (let ((thread (sb-thread:make-thread
680 (declare (special runningp
))
682 (sb-thread:interrupt-thread thread
684 (let ((*runningp
* t
))
685 (sb-sys:with-interrupts
688 (sb-thread:interrupt-thread thread
690 (throw 'xxx
*runningp
*)))
691 (assert (sb-thread:join-thread thread
))))
693 (with-test (:name
(:two-threads-running-gc
))
695 (make-join-thread (lambda ()
697 (sb-ext:gc
) (princ "\\") (force-output))
699 (make-join-thread (lambda ()
702 (princ "/") (force-output))
705 (when (and a-done b-done
) (return))
710 (defun waste (&optional
(n 100000))
711 (loop repeat n do
(make-string 16384)))
713 (with-test (:name
(:one-thread-runs-gc-while-other-conses
))
714 (loop for i below
100 do
725 (defparameter *aaa
* nil
)
726 (with-test (:name
(:one-thread-runs-gc-while-other-conses
:again
))
727 (loop for i below
100 do
732 (let ((*aaa
* (waste)))
734 (let ((*aaa
* (waste)))
738 (format t
"~&gc test done~%")
740 ;; this used to deadlock on session-lock
741 (with-test (:name
(:no-session-deadlock
))
742 (make-join-thread (lambda () (sb-ext:gc
))))
744 (defun exercise-syscall (fn reference-errno
)
749 (let ((errno (sb-unix::get-errno
)))
750 (sleep (random 0.1d0
))
751 (unless (eql errno reference-errno
)
752 (format t
"Got errno: ~A (~A) instead of ~A~%"
759 ;; (nanosleep -1 0) does not fail on FreeBSD
760 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
762 (nanosleep-errno (progn
763 (sb-unix:nanosleep -
1 0)
764 (sb-unix::get-errno
)))
767 :if-does-not-exist nil
)
768 (sb-unix::get-errno
)))
772 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
773 (exercise-syscall (lambda () (open "no-such-file"
774 :if-does-not-exist nil
))
776 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
778 (princ "terminating threads")
779 (dolist (thread threads
)
780 (sb-thread:terminate-thread thread
))))
782 (format t
"~&errno test done~%")
784 (with-test (:name
:all-threads-have-abort-restart
)
786 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
787 (sb-thread:interrupt-thread
790 (assert (find-restart 'abort
)))))))
794 (format t
"~&thread startup sigmask test done~%")
796 (with-test (:name
(:debugger-no-hang-on-session-lock-if-interrupted
)
798 #+win32
(error "user would have to touch a key interactively to proceed")
799 (sb-debug::enable-debugger
)
800 (let* ((main-thread *current-thread
*)
802 (make-thread (lambda ()
804 (interrupt-thread main-thread
809 (interrupt-thread main-thread
#'continue
))
810 :name
"interruptor")))
811 (with-session-lock (*session
*)
813 (loop while
(thread-alive-p interruptor-thread
))))
815 (format t
"~&session lock test done~%")
817 ;; expose thread creation races by exiting quickly
818 (with-test (:name
(:no-thread-creation-race
:light
))
819 (make-join-thread (lambda ())))
821 (with-test (:name
(:no-thread-creation-race
:heavy
))
824 (loop for i below
100 collect
825 (sb-thread:make-thread
(lambda ()))))))
827 (format t
"~&creation test done~%")
829 ;; interrupt handlers are per-thread with pthreads, make sure the
830 ;; handler installed in one thread is global
831 (with-test (:name
(:global-interrupt-handler
))
834 (sb-ext:run-program
"sleep" '("1") :search t
:wait nil
))))
836 ;;;; Binding stack safety
838 (defparameter *x
* nil
)
839 (defparameter *n-gcs-requested
* 0)
840 (defparameter *n-gcs-done
* 0)
843 (defun make-something-big ()
844 (let ((x (make-string 32000)))
846 (let ((counter counter
))
847 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
850 (defmacro wait-for-gc
()
852 (incf *n-gcs-requested
*)
853 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
856 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
862 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
863 (defun exercise-binding ()
865 (let ((*x
* (make-something-big)))
867 ;; at this point the binding stack looks like this:
868 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
871 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
872 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
873 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
874 ;; unbinding but values are not).
876 (binding-pointer-delta (ash 2 (- sb-vm
:word-shift sb-vm
:n-fixnum-tag-bits
))))
877 ;; bump bsp as if a BIND had just started
878 (incf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
)
880 (decf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
))))
882 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
883 (with-test (:name
(:binding-stack-gc-safety
))
887 (push (make-kill-thread #'exercise-binding
) threads
)
888 (push (make-kill-thread (lambda ()
894 (mapc #'sb-thread
:terminate-thread threads
))))
896 (with-test (:name
:test-%thread-local-references
)
897 (let ((mysym (gensym))
900 (progv (list mysym
) '(nil)
901 (let* ((i (get-lisp-obj-address (sb-vm:symbol-tls-index mysym
)))
902 (j (+ i sb-vm
:n-word-bytes
)))
903 (assert (eql (sap-ref-word (current-thread-sap) j
)
904 sb-vm
:no-tls-value-marker-widetag
))
905 (setf (sap-ref-lispobj (current-thread-sap) i
) fool1
906 (sap-ref-lispobj (current-thread-sap) j
) fool2
)
907 ;; assert that my pointer arithmetic worked as expected
908 (assert (eq (symbol-value mysym
) fool1
))
909 ;; assert that FOOL1 is found by the TLS scan and that FOOL2 is not.
910 (let ((list (%thread-local-references
)))
911 (assert (memq fool1 list
))
912 (assert (not (memq fool2 list
))))
913 ;; repair the TLS entry that was corrupted by the test
914 (setf (sap-ref-word (current-thread-sap) j
)
915 sb-vm
:no-tls-value-marker-widetag
)))))
917 (format t
"~&binding test done~%")
921 (defvar *errors
* nil
)
925 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
929 (with-test (:name
(:unsynchronized-hash-table
)
930 ;; FIXME: This test occasionally eats out craploads
931 ;; of heap instead of expected error early. Not 100%
932 ;; sure if it would finish as expected, but since it
933 ;; hits swap on my system I'm not likely to find out
934 ;; soon. Disabling for now. -- nikodemus
936 ;; We expect a (probable) error here: parellel readers and writers
937 ;; on a hash-table are not expected to work -- but we also don't
938 ;; expect this to corrupt the image.
939 (let* ((hash (make-hash-table))
941 (threads (list (make-kill-thread
944 (handler-bind ((serious-condition 'oops
))
946 ;;(princ "1") (force-output)
947 (setf (gethash (random 100) hash
) 'h
)))))
952 (handler-bind ((serious-condition 'oops
))
954 ;;(princ "2") (force-output)
955 (remhash (random 100) hash
)))))
960 (handler-bind ((serious-condition 'oops
))
963 (sb-ext:gc
:full t
)))))
964 :name
"collector"))))
967 (mapc #'sb-thread
:terminate-thread threads
))))
969 (format t
"~&unsynchronized hash table test done~%")
971 (with-test (:name
(:synchronized-hash-table
))
972 (let* ((hash (make-hash-table :synchronized t
))
974 (threads (list (make-join-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
))
1001 (assert (not *errors
*))))
1003 (format t
"~&synchronized hash table test done~%")
1005 (with-test (:name
(:hash-table-parallel-readers
))
1006 (let ((hash (make-hash-table))
1009 do
(setf (gethash (random 100) hash
) 'xxx
))
1010 (let ((threads (list (make-kill-thread
1013 (handler-bind ((serious-condition 'oops
))
1015 until
(eq t
(gethash (random 100) hash
))))))
1020 (handler-bind ((serious-condition 'oops
))
1022 until
(eq t
(gethash (random 100) hash
))))))
1027 (handler-bind ((serious-condition 'oops
))
1029 until
(eq t
(gethash (random 100) hash
))))))
1034 (handler-bind ((serious-condition 'oops
))
1036 (sleep (random 1.0))
1037 (sb-ext:gc
:full t
)))))
1038 :name
"collector"))))
1041 (mapc #'sb-thread
:terminate-thread threads
))
1042 (assert (not *errors
*)))))
1044 (format t
"~&multiple reader hash table test done~%")
1046 (with-test (:name
(:hash-table-single-accessor-parallel-gc
))
1047 (let ((hash (make-hash-table))
1049 (let ((threads (list (make-kill-thread
1051 (handler-bind ((serious-condition 'oops
))
1053 (let ((n (random 100)))
1054 (if (gethash n hash
)
1056 (setf (gethash n hash
) 'h
))))))
1060 (handler-bind ((serious-condition 'oops
))
1062 (sleep (random 1.0))
1063 (sb-ext:gc
:full t
))))
1064 :name
"collector"))))
1067 (mapc #'sb-thread
:terminate-thread threads
))
1068 (assert (not *errors
*)))))
1070 (format t
"~&single accessor hash table test~%")
1072 #|
;; a cll post from eric marsden
1074 |
(setq *debugger-hook
*
1075 |
(lambda (condition old-debugger-hook
)
1076 |
(debug:backtrace
10)
1077 |
(unix:unix-exit
2)))
1078 |
#+live-dangerously
1079 |
(mp::start-sigalrm-yield
)
1080 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
1081 |
(mp:make-process
#'roomy
)
1082 |
(mp:make-process
#'roomy
)))
1085 (with-test (:name
(:condition-variable
:notify-multiple
))
1086 (flet ((tester (notify-fun)
1087 (let ((queue (make-waitqueue :name
"queue"))
1088 (lock (make-mutex :name
"lock"))
1093 (format t
"condition-wait ~a~%" x
)
1095 (condition-wait queue lock
)
1096 (format t
"woke up ~a~%" x
)
1099 (let ((threads (loop for x from
1 to
10
1102 (make-kill-thread (lambda ()
1106 (funcall notify-fun queue
))
1108 (mapcar #'terminate-thread threads
)
1109 ;; Check that all threads woke up at least once
1110 (assert (= (length (remove-duplicates data
)) 10)))))))
1111 (tester (lambda (queue)
1112 (format t
"~&(condition-notify queue 10)~%")
1114 (condition-notify queue
10)))
1115 (tester (lambda (queue)
1116 (format t
"~&(condition-broadcast queue)~%")
1118 (condition-broadcast queue
)))))
1120 (format t
"waitqueue wakeup tests done~%")
1122 ;;; Make sure that a deadline handler is not invoked twice in a row in
1123 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1125 (with-test (:name
(:condition-wait
:deadlines
:LP-512914
))
1126 (let ((n 2) ; was empirically enough to trigger the bug
1127 (mutex (sb-thread:make-mutex
))
1128 (waitq (sb-thread:make-waitqueue
))
1130 (deadline-handler-run-twice? nil
))
1133 (sb-thread:make-thread
1136 ((sb-sys:deadline-timeout
1137 (let ((already? nil
))
1140 (setq deadline-handler-run-twice? t
))
1143 (sb-thread:condition-broadcast waitq
)
1144 (sb-sys:defer-deadline
10.0 c
)))))
1145 (sb-sys:with-deadline
(:seconds
0.1)
1146 (sb-thread:with-mutex
(mutex)
1147 (sb-thread:condition-wait waitq mutex
))))))))
1148 (push child threads
)))
1149 (mapc #'sb-thread
:join-thread threads
)
1150 (assert (not deadline-handler-run-twice?
))))
1152 (with-test (:name
(:condition-wait
:signal-deadline-with-interrupts-enabled
))
1153 (let ((mutex (sb-thread:make-mutex
))
1154 (waitq (sb-thread:make-waitqueue
))
1157 (A-interrupts-enabled?
:unknown
)
1158 (B-interrupts-enabled?
:unknown
)
1161 ;; W.L.O.G., we assume that A is executed first...
1162 (setq A
(sb-thread:make-thread
1165 ((sb-sys:deadline-timeout
1167 ;; We came here through the call to DECODE-TIMEOUT
1168 ;; in CONDITION-WAIT; hence both here are supposed
1169 ;; to evaluate to T.
1170 (setq A-holds?
(sb-thread:holding-mutex-p mutex
))
1171 (setq A-interrupts-enabled?
1172 sb-sys
:*interrupts-enabled
*)
1174 (sb-thread:condition-broadcast waitq
)
1175 (sb-sys:defer-deadline
10.0 c
))))
1176 (sb-sys:with-deadline
(:seconds
0.1)
1177 (sb-thread:with-mutex
(mutex)
1178 (sb-thread:condition-wait waitq mutex
)))))
1180 (setq B
(sb-thread:make-thread
1184 ((sb-sys:deadline-timeout
1186 ;; We came here through the call to DECODE-TIMEOUT
1187 ;; in CONDITION-WAIT (contended case of
1188 ;; reaquiring the mutex) - so the former will
1189 ;; be NIL, but interrupts should still be enabled.
1190 (setq B-holds?
(sb-thread:holding-mutex-p mutex
))
1191 (setq B-interrupts-enabled?
1192 sb-sys
:*interrupts-enabled
*)
1194 (sb-thread:condition-broadcast waitq
)
1195 (sb-sys:defer-deadline
10.0 c
))))
1196 (sb-sys:with-deadline
(:seconds
0.1)
1197 (sb-thread:with-mutex
(mutex)
1198 (sb-thread:condition-wait waitq mutex
)))))
1200 (sb-thread:join-thread A
)
1201 (sb-thread:join-thread B
)
1202 (let ((A-result (list A-holds? A-interrupts-enabled?
))
1203 (B-result (list B-holds? B-interrupts-enabled?
)))
1204 ;; We also check some subtle behaviour w.r.t. whether a deadline
1205 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1206 ;; probably very internal behaviour (so user should not depend
1207 ;; on it) -- I added the testing here just to manifest current
1209 (cond ((equal A-result
'(t t
)) (assert (equal B-result
'(nil t
))))
1210 ((equal B-result
'(t t
)) (assert (equal A-result
'(nil t
))))
1212 (error "Failure: fell through wit A: ~S, B: ~S"
1216 (with-test (:name
(:mutex
:finalization
))
1219 (setf a
(make-mutex)))))
1221 (format t
"mutex finalization test done~%")
1223 (with-test (:name
:backtrace
)
1224 ;; Printing backtraces from several threads at once used to hang the
1225 ;; whole SBCL process (discovered by accident due to a timer.impure
1226 ;; test misbehaving). The cause was that packages weren't even
1227 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1228 ;; backtraces a loot of symbol lookups need to be done due to
1230 (let* ((threads (loop repeat
10
1231 collect
(sb-thread:make-thread
1234 (with-output-to-string (*debug-io
*)
1235 (sb-debug::backtrace
10))))))))
1236 (wait-for-threads threads
)))
1238 (format t
"backtrace test done~%")
1240 (format t
"~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1242 (with-test (:name
:gc-deadlock
)
1243 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1244 ;; GC due to *all-threads-lock* and session lock. On earlier
1245 ;; versions and at least on one specific box this test is good enough
1246 ;; to catch that typically well before the 1500th iteration.
1253 (when (zerop (mod i
100))
1260 (sleep (random 0.001)))
1261 :name
(format nil
"SLEEP-~D" i
))
1264 ;; KLUDGE: what we are doing here is explicit,
1265 ;; but the same can happen because of a regular
1266 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1267 ;; session functions.
1268 (sb-thread::with-all-threads-lock
1269 (sb-thread::with-session-lock
(sb-thread::*session
*)
1271 :name
(format nil
"GC-~D" i
)))
1273 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
1277 (format t
"~&gc deadlock test done~%")
1279 (let ((count (make-array 8 :initial-element
0)))
1280 (defun closure-one ()
1281 (declare (optimize safety
))
1282 (values (incf (aref count
0)) (incf (aref count
1))
1283 (incf (aref count
2)) (incf (aref count
3))
1284 (incf (aref count
4)) (incf (aref count
5))
1285 (incf (aref count
6)) (incf (aref count
7))))
1286 (defun no-optimizing-away-closure-one ()
1287 (setf count
(make-array 8 :initial-element
0))))
1292 (let ((one (make-box))
1295 (defun closure-two ()
1296 (declare (optimize safety
))
1297 (values (incf (box-count one
)) (incf (box-count two
)) (incf (box-count three
))))
1298 (defun no-optimizing-away-closure-two ()
1299 (setf one
(make-box)
1303 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1304 ;;; sometimes run out of memory) in the following test. For developers
1305 ;;; interested in debugging this combination of features, it might be
1306 ;;; fruitful to concentrate their efforts around this test...
1308 (with-test (:name
(:funcallable-instances
)
1309 :skipped-on
'(and :sb-safepoint
1310 (not :c-stack-is-control-stack
)))
1311 ;; the funcallable-instance implementation used not to be threadsafe
1312 ;; against setting the funcallable-instance function to a closure
1313 ;; (because the code and lexenv were set separately).
1314 (let ((fun (sb-kernel:%make-funcallable-instance
0))
1316 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1318 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1319 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1321 (handler-case (loop (funcall fun
))
1322 (serious-condition (c) (setf condition c
)))))
1323 (let ((changer (make-thread #'changer
))
1324 (test (make-thread #'test
)))
1327 ;; The two closures above are fairly carefully crafted
1328 ;; so that if given the wrong lexenv they will tend to
1329 ;; do some serious damage, but it is of course difficult
1330 ;; to predict where the various bits and pieces will be
1331 ;; allocated. Five seconds failed fairly reliably on
1332 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1333 (sb-ext:with-timeout
5
1334 (wait-for-threads (list test
)))
1335 (error "~@<test thread got condition:~2I~_~A~@:>" condition
))
1337 (terminate-thread changer
)
1338 (terminate-thread test
)
1339 (wait-for-threads (list changer test
))))))))
1341 (format t
"~&funcallable-instance test done~%")
1343 (defun random-type (n)
1344 `(integer ,(random n
) ,(+ n
(random n
))))
1346 (defun subtypep-hash-cache-test ()
1348 (let ((type1 (random-type 500))
1349 (type2 (random-type 500)))
1350 (let ((a (subtypep type1 type2
)))
1352 (assert (eq (subtypep type1 type2
) a
))))))
1356 (with-test (:name
(:hash-cache
:subtypep
))
1359 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
))))
1360 (format t
"hash-cache tests done~%")
1362 ;;;; BLACK BOX TESTS
1364 (in-package :cl-user
)
1365 (use-package :test-util
)
1366 (use-package "ASSERTOID")
1368 (format t
"parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1369 (with-test (:name
:parallel-defclass
)
1370 (defclass test-1
() ((a :initform
:orig-a
)))
1371 (defclass test-2
() ((b :initform
:orig-b
)))
1372 (defclass test-3
(test-1 test-2
) ((c :initform
:orig-c
)))
1373 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1374 ;; calls disabled in the folloving code. (As seen on a Server 2012
1375 ;; installation.) Clearly, this sort of workaround in a test is
1376 ;; cheating, and might be hiding the underlying bug that the test is
1377 ;; exposing. Let's review this later.
1379 (d1 (sb-thread:make-thread
(lambda ()
1381 do
(defclass test-1
() ((a :initform
:new-a
)))
1383 #-win32
(force-output)))
1385 (d2 (sb-thread:make-thread
(lambda ()
1387 do
(defclass test-2
() ((b :initform
:new-b
)))
1389 #-win32
(force-output)))
1391 (d3 (sb-thread:make-thread
(lambda ()
1393 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1395 #-win32
(force-output)))
1397 (i (sb-thread:make-thread
(lambda ()
1399 do
(let ((i (make-instance 'test-3
)))
1400 (assert (member (slot-value i
'a
) '(:orig-a
:new-a
)))
1401 (assert (member (slot-value i
'b
) '(:orig-b
:new-b
)))
1402 (assert (member (slot-value i
'c
) '(:orig-c
:new-c
))))
1404 #-win32
(force-output)))
1406 (format t
"~%sleeping!~%")
1408 (format t
"~%stopping!~%")
1411 (sb-thread:join-thread th
)
1412 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
1415 (format t
"parallel defclass test done~%")
1417 (with-test (:name
(:deadlock-detection
:interrupts
) :fails-on
:win32
)
1418 #+win32
;be more explicit than just :skipped-on
1419 (error "not attempting, because of deadlock error in background thread")
1420 (let* ((m1 (sb-thread:make-mutex
:name
"M1"))
1421 (m2 (sb-thread:make-mutex
:name
"M2"))
1422 (t1-can-go (sb-thread:make-semaphore
:name
"T1 can go"))
1423 (t2-can-go (sb-thread:make-semaphore
:name
"T2 can go"))
1424 (t1 (sb-thread:make-thread
1426 (sb-thread:with-mutex
(m1)
1427 (sb-thread:wait-on-semaphore t1-can-go
)
1430 (t2 (sb-thread:make-thread
1432 (sb-ext:wait-for
(eq t1
(sb-thread:mutex-owner m1
)))
1433 (sb-thread:with-mutex
(m1 :wait-p t
)
1434 (sb-thread:wait-on-semaphore t2-can-go
)
1437 (sb-ext:wait-for
(eq m1
(sb-thread::thread-waiting-for t2
)))
1438 (sb-thread:interrupt-thread t2
(lambda ()
1439 (sb-thread:with-mutex
(m2 :wait-p t
)
1441 (eq m2
(sb-thread::thread-waiting-for t1
)))
1442 (sb-thread:signal-semaphore t2-can-go
))))
1443 (sb-ext:wait-for
(eq t2
(sb-thread:mutex-owner m2
)))
1444 (sb-thread:interrupt-thread t1
(lambda ()
1445 (sb-thread:with-mutex
(m2 :wait-p t
)
1446 (sb-thread:signal-semaphore t1-can-go
))))
1447 ;; both threads should finish without a deadlock or deadlock
1449 (let ((res (list (sb-thread:join-thread t1
)
1450 (sb-thread:join-thread t2
))))
1451 (assert (equal '(:ok1
:ok2
) res
)))))
1453 (with-test (:name
(:deadlock-detection
:gc
))
1454 ;; To semi-reliably trigger the error (in SBCL's where)
1455 ;; it was present you had to run this for > 30 seconds,
1456 ;; but that's a bit long for a single test.
1457 (let* ((stop (+ 5 (get-universal-time)))
1458 (m1 (sb-thread:make-mutex
:name
"m1"))
1459 (t1 (sb-thread:make-thread
1461 (loop until
(> (get-universal-time) stop
)
1462 do
(sb-thread:with-mutex
(m1)
1463 (eval `(make-array 24))))
1465 (t2 (sb-thread:make-thread
1467 (loop until
(> (get-universal-time) stop
)
1468 do
(sb-thread:with-mutex
(m1)
1469 (eval `(make-array 24))))
1471 (let ((res (list (sb-thread:join-thread t1
)
1472 (sb-thread:join-thread t2
))))
1473 (assert (equal '(:ok
:ok
) res
)))))
1475 (with-test (:name
:spinlock-api
)
1478 (handler-bind ((sb-int:early-deprecation-warning
(lambda (_)
1479 (declare (ignore _
))
1481 (list (compile nil
`(lambda (lock)
1482 (sb-thread::with-spinlock
(lock)
1484 (compile nil
`(lambda ()
1485 (sb-thread::make-spinlock
:name
"foo")))
1486 (compile nil
`(lambda (lock)
1487 (sb-thread::get-spinlock lock
)))
1488 (compile nil
`(lambda (lock)
1489 (sb-thread::release-spinlock lock
)))))))
1490 (assert (eql 4 warned
))
1491 (handler-bind ((warning #'error
))
1492 (destructuring-bind (with make get release
) funs
1493 (let ((lock (funcall make
)))
1495 (funcall release lock
)
1496 (assert (eq t
(funcall with lock
))))))))
1498 (with-test (:name
:interrupt-io-unnamed-pipe
)
1502 (let ((stream (sb-sys:make-fd-stream fd
1503 :element-type
:default
1504 :serve-events nil
)))
1506 (let ((ok (handler-case
1517 (format *trace-output
* "~&=> ~A~%" ok
)
1518 (force-output *trace-output
*))))
1520 (ignore-errors (close stream
))))
1523 (multiple-value-bind (read write
)
1525 (let* ((reader (sb-thread:make-thread
(lambda () (reader read
))))
1526 (stream (sb-sys:make-fd-stream write
1528 :element-type
:default
1532 (sb-thread:interrupt-thread reader
(lambda ()
1538 (write-char #\x stream
)
1540 (sb-thread:join-thread reader
)))))
1542 (assert (eq result
:ok
))))
1544 (with-test (:name
:thread-alloca
)
1545 (sb-ext:run-program
"sh"
1546 '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
1547 "alloca.c" "-o" "alloca.so")
1549 (load-shared-object (truename "alloca.so"))
1551 (alien-funcall (extern-alien "alloca_test" (function void
)))
1552 (sb-thread:join-thread
1553 (sb-thread:make-thread
1555 (alien-funcall (extern-alien "alloca_test" (function void
)))))))
1557 (with-test (:name
:fp-mode-inheritance-threads
)
1559 (let ((thread-fp-mode)
1560 (fp-mode (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))))
1561 (sb-thread:join-thread
1562 (sb-thread:make-thread
1564 (setf thread-fp-mode
1565 (dpb 0 sb-vm
::float-sticky-bits
(sb-vm:floating-point-modes
))))))
1566 (assert (= fp-mode thread-fp-mode
)))))
1568 (sb-int:with-float-traps-masked
(:divide-by-zero
)
1570 (setf (sb-vm:floating-point-modes
)
1571 (dpb sb-vm
:float-divide-by-zero-trap-bit
1572 sb-vm
::float-traps-byte
1573 (sb-vm:floating-point-modes
)))