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 :environment
(test-util::test-env
))
217 (sb-alien:load-shared-object
(truename "threads-foreign.so"))
218 (sb-alien:define-alien-routine loop-forever sb-alien
:void
)
219 (delete-file "threads-foreign.c"))
221 ;;; elementary "can we get a lock and release it again"
222 (with-test (:name
(:mutex
:basics
))
223 (let ((l (make-mutex :name
"foo"))
224 (p *current-thread
*))
225 (assert (eql (mutex-value l
) nil
) nil
"1")
226 (sb-thread:grab-mutex l
)
227 (assert (eql (mutex-value l
) p
) nil
"3")
228 (sb-thread:release-mutex l
)
229 (assert (eql (mutex-value l
) nil
) nil
"5")))
231 (with-test (:name
(:with-recursive-lock
:basics
))
232 (labels ((ours-p (value)
233 (eq *current-thread
* value
)))
234 (let ((l (make-mutex :name
"rec")))
235 (assert (eql (mutex-value l
) nil
) nil
"1")
236 (sb-thread:with-recursive-lock
(l)
237 (assert (ours-p (mutex-value l
)) nil
"3")
238 (sb-thread:with-recursive-lock
(l)
239 (assert (ours-p (mutex-value l
)) nil
"4"))
240 (assert (ours-p (mutex-value l
)) nil
"5"))
241 (assert (eql (mutex-value l
) nil
) nil
"6"))))
243 (with-test (:name
(:with-recursive-lock
:wait-p
))
244 (let ((m (make-mutex)))
246 (assert (null (join-thread (make-thread
248 (with-recursive-lock (m :wait-p nil
)
250 (assert (join-thread (make-thread
252 (with-recursive-lock (m :wait-p nil
)
255 (with-test (:name
(:with-recursive-lock
:wait-p
:recursive
))
256 (let ((m (make-mutex)))
257 (assert (join-thread (make-thread
259 (with-recursive-lock (m :wait-p nil
)
260 (with-recursive-lock (m :wait-p nil
)
263 (with-test (:name
(:with-recursive-lock
:timeout
))
264 (let ((m (make-mutex)))
266 (assert (null (join-thread (make-thread
268 (with-recursive-lock (m :timeout
0.1)
270 (assert (join-thread (make-thread
272 (with-recursive-lock (m :timeout
0.1)
275 (with-test (:name
(:with-recursive-lock
:timeout
:recursive
))
276 (let ((m (make-mutex)))
277 (assert (join-thread (make-thread
279 (with-recursive-lock (m :timeout
0.1)
280 (with-recursive-lock (m :timeout
0.1)
283 (with-test (:name
(:mutex
:nesting-mutex-and-recursive-lock
))
284 (let ((l (make-mutex :name
"a mutex")))
286 (with-recursive-lock (l)))))
288 ;; test that SLEEP actually sleeps for at least the given time, even
289 ;; if interrupted by another thread exiting/a gc/anything
290 (with-test (:name
(:sleep
:continue-sleeping-after-interrupt
))
291 (let ((start-time (get-universal-time)))
292 (make-join-thread (lambda () (sleep 1) (sb-ext:gc
:full t
)))
294 (assert (>= (get-universal-time) (+ 5 start-time
)))))
297 (with-test (:name
(:condition-wait
:basics-1
))
298 (let ((queue (make-waitqueue :name
"queue"))
299 (lock (make-mutex :name
"lock"))
301 (labels ((in-new-thread ()
303 (assert (eql (mutex-value lock
) *current-thread
*))
304 (format t
"~A got mutex~%" *current-thread
*)
305 ;; now drop it and sleep
306 (condition-wait queue lock
)
307 ;; after waking we should have the lock again
308 (assert (eql (mutex-value lock
) *current-thread
*))
311 (make-join-thread #'in-new-thread
)
312 (sleep 2) ; give it a chance to start
313 ;; check the lock is free while it's asleep
314 (format t
"parent thread ~A~%" *current-thread
*)
315 (assert (eql (mutex-value lock
) nil
))
318 (condition-notify queue
))
321 (with-test (:name
(:condition-wait
:basics-2
))
322 (let ((queue (make-waitqueue :name
"queue"))
323 (lock (make-mutex :name
"lock")))
324 (labels ((ours-p (value)
325 (eq *current-thread
* value
))
327 (with-recursive-lock (lock)
328 (assert (ours-p (mutex-value lock
)))
329 (format t
"~A got mutex~%" (mutex-value lock
))
330 ;; now drop it and sleep
331 (condition-wait queue lock
)
332 ;; after waking we should have the lock again
333 (format t
"woken, ~A got mutex~%" (mutex-value lock
))
334 (assert (ours-p (mutex-value lock
))))))
335 (make-join-thread #'in-new-thread
)
336 (sleep 2) ; give it a chance to start
337 ;; check the lock is free while it's asleep
338 (format t
"parent thread ~A~%" *current-thread
*)
339 (assert (eql (mutex-value lock
) nil
))
340 (with-recursive-lock (lock)
341 (condition-notify queue
))
344 (with-test (:name
(:mutex
:contention
))
345 (let ((mutex (make-mutex :name
"contended")))
347 (let ((me *current-thread
*))
351 (assert (eql (mutex-value mutex
) me
)))
352 (assert (not (eql (mutex-value mutex
) me
))))
353 (format t
"done ~A~%" *current-thread
*))))
354 (let ((kid1 (make-thread #'run
))
355 (kid2 (make-thread #'run
)))
356 (format t
"contention ~A ~A~%" kid1 kid2
)
357 (wait-for-threads (list kid1 kid2
))))))
361 (with-test (:name
(:grab-mutex
:waitp nil
))
362 (let ((m (make-mutex)))
364 (assert (null (join-thread (make-thread
366 (grab-mutex m
:waitp nil
)))))))))
368 (with-test (:name
(:grab-mutex
:timeout
:acquisition-fail
))
369 (let ((m (make-mutex))
370 (w (make-semaphore)))
372 (let ((th (make-thread
375 (grab-mutex m
:timeout
0.1)
376 (signal-semaphore w
))))))
377 ;; Wait for it to -- otherwise the detect the deadlock chain
379 (wait-on-semaphore w
)
380 (assert (null (join-thread th
)))))))
382 (with-test (:name
(:grab-mutex
:timeout
:acquisition-success
))
383 (let ((m (make-mutex))
386 (setq child
(make-thread #'(lambda () (grab-mutex m
:timeout
1.0))))
388 (assert (eq (join-thread child
) 't
))))
390 (with-test (:name
(:grab-mutex
:timeout
+deadline
))
391 (let ((m (make-mutex))
392 (w (make-semaphore)))
394 (let ((th (make-thread #'(lambda ()
395 (sb-sys:with-deadline
(:seconds
0.0)
397 (grab-mutex m
:timeout
0.0)
398 (sb-sys:deadline-timeout
()
401 (wait-on-semaphore w
)
402 (assert (eq (join-thread th
) :deadline
))))))
404 (with-test (:name
(:grab-mutex
:waitp
+deadline
))
405 (let ((m (make-mutex)))
407 (assert (eq (join-thread
408 (make-thread #'(lambda ()
409 (sb-sys:with-deadline
(:seconds
0.0)
411 (grab-mutex m
:waitp nil
)
412 (sb-sys:deadline-timeout
()
418 (defmacro raises-timeout-p
(&body body
)
419 `(handler-case (progn (progn ,@body
) nil
)
420 (sb-ext:timeout
() t
)))
422 (with-test (:name
(:semaphore
:wait-forever
))
423 (let ((sem (make-semaphore :count
0)))
424 (assert (raises-timeout-p
425 (sb-ext:with-timeout
0.1
426 (wait-on-semaphore sem
))))))
428 (with-test (:name
(:semaphore
:initial-count
))
429 (let ((sem (make-semaphore :count
1)))
430 (sb-ext:with-timeout
0.1
431 (wait-on-semaphore sem
))))
433 (with-test (:name
(:semaphore
:wait-then-signal
))
434 (let ((sem (make-semaphore))
436 (make-join-thread (lambda ()
439 (signal-semaphore sem
)))
440 (wait-on-semaphore sem
)
441 (assert signalled-p
)))
443 (with-test (:name
(:semaphore
:signal-then-wait
))
444 (let ((sem (make-semaphore))
446 (make-join-thread (lambda ()
447 (signal-semaphore sem
)
448 (setq signalled-p t
)))
449 (loop until signalled-p
)
450 (wait-on-semaphore sem
)
451 (assert signalled-p
)))
453 (defun test-semaphore-multiple-signals (wait-on-semaphore)
454 (let* ((sem (make-semaphore :count
5))
455 (threads (loop repeat
20 collecting
456 (make-join-thread (lambda ()
457 (funcall wait-on-semaphore sem
))))))
458 (flet ((count-live-threads ()
459 (count-if #'thread-alive-p threads
)))
461 (assert (= 15 (count-live-threads)))
462 (signal-semaphore sem
10)
464 (assert (= 5 (count-live-threads)))
465 (signal-semaphore sem
3)
467 (assert (= 2 (count-live-threads)))
468 (signal-semaphore sem
4)
470 (assert (= 0 (count-live-threads))))))
472 (with-test (:name
(:semaphore
:multiple-signals
))
473 (test-semaphore-multiple-signals #'wait-on-semaphore
))
475 (with-test (:name
(:try-semaphore
:trivial-fail
))
476 (assert (eq (try-semaphore (make-semaphore :count
0)) 'nil
)))
478 (with-test (:name
(:try-semaphore
:trivial-success
))
479 (let ((sem (make-semaphore :count
1)))
480 (assert (try-semaphore sem
))
481 (assert (zerop (semaphore-count sem
)))))
483 (with-test (:name
(:try-semaphore
:trivial-fail
:n
>1))
484 (assert (eq (try-semaphore (make-semaphore :count
1) 2) 'nil
)))
486 (with-test (:name
(:try-semaphore
:trivial-success
:n
>1))
487 (let ((sem (make-semaphore :count
10)))
488 (assert (try-semaphore sem
5))
489 (assert (try-semaphore sem
5))
490 (assert (zerop (semaphore-count sem
)))))
492 (with-test (:name
(:try-semaphore
:emulate-wait-on-semaphore
))
493 (flet ((busy-wait-on-semaphore (sem)
494 (loop until
(try-semaphore sem
) do
(sleep 0.001))))
495 (test-semaphore-multiple-signals #'busy-wait-on-semaphore
)))
497 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
498 ;;; semaphore in a bad state.
499 (with-test (:name
(:try-semaphore
:interrupt-safe
))
500 (flet ((make-threads (count fn
)
501 (loop repeat count collect
(make-thread fn
)))
502 (kill-thread (thread)
503 (when (thread-alive-p thread
)
504 (ignore-errors (terminate-thread thread
))))
505 (count-live-threads (threads)
506 (count-if #'thread-alive-p threads
)))
507 ;; WAITERS will already be waiting on the semaphore while
508 ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
509 ;; semaphore, and MORE-WAITERS are new threads trying to wait on
510 ;; the semaphore during the interruption-fire.
511 (let* ((sem (make-semaphore :count
100))
512 (waiters (make-threads 20 #'(lambda ()
513 (wait-on-semaphore sem
))))
514 (triers (make-threads 40 #'(lambda ()
515 (sleep (random 0.01))
516 (try-semaphore sem
(1+ (random 5))))))
519 do
(kill-thread (nth (random 40) triers
))
520 collect
(make-thread #'(lambda () (wait-on-semaphore sem
)))
521 do
(kill-thread (nth (random 40) triers
)))))
523 ;; Now ensure that the waiting threads will all be waked up,
524 ;; i.e. that the semaphore is still working.
525 (loop repeat
(+ (count-live-threads waiters
)
526 (count-live-threads more-waiters
))
527 do
(signal-semaphore sem
))
529 (assert (zerop (count-live-threads triers
)))
530 (assert (zerop (count-live-threads waiters
)))
531 (assert (zerop (count-live-threads more-waiters
))))))
535 (format t
"~&semaphore tests done~%")
537 (defun test-interrupt (function-to-interrupt &optional quit-p
)
538 (let ((child (make-kill-thread function-to-interrupt
)))
539 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
541 (format t
"interrupting child ~A~%" child
)
542 (interrupt-thread child
544 (format t
"child pid ~A~%" *current-thread
*)
545 (when quit-p
(abort-thread))))
549 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
550 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
553 (with-test (:name
(:interrupt-thread
:more-basics
))
554 (let ((child (test-interrupt (lambda () (loop)))))
555 (terminate-thread child
)))
557 (with-test (:name
(:interrupt-thread
:interrupt-foreign-loop
)
558 ;; This feature is explicitly unsupported on Win32.
560 (test-interrupt #'loop-forever
:quit
))
562 (with-test (:name
(:interrupt-thread
:interrupt-sleep
))
563 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
564 (terminate-thread child
)
565 (wait-for-threads (list child
))))
567 (with-test (:name
(:interrupt-thread
:interrupt-mutex-acquisition
))
568 (let ((lock (make-mutex :name
"loctite"))
571 (setf child
(test-interrupt
574 (assert (eql (mutex-value lock
) *current-thread
*)))
575 (assert (not (eql (mutex-value lock
) *current-thread
*)))
577 ;;hold onto lock for long enough that child can't get it immediately
579 (interrupt-thread child
(lambda () (format t
"l ~A~%" (mutex-value lock
))))
580 (format t
"parent releasing lock~%"))
581 (terminate-thread child
)
582 (wait-for-threads (list child
))))
584 (format t
"~&locking test done~%")
586 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
588 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
))
589 (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
591 (loop repeat
4 collect
592 (sb-thread:make-thread
595 (sleep (random 0.1d0
))
598 (sb-thread:interrupt-thread thread
(lambda ()))))))))
599 (wait-for-threads killers
)
600 (sb-thread:terminate-thread thread
)
601 (wait-for-threads (list thread
))))
604 (format t
"~&multi interrupt test done~%")
606 #+(or x86 x86-64
) ;; x86oid-only, see internal commentary.
607 (with-test (:name
(:interrupt-thread
:interrupt-consing-child
:again
))
608 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
609 ;; NB this only works on x86: other ports don't have a symbol for
610 ;; pseudo-atomic atomicity
612 (sleep (random 0.1d0
))
615 (princ ".") (force-output)
616 (assert (thread-alive-p *current-thread
*))
618 (not (logbitp 0 SB-KERNEL
:*PSEUDO-ATOMIC-BITS
*))))))
620 (wait-for-threads (list c
))))
622 (format t
"~&interrupt test done~%")
624 (defstruct counter
(n 0 :type sb-vm
:word
))
625 (defvar *interrupt-counter
* (make-counter))
627 (declaim (notinline check-interrupt-count
))
628 (defun check-interrupt-count (i)
629 (declare (optimize (debug 1) (speed 1)))
630 ;; This used to lose if eflags were not restored after an interrupt.
631 (unless (typep i
'fixnum
)
632 (error "!!!!!!!!!!!")))
634 (with-test (:name
(:interrupt-thread
:interrupt-ATOMIC-INCF
))
635 (let ((c (make-thread
637 (handler-bind ((error #'(lambda (cond)
640 most-positive-fixnum
))))
641 (loop (check-interrupt-count
642 (counter-n *interrupt-counter
*))))))))
643 (let ((func (lambda ()
646 (sb-ext:atomic-incf
(counter-n *interrupt-counter
*)))))
647 (setf (counter-n *interrupt-counter
*) 0)
649 (sleep (random 0.1d0
))
650 (interrupt-thread c func
))
651 (loop until
(= (counter-n *interrupt-counter
*) 100) do
(sleep 0.1))
653 (wait-for-threads (list c
)))))
655 (format t
"~&interrupt count test done~%")
657 (defvar *runningp
* nil
)
659 (with-test (:name
(:interrupt-thread
:no-nesting
))
660 (let ((thread (sb-thread:make-thread
664 (declare (special runningp
))
666 (sb-thread:interrupt-thread thread
668 (let ((*runningp
* t
))
671 (sb-thread:interrupt-thread thread
673 (throw 'xxx
*runningp
*)))
674 (assert (not (sb-thread:join-thread thread
)))))
676 (with-test (:name
(:interrupt-thread
:nesting
))
677 (let ((thread (sb-thread:make-thread
681 (declare (special runningp
))
683 (sb-thread:interrupt-thread thread
685 (let ((*runningp
* t
))
686 (sb-sys:with-interrupts
689 (sb-thread:interrupt-thread thread
691 (throw 'xxx
*runningp
*)))
692 (assert (sb-thread:join-thread thread
))))
694 (with-test (:name
(:two-threads-running-gc
))
696 (make-join-thread (lambda ()
698 (sb-ext:gc
) (princ "\\") (force-output))
700 (make-join-thread (lambda ()
703 (princ "/") (force-output))
706 (when (and a-done b-done
) (return))
711 (defun waste (&optional
(n 100000))
712 (loop repeat n do
(make-string 16384)))
714 (with-test (:name
(:one-thread-runs-gc-while-other-conses
))
715 (loop for i below
100 do
726 (defparameter *aaa
* nil
)
727 (with-test (:name
(:one-thread-runs-gc-while-other-conses
:again
))
728 (loop for i below
100 do
733 (let ((*aaa
* (waste)))
735 (let ((*aaa
* (waste)))
739 (format t
"~&gc test done~%")
741 ;; this used to deadlock on session-lock
742 (with-test (:name
(:no-session-deadlock
))
743 (make-join-thread (lambda () (sb-ext:gc
))))
745 (defun exercise-syscall (fn reference-errno
)
750 (let ((errno (sb-unix::get-errno
)))
751 (sleep (random 0.1d0
))
752 (unless (eql errno reference-errno
)
753 (format t
"Got errno: ~A (~A) instead of ~A~%"
760 ;; (nanosleep -1 0) does not fail on FreeBSD
761 (with-test (:name
(:exercising-concurrent-syscalls
) :fails-on
:win32
)
763 (nanosleep-errno (progn
764 (sb-unix:nanosleep -
1 0)
765 (sb-unix::get-errno
)))
768 :if-does-not-exist nil
)
769 (sb-unix::get-errno
)))
773 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
774 (exercise-syscall (lambda () (open "no-such-file"
775 :if-does-not-exist nil
))
777 (make-join-thread (lambda () (loop (sb-ext:gc
) (sleep 1)))))))
779 (princ "terminating threads")
780 (dolist (thread threads
)
781 (sb-thread:terminate-thread thread
))))
783 (format t
"~&errno test done~%")
785 (with-test (:name
:all-threads-have-abort-restart
)
787 (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
788 (sb-thread:interrupt-thread
791 (assert (find-restart 'abort
)))))))
795 (format t
"~&thread startup sigmask test done~%")
797 (with-test (:name
(:debugger-no-hang-on-session-lock-if-interrupted
)
799 #+win32
(error "user would have to touch a key interactively to proceed")
800 (sb-debug::enable-debugger
)
801 (let* ((main-thread *current-thread
*)
803 (make-thread (lambda ()
805 (interrupt-thread main-thread
810 (interrupt-thread main-thread
#'continue
))
811 :name
"interruptor")))
812 (with-session-lock (*session
*)
814 (loop while
(thread-alive-p interruptor-thread
))))
816 (format t
"~&session lock test done~%")
818 ;; expose thread creation races by exiting quickly
819 (with-test (:name
(:no-thread-creation-race
:light
))
820 (make-join-thread (lambda ())))
822 (with-test (:name
(:no-thread-creation-race
:heavy
))
825 (loop for i below
100 collect
826 (sb-thread:make-thread
(lambda ()))))))
828 (format t
"~&creation test done~%")
830 ;; interrupt handlers are per-thread with pthreads, make sure the
831 ;; handler installed in one thread is global
832 (with-test (:name
(:global-interrupt-handler
))
835 (sb-ext:run-program
"sleep" '("1") :search t
:wait nil
))))
837 ;;;; Binding stack safety
839 (defparameter *x
* nil
)
840 (defparameter *n-gcs-requested
* 0)
841 (defparameter *n-gcs-done
* 0)
844 (defun make-something-big ()
845 (let ((x (make-string 32000)))
847 (let ((counter counter
))
848 (sb-ext:finalize x
(lambda () (format t
" ~S" counter
)
851 (defmacro wait-for-gc
()
853 (incf *n-gcs-requested
*)
854 (loop while
(< *n-gcs-done
* *n-gcs-requested
*))))
857 (loop until
(< *n-gcs-done
* *n-gcs-requested
*))
863 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
864 (defun exercise-binding ()
866 (let ((*x
* (make-something-big)))
868 ;; at this point the binding stack looks like this:
869 ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
872 ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
873 ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
874 ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
875 ;; unbinding but values are not).
877 (binding-pointer-delta (ash 2 (- sb-vm
:word-shift sb-vm
:n-fixnum-tag-bits
))))
878 ;; bump bsp as if a BIND had just started
879 (incf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
)
881 (decf sb-vm
::*binding-stack-pointer
* binding-pointer-delta
))))
883 #+(or x86 x86-64
) ;the only platforms with a *binding-stack-pointer* variable
884 (with-test (:name
(:binding-stack-gc-safety
))
888 (push (make-kill-thread #'exercise-binding
) threads
)
889 (push (make-kill-thread (lambda ()
895 (mapc #'sb-thread
:terminate-thread threads
))))
897 (format t
"~&binding test done~%")
901 (defvar *errors
* nil
)
905 (format t
"~&oops: ~A in ~S~%" e
*current-thread
*)
909 (with-test (:name
(:unsynchronized-hash-table
)
910 ;; FIXME: This test occasionally eats out craploads
911 ;; of heap instead of expected error early. Not 100%
912 ;; sure if it would finish as expected, but since it
913 ;; hits swap on my system I'm not likely to find out
914 ;; soon. Disabling for now. -- nikodemus
916 ;; We expect a (probable) error here: parellel readers and writers
917 ;; on a hash-table are not expected to work -- but we also don't
918 ;; expect this to corrupt the image.
919 (let* ((hash (make-hash-table))
921 (threads (list (make-kill-thread
924 (handler-bind ((serious-condition 'oops
))
926 ;;(princ "1") (force-output)
927 (setf (gethash (random 100) hash
) 'h
)))))
932 (handler-bind ((serious-condition 'oops
))
934 ;;(princ "2") (force-output)
935 (remhash (random 100) hash
)))))
940 (handler-bind ((serious-condition 'oops
))
943 (sb-ext:gc
:full t
)))))
944 :name
"collector"))))
947 (mapc #'sb-thread
:terminate-thread threads
))))
949 (format t
"~&unsynchronized hash table test done~%")
951 (with-test (:name
(:synchronized-hash-table
))
952 (let* ((hash (make-hash-table :synchronized t
))
954 (threads (list (make-join-thread
957 (handler-bind ((serious-condition 'oops
))
959 ;;(princ "1") (force-output)
960 (setf (gethash (random 100) hash
) 'h
)))))
965 (handler-bind ((serious-condition 'oops
))
967 ;;(princ "2") (force-output)
968 (remhash (random 100) hash
)))))
973 (handler-bind ((serious-condition 'oops
))
976 (sb-ext:gc
:full t
)))))
977 :name
"collector"))))
980 (mapc #'sb-thread
:terminate-thread threads
))
981 (assert (not *errors
*))))
983 (format t
"~&synchronized hash table test done~%")
985 (with-test (:name
(:hash-table-parallel-readers
))
986 (let ((hash (make-hash-table))
989 do
(setf (gethash (random 100) hash
) 'xxx
))
990 (let ((threads (list (make-kill-thread
993 (handler-bind ((serious-condition 'oops
))
995 until
(eq t
(gethash (random 100) hash
))))))
1000 (handler-bind ((serious-condition 'oops
))
1002 until
(eq t
(gethash (random 100) hash
))))))
1007 (handler-bind ((serious-condition 'oops
))
1009 until
(eq t
(gethash (random 100) hash
))))))
1014 (handler-bind ((serious-condition 'oops
))
1016 (sleep (random 1.0))
1017 (sb-ext:gc
:full t
)))))
1018 :name
"collector"))))
1021 (mapc #'sb-thread
:terminate-thread threads
))
1022 (assert (not *errors
*)))))
1024 (format t
"~&multiple reader hash table test done~%")
1026 (with-test (:name
(:hash-table-single-accessor-parallel-gc
))
1027 (let ((hash (make-hash-table))
1029 (let ((threads (list (make-kill-thread
1031 (handler-bind ((serious-condition 'oops
))
1033 (let ((n (random 100)))
1034 (if (gethash n hash
)
1036 (setf (gethash n hash
) 'h
))))))
1040 (handler-bind ((serious-condition 'oops
))
1042 (sleep (random 1.0))
1043 (sb-ext:gc
:full t
))))
1044 :name
"collector"))))
1047 (mapc #'sb-thread
:terminate-thread threads
))
1048 (assert (not *errors
*)))))
1050 (format t
"~&single accessor hash table test~%")
1052 #|
;; a cll post from eric marsden
1054 |
(setq *debugger-hook
*
1055 |
(lambda (condition old-debugger-hook
)
1056 |
(debug:backtrace
10)
1057 |
(unix:unix-exit
2)))
1058 |
#+live-dangerously
1059 |
(mp::start-sigalrm-yield
)
1060 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
1061 |
(mp:make-process
#'roomy
)
1062 |
(mp:make-process
#'roomy
)))
1065 (with-test (:name
(:condition-variable
:notify-multiple
))
1066 (flet ((tester (notify-fun)
1067 (let ((queue (make-waitqueue :name
"queue"))
1068 (lock (make-mutex :name
"lock"))
1073 (format t
"condition-wait ~a~%" x
)
1075 (condition-wait queue lock
)
1076 (format t
"woke up ~a~%" x
)
1079 (let ((threads (loop for x from
1 to
10
1082 (make-kill-thread (lambda ()
1086 (funcall notify-fun queue
))
1088 (mapcar #'terminate-thread threads
)
1089 ;; Check that all threads woke up at least once
1090 (assert (= (length (remove-duplicates data
)) 10)))))))
1091 (tester (lambda (queue)
1092 (format t
"~&(condition-notify queue 10)~%")
1094 (condition-notify queue
10)))
1095 (tester (lambda (queue)
1096 (format t
"~&(condition-broadcast queue)~%")
1098 (condition-broadcast queue
)))))
1100 (format t
"waitqueue wakeup tests done~%")
1102 ;;; Make sure that a deadline handler is not invoked twice in a row in
1103 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1105 (with-test (:name
(:condition-wait
:deadlines
:LP-512914
))
1106 (let ((n 2) ; was empirically enough to trigger the bug
1107 (mutex (sb-thread:make-mutex
))
1108 (waitq (sb-thread:make-waitqueue
))
1110 (deadline-handler-run-twice? nil
))
1113 (sb-thread:make-thread
1116 ((sb-sys:deadline-timeout
1117 (let ((already? nil
))
1120 (setq deadline-handler-run-twice? t
))
1123 (sb-thread:condition-broadcast waitq
)
1124 (sb-sys:defer-deadline
10.0 c
)))))
1125 (sb-sys:with-deadline
(:seconds
0.1)
1126 (sb-thread:with-mutex
(mutex)
1127 (sb-thread:condition-wait waitq mutex
))))))))
1128 (push child threads
)))
1129 (mapc #'sb-thread
:join-thread threads
)
1130 (assert (not deadline-handler-run-twice?
))))
1132 (with-test (:name
(:condition-wait
:signal-deadline-with-interrupts-enabled
))
1133 (let ((mutex (sb-thread:make-mutex
))
1134 (waitq (sb-thread:make-waitqueue
))
1137 (A-interrupts-enabled?
:unknown
)
1138 (B-interrupts-enabled?
:unknown
)
1141 ;; W.L.O.G., we assume that A is executed first...
1142 (setq A
(sb-thread:make-thread
1145 ((sb-sys:deadline-timeout
1147 ;; We came here through the call to DECODE-TIMEOUT
1148 ;; in CONDITION-WAIT; hence both here are supposed
1149 ;; to evaluate to T.
1150 (setq A-holds?
(sb-thread:holding-mutex-p mutex
))
1151 (setq A-interrupts-enabled?
1152 sb-sys
:*interrupts-enabled
*)
1154 (sb-thread:condition-broadcast waitq
)
1155 (sb-sys:defer-deadline
10.0 c
))))
1156 (sb-sys:with-deadline
(:seconds
0.1)
1157 (sb-thread:with-mutex
(mutex)
1158 (sb-thread:condition-wait waitq mutex
)))))
1160 (setq B
(sb-thread:make-thread
1164 ((sb-sys:deadline-timeout
1166 ;; We came here through the call to DECODE-TIMEOUT
1167 ;; in CONDITION-WAIT (contended case of
1168 ;; reaquiring the mutex) - so the former will
1169 ;; be NIL, but interrupts should still be enabled.
1170 (setq B-holds?
(sb-thread:holding-mutex-p mutex
))
1171 (setq B-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 (sb-thread:join-thread A
)
1181 (sb-thread:join-thread B
)
1182 (let ((A-result (list A-holds? A-interrupts-enabled?
))
1183 (B-result (list B-holds? B-interrupts-enabled?
)))
1184 ;; We also check some subtle behaviour w.r.t. whether a deadline
1185 ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1186 ;; probably very internal behaviour (so user should not depend
1187 ;; on it) -- I added the testing here just to manifest current
1189 (cond ((equal A-result
'(t t
)) (assert (equal B-result
'(nil t
))))
1190 ((equal B-result
'(t t
)) (assert (equal A-result
'(nil t
))))
1192 (error "Failure: fell through wit A: ~S, B: ~S"
1196 (with-test (:name
(:mutex
:finalization
))
1199 (setf a
(make-mutex)))))
1201 (format t
"mutex finalization test done~%")
1203 ;;; Check that INFO is thread-safe, at least when we're just doing reads.
1205 (let* ((symbols (loop repeat
10000 collect
(gensym)))
1206 (functions (loop for
(symbol . rest
) on symbols
1207 for next
= (car rest
)
1208 for fun
= (let ((next next
))
1211 (funcall next
(1- n
))
1213 do
(setf (symbol-function symbol
) fun
)
1215 (defun infodb-test ()
1216 (funcall (car functions
) 9999)))
1218 (with-test (:name
(:infodb
:read
))
1220 (threads (loop for i from
0 to
10
1221 collect
(sb-thread:make-thread
1226 (let ((n (infodb-test)))
1229 (format t
"N != 0 (~A)~%" n
)
1230 (abort-thread)))))))))
1231 (wait-for-threads threads
)
1234 (format t
"infodb test done~%")
1236 (with-test (:name
:backtrace
)
1237 ;; Printing backtraces from several threads at once used to hang the
1238 ;; whole SBCL process (discovered by accident due to a timer.impure
1239 ;; test misbehaving). The cause was that packages weren't even
1240 ;; thread-safe for only doing FIND-SYMBOL, and while printing
1241 ;; backtraces a loot of symbol lookups need to be done due to
1243 (let* ((threads (loop repeat
10
1244 collect
(sb-thread:make-thread
1247 (with-output-to-string (*debug-io
*)
1248 (sb-debug::backtrace
10))))))))
1249 (wait-for-threads threads
)))
1251 (format t
"backtrace test done~%")
1253 (format t
"~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1255 (with-test (:name
:gc-deadlock
)
1256 ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1257 ;; GC due to *all-threads-lock* and session lock. On earlier
1258 ;; versions and at least on one specific box this test is good enough
1259 ;; to catch that typically well before the 1500th iteration.
1266 (when (zerop (mod i
100))
1273 (sleep (random 0.001)))
1274 :name
(format nil
"SLEEP-~D" i
))
1277 ;; KLUDGE: what we are doing here is explicit,
1278 ;; but the same can happen because of a regular
1279 ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1280 ;; session functions.
1281 (sb-thread::with-all-threads-lock
1282 (sb-thread::with-session-lock
(sb-thread::*session
*)
1284 :name
(format nil
"GC-~D" i
)))
1286 (format t
"~%error creating thread ~D: ~A -- backing off for retry~%" i e
)
1290 (format t
"~&gc deadlock test done~%")
1292 (let ((count (make-array 8 :initial-element
0)))
1293 (defun closure-one ()
1294 (declare (optimize safety
))
1295 (values (incf (aref count
0)) (incf (aref count
1))
1296 (incf (aref count
2)) (incf (aref count
3))
1297 (incf (aref count
4)) (incf (aref count
5))
1298 (incf (aref count
6)) (incf (aref count
7))))
1299 (defun no-optimizing-away-closure-one ()
1300 (setf count
(make-array 8 :initial-element
0))))
1305 (let ((one (make-box))
1308 (defun closure-two ()
1309 (declare (optimize safety
))
1310 (values (incf (box-count one
)) (incf (box-count two
)) (incf (box-count three
))))
1311 (defun no-optimizing-away-closure-two ()
1312 (setf one
(make-box)
1316 ;;; PowerPC safepoint builds occasionally hang or busy-loop (or
1317 ;;; sometimes run out of memory) in the following test. For developers
1318 ;;; interested in debugging this combination of features, it might be
1319 ;;; fruitful to concentrate their efforts around this test...
1321 (with-test (:name
(:funcallable-instances
)
1322 :skipped-on
'(and :sb-safepoint
1323 (not :c-stack-is-control-stack
)))
1324 ;; the funcallable-instance implementation used not to be threadsafe
1325 ;; against setting the funcallable-instance function to a closure
1326 ;; (because the code and lexenv were set separately).
1327 (let ((fun (sb-kernel:%make-funcallable-instance
0))
1329 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1331 (loop (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-one
)
1332 (setf (sb-kernel:funcallable-instance-fun fun
) #'closure-two
)))
1334 (handler-case (loop (funcall fun
))
1335 (serious-condition (c) (setf condition c
)))))
1336 (let ((changer (make-thread #'changer
))
1337 (test (make-thread #'test
)))
1340 ;; The two closures above are fairly carefully crafted
1341 ;; so that if given the wrong lexenv they will tend to
1342 ;; do some serious damage, but it is of course difficult
1343 ;; to predict where the various bits and pieces will be
1344 ;; allocated. Five seconds failed fairly reliably on
1345 ;; both my x86 and x86-64 systems. -- CSR, 2006-09-27.
1346 (sb-ext:with-timeout
5
1347 (wait-for-threads (list test
)))
1348 (error "~@<test thread got condition:~2I~_~A~@:>" condition
))
1350 (terminate-thread changer
)
1351 (terminate-thread test
)
1352 (wait-for-threads (list changer test
))))))))
1354 (format t
"~&funcallable-instance test done~%")
1356 (defun random-type (n)
1357 `(integer ,(random n
) ,(+ n
(random n
))))
1359 (defun subtypep-hash-cache-test ()
1361 (let ((type1 (random-type 500))
1362 (type2 (random-type 500)))
1363 (let ((a (subtypep type1 type2
)))
1365 (assert (eq (subtypep type1 type2
) a
))))))
1369 (with-test (:name
(:hash-cache
:subtypep
))
1372 collect
(sb-thread:make-thread
#'subtypep-hash-cache-test
))))
1373 (format t
"hash-cache tests done~%")
1375 ;;;; BLACK BOX TESTS
1377 (in-package :cl-user
)
1378 (use-package :test-util
)
1379 (use-package "ASSERTOID")
1381 (format t
"parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1382 (with-test (:name
:parallel-defclass
)
1383 (defclass test-1
() ((a :initform
:orig-a
)))
1384 (defclass test-2
() ((b :initform
:orig-b
)))
1385 (defclass test-3
(test-1 test-2
) ((c :initform
:orig-c
)))
1386 ;; This test is more likely to pass on Windows with the FORCE-OUTPUT
1387 ;; calls disabled in the folloving code. (As seen on a Server 2012
1388 ;; installation.) Clearly, this sort of workaround in a test is
1389 ;; cheating, and might be hiding the underlying bug that the test is
1390 ;; exposing. Let's review this later.
1392 (d1 (sb-thread:make-thread
(lambda ()
1394 do
(defclass test-1
() ((a :initform
:new-a
)))
1396 #-win32
(force-output)))
1398 (d2 (sb-thread:make-thread
(lambda ()
1400 do
(defclass test-2
() ((b :initform
:new-b
)))
1402 #-win32
(force-output)))
1404 (d3 (sb-thread:make-thread
(lambda ()
1406 do
(defclass test-3
(test-1 test-2
) ((c :initform
:new-c
)))
1408 #-win32
(force-output)))
1410 (i (sb-thread:make-thread
(lambda ()
1412 do
(let ((i (make-instance 'test-3
)))
1413 (assert (member (slot-value i
'a
) '(:orig-a
:new-a
)))
1414 (assert (member (slot-value i
'b
) '(:orig-b
:new-b
)))
1415 (assert (member (slot-value i
'c
) '(:orig-c
:new-c
))))
1417 #-win32
(force-output)))
1419 (format t
"~%sleeping!~%")
1421 (format t
"~%stopping!~%")
1424 (sb-thread:join-thread th
)
1425 (format t
"~%joined ~S~%" (sb-thread:thread-name th
)))
1428 (format t
"parallel defclass test done~%")
1430 (with-test (:name
(:deadlock-detection
:interrupts
) :fails-on
:win32
)
1431 #+win32
;be more explicit than just :skipped-on
1432 (error "not attempting, because of deadlock error in background thread")
1433 (let* ((m1 (sb-thread:make-mutex
:name
"M1"))
1434 (m2 (sb-thread:make-mutex
:name
"M2"))
1435 (t1-can-go (sb-thread:make-semaphore
:name
"T1 can go"))
1436 (t2-can-go (sb-thread:make-semaphore
:name
"T2 can go"))
1437 (t1 (sb-thread:make-thread
1439 (sb-thread:with-mutex
(m1)
1440 (sb-thread:wait-on-semaphore t1-can-go
)
1443 (t2 (sb-thread:make-thread
1445 (sb-ext:wait-for
(eq t1
(sb-thread:mutex-owner m1
)))
1446 (sb-thread:with-mutex
(m1 :wait-p t
)
1447 (sb-thread:wait-on-semaphore t2-can-go
)
1450 (sb-ext:wait-for
(eq m1
(sb-thread::thread-waiting-for t2
)))
1451 (sb-thread:interrupt-thread t2
(lambda ()
1452 (sb-thread:with-mutex
(m2 :wait-p t
)
1454 (eq m2
(sb-thread::thread-waiting-for t1
)))
1455 (sb-thread:signal-semaphore t2-can-go
))))
1456 (sb-ext:wait-for
(eq t2
(sb-thread:mutex-owner m2
)))
1457 (sb-thread:interrupt-thread t1
(lambda ()
1458 (sb-thread:with-mutex
(m2 :wait-p t
)
1459 (sb-thread:signal-semaphore t1-can-go
))))
1460 ;; both threads should finish without a deadlock or deadlock
1462 (let ((res (list (sb-thread:join-thread t1
)
1463 (sb-thread:join-thread t2
))))
1464 (assert (equal '(:ok1
:ok2
) res
)))))
1466 (with-test (:name
(:deadlock-detection
:gc
))
1467 ;; To semi-reliably trigger the error (in SBCL's where)
1468 ;; it was present you had to run this for > 30 seconds,
1469 ;; but that's a bit long for a single test.
1470 (let* ((stop (+ 5 (get-universal-time)))
1471 (m1 (sb-thread:make-mutex
:name
"m1"))
1472 (t1 (sb-thread:make-thread
1474 (loop until
(> (get-universal-time) stop
)
1475 do
(sb-thread:with-mutex
(m1)
1476 (eval `(make-array 24))))
1478 (t2 (sb-thread:make-thread
1480 (loop until
(> (get-universal-time) stop
)
1481 do
(sb-thread:with-mutex
(m1)
1482 (eval `(make-array 24))))
1484 (let ((res (list (sb-thread:join-thread t1
)
1485 (sb-thread:join-thread t2
))))
1486 (assert (equal '(:ok
:ok
) res
)))))
1488 (with-test (:name
:spinlock-api
)
1491 (handler-bind ((sb-int:early-deprecation-warning
(lambda (_)
1492 (declare (ignore _
))
1494 (list (compile nil
`(lambda (lock)
1495 (sb-thread::with-spinlock
(lock)
1497 (compile nil
`(lambda ()
1498 (sb-thread::make-spinlock
:name
"foo")))
1499 (compile nil
`(lambda (lock)
1500 (sb-thread::get-spinlock lock
)))
1501 (compile nil
`(lambda (lock)
1502 (sb-thread::release-spinlock lock
)))))))
1503 (assert (eql 4 warned
))
1504 (handler-bind ((warning #'error
))
1505 (destructuring-bind (with make get release
) funs
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
))))