0.8.10.7:
[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 #-sb-thread (quit :unix-status 104)
15
16 (in-package "SB-THREAD") ; this is white-box testing, really
17
18 ;;; For one of the interupt-thread tests, we want a foreign function
19 ;;; that does not make syscalls
20
21 (with-open-file (o "threads-foreign.c" :direction :output :if-exists :supersede)
22   (format o "void loop_forever() { while(1) ; }~%"))
23 (sb-ext:run-program     
24  "cc"
25  (or #+linux '("-shared" "-o" "threads-foreign.so" "threads-foreign.c")
26      (error "Missing shared library compilation options for this platform"))
27  :search t)
28 (sb-alien:load-1-foreign "threads-foreign.so")
29 (sb-alien:define-alien-routine loop-forever sb-alien:void)
30
31
32 ;;; elementary "can we get a lock and release it again"
33 (let ((l (make-mutex :name "foo"))
34       (p (current-thread-id)))
35   (assert (eql (mutex-value l) nil) nil "1")
36   (assert (eql (mutex-lock l) 0) nil "2")
37   (sb-thread:get-mutex l)
38   (assert (eql (mutex-value l) p) nil "3")
39   (assert (eql (mutex-lock l) 0) nil "4")
40   (sb-thread:release-mutex l)
41   (assert (eql (mutex-value l) nil) nil "5")
42   (assert (eql (mutex-lock l) 0)  nil "6")
43   (describe l))
44
45 (let ((queue (make-waitqueue :name "queue"))
46       (lock (make-mutex :name "lock")))
47   (labels ((in-new-thread ()
48              (with-mutex (lock)
49                (assert (eql (mutex-value lock) (current-thread-id)))
50                (format t "~A got mutex~%" (current-thread-id))
51                ;; now drop it and sleep
52                (condition-wait queue lock)
53                ;; after waking we should have the lock again
54                (assert (eql (mutex-value lock) (current-thread-id))))))
55     (make-thread #'in-new-thread)
56     (sleep 2)                           ; give it  a chance to start
57     ;; check the lock is free while it's asleep
58     (format t "parent thread ~A~%" (current-thread-id))
59     (assert (eql (mutex-value lock) nil))    
60     (assert (eql (mutex-lock lock) 0))
61     (with-mutex (lock)
62       (condition-notify queue))
63     (sleep 1)))
64
65 (let ((queue (make-waitqueue :name "queue"))
66       (lock (make-mutex :name "lock")))
67   (labels ((ours-p (value)
68              (sb-vm:control-stack-pointer-valid-p
69               (sb-sys:int-sap (sb-kernel:get-lisp-obj-address value))))
70            (in-new-thread ()
71              (with-recursive-lock (lock)
72                (assert (ours-p (mutex-value lock)))
73                (format t "~A got mutex~%" (mutex-value lock))
74                ;; now drop it and sleep
75                (condition-wait queue lock)
76                ;; after waking we should have the lock again
77                (format t "woken, ~A got mutex~%" (mutex-value lock))
78                (assert (ours-p (mutex-value lock))))))
79     (make-thread #'in-new-thread)
80     (sleep 2)                           ; give it  a chance to start
81     ;; check the lock is free while it's asleep
82     (format t "parent thread ~A~%" (current-thread-id))
83     (assert (eql (mutex-value lock) nil))    
84     (assert (eql (mutex-lock lock) 0))
85     (with-recursive-lock (lock)
86       (condition-notify queue))
87     (sleep 1)))
88
89 (let ((mutex (make-mutex :name "contended")))
90   (labels ((run ()
91              (let ((me (current-thread-id)))
92                (dotimes (i 100)
93                  (with-mutex (mutex)
94                    (sleep .1)
95                    (assert (eql (mutex-value mutex) me)))
96                  (assert (not (eql (mutex-value mutex) me))))
97                (format t "done ~A~%" (current-thread-id)))))
98     (let ((kid1 (make-thread #'run))
99           (kid2 (make-thread #'run)))
100       (format t "contention ~A ~A~%" kid1 kid2))))
101
102 (defun test-interrupt (function-to-interrupt &optional quit-p)
103   (let ((child  (make-thread function-to-interrupt)))
104     ;;(format t "gdb ./src/runtime/sbcl ~A~%attach ~A~%" child child)
105     (sleep 2)
106     (format t "interrupting child ~A~%" child)
107     (interrupt-thread child
108                       (lambda ()
109                         (format t "child pid ~A~%" (current-thread-id))
110                         (when quit-p (sb-ext:quit))))
111     (sleep 1)
112     child))
113
114 ;;; separate tests for (a) interrupting Lisp code, (b) C code, (c) a syscall,
115 ;;; (d) waiting on a lock, (e) some code which we hope is likely to be
116 ;;; in pseudo-atomic
117
118 (let ((child (test-interrupt (lambda () (loop)))))  (terminate-thread child))
119
120 (test-interrupt #'loop-forever :quit)
121
122 (let ((child (test-interrupt (lambda () (loop (sleep 2000))))))
123   ;; Interrupting a sleep form causes it to return early.  Welcome to Unix.
124   ;; Just to be sure our LOOP form works, let's check the child is still
125   ;; there
126   (assert (zerop (sb-unix:unix-kill child 0)))
127   (terminate-thread child))
128                 
129 (let ((lock (make-mutex :name "loctite"))
130       child)
131   (with-mutex (lock)
132     (setf child (test-interrupt
133                  (lambda ()
134                    (with-mutex (lock)
135                      (assert (eql (mutex-value lock) (current-thread-id))))
136                    (assert (not (eql (mutex-value lock) (current-thread-id))))
137                    (sleep 60))))
138     ;;hold onto lock for long enough that child can't get it immediately
139     (sleep 20)
140     (interrupt-thread child (lambda () (format t "l ~A~%" (mutex-value lock))))
141     (format t "parent releasing lock~%"))
142   (terminate-thread child))
143
144 (defun alloc-stuff () (copy-list '(1 2 3 4 5)))
145
146 (let ((c (test-interrupt (lambda () (loop (alloc-stuff))))))
147   ;; NB this only works on x86: other ports don't have a symbol for
148   ;; pseudo-atomic atomicity
149   (format t "new thread ~A~%" c)
150   (dotimes (i 100)
151     (sleep (random 1d0))
152     (interrupt-thread c
153                       (lambda ()
154                         (princ ".") (force-output)
155                         (assert (zerop SB-KERNEL:*PSEUDO-ATOMIC-ATOMIC*)))))
156   (terminate-thread c))
157
158 (format t "~&interrupt test done~%")
159
160 (let (a-done b-done)
161   (make-thread (lambda ()
162                  (dotimes (i 100) 
163                    (sb-ext:gc) (princ "\\") (force-output) )
164                  (setf a-done t)))
165   (make-thread (lambda ()
166                  (dotimes (i 25) 
167                    (sb-ext:gc :full t)
168                    (princ "/") (force-output))
169                  (setf b-done t)))
170   (loop
171    (when (and a-done b-done) (return))
172    (sleep 1)))
173 (format t "~&gc test done~%")
174
175 #|  ;; a cll post from eric marsden
176 | (defun crash ()
177 |   (setq *debugger-hook*
178 |         (lambda (condition old-debugger-hook)
179 |           (debug:backtrace 10)
180 |           (unix:unix-exit 2)))
181 |   #+live-dangerously
182 |   (mp::start-sigalrm-yield)
183 |   (flet ((roomy () (loop (with-output-to-string (*standard-output*) (room)))))
184 |     (mp:make-process #'roomy)
185 |     (mp:make-process #'roomy)))
186 |#
187
188 ;; give the other thread time to die before we leave, otherwise the
189 ;; overall exit status is 0, not 104
190 (sleep 2) 
191
192 (sb-ext:quit :unix-status 104)