1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373
|
;;;; miscellaneous tests of thread stuff
;;;; This software is part of the SBCL system. See the README file for
;;;; more information.
;;;;
;;;; While most of SBCL is derived from the CMU CL system, the test
;;;; files (like this one) were written from scratch after the fork
;;;; from CMU CL.
;;;
;;;; This software is in the public domain and is provided with
;;;; absoluely no warranty. See the COPYING and CREDITS files for
;;;; more information.
;;;; STRUCTURAL TESTS
(shadowing-import 'assertoid:assert-error)
(use-package "SB-THREAD")
(use-package "SB-SYS")
(setf sb-unix::*on-dangerous-wait* :error)
(with-test (:name (:threads :trivia))
(assert (eq *current-thread*
(find (thread-name *current-thread*) (list-all-threads)
:key #'thread-name :test #'equal)))
(assert (thread-alive-p *current-thread*)))
(with-test (:name (with-mutex :basics))
(let ((mutex (make-mutex)))
(with-mutex (mutex)
mutex)))
(sb-alien:define-alien-routine "check_deferrables_blocked_or_lose"
void
(where sb-alien:unsigned-long))
(sb-alien:define-alien-routine "check_deferrables_unblocked_or_lose"
void
(where sb-alien:unsigned-long))
(with-test (:name (interrupt-thread :basics :no-unwinding))
(let ((a 0))
(interrupt-thread *current-thread* (lambda () (setq a 1)))
(process-all-interrupts)
(assert (eql a 1))))
(with-test (:name (interrupt-thread :deferrables-blocked))
(interrupt-thread *current-thread*
(lambda ()
;; Make sure sb-ext:gc doesn't leave the
;; deferrables unblocked
(sb-ext:gc)
(check-deferrables-blocked-or-lose 0)))
(process-all-interrupts))
(with-test (:name (interrupt-thread :deferrables-unblocked))
(interrupt-thread *current-thread*
(lambda ()
(with-interrupts
(check-deferrables-unblocked-or-lose 0))))
(process-all-interrupts))
(with-test (:name (interrupt-thread :nlx))
(catch 'xxx
(interrupt-thread *current-thread*
(lambda ()
(check-deferrables-blocked-or-lose 0)
(throw 'xxx nil)))
(process-all-interrupts))
(check-deferrables-unblocked-or-lose 0))
#-sb-thread (invoke-restart 'run-tests::skip-file)
;;;; Now the real tests...
(with-test (:name (with-mutex :timeout)
:broken-on :gc-stress)
(let ((m (make-mutex)))
(with-mutex (m)
(assert (null (join-thread (make-thread
(lambda ()
(with-mutex (m :timeout 0.1)
t)))))))
(assert (join-thread (make-thread
(lambda ()
(with-mutex (m :timeout 0.1)
t)))))))
;;; compare-and-swap
(defmacro defincf (name accessor &rest args)
`(defun ,name (x)
(let* ((old (,accessor x ,@args))
(new (1+ old)))
(loop until (eq old (sb-ext:compare-and-swap (,accessor x ,@args) old new))
do (setf old (,accessor x ,@args)
new (1+ old)))
new)))
(defstruct cas-struct (slot 0))
(defincf incf-car car)
(defincf incf-cdr cdr)
(defincf incf-slot cas-struct-slot)
(defincf incf-symbol-value symbol-value)
(defincf incf-svref/1 svref 1)
(defincf incf-svref/0 svref 0)
(macrolet
((def (name init incf op)
`(with-test (:name ,name)
(let* ((n 200000)
(x ,init)
(run (sb-thread:make-semaphore))
(threadcount 10)
(threads
(loop repeat threadcount
collect (make-thread
(lambda ()
(sb-thread:wait-on-semaphore run)
(loop repeat n do (,incf x)))))))
(sb-thread:signal-semaphore run threadcount)
(map nil #'join-thread threads)
(assert (= (,op x) (* threadcount n)))))))
(def (cas car) (cons 0 nil) incf-car car)
(def (cas cdr) (cons nil 0) incf-cdr cdr)
(def (cas :slot) (make-cas-struct) incf-slot cas-struct-slot)
(def (cas :value)
(let ((x '.x.))
(set x 0)
x)
incf-symbol-value
symbol-value)
(def (cas :svref/0) (vector 0 nil) incf-svref/0 (lambda (x) (svref x 0)))
(def (cas :svref/1) (vector nil 0) incf-svref/1 (lambda (x) (svref x 1))))
(with-test (:name (:threads :more-trivia))
(let ((old-threads (list-all-threads))
(thread (make-thread
(lambda ()
;; I honestly have no idea what this is testing.
;; It seems to be nothing more than an implementation change detector test.
(assert (sb-thread::avl-find
(sb-thread::thread-primitive-thread sb-thread:*current-thread*)
sb-thread::*all-threads*))
(sleep 2))))
(new-threads (list-all-threads)))
(assert (thread-alive-p thread))
;; there is no order guarantee
;;(assert (eq thread (first new-threads)))
(assert (= (1+ (length old-threads)) (length new-threads)))
(sleep 3)
(assert (not (thread-alive-p thread)))))
(with-test (:name (join-thread :abort :default))
(let* ((sym (gensym))
(thread (make-thread (lambda () (abort-thread)))))
(assert (equal (multiple-value-list
(join-thread thread :default sym))
(list sym :abort)))))
(with-test (:name (join-thread :abort :error))
(assert-error (join-thread (make-thread (lambda () (abort-thread))))
join-thread-error))
(with-test (:name (join-thread :timeout :default))
(let* ((sym (gensym))
(sem (make-semaphore))
(thread (make-thread (lambda () (wait-on-semaphore sem)))))
(assert (equal (multiple-value-list
(join-thread thread :timeout .001 :default sym))
(list sym :timeout)))
(signal-semaphore sem)
(assert (join-thread thread))))
(with-test (:name (join-thread :timeout :error))
(let* ((sem (make-semaphore))
(thread (make-thread (lambda () (wait-on-semaphore sem)))))
(assert-error (join-thread thread :timeout .001) join-thread-error)
(signal-semaphore sem)
(assert (join-thread thread))))
(with-test (:name (join-thread :multiple-values))
(assert (equal '(1 2 3)
(multiple-value-list
(join-thread (make-thread (lambda () (values 1 2 3))))))))
;; Used to signal a SIMPLE-ERROR about a recursive lock attempt.
(with-test (:name (join-thread :self-join))
(assert-error (join-thread *current-thread*) join-thread-error))
;;; elementary "can we get a lock and release it again"
(with-test (:name (:mutex :basics))
(let ((l (make-mutex :name "foo"))
(p *current-thread*))
(assert (eql (mutex-owner l) nil) nil "1")
(grab-mutex l)
(assert (eql (mutex-owner l) p) nil "3")
(release-mutex l)
(assert (eql (mutex-owner l) nil) nil "5")))
(with-test (:name (with-recursive-lock :basics))
(labels ((ours-p (value)
(eq *current-thread* value)))
(let ((l (make-mutex :name "rec")))
(assert (eql (mutex-owner l) nil) nil "1")
(with-recursive-lock (l)
(assert (ours-p (mutex-owner l)) nil "3")
(with-recursive-lock (l)
(assert (ours-p (mutex-owner l)) nil "4"))
(assert (ours-p (mutex-owner l)) nil "5"))
(assert (eql (mutex-owner l) nil) nil "6"))))
(with-test (:name (with-recursive-lock :wait-p))
(let ((m (make-mutex)))
(with-mutex (m)
(assert (null (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :wait-p nil)
t)))))))
(assert (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :wait-p nil)
t)))))))
(with-test (:name (with-recursive-lock :wait-p :recursive))
(let ((m (make-mutex)))
(assert (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :wait-p nil)
(with-recursive-lock (m :wait-p nil)
t))))))))
(with-test (:name (with-recursive-lock :timeout)
:broken-on :gc-stress)
(let ((m (make-mutex)))
(with-mutex (m)
(assert (null (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :timeout 0.1)
t)))))))
(assert (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :timeout 0.1)
t)))))))
(with-test (:name (with-recursive-lock :timeout :recursive))
(let ((m (make-mutex)))
(assert (join-thread (make-thread
(lambda ()
(with-recursive-lock (m :timeout 0.1)
(with-recursive-lock (m :timeout 0.1)
t))))))))
(with-test (:name (:mutex :nesting-mutex-and-recursive-lock))
(let ((l (make-mutex :name "a mutex")))
(with-mutex (l)
(with-recursive-lock (l)))))
(with-test (:name (condition-wait :basics-1)
:skipped-on :gc-stress)
(let ((queue (make-waitqueue :name "queue"))
(lock (make-mutex :name "lock"))
(n 0))
(labels ((in-new-thread ()
(with-mutex (lock)
(assert (eql (mutex-owner lock) *current-thread*))
(format t "~A got mutex~%" *current-thread*)
;; now drop it and sleep
;; FIXME: condition-wait returning doesn't mean there was condition-notify
(condition-wait queue lock)
;; after waking we should have the lock again
(assert (eql (mutex-owner lock) *current-thread*))
(assert (eql n 1))
(decf n))))
(make-join-thread #'in-new-thread)
(sleep 2) ; give it a chance to start
;; check the lock is free while it's asleep
(format t "parent thread ~A~%" *current-thread*)
(assert (eql (mutex-owner lock) nil))
(with-mutex (lock)
(incf n)
(condition-notify queue))
(sleep 1))))
(with-test (:name (condition-wait :basics-2)
:skipped-on :gc-stress)
(let ((queue (make-waitqueue :name "queue"))
(lock (make-mutex :name "lock")))
(labels ((ours-p (value)
(eq *current-thread* value))
(in-new-thread ()
(with-recursive-lock (lock)
(assert (ours-p (mutex-owner lock)))
(format t "~A got mutex~%" (mutex-owner lock))
;; now drop it and sleep
;; FIXME: condition-wait returning doesn't mean there was condition-notify
(condition-wait queue lock)
;; after waking we should have the lock again
(format t "woken, ~A got mutex~%" (mutex-owner lock))
(assert (ours-p (mutex-owner lock))))))
(make-join-thread #'in-new-thread)
(sleep 2) ; give it a chance to start
;; check the lock is free while it's asleep
(format t "parent thread ~A~%" *current-thread*)
(assert (eql (mutex-owner lock) nil))
(with-recursive-lock (lock)
(condition-notify queue))
(sleep 1))))
;;; GRAB-MUTEX
(with-test (:name (grab-mutex :waitp nil))
(let ((m (make-mutex)))
(with-mutex (m)
(assert (null (join-thread (make-thread
#'(lambda ()
(grab-mutex m :waitp nil)))))))))
(with-test (:name (grab-mutex :timeout :acquisition-fail))
(let ((m (make-mutex))
(w (make-semaphore)))
(with-mutex (m)
(let ((th (make-thread
#'(lambda ()
(prog1
(grab-mutex m :timeout 0.1)
(signal-semaphore w))))))
;; Wait for it to -- otherwise the detect the deadlock chain
;; from JOIN-THREAD.
(wait-on-semaphore w)
(assert (null (join-thread th)))))))
(with-test (:name (grab-mutex :timeout :acquisition-success)
:skipped-on :gc-stress)
(let ((m (make-mutex))
(child))
(with-mutex (m)
(setq child (make-thread #'(lambda () (grab-mutex m :timeout 1.0))))
(sleep 0.2))
(assert (eq (join-thread child) 't))))
(with-test (:name (grab-mutex :timeout+deadline :lp-1727789))
(flet ((test (deadline)
(let ((m (make-mutex))
(w (make-semaphore)))
(with-mutex (m)
(let ((th (make-thread #'(lambda ()
(sb-sys:with-deadline (:seconds 0.0)
(handler-case
(grab-mutex m :timeout deadline)
(sb-sys:deadline-timeout ()
(signal-semaphore w)
:deadline)))))))
(wait-on-semaphore w)
(assert (eq (join-thread th) :deadline)))))))
(test 0.0)
(test 10000000000000000000000)))
(with-test (:name (grab-mutex :waitp+deadline))
(let ((m (make-mutex)))
(with-mutex (m)
(assert (eq (join-thread
(make-thread #'(lambda ()
(sb-sys:with-deadline (:seconds 0.0)
(handler-case
(grab-mutex m :waitp nil)
(sb-sys:deadline-timeout ()
:deadline))))))
'nil)))))
|