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 (defpackage :thread-test
17 (:use :cl :sb-thread))
19 (in-package :thread-test)
21 (use-package :test-util)
23 (with-test (:name mutex-owner)
24 ;; Make sure basics are sane on unithreaded ports as well
25 (let ((mutex (make-mutex)))
27 (assert (eq *current-thread* (mutex-value mutex)))
28 (handler-bind ((warning #'error))
29 (release-mutex mutex))
30 (assert (not (mutex-value mutex)))))
32 ;;; Terminating a thread that's waiting for the terminal.
35 (let ((thread (make-thread (lambda ()
36 (sb-thread::get-foreground)))))
38 (assert (thread-alive-p thread))
39 (terminate-thread thread)
41 (assert (not (thread-alive-p thread))))
43 ;;; Condition-wait should not be interruptible under WITHOUT-INTERRUPTS
45 (with-test (:name without-interrupts+condition-wait
46 :skipped-on '(not :sb-thread))
47 (let* ((lock (make-mutex))
48 (queue (make-waitqueue))
49 (thread (make-thread (lambda ()
50 (sb-sys:without-interrupts
52 (condition-wait queue lock)))))))
54 (assert (thread-alive-p thread))
55 (terminate-thread thread)
57 (assert (thread-alive-p thread))
58 (condition-notify queue)
60 (assert (not (thread-alive-p thread)))))
62 ;;; GET-MUTEX should not be interruptible under WITHOUT-INTERRUPTS
64 (with-test (:name without-interrupts+get-mutex :skipped-on '(not :sb-thread))
65 (let* ((lock (make-mutex))
66 (bar (progn (get-mutex lock) nil))
67 (thread (make-thread (lambda ()
68 (sb-sys:without-interrupts
72 (assert (thread-alive-p thread))
73 (terminate-thread thread)
75 (assert (thread-alive-p thread))
78 (assert (not (thread-alive-p thread)))
79 (assert (eq :aborted (join-thread thread :default :aborted)))
82 (with-test (:name parallel-find-class :skipped-on '(not :sb-thread))
84 (threads (loop repeat 10
85 collect (make-thread (lambda ()
88 do (find-class (gensym) nil))
91 (mapcar #'sb-thread:join-thread threads)
94 (with-test (:name :semaphore-multiple-waiters :skipped-on '(not :sb-thread))
95 (let ((semaphore (make-semaphore :name "test sem")))
96 (labels ((make-readers (n i)
98 (loop for r from 0 below n
100 (sb-thread:make-thread
102 (let ((sem semaphore))
104 (sb-thread:wait-on-semaphore sem))))
107 (make-writers (n readers i)
108 (let ((j (* readers i)))
109 (multiple-value-bind (k rem) (truncate j n)
112 (loop for w from 0 below n
114 (sb-thread:make-thread
116 (let ((sem semaphore))
118 (sb-thread:signal-semaphore sem))))
124 (multiple-value-bind (readers x) (make-readers r n)
125 (assert (= (length readers) r))
126 (multiple-value-bind (writers y) (make-writers w r n)
127 (assert (= (length writers) w))
129 (mapc #'sb-thread:join-thread writers)
130 (mapc #'sb-thread:join-thread readers)
131 (assert (zerop (sb-thread:semaphore-count semaphore)))
136 (sb-ext:with-timeout 10
147 ;;;; Printing waitqueues
149 (with-test (:name :waitqueue-circle-print :skipped-on '(not :sb-thread))
150 (let* ((*print-circle* nil)
151 (lock (sb-thread:make-mutex))
152 (wq (sb-thread:make-waitqueue)))
153 (sb-thread:with-recursive-lock (lock)
154 (sb-thread:condition-notify wq))
155 ;; Used to blow stack due to recursive structure.
156 (assert (princ-to-string wq))))
158 ;;;; SYMBOL-VALUE-IN-THREAD
160 (with-test (:name symbol-value-in-thread.1)
161 (let ((* (cons t t)))
162 (assert (eq * (symbol-value-in-thread '* *current-thread*)))
163 (setf (symbol-value-in-thread '* *current-thread*) 123)
164 (assert (= 123 (symbol-value-in-thread '* *current-thread*)))
167 (with-test (:name symbol-value-in-thread.2 :skipped-on '(not :sb-thread))
168 (let* ((parent *current-thread*)
169 (semaphore (make-semaphore))
170 (child (make-thread (lambda ()
171 (wait-on-semaphore semaphore)
172 (let ((old (symbol-value-in-thread 'this-is-new parent)))
173 (setf (symbol-value-in-thread 'this-is-new parent) :from-child)
175 (progv '(this-is-new) '(42)
176 (signal-semaphore semaphore)
177 (assert (= 42 (join-thread child)))
178 (assert (eq :from-child (symbol-value 'this-is-new))))))
180 ;;; Disabled on Darwin due to deadlocks caused by apparent OS specific deadlocks,
181 ;;; wich _appear_ to be caused by malloc() and free() not being thread safe: an
182 ;;; interrupted malloc in one thread can apparently block a free in another.
183 (with-test (:name symbol-value-in-thread.3
184 :skipped-on '(not :sb-thread))
185 (let* ((parent *current-thread*)
186 (semaphore (make-semaphore))
188 (noise (make-thread (lambda ()
190 do (setf * (make-array 1024))
191 ;; Busy-wait a bit so we don't TOTALLY flood the
192 ;; system with GCs: a GC occurring in the middle of
193 ;; S-V-I-T causes it to start over -- we want that
194 ;; to occur occasionally, but not _all_ the time.
195 (loop repeat (random 128)
199 (when (zerop (mod i 200))
202 (let* ((mom-mark (cons t t))
203 (kid-mark (cons t t))
204 (child (make-thread (lambda ()
205 (wait-on-semaphore semaphore)
206 (let ((old (symbol-value-in-thread 'this-is-new parent)))
207 (setf (symbol-value-in-thread 'this-is-new parent)
208 (make-array 24 :initial-element kid-mark))
210 (progv '(this-is-new) (list (make-array 24 :initial-element mom-mark))
211 (signal-semaphore semaphore)
212 (assert (eq mom-mark (aref (join-thread child) 0)))
213 (assert (eq kid-mark (aref (symbol-value 'this-is-new) 0))))))
215 (join-thread noise)))
217 (with-test (:name symbol-value-in-thread.4 :skipped-on '(not :sb-thread))
218 (let* ((parent *current-thread*)
219 (semaphore (make-semaphore))
220 (child (make-thread (lambda ()
221 (wait-on-semaphore semaphore)
222 (symbol-value-in-thread 'this-is-new parent nil)))))
223 (signal-semaphore semaphore)
224 (assert (equal '(nil nil) (multiple-value-list (join-thread child))))))
226 (with-test (:name symbol-value-in-thread.5 :skipped-on '(not :sb-thread))
227 (let* ((parent *current-thread*)
228 (semaphore (make-semaphore))
229 (child (make-thread (lambda ()
230 (wait-on-semaphore semaphore)
232 (symbol-value-in-thread 'this-is-new parent)
233 (symbol-value-in-thread-error (e)
234 (list (thread-error-thread e)
236 (sb-thread::symbol-value-in-thread-error-info e))))))))
237 (signal-semaphore semaphore)
238 (assert (equal (list *current-thread* 'this-is-new (list :read :unbound-in-thread))
239 (join-thread child)))))
241 (with-test (:name symbol-value-in-thread.6 :skipped-on '(not :sb-thread))
242 (let* ((parent *current-thread*)
243 (semaphore (make-semaphore))
245 (child (make-thread (lambda ()
246 (wait-on-semaphore semaphore)
248 (setf (symbol-value-in-thread name parent) t)
249 (symbol-value-in-thread-error (e)
250 (list (thread-error-thread e)
252 (sb-thread::symbol-value-in-thread-error-info e))))))))
253 (signal-semaphore semaphore)
254 (let ((res (join-thread child))
255 (want (list *current-thread* name (list :write :no-tls-value))))
256 (unless (equal res want)
257 (error "wanted ~S, got ~S" want res)))))
259 (with-test (:name symbol-value-in-thread.7 :skipped-on '(not :sb-thread))
260 (let ((child (make-thread (lambda ())))
261 (error-occurred nil))
264 (symbol-value-in-thread 'this-is-new child)
265 (symbol-value-in-thread-error (e)
266 (setf error-occurred t)
267 (assert (eq child (thread-error-thread e)))
268 (assert (eq 'this-is-new (cell-error-name e)))
269 (assert (equal (list :read :thread-dead)
270 (sb-thread::symbol-value-in-thread-error-info e)))))
271 (assert error-occurred)))
273 (with-test (:name symbol-value-in-thread.8 :skipped-on '(not :sb-thread))
274 (let ((child (make-thread (lambda ())))
275 (error-occurred nil))
278 (setf (symbol-value-in-thread 'this-is-new child) t)
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 :write :thread-dead)
284 (sb-thread::symbol-value-in-thread-error-info e)))))
285 (assert error-occurred)))
287 (with-test (:name deadlock-detection.1 :skipped-on '(not :sb-thread))
290 do (flet ((test (ma mb sa sb)
293 (sb-thread:with-mutex (ma)
294 (sb-thread:signal-semaphore sa)
295 (sb-thread:wait-on-semaphore sb)
296 (sb-thread:with-mutex (mb)
298 (sb-thread:thread-deadlock (e)
301 (let* ((m1 (sb-thread:make-mutex :name "M1"))
302 (m2 (sb-thread:make-mutex :name "M2"))
303 (s1 (sb-thread:make-semaphore :name "S1"))
304 (s2 (sb-thread:make-semaphore :name "S2"))
305 (t1 (sb-thread:make-thread (test m1 m2 s1 s2) :name "T1"))
306 (t2 (sb-thread:make-thread (test m2 m1 s2 s1) :name "T2")))
307 ;; One will deadlock, and the other will then complete normally.
308 (let ((res (list (sb-thread:join-thread t1)
309 (sb-thread:join-thread t2))))
310 (assert (or (equal '(:deadlock :ok) res)
311 (equal '(:ok :deadlock) res))))))))
313 (with-test (:name deadlock-detection.2 :skipped-on '(not :sb-thread))
314 (let* ((m1 (sb-thread:make-mutex :name "M1"))
315 (m2 (sb-thread:make-mutex :name "M2"))
316 (s1 (sb-thread:make-semaphore :name "S1"))
317 (s2 (sb-thread:make-semaphore :name "S2"))
318 (t1 (sb-thread:make-thread
320 (sb-thread:with-mutex (m1)
321 (sb-thread:signal-semaphore s1)
322 (sb-thread:wait-on-semaphore s2)
323 (sb-thread:with-mutex (m2)
328 (handler-bind ((sb-thread:thread-deadlock
331 ;; Make sure we can print the condition
333 (let ((*print-circle* nil))
334 (setf err (princ-to-string e)))
338 (assert (eq :ok (sb-thread:with-mutex (m2)
340 (sb-thread:signal-semaphore s2)
341 (sb-thread:wait-on-semaphore s1)
343 (sb-thread:with-mutex (m1)
345 (assert (stringp err)))
346 (assert (eq :ok (sb-thread:join-thread t1)))))
348 (with-test (:name deadlock-detection.3 :skipped-on '(not :sb-thread))
349 (let* ((m1 (sb-thread:make-mutex :name "M1"))
350 (m2 (sb-thread:make-mutex :name "M2"))
351 (s1 (sb-thread:make-semaphore :name "S1"))
352 (s2 (sb-thread:make-semaphore :name "S2"))
353 (t1 (sb-thread:make-thread
355 (sb-thread:with-mutex (m1)
356 (sb-thread:signal-semaphore s1)
357 (sb-thread:wait-on-semaphore s2)
358 (sb-thread:with-mutex (m2)
361 ;; Currently we don't consider it a deadlock
362 ;; if there is a timeout in the chain.
363 (assert (eq :deadline
365 (sb-thread:with-mutex (m2)
366 (sb-thread:signal-semaphore s2)
367 (sb-thread:wait-on-semaphore s1)
369 (sb-sys:with-deadline (:seconds 0.1)
370 (sb-thread:with-mutex (m1)
372 (sb-sys:deadline-timeout ()
374 (sb-thread:thread-deadlock ()
376 (assert (eq :ok (join-thread t1)))))
379 (with-test (:name :pass-arguments-to-thread)
380 (assert (= 3 (join-thread (make-thread #'+ :arguments '(1 2))))))
383 (with-test (:name :pass-atom-to-thread)
384 (assert (= 1/2 (join-thread (make-thread #'/ :arguments 2)))))
387 (with-test (:name :pass-nil-to-thread)
388 (assert (= 1 (join-thread (make-thread #'* :arguments '())))))
391 (with-test (:name :pass-nothing-to-thread)
392 (assert (= 1 (join-thread (make-thread #'*)))))
395 (with-test (:name :pass-improper-list-to-thread)
396 (multiple-value-bind (value error)
397 (ignore-errors (make-thread #'+ :arguments '(1 . 1)))
400 (assert (and (null value)
403 (with-test (:name (:wait-for :basics))
404 (assert (not (sb-ext:wait-for nil :timeout 0.1)))
405 (assert (eql 42 (sb-ext:wait-for 42)))
407 (assert (eql 100 (sb-ext:wait-for (when (= 100 (incf n))
410 (with-test (:name (:wait-for :deadline))
412 (sb-sys:with-deadline (:seconds 10)
413 (assert (not (sb-ext:wait-for nil :timeout 0.1)))
415 (assert (eq :deadline
417 (sb-sys:with-deadline (:seconds 0.1)
418 (sb-ext:wait-for nil :timeout 10)
420 (sb-sys:deadline-timeout () :deadline)))))
422 (with-test (:name (:condition-wait :timeout :one-thread))
423 (let ((mutex (make-mutex))
424 (waitqueue (make-waitqueue)))
425 (assert (not (with-mutex (mutex)
426 (condition-wait waitqueue mutex :timeout 0.01))))))
428 (with-test (:name (:condition-wait :timeout :many-threads)
429 :skipped-on '(not :sb-thread))
430 (let* ((mutex (make-mutex))
431 (waitqueue (make-waitqueue))
432 (sem (make-semaphore))
438 (wait-on-semaphore sem)
442 do (or (condition-wait waitqueue mutex :timeout 0.01)
443 (return-from thread nil)))
444 (assert (eq t (pop data)))
447 do (with-mutex (mutex)
449 (condition-notify waitqueue)))
450 (signal-semaphore sem 100)
451 (let ((ok (count-if #'join-thread workers)))
453 (error "Wanted 50, got ~S" ok)))))
455 (with-test (:name (:wait-on-semaphore :timeout :one-thread))
456 (let ((sem (make-semaphore))
458 (signal-semaphore sem 10)
460 do (when (wait-on-semaphore sem :timeout 0.001)
464 (with-test (:name (:wait-on-semaphore :timeout :many-threads)
465 :skipped-on '(not :sb-thread))
466 (let* ((sem (make-semaphore))
469 (signal-semaphore sem 10)
473 (sleep (random 0.02))
474 (wait-on-semaphore sem :timeout 0.01)))))))
476 do (signal-semaphore sem 2))
477 (let ((ok (count-if #'join-thread threads)))
479 (error "Wanted 20, got ~S" ok)))))
481 (with-test (:name (:join-thread :timeout)
482 :skipped-on '(not :sb-thread))
485 (join-thread (make-thread (lambda () (sleep 10))) :timeout 0.01)
486 (join-thread-error ()
488 (let ((cookie (cons t t)))
490 (join-thread (make-thread (lambda () (sleep 10)))
494 (with-test (:name (:semaphore-notification :wait-on-semaphore)
495 :skipped-on '(not :sb-thread))
496 (let ((sem (make-semaphore))
500 (let ((note (make-semaphore-notification)))
501 (sb-sys:without-interrupts
504 (sb-sys:with-local-interrupts
505 (wait-on-semaphore sem :notification note)
506 (sleep (random 0.1)))
508 ;; Re-increment on exit if we decremented it.
509 (when (semaphore-notification-status note)
510 (signal-semaphore sem))
511 ;; KLUDGE: Prevent interrupts after this point from
512 ;; unwinding us, so that we can reason about the counts.
514 (sb-thread::block-deferrable-signals))))))
515 (let* ((threads (loop for i from 1 upto 100
516 collect (make-thread #'critical :name (format nil "T~A" i))))
519 (interruptor (make-thread (lambda ()
522 (dolist (thread threads)
527 (terminate-thread thread)))
530 (setf x (not x))))))))
531 (signal-semaphore sem)
533 (join-thread interruptor)
534 (mapc #'join-thread safe)
535 (let ((k (count-if (lambda (th)
536 (join-thread th :default nil))
538 (assert (= n (+ k (length safe))))
541 (with-test (:name (:semaphore-notification :try-sempahore)
542 :skipped-on '(not :sb-thread))
543 (let* ((sem (make-semaphore))
544 (note (make-semaphore-notification)))
545 (try-semaphore sem 1 note)
546 (assert (not (semaphore-notification-status note)))
547 (signal-semaphore sem)
548 (try-semaphore sem 1 note)
549 (assert (semaphore-notification-status note))))