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 (with-test (:name spinlock-owner)
33 ;; Make sure basics are sane on unithreaded ports as well
34 (let ((spinlock (sb-thread::make-spinlock)))
35 (sb-thread::get-spinlock spinlock)
36 (assert (eq *current-thread* (sb-thread::spinlock-value spinlock)))
37 (handler-bind ((warning #'error))
38 (sb-thread::release-spinlock spinlock))
39 (assert (not (sb-thread::spinlock-value spinlock)))))
41 ;;; Terminating a thread that's waiting for the terminal.
44 (let ((thread (make-thread (lambda ()
45 (sb-thread::get-foreground)))))
47 (assert (thread-alive-p thread))
48 (terminate-thread thread)
50 (assert (not (thread-alive-p thread))))
52 ;;; Condition-wait should not be interruptible under WITHOUT-INTERRUPTS
55 (with-test (:name without-interrupts+condition-wait
57 (let* ((lock (make-mutex))
58 (queue (make-waitqueue))
59 (thread (make-thread (lambda ()
60 (sb-sys:without-interrupts
62 (condition-wait queue lock)))))))
64 (assert (thread-alive-p thread))
65 (terminate-thread thread)
67 (assert (thread-alive-p thread))
68 (condition-notify queue)
70 (assert (not (thread-alive-p thread)))))
72 ;;; GET-MUTEX should not be interruptible under WITHOUT-INTERRUPTS
75 (with-test (:name without-interrupts+get-mutex)
76 (let* ((lock (make-mutex))
77 (bar (progn (get-mutex lock) nil))
78 (thread (make-thread (lambda ()
79 (sb-sys:without-interrupts
83 (assert (thread-alive-p thread))
84 (terminate-thread thread)
86 (assert (thread-alive-p thread))
89 (assert (not (thread-alive-p thread)))
90 (assert (eq :aborted (join-thread thread :default :aborted)))
94 (with-test (:name parallel-find-class)
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))))
107 (with-test (:name :semaphore-multiple-waiters)
108 (let ((semaphore (make-semaphore :name "test sem")))
109 (labels ((make-readers (n i)
111 (loop for r from 0 below n
113 (sb-thread:make-thread
115 (let ((sem semaphore))
117 (sb-thread: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
127 (sb-thread:make-thread
129 (let ((sem semaphore))
131 (sb-thread: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 #'sb-thread:join-thread writers)
143 (mapc #'sb-thread:join-thread readers)
144 (assert (zerop (sb-thread:semaphore-count semaphore)))
149 (sb-ext:with-timeout 10
160 ;;;; Printing waitqueues
162 (with-test (:name :waitqueue-circle-print)
163 (let* ((*print-circle* nil)
164 (lock (sb-thread:make-mutex))
165 (wq (sb-thread:make-waitqueue)))
166 (sb-thread:with-recursive-lock (lock)
167 (sb-thread: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*)))
181 (with-test (:name symbol-value-in-thread.2)
182 (let* ((parent *current-thread*)
183 (semaphore (make-semaphore))
184 (child (make-thread (lambda ()
185 (wait-on-semaphore semaphore)
186 (let ((old (symbol-value-in-thread 'this-is-new parent)))
187 (setf (symbol-value-in-thread 'this-is-new parent) :from-child)
189 (progv '(this-is-new) '(42)
190 (signal-semaphore semaphore)
191 (assert (= 42 (join-thread child)))
192 (assert (eq :from-child (symbol-value 'this-is-new))))))
194 ;;; Disabled on Darwin due to deadlocks caused by apparent OS specific deadlocks,
195 ;;; wich _appear_ to be caused by malloc() and free() not being thread safe: an
196 ;;; interrupted malloc in one thread can apparently block a free in another. There
197 ;;; are also some indications that pthread_mutex_lock is not re-entrant.
198 #+(and sb-thread (not darwin))
199 (with-test (:name symbol-value-in-thread.3)
200 (let* ((parent *current-thread*)
201 (semaphore (make-semaphore))
203 (noise (make-thread (lambda ()
205 do (setf * (make-array 1024))
206 ;; Busy-wait a bit so we don't TOTALLY flood the
207 ;; system with GCs: a GC occurring in the middle of
208 ;; S-V-I-T causes it to start over -- we want that
209 ;; to occur occasionally, but not _all_ the time.
210 (loop repeat (random 128)
214 (when (zerop (mod i 200))
217 (let* ((mom-mark (cons t t))
218 (kid-mark (cons t t))
219 (child (make-thread (lambda ()
220 (wait-on-semaphore semaphore)
221 (let ((old (symbol-value-in-thread 'this-is-new parent)))
222 (setf (symbol-value-in-thread 'this-is-new parent)
223 (make-array 24 :initial-element kid-mark))
225 (progv '(this-is-new) (list (make-array 24 :initial-element mom-mark))
226 (signal-semaphore semaphore)
227 (assert (eq mom-mark (aref (join-thread child) 0)))
228 (assert (eq kid-mark (aref (symbol-value 'this-is-new) 0))))))
230 (join-thread noise)))
233 (with-test (:name symbol-value-in-thread.4)
234 (let* ((parent *current-thread*)
235 (semaphore (make-semaphore))
236 (child (make-thread (lambda ()
237 (wait-on-semaphore semaphore)
238 (symbol-value-in-thread 'this-is-new parent nil)))))
239 (signal-semaphore semaphore)
240 (assert (equal '(nil nil) (multiple-value-list (join-thread child))))))
243 (with-test (:name symbol-value-in-thread.5)
244 (let* ((parent *current-thread*)
245 (semaphore (make-semaphore))
246 (child (make-thread (lambda ()
247 (wait-on-semaphore semaphore)
249 (symbol-value-in-thread 'this-is-new parent)
250 (symbol-value-in-thread-error (e)
251 (list (thread-error-thread e)
253 (sb-thread::symbol-value-in-thread-error-info e))))))))
254 (signal-semaphore semaphore)
255 (assert (equal (list *current-thread* 'this-is-new (list :read :unbound-in-thread))
256 (join-thread child)))))
259 (with-test (:name symbol-value-in-thread.6)
260 (let* ((parent *current-thread*)
261 (semaphore (make-semaphore))
263 (child (make-thread (lambda ()
264 (wait-on-semaphore semaphore)
266 (setf (symbol-value-in-thread name parent) t)
267 (symbol-value-in-thread-error (e)
268 (list (thread-error-thread e)
270 (sb-thread::symbol-value-in-thread-error-info e))))))))
271 (signal-semaphore semaphore)
272 (let ((res (join-thread child))
273 (want (list *current-thread* name (list :write :no-tls-value))))
274 (unless (equal res want)
275 (error "wanted ~S, got ~S" want res)))))
278 (with-test (:name symbol-value-in-thread.7)
279 (let ((child (make-thread (lambda ())))
280 (error-occurred nil))
283 (symbol-value-in-thread 'this-is-new child)
284 (symbol-value-in-thread-error (e)
285 (setf error-occurred t)
286 (assert (eq child (thread-error-thread e)))
287 (assert (eq 'this-is-new (cell-error-name e)))
288 (assert (equal (list :read :thread-dead)
289 (sb-thread::symbol-value-in-thread-error-info e)))))
290 (assert error-occurred)))
293 (with-test (:name symbol-value-in-thread.8)
294 (let ((child (make-thread (lambda ())))
295 (error-occurred nil))
298 (setf (symbol-value-in-thread 'this-is-new child) t)
299 (symbol-value-in-thread-error (e)
300 (setf error-occurred t)
301 (assert (eq child (thread-error-thread e)))
302 (assert (eq 'this-is-new (cell-error-name e)))
303 (assert (equal (list :write :thread-dead)
304 (sb-thread::symbol-value-in-thread-error-info e)))))
305 (assert error-occurred)))