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 :sb-ext))
19 (in-package :thread-test)
21 (use-package :test-util)
23 (with-test (:name atomic-update
24 :skipped-on '(not :sb-thread))
25 (let ((x (cons :count 0)))
26 (mapc #'sb-thread:join-thread
28 collect (sb-thread:make-thread
31 do (atomic-update (cdr x) #'1+)
33 (assert (equal x '(:count . 1000000)))))
35 (with-test (:name mutex-owner)
36 ;; Make sure basics are sane on unithreaded ports as well
37 (let ((mutex (make-mutex)))
39 (assert (eq *current-thread* (mutex-value mutex)))
40 (handler-bind ((warning #'error))
41 (release-mutex mutex))
42 (assert (not (mutex-value mutex)))))
44 ;;; Terminating a thread that's waiting for the terminal.
47 (let ((thread (make-thread (lambda ()
48 (sb-thread::get-foreground)))))
50 (assert (thread-alive-p thread))
51 (terminate-thread thread)
53 (assert (not (thread-alive-p thread))))
55 ;;; Condition-wait should not be interruptible under WITHOUT-INTERRUPTS
57 (with-test (:name without-interrupts+condition-wait
58 :skipped-on '(not :sb-thread))
59 (let* ((lock (make-mutex))
60 (queue (make-waitqueue))
61 (thread (make-thread (lambda ()
62 (sb-sys:without-interrupts
64 (condition-wait queue lock)))))))
66 (assert (thread-alive-p thread))
67 (terminate-thread thread)
69 (assert (thread-alive-p thread))
70 (condition-notify queue)
72 (assert (not (thread-alive-p thread)))))
74 ;;; GRAB-MUTEX should not be interruptible under WITHOUT-INTERRUPTS
76 (with-test (:name without-interrupts+grab-mutex :skipped-on '(not :sb-thread))
77 (let* ((lock (make-mutex))
78 (bar (progn (grab-mutex lock) nil))
79 (thread (make-thread (lambda ()
80 (sb-sys:without-interrupts
84 (assert (thread-alive-p thread))
85 (terminate-thread thread)
87 (assert (thread-alive-p thread))
90 (assert (not (thread-alive-p thread)))
91 (assert (eq :aborted (join-thread thread :default :aborted)))
94 (with-test (:name parallel-find-class :skipped-on '(not :sb-thread))
96 (threads (loop repeat 10
97 collect (make-thread (lambda ()
100 do (find-class (gensym) nil))
101 (serious-condition ()
103 (mapcar #'sb-thread:join-thread threads)
104 (assert (not oops))))
106 (with-test (:name :semaphore-multiple-waiters :skipped-on '(not :sb-thread))
107 (let ((semaphore (make-semaphore :name "test sem")))
108 (labels ((make-readers (n i)
110 (loop for r from 0 below n
112 (sb-thread:make-thread
114 (let ((sem semaphore))
116 (sb-thread:wait-on-semaphore sem))))
119 (make-writers (n readers i)
120 (let ((j (* readers i)))
121 (multiple-value-bind (k rem) (truncate j n)
124 (loop for w from 0 below n
126 (sb-thread:make-thread
128 (let ((sem semaphore))
130 (sb-thread:signal-semaphore sem))))
136 (multiple-value-bind (readers x) (make-readers r n)
137 (assert (= (length readers) r))
138 (multiple-value-bind (writers y) (make-writers w r n)
139 (assert (= (length writers) w))
141 (mapc #'sb-thread:join-thread writers)
142 (mapc #'sb-thread:join-thread readers)
143 (assert (zerop (sb-thread:semaphore-count semaphore)))
148 (sb-ext:with-timeout 10
159 ;;;; Printing waitqueues
161 (with-test (:name :waitqueue-circle-print :skipped-on '(not :sb-thread))
162 (let* ((*print-circle* nil)
163 (lock (sb-thread:make-mutex))
164 (wq (sb-thread:make-waitqueue)))
165 (sb-thread:with-recursive-lock (lock)
166 (sb-thread:condition-notify wq))
167 ;; Used to blow stack due to recursive structure.
168 (assert (princ-to-string wq))))
170 ;;;; SYMBOL-VALUE-IN-THREAD
172 (with-test (:name symbol-value-in-thread.1)
173 (let ((* (cons t t)))
174 (assert (eq * (symbol-value-in-thread '* *current-thread*)))
175 (setf (symbol-value-in-thread '* *current-thread*) 123)
176 (assert (= 123 (symbol-value-in-thread '* *current-thread*)))
179 (with-test (:name symbol-value-in-thread.2 :skipped-on '(not :sb-thread))
180 (let* ((parent *current-thread*)
181 (semaphore (make-semaphore))
182 (child (make-thread (lambda ()
183 (wait-on-semaphore semaphore)
184 (let ((old (symbol-value-in-thread 'this-is-new parent)))
185 (setf (symbol-value-in-thread 'this-is-new parent) :from-child)
187 (progv '(this-is-new) '(42)
188 (signal-semaphore semaphore)
189 (assert (= 42 (join-thread child)))
190 (assert (eq :from-child (symbol-value 'this-is-new))))))
192 ;;; Disabled on Darwin due to deadlocks caused by apparent OS specific deadlocks,
193 ;;; wich _appear_ to be caused by malloc() and free() not being thread safe: an
194 ;;; interrupted malloc in one thread can apparently block a free in another.
195 (with-test (:name symbol-value-in-thread.3
196 :skipped-on '(not :sb-thread))
197 (let* ((parent *current-thread*)
198 (semaphore (make-semaphore))
200 (noise (make-thread (lambda ()
202 do (setf * (make-array 1024))
203 ;; Busy-wait a bit so we don't TOTALLY flood the
204 ;; system with GCs: a GC occurring in the middle of
205 ;; S-V-I-T causes it to start over -- we want that
206 ;; to occur occasionally, but not _all_ the time.
207 (loop repeat (random 128)
211 (when (zerop (mod i 200))
214 (let* ((mom-mark (cons t t))
215 (kid-mark (cons t t))
216 (child (make-thread (lambda ()
217 (wait-on-semaphore semaphore)
218 (let ((old (symbol-value-in-thread 'this-is-new parent)))
219 (setf (symbol-value-in-thread 'this-is-new parent)
220 (make-array 24 :initial-element kid-mark))
222 (progv '(this-is-new) (list (make-array 24 :initial-element mom-mark))
223 (signal-semaphore semaphore)
224 (assert (eq mom-mark (aref (join-thread child) 0)))
225 (assert (eq kid-mark (aref (symbol-value 'this-is-new) 0))))))
227 (join-thread noise)))
229 (with-test (:name symbol-value-in-thread.4 :skipped-on '(not :sb-thread))
230 (let* ((parent *current-thread*)
231 (semaphore (make-semaphore))
232 (child (make-thread (lambda ()
233 (wait-on-semaphore semaphore)
234 (symbol-value-in-thread 'this-is-new parent nil)))))
235 (signal-semaphore semaphore)
236 (assert (equal '(nil nil) (multiple-value-list (join-thread child))))))
238 (with-test (:name symbol-value-in-thread.5 :skipped-on '(not :sb-thread))
239 (let* ((parent *current-thread*)
240 (semaphore (make-semaphore))
241 (child (make-thread (lambda ()
242 (wait-on-semaphore semaphore)
244 (symbol-value-in-thread 'this-is-new parent)
245 (symbol-value-in-thread-error (e)
246 (list (thread-error-thread e)
248 (sb-thread::symbol-value-in-thread-error-info e))))))))
249 (signal-semaphore semaphore)
250 (assert (equal (list *current-thread* 'this-is-new (list :read :unbound-in-thread))
251 (join-thread child)))))
253 (with-test (:name symbol-value-in-thread.6 :skipped-on '(not :sb-thread))
254 (let* ((parent *current-thread*)
255 (semaphore (make-semaphore))
257 (child (make-thread (lambda ()
258 (wait-on-semaphore semaphore)
260 (setf (symbol-value-in-thread name parent) t)
261 (symbol-value-in-thread-error (e)
262 (list (thread-error-thread e)
264 (sb-thread::symbol-value-in-thread-error-info e))))))))
265 (signal-semaphore semaphore)
266 (let ((res (join-thread child))
267 (want (list *current-thread* name (list :write :no-tls-value))))
268 (unless (equal res want)
269 (error "wanted ~S, got ~S" want res)))))
271 (with-test (:name symbol-value-in-thread.7 :skipped-on '(not :sb-thread))
272 (let ((child (make-thread (lambda ())))
273 (error-occurred nil))
276 (symbol-value-in-thread 'this-is-new child)
277 (symbol-value-in-thread-error (e)
278 (setf error-occurred t)
279 (assert (eq child (thread-error-thread e)))
280 (assert (eq 'this-is-new (cell-error-name e)))
281 (assert (equal (list :read :thread-dead)
282 (sb-thread::symbol-value-in-thread-error-info e)))))
283 (assert error-occurred)))
285 (with-test (:name symbol-value-in-thread.8 :skipped-on '(not :sb-thread))
286 (let ((child (make-thread (lambda ())))
287 (error-occurred nil))
290 (setf (symbol-value-in-thread 'this-is-new child) t)
291 (symbol-value-in-thread-error (e)
292 (setf error-occurred t)
293 (assert (eq child (thread-error-thread e)))
294 (assert (eq 'this-is-new (cell-error-name e)))
295 (assert (equal (list :write :thread-dead)
296 (sb-thread::symbol-value-in-thread-error-info e)))))
297 (assert error-occurred)))
299 (with-test (:name deadlock-detection.1 :skipped-on '(not :sb-thread))
302 do (flet ((test (ma mb sa sb)
305 (sb-thread:with-mutex (ma)
306 (sb-thread:signal-semaphore sa)
307 (sb-thread:wait-on-semaphore sb)
308 (sb-thread:with-mutex (mb)
310 (sb-thread:thread-deadlock (e)
313 (let* ((m1 (sb-thread:make-mutex :name "M1"))
314 (m2 (sb-thread:make-mutex :name "M2"))
315 (s1 (sb-thread:make-semaphore :name "S1"))
316 (s2 (sb-thread:make-semaphore :name "S2"))
317 (t1 (sb-thread:make-thread (test m1 m2 s1 s2) :name "T1"))
318 (t2 (sb-thread:make-thread (test m2 m1 s2 s1) :name "T2")))
319 ;; One will deadlock, and the other will then complete normally.
320 (let ((res (list (sb-thread:join-thread t1)
321 (sb-thread:join-thread t2))))
322 (assert (or (equal '(:deadlock :ok) res)
323 (equal '(:ok :deadlock) res))))))))
325 (with-test (:name deadlock-detection.2 :skipped-on '(not :sb-thread))
326 (let* ((m1 (sb-thread:make-mutex :name "M1"))
327 (m2 (sb-thread:make-mutex :name "M2"))
328 (s1 (sb-thread:make-semaphore :name "S1"))
329 (s2 (sb-thread:make-semaphore :name "S2"))
330 (t1 (sb-thread:make-thread
332 (sb-thread:with-mutex (m1)
333 (sb-thread:signal-semaphore s1)
334 (sb-thread:wait-on-semaphore s2)
335 (sb-thread:with-mutex (m2)
340 (handler-bind ((sb-thread:thread-deadlock
343 ;; Make sure we can print the condition
345 (let ((*print-circle* nil))
346 (setf err (princ-to-string e)))
350 (assert (eq :ok (sb-thread:with-mutex (m2)
352 (sb-thread:signal-semaphore s2)
353 (sb-thread:wait-on-semaphore s1)
355 (sb-thread:with-mutex (m1)
357 (assert (stringp err)))
358 (assert (eq :ok (sb-thread:join-thread t1)))))
360 (with-test (:name deadlock-detection.3 :skipped-on '(not :sb-thread))
361 (let* ((m1 (sb-thread:make-mutex :name "M1"))
362 (m2 (sb-thread:make-mutex :name "M2"))
363 (s1 (sb-thread:make-semaphore :name "S1"))
364 (s2 (sb-thread:make-semaphore :name "S2"))
365 (t1 (sb-thread:make-thread
367 (sb-thread:with-mutex (m1)
368 (sb-thread:signal-semaphore s1)
369 (sb-thread:wait-on-semaphore s2)
370 (sb-thread:with-mutex (m2)
373 ;; Currently we don't consider it a deadlock
374 ;; if there is a timeout in the chain.
375 (assert (eq :deadline
377 (sb-thread:with-mutex (m2)
378 (sb-thread:signal-semaphore s2)
379 (sb-thread:wait-on-semaphore s1)
381 (sb-sys:with-deadline (:seconds 0.1)
382 (sb-thread:with-mutex (m1)
384 (sb-sys:deadline-timeout ()
386 (sb-thread:thread-deadlock ()
388 (assert (eq :ok (join-thread t1)))))
391 (with-test (:name :pass-arguments-to-thread)
392 (assert (= 3 (join-thread (make-thread #'+ :arguments '(1 2))))))
395 (with-test (:name :pass-atom-to-thread)
396 (assert (= 1/2 (join-thread (make-thread #'/ :arguments 2)))))
399 (with-test (:name :pass-nil-to-thread)
400 (assert (= 1 (join-thread (make-thread #'* :arguments '())))))
403 (with-test (:name :pass-nothing-to-thread)
404 (assert (= 1 (join-thread (make-thread #'*)))))
407 (with-test (:name :pass-improper-list-to-thread)
408 (multiple-value-bind (value error)
409 (ignore-errors (make-thread #'+ :arguments '(1 . 1)))
412 (assert (and (null value)
415 (with-test (:name (:wait-for :basics) :fails-on :win32)
416 (assert (not (sb-ext:wait-for nil :timeout 0.1)))
417 (assert (eql 42 (sb-ext:wait-for 42)))
419 (assert (eql 100 (sb-ext:wait-for (when (= 100 (incf n))
422 (with-test (:name (:wait-for :deadline) :fails-on :win32)
424 (sb-sys:with-deadline (:seconds 10)
425 (assert (not (sb-ext:wait-for nil :timeout 0.1)))
427 (assert (eq :deadline
429 (sb-sys:with-deadline (:seconds 0.1)
430 (sb-ext:wait-for nil :timeout 10)
432 (sb-sys:deadline-timeout () :deadline)))))
434 (with-test (:name (:condition-wait :timeout :one-thread) :fails-on :win32)
435 (let ((mutex (make-mutex))
436 (waitqueue (make-waitqueue)))
437 (assert (not (with-mutex (mutex)
438 (condition-wait waitqueue mutex :timeout 0.01))))))
440 (with-test (:name (:condition-wait :timeout :many-threads)
441 :skipped-on '(not :sb-thread))
442 (let* ((mutex (make-mutex))
443 (waitqueue (make-waitqueue))
444 (sem (make-semaphore))
450 (wait-on-semaphore sem)
454 do (or (condition-wait waitqueue mutex :timeout 0.01)
455 (return-from thread nil)))
456 (assert (eq t (pop data)))
459 do (with-mutex (mutex)
461 (condition-notify waitqueue)))
462 (signal-semaphore sem 100)
463 (let ((ok (count-if #'join-thread workers)))
465 (error "Wanted 50, got ~S" ok)))))
467 (with-test (:name (:wait-on-semaphore :timeout :one-thread) :fails-on :win32)
468 (let ((sem (make-semaphore))
470 (signal-semaphore sem 10)
472 do (when (wait-on-semaphore sem :timeout 0.001)
476 (with-test (:name (:wait-on-semaphore :timeout :many-threads)
477 :skipped-on '(not :sb-thread))
478 (let* ((sem (make-semaphore))
481 (signal-semaphore sem 10)
485 (sleep (random 0.02))
486 (wait-on-semaphore sem :timeout 0.5)))))))
488 do (signal-semaphore sem 2))
489 (let ((ok (count-if #'join-thread threads)))
491 (error "Wanted 20, got ~S" ok)))))
493 (with-test (:name (:join-thread :timeout)
494 :skipped-on '(not :sb-thread))
497 (join-thread (make-thread (lambda () (sleep 10))) :timeout 0.01)
498 (join-thread-error ()
500 (let ((cookie (cons t t)))
502 (join-thread (make-thread (lambda () (sleep 10)))
506 (with-test (:name (:semaphore-notification :wait-on-semaphore)
507 :skipped-on '(not :sb-thread))
508 (let ((sem (make-semaphore))
512 (let ((note (make-semaphore-notification)))
513 (sb-sys:without-interrupts
516 (sb-sys:with-local-interrupts
517 (wait-on-semaphore sem :notification note)
518 (sleep (random 0.1)))
520 ;; Re-increment on exit if we decremented it.
521 (when (semaphore-notification-status note)
522 (signal-semaphore sem))
523 ;; KLUDGE: Prevent interrupts after this point from
524 ;; unwinding us, so that we can reason about the counts.
526 (sb-thread::block-deferrable-signals))))))
527 (let* ((threads (loop for i from 1 upto 100
528 collect (make-thread #'critical :name (format nil "T~A" i))))
531 (interruptor (make-thread (lambda ()
534 (dolist (thread threads)
539 (terminate-thread thread)))
542 (setf x (not x))))))))
543 (signal-semaphore sem)
545 (join-thread interruptor)
546 (mapc #'join-thread safe)
547 (let ((k (count-if (lambda (th)
548 (join-thread th :default nil))
550 (assert (= n (+ k (length safe))))
553 (with-test (:name (:semaphore-notification :try-sempahore)
554 :skipped-on '(not :sb-thread))
555 (let* ((sem (make-semaphore))
556 (note (make-semaphore-notification)))
557 (try-semaphore sem 1 note)
558 (assert (not (semaphore-notification-status note)))
559 (signal-semaphore sem)
560 (try-semaphore sem 1 note)
561 (assert (semaphore-notification-status note))))
563 (with-test (:name (:return-from-thread :normal-thread)
564 :skipped-on '(not :sb-thread))
565 (let* ((thread (make-thread (lambda ()
566 (return-from-thread (values 1 2 3))
568 (values (multiple-value-list (join-thread thread))))
569 (unless (equal (list 1 2 3) values)
570 (error "got ~S, wanted (1 2 3)" values))))
572 (with-test (:name (:return-from-thread :main-thread))
573 (assert (main-thread-p))
576 (return-from-thread t)
580 (with-test (:name (:abort-thread :normal-thread)
581 :skipped-on '(not :sb-thread))
582 (let ((thread (make-thread (lambda ()
585 (assert (eq :aborted! (join-thread thread :default :aborted!)))))
587 (with-test (:name (:abort-thread :main-thread))
588 (assert (main-thread-p))