Update tests for threaded windows builds
[sbcl.git] / tests / threads.impure.lisp
1 ;;;; miscellaneous tests of thread stuff
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
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
8 ;;;; from CMU CL.
9 ;;;
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.
13
14 ; WHITE-BOX TESTS
15
16 (in-package "SB-THREAD")
17 (use-package :test-util)
18 (use-package "ASSERTOID")
19
20 (setf sb-unix::*on-dangerous-wait* :error)
21
22 (defun wait-for-threads (threads)
23   (mapc (lambda (thread) (sb-thread:join-thread thread :default nil)) threads)
24   (assert (not (some #'sb-thread:thread-alive-p threads))))
25
26 (with-test (:name (:threads :trivia))
27   (assert (eql 1 (length (list-all-threads))))
28
29   (assert (eq *current-thread*
30               (find (thread-name *current-thread*) (list-all-threads)
31                     :key #'thread-name :test #'equal)))
32
33   (assert (thread-alive-p *current-thread*)))
34
35 (with-test (:name (:with-mutex :basics))
36   (let ((mutex (make-mutex)))
37     (with-mutex (mutex)
38       mutex)))
39
40 (with-test (:name (:with-mutex :timeout))
41   (let ((m (make-mutex)))
42     (with-mutex (m)
43       (assert (null (join-thread (make-thread
44                                   (lambda ()
45                                     (with-mutex (m :timeout 0.1)
46                                       t)))))))
47     (assert (join-thread (make-thread
48                           (lambda ()
49                             (with-mutex (m :timeout 0.1)
50                               t)))))))
51
52 (sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
53     void
54   (where sb-alien:unsigned-long))
55 (sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
56     void
57   (where sb-alien:unsigned-long))
58
59 (with-test (:name (:interrupt-thread :basics :no-unwinding))
60   (let ((a 0))
61     (interrupt-thread *current-thread* (lambda () (setq a 1)))
62     (assert (eql a 1))))
63
64 (with-test (:name (:interrupt-thread :deferrables-blocked))
65   (sb-thread:interrupt-thread sb-thread:*current-thread*
66                               (lambda ()
67                                 (check-deferrables-blocked-or-lose 0))))
68
69 (with-test (:name (:interrupt-thread :deferrables-unblocked))
70   (sb-thread:interrupt-thread sb-thread:*current-thread*
71                               (lambda ()
72                                 (with-interrupts
73                                   (check-deferrables-unblocked-or-lose 0)))))
74
75 (with-test (:name (:interrupt-thread :nlx))
76   (catch 'xxx
77     (sb-thread:interrupt-thread sb-thread:*current-thread*
78                                 (lambda ()
79                                   (check-deferrables-blocked-or-lose 0)
80                                   (throw 'xxx nil))))
81   (check-deferrables-unblocked-or-lose 0))
82
83 #-sb-thread (sb-ext:exit :code 104)
84
85 ;;;; Now the real tests...
86
87 (with-test (:name (:interrupt-thread :deferrables-unblocked-by-lock))
88   (let ((lock (sb-thread::make-mutex))
89         (thread (make-join-thread (lambda ()
90                                     (loop (sleep 1))))))
91     (sb-thread::grab-mutex lock)
92     (sb-thread:interrupt-thread thread
93                                 (lambda ()
94                                   (check-deferrables-blocked-or-lose 0)
95                                   (sb-thread::grab-mutex lock)
96                                   (check-deferrables-unblocked-or-lose 0)
97                                   (sb-thread:abort-thread)))
98     (sleep 3)
99     (sb-thread::release-mutex lock)))
100
101 ;;; compare-and-swap
102
103 (defmacro defincf (name accessor &rest args)
104   `(defun ,name (x)
105      (let* ((old (,accessor x ,@args))
106          (new (1+ old)))
107     (loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
108        do (setf old (,accessor x ,@args)
109                 new (1+ old)))
110     new)))
111
112 (defstruct cas-struct (slot 0))
113
114 (defincf incf-car car)
115 (defincf incf-cdr cdr)
116 (defincf incf-slot cas-struct-slot)
117 (defincf incf-symbol-value symbol-value)
118 (defincf incf-svref/1 svref 1)
119 (defincf incf-svref/0 svref 0)
120
121 (defmacro def-test-cas (name init incf op)
122   `(with-test (:name ,name)
123      (flet ((,name (n)
124               (declare (fixnum n))
125               (let* ((x ,init)
126                      (run nil)
127                      (threads
128                       (loop repeat 10
129                             collect (sb-thread:make-thread
130                                      (lambda ()
131                                        (loop until run
132                                              do (sb-thread:thread-yield))
133                                        (loop repeat n do (,incf x)))))))
134                 (setf run t)
135                 (dolist (th threads)
136                   (sb-thread:join-thread th))
137                 (assert (= (,op x) (* 10 n))))))
138        (,name 200000))))
139
140 (def-test-cas test-cas-car (cons 0 nil) incf-car car)
141 (def-test-cas test-cas-cdr (cons nil 0) incf-cdr cdr)
142 (def-test-cas test-cas-slot (make-cas-struct) incf-slot cas-struct-slot)
143 (def-test-cas test-cas-value (let ((x '.x.))
144                                (set x 0)
145                                x)
146   incf-symbol-value symbol-value)
147 (def-test-cas test-cas-svref/0 (vector 0 nil) incf-svref/0 (lambda (x)
148                                                              (svref x 0)))
149 (def-test-cas test-cas-svref/1 (vector nil 0) incf-svref/1 (lambda (x)
150                                                              (svref x 1)))
151 (format t "~&compare-and-swap tests done~%")
152
153 (with-test (:name (:threads :more-trivia)))
154 (let ((old-threads (list-all-threads))
155       (thread (make-thread (lambda ()
156                              (assert (find *current-thread* *all-threads*))
157                              (sleep 2))))
158       (new-threads (list-all-threads)))
159   (assert (thread-alive-p thread))
160   (assert (eq thread (first new-threads)))
161   (assert (= (1+ (length old-threads)) (length new-threads)))
162   (sleep 3)
163   (assert (not (thread-alive-p thread))))
164
165 (with-test (:name (:join-thread :nlx :default))
166   (let ((sym (gensym)))
167     (assert (eq sym (join-thread (make-thread (lambda () (sb-thread:abort-thread)))
168                                  :default sym)))))
169
170 (with-test (:name (:join-thread :nlx :error))
171   (raises-error? (join-thread (make-thread (lambda () (sb-thread:abort-thread))))
172                  join-thread-error))
173
174 (with-test (:name (:join-thread :multiple-values))
175   (assert (equal '(1 2 3)
176                  (multiple-value-list
177                   (join-thread (make-thread (lambda () (values 1 2 3))))))))
178
179 ;;; We had appalling scaling properties for a while.  Make sure they
180 ;;; don't reappear.
181 (defun scaling-test (function &optional (nthreads 5))
182   "Execute FUNCTION with NTHREADS lurking to slow it down."
183   (let ((queue (sb-thread:make-waitqueue))
184         (mutex (sb-thread:make-mutex)))
185     ;; Start NTHREADS idle threads.
186     (dotimes (i nthreads)
187       (make-join-thread (lambda ()
188                           (with-mutex (mutex)
189                             (sb-thread:condition-wait queue mutex))
190                           (sb-thread:abort-thread))))
191     (let ((start-time (get-internal-run-time)))
192       (funcall function)
193       (prog1 (- (get-internal-run-time) start-time)
194         (sb-thread:condition-broadcast queue)))))
195 (defun fact (n)
196   "A function that does work with the CPU."
197   (if (zerop n) 1 (* n (fact (1- n)))))
198
199 (with-test (:name :lurking-threads)
200   (let ((work (lambda () (fact 15000))))
201     (let ((zero (scaling-test work 0))
202           (four (scaling-test work 4)))
203       ;; a slightly weak assertion, but good enough for starters.
204       (assert (< four (* 1.5 zero))))))
205
206 ;;; For one of the interupt-thread tests, we want a foreign function
207 ;;; that does not make syscalls
208
209 #-win32
210 (progn
211   (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
212     (format o "void loop_forever() { while(1) ; }~%"))
213   (sb-ext:run-program "/bin/sh"
214                       '("run-compiler.sh" "-sbcl-pic" "-sbcl-shared"
215                         "-o" "threads-foreign.so" "threads-foreign.c")
216                       :environment (test-util::test-env))
217   (sb-alien:load-shared-object (truename "threads-foreign.so"))
218   (sb-alien:define-alien-routine loop-forever sb-alien:void)
219   (delete-file "threads-foreign.c"))
220
221 ;;; elementary "can we get a lock and release it again"
222 (with-test (:name (:mutex :basics))
223   (let ((l (make-mutex :name "foo"))
224         (p *current-thread*))
225     (assert (eql (mutex-value l) nil) nil "1")
226     (sb-thread:grab-mutex l)
227     (assert (eql (mutex-value l) p) nil "3")
228     (sb-thread:release-mutex l)
229     (assert (eql (mutex-value l) nil) nil "5")))
230
231 (with-test (:name (:with-recursive-lock :basics))
232   (labels ((ours-p (value)
233              (eq *current-thread* value)))
234     (let ((l (make-mutex :name "rec")))
235       (assert (eql (mutex-value l) nil) nil "1")
236       (sb-thread:with-recursive-lock (l)
237         (assert (ours-p (mutex-value l)) nil "3")
238         (sb-thread:with-recursive-lock (l)
239           (assert (ours-p (mutex-value l)) nil "4"))
240         (assert (ours-p (mutex-value l)) nil "5"))
241       (assert (eql (mutex-value l) nil) nil "6"))))
242
243 (with-test (:name (:with-recursive-lock :wait-p))
244   (let ((m (make-mutex)))
245     (with-mutex (m)
246       (assert (null (join-thread (make-thread
247                                   (lambda ()
248                                     (with-recursive-lock (m :wait-p nil)
249                                       t)))))))
250     (assert (join-thread (make-thread
251                           (lambda ()
252                             (with-recursive-lock (m :wait-p nil)
253                               t)))))))
254
255 (with-test (:name (:with-recursive-lock :wait-p :recursive))
256   (let ((m (make-mutex)))
257     (assert (join-thread (make-thread
258                           (lambda ()
259                             (with-recursive-lock (m :wait-p nil)
260                               (with-recursive-lock (m :wait-p nil)
261                                 t))))))))
262
263 (with-test (:name (:with-recursive-lock :timeout))
264   (let ((m (make-mutex)))
265     (with-mutex (m)
266       (assert (null (join-thread (make-thread
267                                   (lambda ()
268                                     (with-recursive-lock (m :timeout 0.1)
269                                       t)))))))
270     (assert (join-thread (make-thread
271                           (lambda ()
272                             (with-recursive-lock (m :timeout 0.1)
273                               t)))))))
274
275 (with-test (:name (:with-recursive-lock :timeout :recursive))
276   (let ((m (make-mutex)))
277     (assert (join-thread (make-thread
278                           (lambda ()
279                             (with-recursive-lock (m :timeout 0.1)
280                               (with-recursive-lock (m :timeout 0.1)
281                                 t))))))))
282
283 (with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
284   (let ((l (make-mutex :name "a mutex")))
285     (with-mutex (l)
286       (with-recursive-lock (l)))))
287
288 ;; test that SLEEP actually sleeps for at least the given time, even
289 ;; if interrupted by another thread exiting/a gc/anything
290 (with-test (:name (:sleep :continue-sleeping-after-interrupt))
291   (let ((start-time (get-universal-time)))
292     (make-join-thread (lambda () (sleep 1) (sb-ext:gc :full t)))
293     (sleep 5)
294     (assert (>= (get-universal-time) (+ 5 start-time)))))
295
296
297 (with-test (:name (:condition-wait :basics-1))
298   (let ((queue (make-waitqueue :name "queue"))
299         (lock (make-mutex :name "lock"))
300         (n 0))
301     (labels ((in-new-thread ()
302                (with-mutex (lock)
303                  (assert (eql (mutex-value lock) *current-thread*))
304                  (format t "~A got mutex~%" *current-thread*)
305                  ;; now drop it and sleep
306                  (condition-wait queue lock)
307                  ;; after waking we should have the lock again
308                  (assert (eql (mutex-value lock) *current-thread*))
309                  (assert (eql n 1))
310                  (decf n))))
311       (make-join-thread #'in-new-thread)
312       (sleep 2)            ; give it  a chance to start
313       ;; check the lock is free while it's asleep
314       (format t "parent thread ~A~%" *current-thread*)
315       (assert (eql (mutex-value lock) nil))
316       (with-mutex (lock)
317         (incf n)
318         (condition-notify queue))
319       (sleep 1))))
320
321 (with-test (:name (:condition-wait :basics-2))
322   (let ((queue (make-waitqueue :name "queue"))
323         (lock (make-mutex :name "lock")))
324     (labels ((ours-p (value)
325                (eq *current-thread* value))
326              (in-new-thread ()
327                (with-recursive-lock (lock)
328                  (assert (ours-p (mutex-value lock)))
329                  (format t "~A got mutex~%" (mutex-value lock))
330                  ;; now drop it and sleep
331                  (condition-wait queue lock)
332                  ;; after waking we should have the lock again
333                  (format t "woken, ~A got mutex~%" (mutex-value lock))
334                  (assert (ours-p (mutex-value lock))))))
335       (make-join-thread #'in-new-thread)
336       (sleep 2)            ; give it  a chance to start
337       ;; check the lock is free while it's asleep
338       (format t "parent thread ~A~%" *current-thread*)
339       (assert (eql (mutex-value lock) nil))
340       (with-recursive-lock (lock)
341         (condition-notify queue))
342       (sleep 1))))
343
344 (with-test (:name (:mutex :contention))
345   (let ((mutex (make-mutex :name "contended")))
346     (labels ((run ()
347                (let ((me *current-thread*))
348                  (dotimes (i 100)
349                    (with-mutex (mutex)
350                      (sleep .03)
351                      (assert (eql (mutex-value mutex) me)))
352                    (assert (not (eql (mutex-value mutex) me))))
353                  (format t "done ~A~%" *current-thread*))))
354       (let ((kid1 (make-thread #'run))
355             (kid2 (make-thread #'run)))
356         (format t "contention ~A ~A~%" kid1 kid2)
357         (wait-for-threads (list kid1 kid2))))))
358
359 ;;; GRAB-MUTEX
360
361 (with-test (:name (:grab-mutex :waitp nil))
362   (let ((m (make-mutex)))
363     (with-mutex (m)
364       (assert (null (join-thread (make-thread
365                                   #'(lambda ()
366                                       (grab-mutex m :waitp nil)))))))))
367
368 (with-test (:name (:grab-mutex :timeout :acquisition-fail))
369   (let ((m (make-mutex))
370         (w (make-semaphore)))
371     (with-mutex (m)
372       (let ((th (make-thread
373                  #'(lambda ()
374                      (prog1
375                          (grab-mutex m :timeout 0.1)
376                        (signal-semaphore w))))))
377         ;; Wait for it to -- otherwise the detect the deadlock chain
378         ;; from JOIN-THREAD.
379         (wait-on-semaphore w)
380         (assert (null (join-thread th)))))))
381
382 (with-test (:name (:grab-mutex :timeout :acquisition-success))
383   (let ((m (make-mutex))
384         (child))
385     (with-mutex (m)
386       (setq child (make-thread #'(lambda () (grab-mutex m :timeout 1.0))))
387       (sleep 0.2))
388     (assert (eq (join-thread child) 't))))
389
390 (with-test (:name (:grab-mutex :timeout+deadline))
391   (let ((m (make-mutex))
392         (w (make-semaphore)))
393     (with-mutex (m)
394       (let ((th (make-thread #'(lambda ()
395                                  (sb-sys:with-deadline (:seconds 0.0)
396                                    (handler-case
397                                        (grab-mutex m :timeout 0.0)
398                                      (sb-sys:deadline-timeout ()
399                                        (signal-semaphore w)
400                                        :deadline)))))))
401         (wait-on-semaphore w)
402         (assert (eq (join-thread th) :deadline))))))
403
404 (with-test (:name (:grab-mutex :waitp+deadline))
405   (let ((m (make-mutex)))
406     (with-mutex (m)
407       (assert (eq (join-thread
408                    (make-thread #'(lambda ()
409                                     (sb-sys:with-deadline (:seconds 0.0)
410                                       (handler-case
411                                           (grab-mutex m :waitp nil)
412                                         (sb-sys:deadline-timeout ()
413                                           :deadline))))))
414                   'nil)))))
415
416 ;;; semaphores
417
418 (defmacro raises-timeout-p (&body body)
419   `(handler-case (progn (progn ,@body) nil)
420     (sb-ext:timeout () t)))
421
422 (with-test (:name (:semaphore :wait-forever))
423   (let ((sem (make-semaphore :count 0)))
424     (assert (raises-timeout-p
425               (sb-ext:with-timeout 0.1
426                 (wait-on-semaphore sem))))))
427
428 (with-test (:name (:semaphore :initial-count))
429   (let ((sem (make-semaphore :count 1)))
430     (sb-ext:with-timeout 0.1
431       (wait-on-semaphore sem))))
432
433 (with-test (:name (:semaphore :wait-then-signal))
434   (let ((sem (make-semaphore))
435         (signalled-p nil))
436     (make-join-thread (lambda ()
437                         (sleep 0.1)
438                         (setq signalled-p t)
439                         (signal-semaphore sem)))
440     (wait-on-semaphore sem)
441     (assert signalled-p)))
442
443 (with-test (:name (:semaphore :signal-then-wait))
444   (let ((sem (make-semaphore))
445         (signalled-p nil))
446     (make-join-thread (lambda ()
447                         (signal-semaphore sem)
448                         (setq signalled-p t)))
449     (loop until signalled-p)
450     (wait-on-semaphore sem)
451     (assert signalled-p)))
452
453 (defun test-semaphore-multiple-signals (wait-on-semaphore)
454   (let* ((sem (make-semaphore :count 5))
455          (threads (loop repeat 20 collecting
456                         (make-join-thread (lambda ()
457                                             (funcall wait-on-semaphore sem))))))
458     (flet ((count-live-threads ()
459              (count-if #'thread-alive-p threads)))
460       (sleep 0.5)
461       (assert (= 15 (count-live-threads)))
462       (signal-semaphore sem 10)
463       (sleep 0.5)
464       (assert (= 5 (count-live-threads)))
465       (signal-semaphore sem 3)
466       (sleep 0.5)
467       (assert (= 2 (count-live-threads)))
468       (signal-semaphore sem 4)
469       (sleep 0.5)
470       (assert (= 0 (count-live-threads))))))
471
472 (with-test (:name (:semaphore :multiple-signals))
473   (test-semaphore-multiple-signals #'wait-on-semaphore))
474
475 (with-test (:name (:try-semaphore :trivial-fail))
476   (assert (eq (try-semaphore (make-semaphore :count 0)) 'nil)))
477
478 (with-test (:name (:try-semaphore :trivial-success))
479   (let ((sem (make-semaphore :count 1)))
480     (assert (try-semaphore sem))
481     (assert (zerop (semaphore-count sem)))))
482
483 (with-test (:name (:try-semaphore :trivial-fail :n>1))
484   (assert (eq (try-semaphore (make-semaphore :count 1) 2) 'nil)))
485
486 (with-test (:name (:try-semaphore :trivial-success :n>1))
487   (let ((sem (make-semaphore :count 10)))
488     (assert (try-semaphore sem 5))
489     (assert (try-semaphore sem 5))
490     (assert (zerop (semaphore-count sem)))))
491
492 (with-test (:name (:try-semaphore :emulate-wait-on-semaphore))
493   (flet ((busy-wait-on-semaphore (sem)
494            (loop until (try-semaphore sem) do (sleep 0.001))))
495     (test-semaphore-multiple-signals #'busy-wait-on-semaphore)))
496
497 ;;; Here we test that interrupting TRY-SEMAPHORE does not leave a
498 ;;; semaphore in a bad state.
499 (with-test (:name (:try-semaphore :interrupt-safe))
500   (flet ((make-threads (count fn)
501            (loop repeat count collect (make-thread fn)))
502          (kill-thread (thread)
503            (when (thread-alive-p thread)
504              (ignore-errors (terminate-thread thread))))
505          (count-live-threads (threads)
506            (count-if #'thread-alive-p threads)))
507     ;; WAITERS will already be waiting on the semaphore while
508     ;; threads-being-interrupted will perform TRY-SEMAPHORE on that
509     ;; semaphore, and MORE-WAITERS are new threads trying to wait on
510     ;; the semaphore during the interruption-fire.
511     (let* ((sem (make-semaphore :count 100))
512            (waiters (make-threads 20 #'(lambda ()
513                                          (wait-on-semaphore sem))))
514            (triers  (make-threads 40 #'(lambda ()
515                                          (sleep (random 0.01))
516                                          (try-semaphore sem (1+ (random 5))))))
517            (more-waiters
518             (loop repeat 10
519                   do (kill-thread (nth (random 40) triers))
520                   collect (make-thread #'(lambda () (wait-on-semaphore sem)))
521                   do (kill-thread (nth (random 40) triers)))))
522       (sleep 0.5)
523       ;; Now ensure that the waiting threads will all be waked up,
524       ;; i.e. that the semaphore is still working.
525       (loop repeat (+ (count-live-threads waiters)
526                       (count-live-threads more-waiters))
527             do (signal-semaphore sem))
528       (sleep 0.5)
529       (assert (zerop (count-live-threads triers)))
530       (assert (zerop (count-live-threads waiters)))
531       (assert (zerop (count-live-threads more-waiters))))))
532
533
534
535 (format t "~&semaphore tests done~%")
536
537 (defun test-interrupt (function-to-interrupt &optional quit-p)
538   (let ((child  (make-kill-thread function-to-interrupt)))
539     ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
540     (sleep 2)
541     (format t "interrupting child ~A~%" child)
542     (interrupt-thread child
543                       (lambda ()
544                         (format t "child pid ~A~%" *current-thread*)
545                         (when quit-p (abort-thread))))
546     (sleep 1)
547     child))
548
549 ;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
550 ;; (d) waiting on a lock, (e) some code which we hope is likely to be
551 ;; in pseudo-atomic
552
553 (with-test (:name (:interrupt-thread :more-basics))
554   (let ((child (test-interrupt (lambda () (loop)))))
555     (terminate-thread child)))
556
557 (with-test (:name (:interrupt-thread :interrupt-foreign-loop)
558                   ;; This feature is explicitly unsupported on Win32.
559                   :skipped-on :win32)
560   (test-interrupt #'loop-forever :quit))
561
562 (with-test (:name (:interrupt-thread :interrupt-sleep))
563   (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
564     (terminate-thread child)
565     (wait-for-threads (list child))))
566
567 (with-test (:name (:interrupt-thread :interrupt-mutex-acquisition))
568   (let ((lock (make-mutex :name "loctite"))
569         child)
570     (with-mutex (lock)
571       (setf child (test-interrupt
572                    (lambda ()
573                      (with-mutex (lock)
574                        (assert (eql (mutex-value lock) *current-thread*)))
575                      (assert (not (eql (mutex-value lock) *current-thread*)))
576                      (sleep 10))))
577       ;;hold onto lock for long enough that child can't get it immediately
578       (sleep 5)
579       (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
580       (format t "parent releasing lock~%"))
581     (terminate-thread child)
582     (wait-for-threads (list child))))
583
584 (format t "~&locking test done~%")
585
586 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
587
588 (with-test (:name (:interrupt-thread :interrupt-consing-child))
589   (let ((thread (make-thread (lambda () (loop (alloc-stuff))))))
590     (let ((killers
591            (loop repeat 4 collect
592                  (sb-thread:make-thread
593                   (lambda ()
594                     (loop repeat 25 do
595                           (sleep (random 0.1d0))
596                           (princ ".")
597                           (force-output)
598                           (sb-thread:interrupt-thread thread (lambda ()))))))))
599       (wait-for-threads killers)
600       (sb-thread:terminate-thread thread)
601       (wait-for-threads (list thread))))
602   (sb-ext:gc :full t))
603
604 (format t "~&multi interrupt test done~%")
605
606 #+(or x86 x86-64) ;; x86oid-only, see internal commentary.
607 (with-test (:name (:interrupt-thread :interrupt-consing-child :again))
608   (let ((c (make-thread (lambda () (loop (alloc-stuff))))))
609     ;; NB this only works on x86: other ports don't have a symbol for
610     ;; pseudo-atomic atomicity
611     (dotimes (i 100)
612       (sleep (random 0.1d0))
613       (interrupt-thread c
614                         (lambda ()
615                           (princ ".") (force-output)
616                           (assert (thread-alive-p *current-thread*))
617                           (assert
618                            (not (logbitp 0 SB-KERNEL:*PSEUDO-ATOMIC-BITS*))))))
619     (terminate-thread c)
620     (wait-for-threads (list c))))
621
622 (format t "~&interrupt test done~%")
623
624 (defstruct counter (n 0 :type sb-vm:word))
625 (defvar *interrupt-counter* (make-counter))
626
627 (declaim (notinline check-interrupt-count))
628 (defun check-interrupt-count (i)
629   (declare (optimize (debug 1) (speed 1)))
630   ;; This used to lose if eflags were not restored after an interrupt.
631   (unless (typep i 'fixnum)
632     (error "!!!!!!!!!!!")))
633
634 (with-test (:name (:interrupt-thread :interrupt-ATOMIC-INCF))
635   (let ((c (make-thread
636             (lambda ()
637               (handler-bind ((error #'(lambda (cond)
638                                         (princ cond)
639                                         (sb-debug:backtrace
640                                          most-positive-fixnum))))
641                 (loop (check-interrupt-count
642                        (counter-n *interrupt-counter*))))))))
643     (let ((func (lambda ()
644                   (princ ".")
645                   (force-output)
646                   (sb-ext:atomic-incf (counter-n *interrupt-counter*)))))
647       (setf (counter-n *interrupt-counter*) 0)
648       (dotimes (i 100)
649         (sleep (random 0.1d0))
650         (interrupt-thread c func))
651       (loop until (= (counter-n *interrupt-counter*) 100) do (sleep 0.1))
652       (terminate-thread c)
653       (wait-for-threads (list c)))))
654
655 (format t "~&interrupt count test done~%")
656
657 (defvar *runningp* nil)
658
659 (with-test (:name (:interrupt-thread :no-nesting))
660   (let ((thread (sb-thread:make-thread
661                  (lambda ()
662                    (catch 'xxx
663                      (loop))))))
664     (declare (special runningp))
665     (sleep 0.2)
666     (sb-thread:interrupt-thread thread
667                                 (lambda ()
668                                     (let ((*runningp* t))
669                                       (sleep 1))))
670     (sleep 0.2)
671     (sb-thread:interrupt-thread thread
672                                 (lambda ()
673                                   (throw 'xxx *runningp*)))
674     (assert (not (sb-thread:join-thread thread)))))
675
676 (with-test (:name (:interrupt-thread :nesting))
677   (let ((thread (sb-thread:make-thread
678                  (lambda ()
679                    (catch 'xxx
680                      (loop))))))
681     (declare (special runningp))
682     (sleep 0.2)
683     (sb-thread:interrupt-thread thread
684                                 (lambda ()
685                                   (let ((*runningp* t))
686                                     (sb-sys:with-interrupts
687                                       (sleep 1)))))
688     (sleep 0.2)
689     (sb-thread:interrupt-thread thread
690                                 (lambda ()
691                                   (throw 'xxx *runningp*)))
692     (assert (sb-thread:join-thread thread))))
693
694 (with-test (:name (:two-threads-running-gc))
695   (let (a-done b-done)
696     (make-join-thread (lambda ()
697                         (dotimes (i 100)
698                           (sb-ext:gc) (princ "\\") (force-output))
699                         (setf a-done t)))
700     (make-join-thread (lambda ()
701                         (dotimes (i 25)
702                           (sb-ext:gc :full t)
703                           (princ "/") (force-output))
704                         (setf b-done t)))
705     (loop
706       (when (and a-done b-done) (return))
707       (sleep 1))))
708
709 (terpri)
710
711 (defun waste (&optional (n 100000))
712   (loop repeat n do (make-string 16384)))
713
714 (with-test (:name (:one-thread-runs-gc-while-other-conses))
715   (loop for i below 100 do
716         (princ "!")
717         (force-output)
718         (make-join-thread
719          #'(lambda ()
720              (waste)))
721         (waste)
722         (sb-ext:gc)))
723
724 (terpri)
725
726 (defparameter *aaa* nil)
727 (with-test (:name (:one-thread-runs-gc-while-other-conses :again))
728   (loop for i below 100 do
729         (princ "!")
730         (force-output)
731         (make-join-thread
732          #'(lambda ()
733              (let ((*aaa* (waste)))
734                (waste))))
735         (let ((*aaa* (waste)))
736           (waste))
737         (sb-ext:gc)))
738
739 (format t "~&gc test done~%")
740
741 ;; this used to deadlock on session-lock
742 (with-test (:name (:no-session-deadlock))
743   (make-join-thread (lambda () (sb-ext:gc))))
744
745 (defun exercise-syscall (fn reference-errno)
746   (make-kill-thread
747    (lambda ()
748      (loop do
749           (funcall fn)
750           (let ((errno (sb-unix::get-errno)))
751             (sleep (random 0.1d0))
752             (unless (eql errno reference-errno)
753               (format t "Got errno: ~A (~A) instead of ~A~%"
754                       errno
755                       (sb-unix::strerror)
756                       reference-errno)
757               (force-output)
758               (abort-thread)))))))
759
760 ;; (nanosleep -1 0) does not fail on FreeBSD
761 (with-test (:name (:exercising-concurrent-syscalls) :fails-on :win32)
762   (let* (#-freebsd
763          (nanosleep-errno (progn
764                             (sb-unix:nanosleep -1 0)
765                             (sb-unix::get-errno)))
766          (open-errno (progn
767                        (open "no-such-file"
768                              :if-does-not-exist nil)
769                        (sb-unix::get-errno)))
770          (threads
771           (list
772            #-freebsd
773            (exercise-syscall (lambda () (sb-unix:nanosleep -1 0)) nanosleep-errno)
774            (exercise-syscall (lambda () (open "no-such-file"
775                                               :if-does-not-exist nil))
776                              open-errno)
777            (make-join-thread (lambda () (loop (sb-ext:gc) (sleep 1)))))))
778     (sleep 10)
779     (princ "terminating threads")
780     (dolist (thread threads)
781       (sb-thread:terminate-thread thread))))
782
783 (format t "~&errno test done~%")
784
785 (with-test (:name :all-threads-have-abort-restart)
786   (loop repeat 100 do
787         (let ((thread (make-kill-thread (lambda () (sleep 0.1)))))
788           (sb-thread:interrupt-thread
789            thread
790            (lambda ()
791              (assert (find-restart 'abort)))))))
792
793 (sb-ext:gc :full t)
794
795 (format t "~&thread startup sigmask test done~%")
796
797 (with-test (:name (:debugger-no-hang-on-session-lock-if-interrupted)
798                   :fails-on :win32)
799   #+win32 (error "user would have to touch a key interactively to proceed")
800   (sb-debug::enable-debugger)
801   (let* ((main-thread *current-thread*)
802          (interruptor-thread
803           (make-thread (lambda ()
804                          (sleep 2)
805                          (interrupt-thread main-thread
806                                            (lambda ()
807                                              (with-interrupts
808                                                (break))))
809                          (sleep 2)
810                          (interrupt-thread main-thread #'continue))
811                        :name "interruptor")))
812     (with-session-lock (*session*)
813       (sleep 3))
814     (loop while (thread-alive-p interruptor-thread))))
815
816 (format t "~&session lock test done~%")
817
818 ;; expose thread creation races by exiting quickly
819 (with-test (:name (:no-thread-creation-race :light))
820   (make-join-thread (lambda ())))
821
822 (with-test (:name (:no-thread-creation-race :heavy))
823   (loop repeat 20 do
824         (wait-for-threads
825          (loop for i below 100 collect
826                (sb-thread:make-thread (lambda ()))))))
827
828 (format t "~&creation test done~%")
829
830 ;; interrupt handlers are per-thread with pthreads, make sure the
831 ;; handler installed in one thread is global
832 (with-test (:name (:global-interrupt-handler))
833   (make-join-thread
834    (lambda ()
835      (sb-ext:run-program "sleep" '("1") :search t :wait nil))))
836
837 ;;;; Binding stack safety
838
839 (defparameter *x* nil)
840 (defparameter *n-gcs-requested* 0)
841 (defparameter *n-gcs-done* 0)
842
843 (let ((counter 0))
844   (defun make-something-big ()
845     (let ((x (make-string 32000)))
846       (incf counter)
847       (let ((counter counter))
848         (sb-ext:finalize x (lambda () (format t " ~S" counter)
849                                    (force-output)))))))
850
851 (defmacro wait-for-gc ()
852   `(progn
853      (incf *n-gcs-requested*)
854      (loop while (< *n-gcs-done* *n-gcs-requested*))))
855
856 (defun send-gc ()
857   (loop until (< *n-gcs-done* *n-gcs-requested*))
858   (format t "G")
859   (force-output)
860   (sb-ext:gc)
861   (incf *n-gcs-done*))
862
863 (defun exercise-binding ()
864   (loop
865    (let ((*x* (make-something-big)))
866      (let ((*x* 42))
867        ;; at this point the binding stack looks like this:
868        ;; NO-TLS-VALUE-MARKER, *x*, SOMETHING, *x*
869        t))
870    (wait-for-gc)
871    ;; sig_stop_for_gc_handler binds FREE_INTERRUPT_CONTEXT_INDEX. By
872    ;; now SOMETHING is gc'ed and the binding stack looks like this: 0,
873    ;; 0, SOMETHING, 0 (because the symbol slots are zeroed on
874    ;; unbinding but values are not).
875    (let ((*x* nil)
876          (binding-pointer-delta (ash 2 (- sb-vm:word-shift sb-vm:n-fixnum-tag-bits))))
877      ;; bump bsp as if a BIND had just started
878      (incf sb-vm::*binding-stack-pointer* binding-pointer-delta)
879      (wait-for-gc)
880      (decf sb-vm::*binding-stack-pointer* binding-pointer-delta))))
881
882 (with-test (:name (:binding-stack-gc-safety))
883   (let (threads)
884     (unwind-protect
885          (progn
886            (push (make-kill-thread #'exercise-binding) threads)
887            (push (make-kill-thread (lambda ()
888                                      (loop
889                                       (sleep 0.1)
890                                       (send-gc))))
891                  threads)
892            (sleep 4))
893       (mapc #'sb-thread:terminate-thread threads))))
894
895 (format t "~&binding test done~%")
896
897 ;;; HASH TABLES
898
899 (defvar *errors* nil)
900
901 (defun oops (e)
902   (setf *errors* e)
903   (format t "~&oops: ~A in ~S~%" e *current-thread*)
904   (sb-debug:backtrace)
905   (catch 'done))
906
907 (with-test (:name (:unsynchronized-hash-table)
908                   ;; FIXME: This test occasionally eats out craploads
909                   ;; of heap instead of expected error early. Not 100%
910                   ;; sure if it would finish as expected, but since it
911                   ;; hits swap on my system I'm not likely to find out
912                   ;; soon. Disabling for now. -- nikodemus
913             :skipped-on :sbcl)
914   ;; We expect a (probable) error here: parellel readers and writers
915   ;; on a hash-table are not expected to work -- but we also don't
916   ;; expect this to corrupt the image.
917   (let* ((hash (make-hash-table))
918          (*errors* nil)
919          (threads (list (make-kill-thread
920                          (lambda ()
921                            (catch 'done
922                              (handler-bind ((serious-condition 'oops))
923                                (loop
924                                  ;;(princ "1") (force-output)
925                                  (setf (gethash (random 100) hash) 'h)))))
926                          :name "writer")
927                         (make-kill-thread
928                          (lambda ()
929                            (catch 'done
930                              (handler-bind ((serious-condition 'oops))
931                                (loop
932                                  ;;(princ "2") (force-output)
933                                  (remhash (random 100) hash)))))
934                          :name "reader")
935                         (make-kill-thread
936                          (lambda ()
937                            (catch 'done
938                              (handler-bind ((serious-condition 'oops))
939                                (loop
940                                  (sleep (random 1.0))
941                                  (sb-ext:gc :full t)))))
942                          :name "collector"))))
943     (unwind-protect
944          (sleep 10)
945       (mapc #'sb-thread:terminate-thread threads))))
946
947 (format t "~&unsynchronized hash table test done~%")
948
949 (with-test (:name (:synchronized-hash-table))
950   (let* ((hash (make-hash-table :synchronized t))
951          (*errors* nil)
952          (threads (list (make-kill-thread
953                          (lambda ()
954                            (catch 'done
955                              (handler-bind ((serious-condition 'oops))
956                                (loop
957                                  ;;(princ "1") (force-output)
958                                  (setf (gethash (random 100) hash) 'h)))))
959                          :name "writer")
960                         (make-kill-thread
961                          (lambda ()
962                            (catch 'done
963                              (handler-bind ((serious-condition 'oops))
964                                (loop
965                                  ;;(princ "2") (force-output)
966                                  (remhash (random 100) hash)))))
967                          :name "reader")
968                         (make-kill-thread
969                          (lambda ()
970                            (catch 'done
971                              (handler-bind ((serious-condition 'oops))
972                                (loop
973                                  (sleep (random 1.0))
974                                  (sb-ext:gc :full t)))))
975                          :name "collector"))))
976     (unwind-protect
977          (sleep 10)
978       (mapc #'sb-thread:terminate-thread threads))
979     (assert (not *errors*))))
980
981 (format t "~&synchronized hash table test done~%")
982
983 (with-test (:name (:hash-table-parallel-readers))
984   (let ((hash (make-hash-table))
985         (*errors* nil))
986     (loop repeat 50
987           do (setf (gethash (random 100) hash) 'xxx))
988     (let ((threads (list (make-kill-thread
989                           (lambda ()
990                             (catch 'done
991                               (handler-bind ((serious-condition 'oops))
992                                 (loop
993                                       until (eq t (gethash (random 100) hash))))))
994                           :name "reader 1")
995                          (make-kill-thread
996                           (lambda ()
997                             (catch 'done
998                               (handler-bind ((serious-condition 'oops))
999                                 (loop
1000                                       until (eq t (gethash (random 100) hash))))))
1001                           :name "reader 2")
1002                          (make-kill-thread
1003                           (lambda ()
1004                             (catch 'done
1005                               (handler-bind ((serious-condition 'oops))
1006                                 (loop
1007                                       until (eq t (gethash (random 100) hash))))))
1008                           :name "reader 3")
1009                          (make-kill-thread
1010                           (lambda ()
1011                             (catch 'done
1012                               (handler-bind ((serious-condition 'oops))
1013                                (loop
1014                                  (sleep (random 1.0))
1015                                  (sb-ext:gc :full t)))))
1016                           :name "collector"))))
1017       (unwind-protect
1018            (sleep 10)
1019         (mapc #'sb-thread:terminate-thread threads))
1020       (assert (not *errors*)))))
1021
1022 (format t "~&multiple reader hash table test done~%")
1023
1024 (with-test (:name (:hash-table-single-accessor-parallel-gc))
1025   (let ((hash (make-hash-table))
1026         (*errors* nil))
1027     (let ((threads (list (make-kill-thread
1028                           (lambda ()
1029                             (handler-bind ((serious-condition 'oops))
1030                               (loop
1031                                 (let ((n (random 100)))
1032                                   (if (gethash n hash)
1033                                       (remhash n hash)
1034                                       (setf (gethash n hash) 'h))))))
1035                           :name "accessor")
1036                          (make-kill-thread
1037                           (lambda ()
1038                             (handler-bind ((serious-condition 'oops))
1039                               (loop
1040                                 (sleep (random 1.0))
1041                                 (sb-ext:gc :full t))))
1042                           :name "collector"))))
1043       (unwind-protect
1044            (sleep 10)
1045         (mapc #'sb-thread:terminate-thread threads))
1046       (assert (not *errors*)))))
1047
1048 (format t "~&single accessor hash table test~%")
1049
1050 #|  ;; a cll post from eric marsden
1051 | (defun crash ()
1052 |   (setq *debugger-hook*
1053 |         (lambda (condition old-debugger-hook)
1054 |           (debug:backtrace 10)
1055 |           (unix:unix-exit 2)))
1056 |   #+live-dangerously
1057 |   (mp::start-sigalrm-yield)
1058 |   (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
1059 |     (mp:make-process #'roomy)
1060 |     (mp:make-process #'roomy)))
1061 |#
1062
1063 (with-test (:name (:condition-variable :notify-multiple))
1064   (flet ((tester (notify-fun)
1065            (let ((queue (make-waitqueue :name "queue"))
1066                  (lock (make-mutex :name "lock"))
1067                  (data nil))
1068              (labels ((test (x)
1069                         (loop
1070                            (with-mutex (lock)
1071                              (format t "condition-wait ~a~%" x)
1072                              (force-output)
1073                              (condition-wait queue lock)
1074                              (format t "woke up ~a~%" x)
1075                              (force-output)
1076                              (push x data)))))
1077                (let ((threads (loop for x from 1 to 10
1078                                     collect
1079                                     (let ((x x))
1080                                       (make-kill-thread (lambda ()
1081                                                           (test x)))))))
1082                  (sleep 5)
1083                  (with-mutex (lock)
1084                    (funcall notify-fun queue))
1085                  (sleep 5)
1086                  (mapcar #'terminate-thread threads)
1087                  ;; Check that all threads woke up at least once
1088                  (assert (= (length (remove-duplicates data)) 10)))))))
1089     (tester (lambda (queue)
1090               (format t "~&(condition-notify queue 10)~%")
1091               (force-output)
1092               (condition-notify queue 10)))
1093     (tester (lambda (queue)
1094               (format t "~&(condition-broadcast queue)~%")
1095               (force-output)
1096               (condition-broadcast queue)))))
1097
1098 (format t "waitqueue wakeup tests done~%")
1099
1100 ;;; Make sure that a deadline handler is not invoked twice in a row in
1101 ;;; CONDITION-WAIT. See LP #512914 for a detailed explanation.
1102 ;;;
1103 (with-test (:name (:condition-wait :deadlines :LP-512914))
1104   (let ((n 2)                      ; was empirically enough to trigger the bug
1105         (mutex (sb-thread:make-mutex))
1106         (waitq (sb-thread:make-waitqueue))
1107         (threads nil)
1108         (deadline-handler-run-twice? nil))
1109     (dotimes (i n)
1110       (let ((child
1111               (sb-thread:make-thread
1112                #'(lambda ()
1113                    (handler-bind
1114                        ((sb-sys:deadline-timeout
1115                           (let ((already? nil))
1116                             #'(lambda (c)
1117                                 (when already?
1118                                   (setq deadline-handler-run-twice? t))
1119                                 (setq already? t)
1120                                 (sleep 0.2)
1121                                 (sb-thread:condition-broadcast waitq)
1122                                 (sb-sys:defer-deadline 10.0 c)))))
1123                      (sb-sys:with-deadline (:seconds 0.1)
1124                        (sb-thread:with-mutex (mutex)
1125                          (sb-thread:condition-wait waitq mutex))))))))
1126         (push child threads)))
1127     (mapc #'sb-thread:join-thread threads)
1128     (assert (not deadline-handler-run-twice?))))
1129
1130 (with-test (:name (:condition-wait :signal-deadline-with-interrupts-enabled))
1131   (let ((mutex (sb-thread:make-mutex))
1132         (waitq (sb-thread:make-waitqueue))
1133         (A-holds? :unknown)
1134         (B-holds? :unknown)
1135         (A-interrupts-enabled? :unknown)
1136         (B-interrupts-enabled? :unknown)
1137         (A)
1138         (B))
1139     ;; W.L.O.G., we assume that A is executed first...
1140     (setq A (sb-thread:make-thread
1141              #'(lambda ()
1142                  (handler-bind
1143                      ((sb-sys:deadline-timeout
1144                        #'(lambda (c)
1145                            ;; We came here through the call to DECODE-TIMEOUT
1146                            ;; in CONDITION-WAIT; hence both here are supposed
1147                            ;; to evaluate to T.
1148                            (setq A-holds? (sb-thread:holding-mutex-p mutex))
1149                            (setq A-interrupts-enabled?
1150                                  sb-sys:*interrupts-enabled*)
1151                            (sleep 0.2)
1152                            (sb-thread:condition-broadcast waitq)
1153                            (sb-sys:defer-deadline 10.0 c))))
1154                    (sb-sys:with-deadline (:seconds 0.1)
1155                      (sb-thread:with-mutex (mutex)
1156                        (sb-thread:condition-wait waitq mutex)))))
1157              :name "A"))
1158     (setq B (sb-thread:make-thread
1159              #'(lambda ()
1160                  (thread-yield)
1161                  (handler-bind
1162                      ((sb-sys:deadline-timeout
1163                        #'(lambda (c)
1164                            ;; We came here through the call to DECODE-TIMEOUT
1165                            ;; in CONDITION-WAIT (contended case of
1166                            ;; reaquiring the mutex) - so the former will
1167                            ;; be NIL, but interrupts should still be enabled.
1168                            (setq B-holds? (sb-thread:holding-mutex-p mutex))
1169                            (setq B-interrupts-enabled?
1170                                  sb-sys:*interrupts-enabled*)
1171                            (sleep 0.2)
1172                            (sb-thread:condition-broadcast waitq)
1173                            (sb-sys:defer-deadline 10.0 c))))
1174                    (sb-sys:with-deadline (:seconds 0.1)
1175                      (sb-thread:with-mutex (mutex)
1176                        (sb-thread:condition-wait waitq mutex)))))
1177              :name "B"))
1178     (sb-thread:join-thread A)
1179     (sb-thread:join-thread B)
1180     (let ((A-result (list A-holds? A-interrupts-enabled?))
1181           (B-result (list B-holds? B-interrupts-enabled?)))
1182       ;; We also check some subtle behaviour w.r.t. whether a deadline
1183       ;; handler in CONDITION-WAIT got the mutex, or not. This is most
1184       ;; probably very internal behaviour (so user should not depend
1185       ;; on it) -- I added the testing here just to manifest current
1186       ;; behaviour.
1187       (cond ((equal A-result '(t t)) (assert (equal B-result '(nil t))))
1188             ((equal B-result '(t t)) (assert (equal A-result '(nil t))))
1189             (t
1190              (error "Failure: fell through wit A: ~S, B: ~S"
1191                     A-result
1192                     B-result))))))
1193
1194 (with-test (:name (:mutex :finalization))
1195   (let ((a nil))
1196     (dotimes (i 500000)
1197       (setf a (make-mutex)))))
1198
1199 (format t "mutex finalization test done~%")
1200
1201 ;;; Check that INFO is thread-safe, at least when we're just doing reads.
1202
1203 (let* ((symbols (loop repeat 10000 collect (gensym)))
1204        (functions (loop for (symbol . rest) on symbols
1205                         for next = (car rest)
1206                         for fun = (let ((next next))
1207                                     (lambda (n)
1208                                       (if next
1209                                           (funcall next (1- n))
1210                                           n)))
1211                         do (setf (symbol-function symbol) fun)
1212                         collect fun)))
1213   (defun infodb-test ()
1214     (funcall (car functions) 9999)))
1215
1216 (with-test (:name (:infodb :read))
1217   (let* ((ok t)
1218          (threads (loop for i from 0 to 10
1219                         collect (sb-thread:make-thread
1220                                  (lambda ()
1221                                    (dotimes (j 100)
1222                                      (write-char #\-)
1223                                      (finish-output)
1224                                      (let ((n (infodb-test)))
1225                                        (unless (zerop n)
1226                                          (setf ok nil)
1227                                          (format t "N != 0 (~A)~%" n)
1228                                          (abort-thread)))))))))
1229     (wait-for-threads threads)
1230     (assert ok)))
1231
1232 (format t "infodb test done~%")
1233
1234 (with-test (:name :backtrace)
1235   ;; Printing backtraces from several threads at once used to hang the
1236   ;; whole SBCL process (discovered by accident due to a timer.impure
1237   ;; test misbehaving). The cause was that packages weren't even
1238   ;; thread-safe for only doing FIND-SYMBOL, and while printing
1239   ;; backtraces a loot of symbol lookups need to be done due to
1240   ;; *PRINT-ESCAPE*.
1241   (let* ((threads (loop repeat 10
1242                         collect (sb-thread:make-thread
1243                                  (lambda ()
1244                                    (dotimes (i 1000)
1245                                      (with-output-to-string (*debug-io*)
1246                                        (sb-debug::backtrace 10))))))))
1247     (wait-for-threads threads)))
1248
1249 (format t "backtrace test done~%")
1250
1251 (format t "~&starting gc deadlock test: WARNING: THIS TEST WILL HANG ON FAILURE!~%")
1252
1253 (with-test (:name :gc-deadlock)
1254   ;; Prior to 0.9.16.46 thread exit potentially deadlocked the
1255   ;; GC due to *all-threads-lock* and session lock. On earlier
1256   ;; versions and at least on one specific box this test is good enough
1257   ;; to catch that typically well before the 1500th iteration.
1258   (loop
1259      with i = 0
1260      with n = 3000
1261      while (< i n)
1262      do
1263        (incf i)
1264        (when (zerop (mod i 100))
1265          (write-char #\.)
1266          (force-output))
1267        (handler-case
1268            (if (oddp i)
1269                (make-join-thread
1270                 (lambda ()
1271                   (sleep (random 0.001)))
1272                 :name (format nil "SLEEP-~D" i))
1273                (make-join-thread
1274                 (lambda ()
1275                   ;; KLUDGE: what we are doing here is explicit,
1276                   ;; but the same can happen because of a regular
1277                   ;; MAKE-THREAD or LIST-ALL-THREADS, and various
1278                   ;; session functions.
1279                   (sb-thread::with-all-threads-lock
1280                     (sb-thread::with-session-lock (sb-thread::*session*)
1281                       (sb-ext:gc))))
1282                 :name (format nil "GC-~D" i)))
1283          (error (e)
1284            (format t "~%error creating thread ~D: ~A -- backing off for retry~%" i e)
1285            (sleep 0.1)
1286            (incf i)))))
1287
1288 (format t "~&gc deadlock test done~%")
1289 \f
1290 (let ((count (make-array 8 :initial-element 0)))
1291   (defun closure-one ()
1292     (declare (optimize safety))
1293     (values (incf (aref count 0)) (incf (aref count 1))
1294             (incf (aref count 2)) (incf (aref count 3))
1295             (incf (aref count 4)) (incf (aref count 5))
1296             (incf (aref count 6)) (incf (aref count 7))))
1297   (defun no-optimizing-away-closure-one ()
1298     (setf count (make-array 8 :initial-element 0))))
1299
1300 (defstruct box
1301   (count 0))
1302
1303 (let ((one (make-box))
1304       (two (make-box))
1305       (three (make-box)))
1306   (defun closure-two ()
1307     (declare (optimize safety))
1308     (values (incf (box-count one)) (incf (box-count two)) (incf (box-count three))))
1309   (defun no-optimizing-away-closure-two ()
1310     (setf one (make-box)
1311           two (make-box)
1312           three (make-box))))
1313
1314 (with-test (:name (:funcallable-instances))
1315   ;; the funcallable-instance implementation used not to be threadsafe
1316   ;; against setting the funcallable-instance function to a closure
1317   ;; (because the code and lexenv were set separately).
1318   (let ((fun (sb-kernel:%make-funcallable-instance 0))
1319         (condition nil))
1320     (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1321     (flet ((changer ()
1322              (loop (setf (sb-kernel:funcallable-instance-fun fun) #'closure-one)
1323                    (setf (sb-kernel:funcallable-instance-fun fun) #'closure-two)))
1324            (test ()
1325              (handler-case (loop (funcall fun))
1326                (serious-condition (c) (setf condition c)))))
1327       (let ((changer (make-thread #'changer))
1328             (test (make-thread #'test)))
1329         (handler-case
1330             (progn
1331               ;; The two closures above are fairly carefully crafted
1332               ;; so that if given the wrong lexenv they will tend to
1333               ;; do some serious damage, but it is of course difficult
1334               ;; to predict where the various bits and pieces will be
1335               ;; allocated.  Five seconds failed fairly reliably on
1336               ;; both my x86 and x86-64 systems.  -- CSR, 2006-09-27.
1337               (sb-ext:with-timeout 5
1338                 (wait-for-threads (list test)))
1339               (error "~@<test thread got condition:~2I~_~A~@:>" condition))
1340           (sb-ext:timeout ()
1341             (terminate-thread changer)
1342             (terminate-thread test)
1343             (wait-for-threads (list changer test))))))))
1344
1345 (format t "~&funcallable-instance test done~%")
1346
1347 (defun random-type (n)
1348   `(integer ,(random n) ,(+ n (random n))))
1349
1350 (defun subtypep-hash-cache-test ()
1351   (dotimes (i 10000)
1352     (let ((type1 (random-type 500))
1353           (type2 (random-type 500)))
1354       (let ((a (subtypep type1 type2)))
1355         (dotimes (i 100)
1356           (assert (eq (subtypep type1 type2) a))))))
1357   (format t "ok~%")
1358   (force-output))
1359
1360 (with-test (:name (:hash-cache :subtypep))
1361   (mapc #'join-thread
1362         (loop repeat 10
1363               collect (sb-thread:make-thread #'subtypep-hash-cache-test))))
1364 (format t "hash-cache tests done~%")
1365
1366 ;;;; BLACK BOX TESTS
1367
1368 (in-package :cl-user)
1369 (use-package :test-util)
1370 (use-package "ASSERTOID")
1371
1372 (format t "parallel defclass test -- WARNING, WILL HANG ON FAILURE!~%")
1373 (with-test (:name :parallel-defclass)
1374   (defclass test-1 () ((a :initform :orig-a)))
1375   (defclass test-2 () ((b :initform :orig-b)))
1376   (defclass test-3 (test-1 test-2) ((c :initform :orig-c)))
1377   (let* ((run t)
1378          (d1 (sb-thread:make-thread (lambda ()
1379                                       (loop while run
1380                                             do (defclass test-1 () ((a :initform :new-a)))
1381                                             (write-char #\1)
1382                                             (force-output)))
1383                                     :name "d1"))
1384          (d2 (sb-thread:make-thread (lambda ()
1385                                       (loop while run
1386                                             do (defclass test-2 () ((b :initform :new-b)))
1387                                                (write-char #\2)
1388                                                (force-output)))
1389                                     :name "d2"))
1390          (d3 (sb-thread:make-thread (lambda ()
1391                                       (loop while run
1392                                             do (defclass test-3 (test-1 test-2) ((c :initform :new-c)))
1393                                                (write-char #\3)
1394                                                (force-output)))
1395                                     :name "d3"))
1396          (i (sb-thread:make-thread (lambda ()
1397                                      (loop while run
1398                                            do (let ((i (make-instance 'test-3)))
1399                                                 (assert (member (slot-value i 'a) '(:orig-a :new-a)))
1400                                                 (assert (member (slot-value i 'b) '(:orig-b :new-b)))
1401                                                 (assert (member (slot-value i 'c) '(:orig-c :new-c))))
1402                                               (write-char #\i)
1403                                               (force-output)))
1404                                    :name "i")))
1405     (format t "~%sleeping!~%")
1406     (sleep 2.0)
1407     (format t "~%stopping!~%")
1408     (setf run nil)
1409     (mapc (lambda (th)
1410             (sb-thread:join-thread th)
1411             (format t "~%joined ~S~%" (sb-thread:thread-name th)))
1412           (list d1 d2 d3 i))))
1413 (format t "parallel defclass test done~%")
1414
1415 (with-test (:name (:deadlock-detection :interrupts) :fails-on :win32)
1416   #+win32                               ;be more explicit than just :skipped-on
1417   (error "not attempting, because of deadlock error in background thread")
1418   (let* ((m1 (sb-thread:make-mutex :name "M1"))
1419          (m2 (sb-thread:make-mutex :name "M2"))
1420          (t1-can-go (sb-thread:make-semaphore :name "T1 can go"))
1421          (t2-can-go (sb-thread:make-semaphore :name "T2 can go"))
1422          (t1 (sb-thread:make-thread
1423               (lambda ()
1424                 (sb-thread:with-mutex (m1)
1425                   (sb-thread:wait-on-semaphore t1-can-go)
1426                   :ok1))
1427               :name "T1"))
1428          (t2 (sb-thread:make-thread
1429               (lambda ()
1430                 (sb-ext:wait-for (eq t1 (sb-thread:mutex-owner m1)))
1431                 (sb-thread:with-mutex (m1 :wait-p t)
1432                   (sb-thread:wait-on-semaphore t2-can-go)
1433                   :ok2))
1434               :name "T2")))
1435     (sb-ext:wait-for (eq m1 (sb-thread::thread-waiting-for t2)))
1436     (sb-thread:interrupt-thread t2 (lambda ()
1437                                      (sb-thread:with-mutex (m2 :wait-p t)
1438                                        (sb-ext:wait-for
1439                                         (eq m2 (sb-thread::thread-waiting-for t1)))
1440                                        (sb-thread:signal-semaphore t2-can-go))))
1441     (sb-ext:wait-for (eq t2 (sb-thread:mutex-owner m2)))
1442     (sb-thread:interrupt-thread t1 (lambda ()
1443                                      (sb-thread:with-mutex (m2 :wait-p t)
1444                                        (sb-thread:signal-semaphore t1-can-go))))
1445     ;; both threads should finish without a deadlock or deadlock
1446     ;; detection error
1447     (let ((res (list (sb-thread:join-thread t1)
1448                      (sb-thread:join-thread t2))))
1449       (assert (equal '(:ok1 :ok2) res)))))
1450
1451 (with-test (:name (:deadlock-detection :gc))
1452   ;; To semi-reliably trigger the error (in SBCL's where)
1453   ;; it was present you had to run this for > 30 seconds,
1454   ;; but that's a bit long for a single test.
1455   (let* ((stop (+ 5 (get-universal-time)))
1456          (m1 (sb-thread:make-mutex :name "m1"))
1457          (t1 (sb-thread:make-thread
1458               (lambda ()
1459                 (loop until (> (get-universal-time) stop)
1460                       do (sb-thread:with-mutex (m1)
1461                            (eval `(make-array 24))))
1462                 :ok)))
1463          (t2 (sb-thread:make-thread
1464               (lambda ()
1465                 (loop until (> (get-universal-time) stop)
1466                       do (sb-thread:with-mutex (m1)
1467                            (eval `(make-array 24))))
1468                 :ok))))
1469     (let ((res (list (sb-thread:join-thread t1)
1470                      (sb-thread:join-thread t2))))
1471       (assert (equal '(:ok :ok) res)))))
1472
1473 (with-test (:name :spinlock-api)
1474   (let* ((warned 0)
1475          (funs
1476            (handler-bind ((sb-int:early-deprecation-warning (lambda (_)
1477                                                               (declare (ignore _))
1478                                                               (incf warned))))
1479              (list (compile nil `(lambda (lock)
1480                                    (sb-thread::with-spinlock (lock)
1481                                      t)))
1482                    (compile nil `(lambda ()
1483                                    (sb-thread::make-spinlock :name "foo")))
1484                    (compile nil `(lambda (lock)
1485                                    (sb-thread::get-spinlock lock)))
1486                    (compile nil `(lambda (lock)
1487                                    (sb-thread::release-spinlock lock)))))))
1488     (assert (eql 4 warned))
1489     (handler-bind ((warning #'error))
1490       (destructuring-bind (with make get release) funs
1491         (let ((lock (funcall make)))
1492           (funcall get lock)
1493           (funcall release lock)
1494           (assert (eq t (funcall with lock))))))))