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 #-sb-thread
(quit :unix-status
104)
16 (in-package "SB-THREAD") ; this is white-box testing, really
18 (let ((old-threads (list-all-threads))
19 (thread (make-thread (lambda ()
20 (assert (find *current-thread
* *all-threads
*))
22 (new-threads (list-all-threads)))
23 (assert (thread-alive-p thread
))
24 (assert (eq thread
(first new-threads
)))
25 (assert (= (1+ (length old-threads
)) (length new-threads
)))
27 (assert (not (thread-alive-p thread
))))
29 ;;; We had appalling scaling properties for a while. Make sure they
31 (defun scaling-test (function &optional
(nthreads 5))
32 "Execute FUNCTION with NTHREADS lurking to slow it down."
33 (let ((queue (sb-thread:make-waitqueue
))
34 (mutex (sb-thread:make-mutex
)))
35 ;; Start NTHREADS idle threads.
37 (sb-thread:make-thread
(lambda ()
38 (sb-thread:condition-wait queue mutex
)
40 (let ((start-time (get-internal-run-time)))
42 (prog1 (- (get-internal-run-time) start-time
)
43 (sb-thread:condition-broadcast queue
)))))
45 "A function that does work with the CPU."
46 (if (zerop n
) 1 (* n
(fact (1- n
)))))
47 (let ((work (lambda () (fact 15000))))
48 (let ((zero (scaling-test work
0))
49 (four (scaling-test work
4)))
50 ;; a slightly weak assertion, but good enough for starters.
51 (assert (< four
(* 1.5 zero
)))))
53 ;;; For one of the interupt-thread tests, we want a foreign function
54 ;;; that does not make syscalls
56 (with-open-file (o "threads-foreign.c" :direction
:output
:if-exists
:supersede
)
57 (format o
"void loop_forever() { while(1) ; }~%"))
60 (or #+linux
'("-shared" "-o" "threads-foreign.so" "threads-foreign.c")
61 (error "Missing shared library compilation options for this platform"))
63 (sb-alien:load-shared-object
"threads-foreign.so")
64 (sb-alien:define-alien-routine loop-forever sb-alien
:void
)
67 ;;; elementary "can we get a lock and release it again"
68 (let ((l (make-mutex :name
"foo"))
70 (assert (eql (mutex-value l
) nil
) nil
"1")
71 (assert (eql (mutex-lock l
) 0) nil
"2")
72 (sb-thread:get-mutex l
)
73 (assert (eql (mutex-value l
) p
) nil
"3")
74 (assert (eql (mutex-lock l
) 0) nil
"4")
75 (sb-thread:release-mutex l
)
76 (assert (eql (mutex-value l
) nil
) nil
"5")
77 (assert (eql (mutex-lock l
) 0) nil
"6")
80 (let ((l (make-waitqueue :name
"spinlock"))
82 (assert (eql (waitqueue-lock l
) 0) nil
"1")
84 (assert (eql (waitqueue-lock l
) p
) nil
"2"))
85 (assert (eql (waitqueue-lock l
) 0) nil
"3")
88 ;; test that SLEEP actually sleeps for at least the given time, even
89 ;; if interrupted by another thread exiting/a gc/anything
90 (let ((start-time (get-universal-time)))
91 (make-thread (lambda () (sleep 1) (sb-ext:gc
:full t
)))
93 (assert (>= (get-universal-time) (+ 5 start-time
))))
96 (let ((queue (make-waitqueue :name
"queue"))
97 (lock (make-mutex :name
"lock")))
98 (labels ((in-new-thread ()
100 (assert (eql (mutex-value lock
) *current-thread
*))
101 (format t
"~A got mutex~%" *current-thread
*)
102 ;; now drop it and sleep
103 (condition-wait queue lock
)
104 ;; after waking we should have the lock again
105 (assert (eql (mutex-value lock
) *current-thread
*)))))
106 (make-thread #'in-new-thread
)
107 (sleep 2) ; give it a chance to start
108 ;; check the lock is free while it's asleep
109 (format t
"parent thread ~A~%" *current-thread
*)
110 (assert (eql (mutex-value lock
) nil
))
111 (assert (eql (mutex-lock lock
) 0))
113 (condition-notify queue
))
116 (let ((queue (make-waitqueue :name
"queue"))
117 (lock (make-mutex :name
"lock")))
118 (labels ((ours-p (value)
119 (sb-vm:control-stack-pointer-valid-p
120 (sb-sys:int-sap
(sb-kernel:get-lisp-obj-address value
))))
122 (with-recursive-lock (lock)
123 (assert (ours-p (mutex-value lock
)))
124 (format t
"~A got mutex~%" (mutex-value lock
))
125 ;; now drop it and sleep
126 (condition-wait queue lock
)
127 ;; after waking we should have the lock again
128 (format t
"woken, ~A got mutex~%" (mutex-value lock
))
129 (assert (ours-p (mutex-value lock
))))))
130 (make-thread #'in-new-thread
)
131 (sleep 2) ; give it a chance to start
132 ;; check the lock is free while it's asleep
133 (format t
"parent thread ~A~%" *current-thread
*)
134 (assert (eql (mutex-value lock
) nil
))
135 (assert (eql (mutex-lock lock
) 0))
136 (with-recursive-lock (lock)
137 (condition-notify queue
))
140 (let ((mutex (make-mutex :name
"contended")))
142 (let ((me *current-thread
*))
146 (assert (eql (mutex-value mutex
) me
)))
147 (assert (not (eql (mutex-value mutex
) me
))))
148 (format t
"done ~A~%" *current-thread
*))))
149 (let ((kid1 (make-thread #'run
))
150 (kid2 (make-thread #'run
)))
151 (format t
"contention ~A ~A~%" kid1 kid2
))))
153 (defun test-interrupt (function-to-interrupt &optional quit-p
)
154 (let ((child (make-thread function-to-interrupt
)))
155 ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
157 (format t
"interrupting child ~A~%" child
)
158 (interrupt-thread child
160 (format t
"child pid ~A~%" *current-thread
*)
161 (when quit-p
(sb-ext:quit
))))
165 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
166 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
169 (let ((child (test-interrupt (lambda () (loop))))) (terminate-thread child
))
171 (test-interrupt #'loop-forever
:quit
)
173 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
174 (terminate-thread child
))
176 (let ((lock (make-mutex :name
"loctite"))
179 (setf child
(test-interrupt
182 (assert (eql (mutex-value lock
) *current-thread
*)))
183 (assert (not (eql (mutex-value lock
) *current-thread
*)))
185 ;;hold onto lock for long enough that child can't get it immediately
187 (interrupt-thread child
(lambda () (format t
"l ~A~%" (mutex-value lock
))))
188 (format t
"parent releasing lock~%"))
189 (terminate-thread child
))
191 (format t
"~&locking test done~%")
193 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
196 (let ((thread (sb-thread:make-thread
(lambda () (loop (alloc-stuff))))))
198 (loop repeat
4 collect
199 (sb-thread:make-thread
205 (sb-thread:interrupt-thread
208 (loop while
(some #'thread-alive-p killers
) do
(sleep 0.1))
209 (sb-thread:terminate-thread thread
)))
212 (format t
"~&multi interrupt test done~%")
214 (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
215 ;; NB this only works on x86: other ports don't have a symbol for
216 ;; pseudo-atomic atomicity
217 (format t
"new thread ~A~%" c
)
222 (princ ".") (force-output)
223 (assert (eq (thread-state *current-thread
*) :running
))
224 (assert (zerop SB-KERNEL
:*PSEUDO-ATOMIC-ATOMIC
*)))))
225 (terminate-thread c
))
227 (format t
"~&interrupt test done~%")
229 (defparameter *interrupt-count
* 0)
231 (declaim (notinline check-interrupt-count
))
232 (defun check-interrupt-count (i)
233 (declare (optimize (debug 1) (speed 1)))
234 ;; This used to lose if eflags were not restored after an interrupt.
235 (unless (typep i
'fixnum
)
236 (error "!!!!!!!!!!!")))
238 (let ((c (make-thread
240 (handler-bind ((error #'(lambda (cond)
243 most-positive-fixnum
))))
244 (loop (check-interrupt-count *interrupt-count
*)))))))
245 (let ((func (lambda ()
248 (sb-impl::atomic-incf
/symbol
*interrupt-count
*))))
249 (setq *interrupt-count
* 0)
252 (interrupt-thread c func
))
254 (assert (= 100 *interrupt-count
*))
255 (terminate-thread c
)))
257 (format t
"~&interrupt count test done~%")
260 (make-thread (lambda ()
262 (sb-ext:gc
) (princ "\\") (force-output))
264 (make-thread (lambda ()
267 (princ "/") (force-output))
270 (when (and a-done b-done
) (return))
275 (defun waste (&optional
(n 100000))
276 (loop repeat n do
(make-string 16384)))
278 (loop for i below
100 do
281 (sb-thread:make-thread
289 (defparameter *aaa
* nil
)
290 (loop for i below
100 do
293 (sb-thread:make-thread
295 (let ((*aaa
* (waste)))
297 (let ((*aaa
* (waste)))
301 (format t
"~&gc test done~%")
303 ;; this used to deadlock on session-lock
304 (sb-thread:make-thread
(lambda () (sb-ext:gc
)))
305 ;; expose thread creation races by exiting quickly
306 (sb-thread:make-thread
(lambda ()))
308 (defun exercise-syscall (fn reference-errno
)
309 (sb-thread:make-thread
313 (let ((errno (sb-unix::get-errno
)))
315 (unless (eql errno reference-errno
)
316 (format t
"Got errno: ~A (~A) instead of ~A~%"
321 (sb-ext:quit
:unix-status
1)))))))
323 (let* ((nanosleep-errno (progn
324 (sb-unix:nanosleep -
1 0)
325 (sb-unix::get-errno
)))
328 :if-does-not-exist nil
)
329 (sb-unix::get-errno
)))
332 (exercise-syscall (lambda () (sb-unix:nanosleep -
1 0)) nanosleep-errno
)
333 (exercise-syscall (lambda () (open "no-such-file"
334 :if-does-not-exist nil
))
336 (sb-thread:make-thread
(lambda () (loop (sb-ext:gc
) (sleep 1)))))))
338 (princ "terminating threads")
339 (dolist (thread threads
)
340 (sb-thread:terminate-thread thread
)))
342 (format t
"~&errno test done~%")
345 (let ((thread (sb-thread:make-thread
(lambda () (sleep 0.1)))))
346 (sb-thread:interrupt-thread
349 (assert (find-restart 'sb-thread
:terminate-thread
))))))
353 (format t
"~&thread startup sigmask test done~%")
355 #|
;; a cll post from eric marsden
357 |
(setq *debugger-hook
*
358 |
(lambda (condition old-debugger-hook
)
359 |
(debug:backtrace
10)
360 |
(unix:unix-exit
2)))
362 |
(mp::start-sigalrm-yield
)
363 |
(flet ((roomy () (loop (with-output-to-string (*standard-output
*) (room)))))
364 |
(mp:make-process
#'roomy
)
365 |
(mp:make-process
#'roomy
)))
368 ;; give the other thread time to die before we leave, otherwise the
369 ;; overall exit status is 0, not 104
372 (sb-ext:quit
:unix-status
104)