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.
14 (cl:defpackage
#:thread-test
15 (:use
#:cl
#:sb-thread
#:sb-ext
#:test-util
#:assertoid
))
17 (cl:in-package
#:thread-test
)
19 (with-test (:name atomic-update
20 :skipped-on
(not :sb-thread
))
21 (let ((x (cons :count
0))
22 (nthreads (ecase sb-vm
:n-word-bits
(32 100) (64 1000))))
25 collect
(make-thread (lambda ()
27 do
(atomic-update (cdr x
) #'1+)
29 (assert (equal x
`(:count
,@(* 1000 nthreads
))))))
31 (with-test (:name mutex-owner
)
32 ;; Make sure basics are sane on unithreaded ports as well
33 (let ((mutex (make-mutex)))
35 (assert (eq *current-thread
* (mutex-value mutex
)))
36 (handler-bind ((warning #'error
))
37 (release-mutex mutex
))
38 (assert (not (mutex-value mutex
)))))
40 ;;; Terminating a thread that's waiting for the terminal.
42 (with-test (:name
(:terminate-thread
:get-foreground
)
43 :skipped-on
(not :sb-thread
)
45 (let ((thread (make-thread (lambda ()
46 (sb-thread::get-foreground
)))))
48 (assert (thread-alive-p thread
))
49 (terminate-thread thread
)
51 (assert (not (thread-alive-p thread
)))))
53 ;;; Condition-wait should not be interruptible under WITHOUT-INTERRUPTS
55 (with-test (:name
:without-interrupts
+condition-wait
56 :skipped-on
(not :sb-thread
)
58 (let* ((lock (make-mutex))
59 (queue (make-waitqueue))
60 (thread (make-thread (lambda ()
61 (sb-sys:without-interrupts
63 (condition-wait queue lock
)))))))
65 (assert (thread-alive-p thread
))
66 (terminate-thread thread
)
68 (assert (thread-alive-p thread
))
69 (condition-notify queue
)
71 (assert (not (thread-alive-p thread
)))))
73 ;;; GRAB-MUTEX should not be interruptible under WITHOUT-INTERRUPTS
75 (with-test (:name
:without-interrupts
+grab-mutex
76 :skipped-on
(not :sb-thread
)
78 (let* ((lock (make-mutex))
79 (bar (progn (grab-mutex lock
) nil
))
80 (thread (make-thread (lambda ()
81 (sb-sys:without-interrupts
85 (assert (thread-alive-p thread
))
86 (terminate-thread thread
)
88 (assert (thread-alive-p thread
))
91 (assert (not (thread-alive-p thread
)))
92 (assert (eq :aborted
(join-thread thread
:default
:aborted
)))
95 (with-test (:name
:parallel-find-class
:skipped-on
(not :sb-thread
))
97 (threads (loop repeat
10
98 collect
(make-thread (lambda ()
101 do
(find-class (gensym) nil
))
102 (serious-condition ()
104 (mapc #'join-thread threads
)
105 (assert (not oops
))))
107 (with-test (:name
:semaphore-multiple-waiters
:skipped-on
(not :sb-thread
))
108 (let ((semaphore (make-semaphore :name
"test sem")))
109 (labels ((make-readers (n i
)
111 (loop for r from
0 below n
115 (let ((sem semaphore
))
117 (wait-on-semaphore sem
))))
120 (make-writers (n readers i
)
121 (let ((j (* readers i
)))
122 (multiple-value-bind (k rem
) (truncate j n
)
125 (loop for w from
0 below n
129 (let ((sem semaphore
))
131 (signal-semaphore sem
))))
137 (multiple-value-bind (readers x
) (make-readers r n
)
138 (assert (= (length readers
) r
))
139 (multiple-value-bind (writers y
) (make-writers w r n
)
140 (assert (= (length writers
) w
))
142 (mapc #'join-thread writers
)
143 (mapc #'join-thread readers
)
144 (assert (zerop (semaphore-count semaphore
)))
149 (sb-ext:with-timeout
10
160 ;;;; Printing waitqueues
162 (with-test (:name
:waitqueue-circle-print
:skipped-on
(not :sb-thread
))
163 (let* ((*print-circle
* nil
)
165 (wq (make-waitqueue)))
166 (with-recursive-lock (lock)
167 (condition-notify wq
))
168 ;; Used to blow stack due to recursive structure.
169 (assert (princ-to-string wq
))))
171 ;;;; SYMBOL-VALUE-IN-THREAD
173 (with-test (:name
:symbol-value-in-thread
.1)
174 (let ((* (cons t t
)))
175 (assert (eq * (symbol-value-in-thread '* *current-thread
*)))
176 (setf (symbol-value-in-thread '* *current-thread
*) 123)
177 (assert (= 123 (symbol-value-in-thread '* *current-thread
*)))
180 (with-test (:name
:symbol-value-in-thread
.2 :skipped-on
(not :sb-thread
))
181 (let* ((parent *current-thread
*)
182 (semaphore (make-semaphore))
183 (child (make-thread (lambda ()
184 (wait-on-semaphore semaphore
)
185 (let ((old (symbol-value-in-thread 'this-is-new parent
)))
186 (setf (symbol-value-in-thread 'this-is-new parent
) :from-child
)
188 (progv '(this-is-new) '(42)
189 (signal-semaphore semaphore
)
190 (assert (= 42 (join-thread child
)))
191 (assert (eq :from-child
(symbol-value 'this-is-new
))))))
193 ;;; Disabled on Darwin due to deadlocks caused by apparent OS specific deadlocks,
194 ;;; wich _appear_ to be caused by malloc() and free() not being thread safe: an
195 ;;; interrupted malloc in one thread can apparently block a free in another.
196 (with-test (:name
:symbol-value-in-thread
.3
197 :skipped-on
(not :sb-thread
))
198 (let* ((parent *current-thread
*)
199 (semaphore (make-semaphore))
201 (noise (make-thread (lambda ()
203 do
(setf * (make-array 1024))
204 ;; Busy-wait a bit so we don't TOTALLY flood the
205 ;; system with GCs: a GC occurring in the middle of
206 ;; S-V-I-T causes it to start over -- we want that
207 ;; to occur occasionally, but not _all_ the time.
208 (loop repeat
(random 128)
211 (dotimes (i #+(or win32 openbsd
) 2000
212 #-
(or win32 openbsd
) 15000)
213 (when (zerop (mod i
200))
216 (let* ((mom-mark (cons t t
))
217 (kid-mark (cons t t
))
218 (child (make-thread (lambda ()
219 (wait-on-semaphore semaphore
)
220 (let ((old (symbol-value-in-thread 'this-is-new parent
)))
221 (setf (symbol-value-in-thread 'this-is-new parent
)
222 (make-array 24 :initial-element kid-mark
))
224 (progv '(this-is-new) (list (make-array 24 :initial-element mom-mark
))
225 (signal-semaphore semaphore
)
226 (assert (eq mom-mark
(aref (join-thread child
) 0)))
227 (assert (eq kid-mark
(aref (symbol-value 'this-is-new
) 0))))))
229 (join-thread noise
)))
231 (with-test (:name
:symbol-value-in-thread
.4 :skipped-on
(not :sb-thread
))
232 (let* ((parent *current-thread
*)
233 (semaphore (make-semaphore))
234 (child (make-thread (lambda ()
235 (wait-on-semaphore semaphore
)
236 (symbol-value-in-thread 'this-is-new parent nil
)))))
237 (signal-semaphore semaphore
)
238 (assert (equal '(nil nil
) (multiple-value-list (join-thread child
))))))
240 (with-test (:name
:symbol-value-in-thread
.5 :skipped-on
(not :sb-thread
))
241 (let* ((parent *current-thread
*)
242 (semaphore (make-semaphore))
243 (child (make-thread (lambda ()
244 (wait-on-semaphore semaphore
)
246 (symbol-value-in-thread 'this-is-new parent
)
247 (symbol-value-in-thread-error (e)
248 (list (thread-error-thread e
)
250 (sb-thread::symbol-value-in-thread-error-info e
))))))))
251 (signal-semaphore semaphore
)
252 (assert (equal (list *current-thread
* 'this-is-new
(list :read
:unbound-in-thread
))
253 (join-thread child
)))))
255 (with-test (:name
:symbol-value-in-thread
.6 :skipped-on
(not :sb-thread
))
256 (let* ((parent *current-thread
*)
257 (semaphore (make-semaphore))
259 (child (make-thread (lambda ()
260 (wait-on-semaphore semaphore
)
262 (setf (symbol-value-in-thread name parent
) t
)
263 (symbol-value-in-thread-error (e)
264 (list (thread-error-thread e
)
266 (sb-thread::symbol-value-in-thread-error-info e
))))))))
267 (signal-semaphore semaphore
)
268 (let ((res (join-thread child
))
269 (want (list *current-thread
* name
(list :write
:no-tls-value
))))
270 (unless (equal res want
)
271 (error "wanted ~S, got ~S" want res
)))))
273 (with-test (:name
:symbol-value-in-thread
.7 :skipped-on
(not :sb-thread
))
274 (let ((child (make-thread (lambda ())))
275 (error-occurred nil
))
278 (symbol-value-in-thread 'this-is-new child
)
279 (symbol-value-in-thread-error (e)
280 (setf error-occurred t
)
281 (assert (eq child
(thread-error-thread e
)))
282 (assert (eq 'this-is-new
(cell-error-name e
)))
283 (assert (equal (list :read
:thread-dead
)
284 (sb-thread::symbol-value-in-thread-error-info e
)))))
285 (assert error-occurred
)))
287 (with-test (:name
:symbol-value-in-thread
.8 :skipped-on
(not :sb-thread
))
288 (let ((child (make-thread (lambda ())))
289 (error-occurred nil
))
292 (setf (symbol-value-in-thread 'this-is-new child
) t
)
293 (symbol-value-in-thread-error (e)
294 (setf error-occurred t
)
295 (assert (eq child
(thread-error-thread e
)))
296 (assert (eq 'this-is-new
(cell-error-name e
)))
297 (assert (equal (list :write
:thread-dead
)
298 (sb-thread::symbol-value-in-thread-error-info e
)))))
299 (assert error-occurred
)))
301 (with-test (:name
:deadlock-detection
.1 :skipped-on
(not :sb-thread
))
304 do
(flet ((test (ma mb sa sb
)
308 (signal-semaphore sa
)
309 (wait-on-semaphore sb
)
313 ;; (assert (plusp (length ...))) prevents
315 (assert (plusp (length (princ-to-string e
))))
317 (let* ((m1 (make-mutex :name
"M1"))
318 (m2 (make-mutex :name
"M2"))
319 (s1 (make-semaphore :name
"S1"))
320 (s2 (make-semaphore :name
"S2"))
321 (t1 (make-thread (test m1 m2 s1 s2
) :name
"T1"))
322 (t2 (make-thread (test m2 m1 s2 s1
) :name
"T2")))
323 ;; One will deadlock, and the other will then complete normally.
324 (let ((res (list (join-thread t1
)
326 (assert (or (equal '(:deadlock
:ok
) res
)
327 (equal '(:ok
:deadlock
) res
))))))))
329 (with-test (:name
:deadlock-detection
.2 :skipped-on
(not :sb-thread
))
330 (let* ((m1 (make-mutex :name
"M1"))
331 (m2 (make-mutex :name
"M2"))
332 (s1 (make-semaphore :name
"S1"))
333 (s2 (make-semaphore :name
"S2"))
337 (signal-semaphore s1
)
338 (wait-on-semaphore s2
)
344 (handler-bind ((thread-deadlock
347 ;; Make sure we can print the condition
349 (let ((*print-circle
* nil
))
350 (setf err
(princ-to-string e
)))
354 (assert (eq :ok
(with-mutex (m2)
356 (signal-semaphore s2
)
357 (wait-on-semaphore s1
)
361 (assert (stringp err
)))
362 (assert (eq :ok
(join-thread t1
)))))
364 (with-test (:name
:deadlock-detection
.3 :skipped-on
(not :sb-thread
))
365 (let* ((m1 (make-mutex :name
"M1"))
366 (m2 (make-mutex :name
"M2"))
367 (s1 (make-semaphore :name
"S1"))
368 (s2 (make-semaphore :name
"S2"))
372 (signal-semaphore s1
)
373 (wait-on-semaphore s2
)
377 ;; Currently we don't consider it a deadlock
378 ;; if there is a timeout in the chain.
379 (assert (eq :deadline
382 (signal-semaphore s2
)
383 (wait-on-semaphore s1
)
385 (sb-sys:with-deadline
(:seconds
0.1)
388 (sb-sys:deadline-timeout
()
392 (assert (eq :ok
(join-thread t1
)))))
395 (with-test (:name
:pass-arguments-to-thread
)
396 (assert (= 3 (join-thread (make-thread #'+ :arguments
'(1 2))))))
399 (with-test (:name
:pass-atom-to-thread
)
400 (assert (= 1/2 (join-thread (make-thread #'/ :arguments
2)))))
403 (with-test (:name
:pass-nil-to-thread
)
404 (assert (= 1 (join-thread (make-thread #'* :arguments
'())))))
407 (with-test (:name
:pass-nothing-to-thread
)
408 (assert (= 1 (join-thread (make-thread #'*)))))
411 (with-test (:name
:pass-improper-list-to-thread
)
412 (multiple-value-bind (value error
)
413 (ignore-errors (make-thread #'+ :arguments
'(1 .
1)))
416 (assert (and (null value
)
419 (with-test (:name
(:wait-for
:basics
))
420 (assert (not (sb-ext:wait-for nil
:timeout
0.1)))
421 (assert (eql 42 (sb-ext:wait-for
42)))
423 (assert (eql 100 (sb-ext:wait-for
(when (= 100 (incf n
))
426 (with-test (:name
(:wait-for
:deadline
))
428 (sb-sys:with-deadline
(:seconds
10)
429 (assert (not (sb-ext:wait-for nil
:timeout
0.1)))
431 (assert (eq :deadline
433 (sb-sys:with-deadline
(:seconds
0.1)
434 (sb-ext:wait-for nil
:timeout
10)
436 (sb-sys:deadline-timeout
() :deadline
)))))
438 (with-test (:name
(:condition-wait
:timeout
:one-thread
))
439 (let ((mutex (make-mutex))
440 (waitqueue (make-waitqueue)))
441 (assert (not (with-mutex (mutex)
442 (condition-wait waitqueue mutex
:timeout
0.01))))))
444 (with-test (:name
(:condition-wait
:timeout
:many-threads
)
445 :skipped-on
(not :sb-thread
))
446 (let* ((mutex (make-mutex))
447 (waitqueue (make-waitqueue))
448 (sem (make-semaphore))
454 (wait-on-semaphore sem
)
458 do
(or (condition-wait waitqueue mutex
:timeout
0.01)
459 (return-from thread nil
)))
460 (assert (eq t
(pop data
)))
463 do
(with-mutex (mutex)
465 (condition-notify waitqueue
)))
466 (signal-semaphore sem
100)
467 (let ((ok (count-if #'join-thread workers
)))
469 (error "Wanted 50, got ~S" ok
)))))
471 (with-test (:name
(wait-on-semaphore :timeout
:one-thread
))
473 (semaphore (make-semaphore)))
474 (signal-semaphore semaphore count
)
475 (let ((values (loop repeat
100
476 collect
(wait-on-semaphore semaphore
:timeout
0.001)))
477 (expected (loop for i from
9 downto
0 collect i
)))
478 (assert (equal (remove nil values
) expected
)))))
480 (with-test (:name
(wait-on-semaphore :timeout
:many-threads
)
481 :skipped-on
(not :sb-thread
))
483 (semaphore (make-semaphore)))
484 ;; Add 10 tokens right away.
485 (signal-semaphore semaphore count
)
486 ;; 100 threads try to decrement the semaphore by 1.
491 (sleep (random 0.02))
492 (wait-on-semaphore semaphore
:timeout
0.5))))))
493 ;; Add 10 more tokens while threads may already be waiting and
495 (loop repeat
(floor count
2) do
(signal-semaphore semaphore
2))
496 ;; 20 threads should have been able to decrement the semaphore
497 ;; and obtain an updated count.
498 (let ((values (mapcar #'join-thread threads
)))
499 ;; 20 threads should succeed waiting for the semaphore.
500 (assert (= (* 2 count
) (count-if-not #'null values
)))
501 ;; The updated semaphore count should be in [0,19] at all
503 (assert (every (lambda (value) (<= 0 value
(1- (* 2 count
))))
504 (remove nil values
)))
505 ;; (At least) one thread should decrease the count to 0.
506 (assert (find 0 values
))))))
508 (with-test (:name
(:join-thread
:timeout
)
509 :skipped-on
(not :sb-thread
))
511 (join-thread (make-join-thread (lambda () (sleep 10))) :timeout
0.01)
513 (let ((cookie (cons t t
)))
515 (join-thread (make-join-thread (lambda () (sleep 10)))
519 (with-test (:name
(wait-on-semaphore semaphore-notification
:lp-1038034
)
520 :skipped-on
(not :sb-thread
)
521 :fails-on
(and :sb-thread
522 (not :darwin
)) ;; Maybe because it doesn't use futexes?
524 ;; Test robustness of semaphore acquisition and notification with
525 ;; asynchronous thread termination... Which we know is currently
528 (let ((sem (make-semaphore)))
529 ;; In CRITICAL, WAIT-ON-SEMAPHORE and SLEEP can be interrupted
530 ;; by TERMINATE-THREAD below. But the SIGNAL-SEMAPHORE cleanup
531 ;; cannot be interrupted.
532 (flet ((critical (sleep)
533 (let ((note (make-semaphore-notification)))
534 (sb-sys:without-interrupts
536 (sb-sys:with-local-interrupts
537 (wait-on-semaphore sem
:notification note
)
539 ;; Re-increment on exit if we decremented it.
540 (when (semaphore-notification-status note
)
541 (signal-semaphore sem
)))))))
542 ;; Create /parallel/ threads trying to acquire and then signal
543 ;; the semaphore. Try to asynchronously abort T2 just as T1 is
545 (destructuring-bind (t1 t2 t3
)
547 for sleep in
'(0.01
0.02 0.02)
548 collect
(make-thread #'critical
:arguments sleep
549 :name
(format nil
"T~A" i
)))
550 (signal-semaphore sem
)
553 (terminate-thread t2
))
554 (flet ((safe-join-thread (thread &key timeout
)
560 (error "Hang in (join-thread ~A) ?" thread
))))
561 (safe-join-thread t1
:timeout
10)
562 (safe-join-thread t3
:timeout
10)))))
563 (when (zerop (mod run
60))
569 (with-test (:name
(wait-on-semaphore :n
))
570 (let ((semaphore (make-semaphore :count
3)))
571 (assert (= 1 (wait-on-semaphore semaphore
:n
2)))
572 (assert (= 1 (semaphore-count semaphore
)))))
574 (with-test (:name
(try-semaphore semaphore-notification
)
575 :skipped-on
(not :sb-thread
))
576 (let* ((sem (make-semaphore))
577 (note (make-semaphore-notification)))
578 (assert (eql nil
(try-semaphore sem
1 note
)))
579 (assert (not (semaphore-notification-status note
)))
580 (signal-semaphore sem
)
581 (assert (eql 0 (try-semaphore sem
1 note
)))
582 (assert (semaphore-notification-status note
))))
584 (with-test (:name
(return-from-thread :normal-thread
)
585 :skipped-on
(not :sb-thread
))
586 (let ((thread (make-thread (lambda ()
587 (return-from-thread (values 1 2 3))
589 (assert (equal '(1 2 3) (multiple-value-list (join-thread thread
))))))
591 (with-test (:name
(return-from-thread :main-thread
))
592 (assert (main-thread-p))
593 (assert-error (return-from-thread t
) thread-error
))
595 (with-test (:name
(abort-thread :normal-thread
)
596 :skipped-on
(not :sb-thread
))
597 (let ((thread (make-thread (lambda ()
600 (assert (equal '(:aborted
! :abort
)
602 (join-thread thread
:default
:aborted
!))))))
604 (with-test (:name
(abort-thread :main-thread
))
605 (assert (main-thread-p))
606 (assert-error (abort-thread) thread-error
))
608 ;; SB-THREAD:MAKE-THREAD used to lock SB-THREAD:*MAKE-THREAD-LOCK*
609 ;; before entering WITHOUT-INTERRUPTS. When a thread which was
610 ;; executing SB-THREAD:MAKE-THREAD was interrupted with code which
611 ;; also called SB-THREAD:MAKE-THREAD, it could happen that the first
612 ;; thread already owned SB-THREAD:*MAKE-THREAD-LOCK* and the
613 ;; interrupting code thus made a recursive lock attempt.
615 ;; See (:TIMER :DISPATCH-THREAD :MAKE-THREAD :BUG-1180102) in
616 ;; timer.impure.lisp.
617 (with-test (:name
(make-thread :interrupt-with make-thread
:bug-1180102
)
618 :skipped-on
(not :sb-thread
)
625 (inner-threads (list nil
))
626 (parent *current-thread
*))
632 (lambda () (atomic-push (make-thread (lambda ()))
633 (car inner-threads
))))))
635 (push (make-thread (lambda ())) outer-threads
))
636 (mapc #'join-thread outer-threads
)
637 (mapc #'join-thread
(car inner-threads
)))