| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;;;; Author: Juan Jose Garcia-Ripoll |
| 5 |
;;;; Created: Fri Apr 14 11:13:17 CEST 2006 |
| 6 |
;;;; Contains: Multithreading API regression tests |
| 7 |
|
| 8 |
(in-package :cl-test) |
| 9 |
|
| 10 |
(suite 'mp) |
| 11 |
|
| 12 |
;;; Important Note: |
| 13 |
;;; |
| 14 |
;;; Testing multithreading primitives such as locks or semaphores |
| 15 |
;;; often requires synchronizing multiple threads. To keep the tests |
| 16 |
;;; as simple as possible, this is synchronization is often done by |
| 17 |
;;; using busy waits. As a consequence, test failures may manifest as |
| 18 |
;;; timeouts. Some tests for semaphores and barriers also use mutexes |
| 19 |
;;; for synchronization purposes and will fail if mutexes don't work |
| 20 |
;;; correctly. Nearly all tests also assume that creating, killing or |
| 21 |
;;; joining threads works properly, which is not tested separately. |
| 22 |
|
| 23 |
|
| 24 |
;; Locks |
| 25 |
|
| 26 |
;;; Date: 12/04/2012 |
| 27 |
;;; Non-recursive mutexes should signal an error when they |
| 28 |
;;; cannot be relocked. |
| 29 |
(test-with-timeout mp.mutex.recursive-error |
| 30 |
(let* ((mutex (mp:make-lock :name 'mutex-001-recursive-error))) |
| 31 |
(is (mp:get-lock mutex)) |
| 32 |
(is (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 33 |
(signals error (mp:get-lock mutex)) |
| 34 |
(is (mp:giveup-lock mutex)) |
| 35 |
(is (null (mp:lock-owner mutex))) |
| 36 |
(is (zerop (mp:lock-count mutex))))) |
| 37 |
|
| 38 |
;;; Date: 12/04/2012 |
| 39 |
;;; Recursive locks increase the counter. |
| 40 |
(test-with-timeout mp.mutex.recursive-count |
| 41 |
(let* ((mutex (mp:make-lock :name 'mutex-002-recursive-count :recursive t))) |
| 42 |
(is (loop for i from 1 upto 10 |
| 43 |
always (and (mp:get-lock mutex) |
| 44 |
(= (mp:lock-count mutex) i) |
| 45 |
(eq (mp:lock-owner mutex) mp:*current-process*)))) |
| 46 |
(is (loop for i from 9 downto 0 |
| 47 |
always (and (eq (mp:lock-owner mutex) mp:*current-process*) |
| 48 |
(mp:giveup-lock mutex) |
| 49 |
(= (mp:lock-count mutex) i)))) |
| 50 |
(is (null (mp:lock-owner mutex))) |
| 51 |
(is (zerop (mp:lock-count mutex))))) |
| 52 |
|
| 53 |
|
| 54 |
;;; Date: 12/04/2012 |
| 55 |
;;; When multiple threads compete for a mutex, they should |
| 56 |
;;; all get the same chance of accessing the resource |
| 57 |
;;; |
| 58 |
;;; Disabled since underlying OS functions don't guarantee fairness -- |
| 59 |
;;; mg 2020-08-22 |
| 60 |
#+(or) |
| 61 |
(test-with-timeout mp.mutex.fairness |
| 62 |
(let* ((mutex (mp:make-lock :name "mutex.fairness")) |
| 63 |
(nthreads 10) |
| 64 |
(count 10) |
| 65 |
(counter (* nthreads count)) |
| 66 |
(array (make-array count :element-type 'fixnum :initial-element 0))) |
| 67 |
(flet ((slave (n) |
| 68 |
(loop with continue = t |
| 69 |
for i from 1 by 1 |
| 70 |
while continue do |
| 71 |
(mp:get-lock mutex) |
| 72 |
(cond ((plusp counter) |
| 73 |
(decf counter) |
| 74 |
(setf (aref array n) i)) |
| 75 |
(t |
| 76 |
(setf continue nil))) |
| 77 |
(mp:giveup-lock mutex)))) |
| 78 |
;; Launch all agents. They will be locked |
| 79 |
(let ((all-processes |
| 80 |
(mp:with-lock (mutex) |
| 81 |
(loop for n from 0 below nthreads |
| 82 |
collect (mp:process-run-function n #'slave n) |
| 83 |
;; ... and give them some time to block on this mutex |
| 84 |
finally (sleep 1))))) |
| 85 |
;; Now they are released and operate. They should all have |
| 86 |
;; the same share of counts. |
| 87 |
(loop for p in all-processes |
| 88 |
do (mp:process-join p)) |
| 89 |
(loop for i from 0 below nthreads |
| 90 |
(is (= (aref array i) count))))))) |
| 91 |
|
| 92 |
(test-with-timeout (mp.mutex.timedlock-timeout 30) |
| 93 |
(let ((mutex (mp:make-lock :name "mutex.timedlock-timeout")) |
| 94 |
(flag 0)) |
| 95 |
(mp:get-lock mutex) |
| 96 |
(setf flag 1) |
| 97 |
(let ((waiting-process |
| 98 |
(mp:process-run-function |
| 99 |
"mutex.timedlock-timeout" |
| 100 |
(lambda () |
| 101 |
(when (mp:get-lock mutex 1) |
| 102 |
(error "Grabbing the mutex shouldn't have succeeded")) |
| 103 |
(when (eq (mp:lock-owner mutex) mp:*current-process*) |
| 104 |
(error "Wrong lock owner")) |
| 105 |
(setf flag 2))))) |
| 106 |
(mp:process-join waiting-process) |
| 107 |
(is (eq mp:*current-process* (mp:lock-owner mutex))) |
| 108 |
(is (= flag 2))))) |
| 109 |
|
| 110 |
(test-with-timeout (mp.mutex.timedlock-acquire 30) |
| 111 |
(let ((mutex (mp:make-lock :name "mutex.timedlock-acquire")) |
| 112 |
(flag 0)) |
| 113 |
(mp:get-lock mutex) |
| 114 |
(setf flag 1) |
| 115 |
(let ((waiting-process |
| 116 |
(mp:process-run-function |
| 117 |
"mutex.timedlock-acquire" |
| 118 |
(lambda () |
| 119 |
(setf flag 2) |
| 120 |
(unless (mp:get-lock mutex 60) |
| 121 |
(error "Grabbing the mutex should have succeeded")) |
| 122 |
(when (not (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 123 |
(error "Wrong lock owner")) |
| 124 |
(setf flag 3) |
| 125 |
(mp:giveup-lock mutex))))) |
| 126 |
(loop until (> flag 1)) |
| 127 |
(sleep 1) |
| 128 |
(mp:giveup-lock mutex) |
| 129 |
(mp:process-join waiting-process) |
| 130 |
(is (= flag 3))))) |
| 131 |
|
| 132 |
;; Semaphores |
| 133 |
|
| 134 |
;;; Date: 14/04/2012 |
| 135 |
;;; Ensure that at creation name and counter are set |
| 136 |
(test mp.sem.make-and-counter |
| 137 |
(loop with name = "sem.make-and-counter" |
| 138 |
for count from 0 to 10 |
| 139 |
for sem = (mp:make-semaphore :name name :count count) |
| 140 |
do (is (eq (mp:semaphore-name sem) name)) |
| 141 |
(is (= (mp:semaphore-count sem) count)) |
| 142 |
(is (zerop (mp:semaphore-wait-count sem))))) |
| 143 |
|
| 144 |
;;; Date: 14/04/2012 |
| 145 |
;;; Ensure that signal changes the counter by the specified amount |
| 146 |
(test-with-timeout mp.sem.signal-semaphore-count |
| 147 |
(loop with name = "sem.signal-semaphore-count" |
| 148 |
for count from 0 to 10 |
| 149 |
do (loop for delta from 0 to 10 |
| 150 |
for sem = (mp:make-semaphore :name name :count count) |
| 151 |
do (is (= (mp:semaphore-count sem) count)) |
| 152 |
(is (null (mp:signal-semaphore sem delta))) |
| 153 |
(is (= (mp:semaphore-count sem ) (+ count delta)))))) |
| 154 |
|
| 155 |
;;; Date: 14/04/2012 |
| 156 |
;;; A semaphore with a count of zero blocks a process |
| 157 |
(test-with-timeout mp.sem.signal-one-process |
| 158 |
(let* ((flag nil) |
| 159 |
(sem (mp:make-semaphore :name "sem.signal-one")) |
| 160 |
(a-process (mp:process-run-function |
| 161 |
"sem.signal-one-process" |
| 162 |
#'(lambda () |
| 163 |
(mp:wait-on-semaphore sem) |
| 164 |
(setf flag t))))) |
| 165 |
(is (null flag)) |
| 166 |
(is (mp:process-active-p a-process)) |
| 167 |
(mp:signal-semaphore sem) |
| 168 |
(mp:process-join a-process) |
| 169 |
(is flag) |
| 170 |
(is (= (mp:semaphore-count sem) 0)))) |
| 171 |
|
| 172 |
;;; Date: 14/04/2012 |
| 173 |
;;; We can signal multiple processes |
| 174 |
(test-with-timeout mp.sem.signal-n-processes |
| 175 |
(loop for count from 1 upto 10 always |
| 176 |
(let* ((counter 0) |
| 177 |
(lock (mp:make-lock :name "sem.signal-n-processes")) |
| 178 |
(sem (mp:make-semaphore :name "sem.signal-n-processs")) |
| 179 |
(all-processes |
| 180 |
(loop for i from 1 upto count |
| 181 |
collect (mp:process-run-function |
| 182 |
"sem.signal-n-processes" |
| 183 |
#'(lambda () |
| 184 |
(mp:wait-on-semaphore sem) |
| 185 |
(mp:with-lock (lock) (incf counter))))))) |
| 186 |
(loop until (= (mp:semaphore-wait-count sem) count)) |
| 187 |
(is (zerop counter)) |
| 188 |
(is (every #'mp:process-active-p all-processes)) |
| 189 |
(mp:signal-semaphore sem count) |
| 190 |
(mapc #'mp:process-join all-processes) |
| 191 |
(is (= counter count) |
| 192 |
"Counter should be ~s (but is ~s)." count counter) |
| 193 |
(is (= (mp:semaphore-count sem) 0))))) |
| 194 |
|
| 195 |
;;; Date: 14/04/2012 |
| 196 |
;;; When we signal N processes and N+M are waiting, only N awake |
| 197 |
(test-with-timeout mp.sem.signal-only-n-processes |
| 198 |
(loop for m from 1 upto 3 do |
| 199 |
(loop for n from 1 upto 4 do |
| 200 |
(let* ((counter 0) |
| 201 |
(lock (mp:make-lock :name "sem.signal-n-processes")) |
| 202 |
(sem (mp:make-semaphore :name "sem.signal-n-processs")) |
| 203 |
(all-processes |
| 204 |
(loop for i from 1 upto (+ n m) |
| 205 |
collect (mp:process-run-function |
| 206 |
"sem.signal-n-processes" |
| 207 |
#'(lambda () |
| 208 |
(mp:wait-on-semaphore sem) |
| 209 |
(mp:with-lock (lock) (incf counter))))))) |
| 210 |
(loop until (= (mp:semaphore-wait-count sem) (+ m n))) |
| 211 |
(is (zerop counter)) |
| 212 |
(is (every #'mp:process-active-p all-processes)) |
| 213 |
(mp:signal-semaphore sem n) |
| 214 |
(loop until (= (count-if #'mp:process-active-p all-processes) m)) |
| 215 |
(is (= counter n)) |
| 216 |
(is (= (mp:semaphore-wait-count sem) m) |
| 217 |
"Number of threads waiting on semaphore should be ~s (but is ~s)." |
| 218 |
m (mp:semaphore-wait-count sem)) |
| 219 |
(mp:signal-semaphore sem m) |
| 220 |
(mapc #'mp:process-join all-processes) |
| 221 |
(is (= counter (+ n m)) |
| 222 |
"Counter should be ~s (but is ~s)." |
| 223 |
(+ n m) counter))))) |
| 224 |
|
| 225 |
;;; Date: 14/04/2012 |
| 226 |
;;; 1 producer and N consumers, non-blocking, because the initial count |
| 227 |
;;; is larger than the consumed data. |
| 228 |
(test-with-timeout mp.sem.1-to-n-non-blocking |
| 229 |
(loop with counter = 0 |
| 230 |
with lock = (mp:make-lock :name "sem.1-to-n-communication") |
| 231 |
for n from 1 to 10 |
| 232 |
for m = (round 128 n) |
| 233 |
for length = (* n m) |
| 234 |
for sem = (mp:make-semaphore :name "sem.1-to-n-communication" :count length) |
| 235 |
for producers = (progn |
| 236 |
(setf counter 0) |
| 237 |
(loop for i from 0 below n |
| 238 |
collect (mp:process-run-function |
| 239 |
"sem.1-to-n-consumer" |
| 240 |
#'(lambda () |
| 241 |
(loop for i from 0 below m |
| 242 |
do (mp:wait-on-semaphore sem) |
| 243 |
do (mp:with-lock (lock) (incf counter))))))) |
| 244 |
do (mapc #'mp:process-join producers) |
| 245 |
(is (= counter length)) |
| 246 |
(is (zerop (mp:semaphore-count sem))) |
| 247 |
(is (zerop (mp:semaphore-wait-count sem))))) |
| 248 |
|
| 249 |
;;; Date: 14/04/2012 |
| 250 |
;;; 1 producer and N consumers, blocking due to a slow producer. |
| 251 |
(test-with-timeout mp.sem.1-to-n-blocking |
| 252 |
(loop with lock = (mp:make-lock :name "sem.1-to-n-communication") |
| 253 |
for n from 1 to 10 |
| 254 |
for m = (round 10000 n) |
| 255 |
for length = (* n m) |
| 256 |
for sem = (mp:make-semaphore :name "sem.1-to-n-communication" :count 0) |
| 257 |
for counter = 0 |
| 258 |
for producers = (loop for i from 0 below n |
| 259 |
collect (mp:process-run-function |
| 260 |
"sem.1-to-n-consumer" |
| 261 |
#'(lambda () |
| 262 |
(loop for i from 0 below m |
| 263 |
do (mp:wait-on-semaphore sem)) |
| 264 |
(mp:with-lock (lock) (incf counter))))) |
| 265 |
do (loop for i from 0 below length |
| 266 |
do (mp:signal-semaphore sem)) |
| 267 |
do (mapc #'mp:process-join producers) |
| 268 |
(is (= counter n)) |
| 269 |
(is (zerop (mp:semaphore-count sem))) |
| 270 |
(is (zerop (mp:semaphore-wait-count sem))))) |
| 271 |
|
| 272 |
;;; Date: 2021-10-19 |
| 273 |
;;; |
| 274 |
;;; A smoke test for the new function wait-semaphore. |
| 275 |
;;; |
| 276 |
(test-with-timeout mp.sem.semaphore-wait/smoke |
| 277 |
(let ((sem (mp:make-semaphore :name "sem.semaphore-wait" :count 3))) |
| 278 |
(flet ((signal-after-fn (count seconds) |
| 279 |
(lambda () |
| 280 |
(sleep seconds) |
| 281 |
(mp:signal-semaphore sem count)))) |
| 282 |
(is (null (mp:semaphore-wait sem 4 0))) |
| 283 |
(is (null (mp:semaphore-wait sem 4 0.1))) |
| 284 |
(is (= 3 (mp:semaphore-wait sem 2 nil))) |
| 285 |
(mp:process-run-function nil (signal-after-fn 1 0.2)) |
| 286 |
(is (null (mp:semaphore-wait sem 2 0.1))) |
| 287 |
(is (= 2 (mp:semaphore-wait sem 2 1))) |
| 288 |
(mp:process-run-function nil (signal-after-fn 2 0.2)) |
| 289 |
(is (= 2 (mp:semaphore-wait sem 1 nil)))))) |
| 290 |
|
| 291 |
|
| 292 |
;; Mailbox |
| 293 |
|
| 294 |
;;; Date: 14/04/2012 |
| 295 |
;;; Ensure that at creation name and counter are set, and mailbox is empty. |
| 296 |
(test mp.mbox.make-and-counter |
| 297 |
(loop with name = "mbox.make-and-counter" |
| 298 |
for count from 4 to 63 |
| 299 |
for mbox = (mp:make-mailbox :name name :count count) |
| 300 |
do (is (eq (mp:mailbox-name mbox) name)) |
| 301 |
(is (>= (mp:mailbox-count mbox) count)) |
| 302 |
(is (mp:mailbox-empty-p mbox)))) |
| 303 |
|
| 304 |
;;; Date: 14/04/2012 |
| 305 |
;;; Ensure that the mailbox works in a nonblocking fashion (when the |
| 306 |
;;; number of messages < mailbox size in a single producer and single |
| 307 |
;;; consumer setting. We do not need to create new threads for this. |
| 308 |
(test-with-timeout mp.mbox.nonblocking-io-1-to-1 |
| 309 |
(loop with count = 30 |
| 310 |
with name = "mbox.nonblocking-io-1-to-1" |
| 311 |
with mbox = (mp:make-mailbox :name name :count count) |
| 312 |
for l from 1 to 10 |
| 313 |
for messages = (loop for i from 1 to l |
| 314 |
do (mp:mailbox-send mbox i) |
| 315 |
collect i) |
| 316 |
do |
| 317 |
(is (not (mp:mailbox-empty-p mbox))) |
| 318 |
(is (equalp (loop for i from 1 to l |
| 319 |
collect (mp:mailbox-read mbox)) |
| 320 |
messages)) |
| 321 |
(is (mp:mailbox-empty-p mbox)))) |
| 322 |
|
| 323 |
;;; Date: 14/04/2012 |
| 324 |
;;; The mailbox blocks a process when it saturates the write queue. |
| 325 |
(test-with-timeout mp.mbox.blocks-1-to-1 |
| 326 |
(let* ((flag nil) |
| 327 |
(mbox (mp:make-mailbox :name "mbox.blocks-1-to-1" :count 32)) |
| 328 |
(size (mp:mailbox-count mbox)) |
| 329 |
(a-process (mp:process-run-function |
| 330 |
"mbox.blocks-1-to-1" |
| 331 |
#'(lambda () |
| 332 |
;; This does not block |
| 333 |
(loop for i from 1 to size |
| 334 |
do (mp:mailbox-send mbox i)) |
| 335 |
;; Here we block |
| 336 |
(setf flag t) |
| 337 |
(mp:mailbox-send mbox (1+ size)) |
| 338 |
;; Now we unblock |
| 339 |
(setf flag nil))))) |
| 340 |
(sleep 0.2) |
| 341 |
(is (not (mp:mailbox-empty-p mbox))) ; the queue has messages |
| 342 |
(is (mp:process-active-p a-process)) ; the process is active |
| 343 |
(is flag) ; and it is blocked |
| 344 |
(is (loop for i from 1 to (1+ size) ; messages arrive in order |
| 345 |
always (= i (mp:mailbox-read mbox)))) |
| 346 |
(mp:process-join a-process) |
| 347 |
(is (null flag)) ; and process unblocked |
| 348 |
(is (mp:mailbox-empty-p mbox)))) |
| 349 |
|
| 350 |
;;; Date: 14/04/2012 |
| 351 |
;;; N producers and 1 consumer |
| 352 |
(test-with-timeout mp.mbox.n-to-1-communication |
| 353 |
(loop with length = 10000 |
| 354 |
with mbox = (mp:make-mailbox :name "mbox.n-to-1-communication" :count 128) |
| 355 |
for n from 1 to 10 |
| 356 |
for m = (round length n) |
| 357 |
for messages = (loop for i from 0 below (* n m) collect i) |
| 358 |
for producers = (loop for i from 0 below n |
| 359 |
do (mp:process-run-function |
| 360 |
"mbox.n-to-1-producer" |
| 361 |
(let ((proc-no i)) |
| 362 |
#'(lambda () |
| 363 |
(loop for i from 0 below m |
| 364 |
for msg = (+ i (* proc-no m)) |
| 365 |
do (mp:mailbox-send mbox msg)))))) |
| 366 |
do (is (equalp |
| 367 |
(sort (loop for i from 1 to (* n m) |
| 368 |
collect (mp:mailbox-read mbox)) |
| 369 |
#'<) |
| 370 |
messages)) |
| 371 |
(is (mp:mailbox-empty-p mbox)))) |
| 372 |
|
| 373 |
;;; Date: 14/04/2012 |
| 374 |
;;; 1 producer and N consumer, but they do not block, because the |
| 375 |
;;; queue is large enough and pre-filled with messages |
| 376 |
(test-with-timeout mp.mbox.1-to-n-non-blocking |
| 377 |
(loop |
| 378 |
for n from 1 to 10 |
| 379 |
for m = (round 128 n) |
| 380 |
for length = (* n m) |
| 381 |
for mbox = (mp:make-mailbox :name "mbox.1-to-n-non-blocking" :count length) |
| 382 |
for flags = (make-array length :initial-element nil) |
| 383 |
for aux = (loop for i from 0 below length |
| 384 |
do (mp:mailbox-send mbox i)) |
| 385 |
for consumers = (loop for i from 0 below n |
| 386 |
collect (mp:process-run-function |
| 387 |
"mbox.1-to-n-consumer" |
| 388 |
#'(lambda () |
| 389 |
(loop for i from 0 below m |
| 390 |
for msg = (mp:mailbox-read mbox) |
| 391 |
do (setf (aref flags msg) t))))) |
| 392 |
do (mapc #'mp:process-join consumers) |
| 393 |
(is (every #'identity flags)) |
| 394 |
(is (mp:mailbox-empty-p mbox)))) |
| 395 |
|
| 396 |
;;; Date: 14/04/2012 |
| 397 |
;;; 1 producer and N consumers, which block, because the producer |
| 398 |
;;; is started _after_ them and is slower. |
| 399 |
(test-with-timeout mp.mbox.1-to-n-blocking |
| 400 |
(loop for n from 1 to 10 |
| 401 |
for m = (round 10000 n) |
| 402 |
for length = (* n m) |
| 403 |
for mbox = (mp:make-mailbox :name "mp.mbox.1-to-n-blocking" :count length) |
| 404 |
for flags = (make-array length :initial-element nil) |
| 405 |
for consumers = (loop for i from 0 below n |
| 406 |
collect (mp:process-run-function |
| 407 |
"mbox.1-to-n-consumer" |
| 408 |
#'(lambda () |
| 409 |
(loop for i from 0 below m |
| 410 |
for msg = (mp:mailbox-read mbox) |
| 411 |
do (setf (aref flags msg) t))))) |
| 412 |
do (loop for i from 0 below length |
| 413 |
do (mp:mailbox-send mbox i)) |
| 414 |
do (mapc #'mp:process-join consumers) |
| 415 |
(is (every #'identity flags)) |
| 416 |
(is (mp:mailbox-empty-p mbox)))) |
| 417 |
|
| 418 |
|
| 419 |
;;; Date: 2016-11-10 |
| 420 |
;;; Description: CLASS-OF called on rwlock crashed lisp process |
| 421 |
(test rwlock |
| 422 |
(finishes (class-of (mp:make-rwlock)))) |
| 423 |
|
| 424 |
|
| 425 |
;;; Date: 2016-10-05 |
| 426 |
;;; From: Daniel Kochmański |
| 427 |
;;; Description: |
| 428 |
;;; |
| 429 |
;;; HOLDING-LOCK-P verifies, if the current process holds the |
| 430 |
;;; lock. |
| 431 |
;;; |
| 432 |
(test-with-timeout mp.mutex.holding-lock-p |
| 433 |
(let ((lock (mp:make-lock :name "mutex.holding-lock-p" :recursive nil))) |
| 434 |
(is-false (mp:holding-lock-p lock)) |
| 435 |
(mp:with-lock (lock) |
| 436 |
(is-true (mp:holding-lock-p lock)) |
| 437 |
(mp:process-run-function |
| 438 |
"mutex.holding-lock-p" |
| 439 |
#'(lambda () (is-false (mp:holding-lock-p lock))))) |
| 440 |
(is-false (mp:holding-lock-p lock)) |
| 441 |
(mp:process-run-function |
| 442 |
"mutex.holding-lock-p" |
| 443 |
#'(lambda () (is-false (mp:holding-lock-p lock)))))) |
| 444 |
|
| 445 |
|
| 446 |
;; Atomics |
| 447 |
|
| 448 |
(ext:with-clean-symbols (test-struct test-class *x*) |
| 449 |
(defstruct (test-struct :atomic-accessors) |
| 450 |
(slot1 0) |
| 451 |
(slot2 1 :read-only t)) |
| 452 |
(defclass test-class () |
| 453 |
((slot1 :initform 0) |
| 454 |
(slot2))) |
| 455 |
(defvar *x*) |
| 456 |
;;; Date: 2018-09-21 |
| 457 |
;;; From: Marius Gerbershagen |
| 458 |
;;; Description: |
| 459 |
;;; |
| 460 |
;;; Verifies that atomic-update works correctly. |
| 461 |
;;; |
| 462 |
(test-with-timeout mp.atomics.atomic-update |
| 463 |
(let* ((n-threads 100) |
| 464 |
(n-updates 1000) |
| 465 |
(n-total (* n-threads n-updates))) |
| 466 |
(let ((cons (cons 0 0)) |
| 467 |
(vector (make-array 3 :initial-element 0)) |
| 468 |
(object (make-instance 'test-class)) |
| 469 |
(struct (make-test-struct))) |
| 470 |
(setf *x* 0) |
| 471 |
(setf (get '*x* 'n) 0) |
| 472 |
(mapc #'mp:process-join |
| 473 |
(loop repeat n-threads |
| 474 |
collect (mp:process-run-function |
| 475 |
"" |
| 476 |
(lambda () |
| 477 |
(loop repeat n-updates do |
| 478 |
(mp:atomic-update (cdr cons) #'1+) |
| 479 |
(mp:atomic-update (car cons) #'1+) |
| 480 |
(mp:atomic-update (svref vector 1) #'1+) |
| 481 |
(mp:atomic-update (symbol-value '*x*) #'1+) |
| 482 |
(mp:atomic-update (symbol-plist '*x*) |
| 483 |
#'(lambda (plist) |
| 484 |
(list (first plist) |
| 485 |
(1+ (second plist))))) |
| 486 |
(mp:atomic-update (slot-value object 'slot1) #'1+) |
| 487 |
(mp:atomic-update (test-struct-slot1 struct) #'1+)))))) |
| 488 |
(is (car cons) n-total) |
| 489 |
(is (cdr cons) n-total) |
| 490 |
(is (svref vector 1) n-total) |
| 491 |
(is *x* n-total) |
| 492 |
(is (get '*x* 'n) n-total) |
| 493 |
(is (slot-value object 'slot1) n-total) |
| 494 |
(signals error (mp:compare-and-swap (slot-value object 'slot2) 0 1)) |
| 495 |
(is (and (eq (mp:compare-and-swap (slot-value object 'slot2) si:unbound 1) si:unbound) |
| 496 |
(eq (slot-value object 'slot2) 1))) |
| 497 |
(signals error (eval '(mp:compare-and-swap (test-struct-slot2 struct) 1 2)))))) |
| 498 |
|
| 499 |
|
| 500 |
;;; Date: 2018-09-22 |
| 501 |
;;; From: Marius Gerbershagen |
| 502 |
;;; Description: |
| 503 |
;;; |
| 504 |
;;; Verifies that atomic-push and atomic-pop work correctly. |
| 505 |
;;; |
| 506 |
(test-with-timeout mp.atomics.atomic-push/pop |
| 507 |
(let* ((n-threads 100) |
| 508 |
(n-updates 1000)) |
| 509 |
(setf *x* nil) |
| 510 |
(mapc #'mp:process-join |
| 511 |
(loop repeat n-threads |
| 512 |
collect (mp:process-run-function |
| 513 |
"" |
| 514 |
(lambda () |
| 515 |
(loop for i below n-updates do |
| 516 |
(mp:atomic-push i (symbol-value '*x*))) |
| 517 |
(loop repeat (1- n-updates) do |
| 518 |
(mp:atomic-pop (symbol-value '*x*))))))) |
| 519 |
(is (length *x*) n-threads))) |
| 520 |
|
| 521 |
;;; Date: 2018-09-29 |
| 522 |
;;; From: Marius Gerbershagen |
| 523 |
;;; Description: |
| 524 |
;;; |
| 525 |
;;; Verifies that atomic-incf and atomic-decf work correctly. |
| 526 |
;;; |
| 527 |
(test-with-timeout mp.atomics.atomic-incf/decf |
| 528 |
(let* ((n-threads 100) |
| 529 |
(n-updates 1000) |
| 530 |
(increment (1+ (random most-positive-fixnum))) |
| 531 |
(final-value (+ (mod (+ (* n-threads n-updates increment) |
| 532 |
most-negative-fixnum) |
| 533 |
(* -2 most-negative-fixnum)) |
| 534 |
most-negative-fixnum))) |
| 535 |
(let ((cons (cons 0 0)) |
| 536 |
(vector (make-array 3 :initial-element 0)) |
| 537 |
(object (make-instance 'test-class))) |
| 538 |
(setf *x* 0) |
| 539 |
(mapc #'mp:process-join |
| 540 |
(loop repeat n-threads |
| 541 |
collect (mp:process-run-function |
| 542 |
"" |
| 543 |
(lambda () |
| 544 |
(loop repeat n-updates do |
| 545 |
(mp:atomic-incf (cdr cons) increment) |
| 546 |
(mp:atomic-incf (car cons) increment) |
| 547 |
(mp:atomic-incf (svref vector 1) increment) |
| 548 |
(mp:atomic-incf (symbol-value '*x*) increment) |
| 549 |
(mp:atomic-incf (slot-value object 'slot1) increment)))))) |
| 550 |
(is (car cons) final-value) |
| 551 |
(is (cdr cons) final-value) |
| 552 |
(is (svref vector 1) final-value) |
| 553 |
(is *x* final-value) |
| 554 |
(is (slot-value object 'slot1) final-value) |
| 555 |
(mapc #'mp:process-join |
| 556 |
(loop repeat n-threads |
| 557 |
collect (mp:process-run-function |
| 558 |
"" |
| 559 |
(lambda () |
| 560 |
(loop repeat n-updates do |
| 561 |
(mp:atomic-decf (cdr cons) increment) |
| 562 |
(mp:atomic-decf (car cons) increment) |
| 563 |
(mp:atomic-decf (svref vector 1) increment) |
| 564 |
(mp:atomic-decf (symbol-value '*x*) increment) |
| 565 |
(mp:atomic-decf (slot-value object 'slot1) increment)))))) |
| 566 |
(is (car cons) 0) |
| 567 |
(is (cdr cons) 0) |
| 568 |
(is (svref vector 1) 0) |
| 569 |
(is *x* 0) |
| 570 |
(is (slot-value object 'slot1) 0))))) |
| 571 |
|
| 572 |
;;; Date: 2019-02-05 |
| 573 |
;;; From: Daniel Kochmański |
| 574 |
;;; Description: |
| 575 |
;;; |
| 576 |
;;; Verifies that CAS expansion may be removed. |
| 577 |
;;; |
| 578 |
(ext:with-clean-symbols (*obj* foo) |
| 579 |
(test mp.atomics.defcas/remcas |
| 580 |
(mp:defcas foo (lambda (object old new) |
| 581 |
(assert (consp object)) |
| 582 |
(setf (car object) old |
| 583 |
(cdr object) new))) |
| 584 |
(defparameter *obj* (cons nil nil)) |
| 585 |
(eval `(mp:compare-and-swap (foo *obj*) :car :cdr)) |
| 586 |
(is (eql (car *obj*) :car)) |
| 587 |
(is (eql (cdr *obj*) :cdr)) |
| 588 |
(mp:remcas 'foo) |
| 589 |
(signals error (eval `(mp:compare-and-swap (foo *obj*) :car :cdr))))) |
| 590 |
|
| 591 |
;;; Date: 2019-02-07 |
| 592 |
;;; From: Daniel Kochmański |
| 593 |
;;; Description: |
| 594 |
;;; |
| 595 |
;;; Verifies that CAS modifications honor the package locks. |
| 596 |
;;; |
| 597 |
(test mp.atomics.cas-locked-package |
| 598 |
(signals package-error (mp:defcas cl:car (lambda (obj old new) nil))) |
| 599 |
(signals package-error (mp:remcas 'cl:car)) |
| 600 |
(finishes (mp:defcas cor (lambda (obj old new) nil))) |
| 601 |
(finishes (mp:remcas 'cor))) |
| 602 |
|
| 603 |
|
| 604 |
;; Barriers |
| 605 |
|
| 606 |
;;; Date: 2020-08-14 |
| 607 |
;;; From: Daniel Kochmański |
| 608 |
;;; Description: |
| 609 |
;;; |
| 610 |
;;; Smoke tests for barriers. |
| 611 |
;;; |
| 612 |
|
| 613 |
(test mp.barrier.slots |
| 614 |
(let ((barrier (mp:make-barrier 3 :name 'foo))) |
| 615 |
(is (eq 'foo (mp:barrier-name barrier))) |
| 616 |
(is (= 3 (mp:barrier-count barrier))) |
| 617 |
(is (= 0 (mp:barrier-arrivers-count barrier))))) |
| 618 |
|
| 619 |
(let (barrier before-barrier after-barrier all-processes mutex) |
| 620 |
(labels ((try-barrier () |
| 621 |
(push (mp:process-run-function |
| 622 |
"try-barrier" |
| 623 |
(lambda () |
| 624 |
(mp:with-lock (mutex) |
| 625 |
(incf before-barrier)) |
| 626 |
(mp:barrier-wait barrier) |
| 627 |
(mp:with-lock (mutex) |
| 628 |
(incf after-barrier)))) |
| 629 |
all-processes)) |
| 630 |
(check-barrier (before after arrivers) |
| 631 |
(try-barrier) |
| 632 |
(loop until (= before before-barrier)) |
| 633 |
(loop until (= after after-barrier)) |
| 634 |
(loop until (= arrivers (mp:barrier-arrivers-count barrier)))) |
| 635 |
(wake-barrier () |
| 636 |
(mp:barrier-unblock barrier :kill-waiting nil)) |
| 637 |
(kill-barrier () |
| 638 |
(mp:barrier-unblock barrier :kill-waiting t))) |
| 639 |
|
| 640 |
(test-with-timeout mp.barrier.blocking |
| 641 |
(setf barrier (mp:make-barrier 3) |
| 642 |
before-barrier 0 |
| 643 |
after-barrier 0 |
| 644 |
all-processes nil |
| 645 |
mutex (mp:make-lock)) |
| 646 |
(check-barrier 1 0 1) |
| 647 |
(check-barrier 2 0 2) |
| 648 |
(check-barrier 3 3 0) |
| 649 |
(check-barrier 4 3 1) |
| 650 |
(check-barrier 5 3 2) |
| 651 |
(check-barrier 6 6 0) |
| 652 |
;; clean up |
| 653 |
(mapc #'mp:process-join all-processes)) |
| 654 |
|
| 655 |
(test-with-timeout mp.barrier.unblock-1 |
| 656 |
(setf barrier (mp:make-barrier 3) |
| 657 |
before-barrier 0 |
| 658 |
after-barrier 0 |
| 659 |
all-processes nil |
| 660 |
mutex (mp:make-lock)) |
| 661 |
(check-barrier 1 0 1) |
| 662 |
(check-barrier 2 0 2) |
| 663 |
(wake-barrier) |
| 664 |
(mapc #'mp:process-join all-processes) |
| 665 |
(check-barrier 3 2 1) |
| 666 |
(check-barrier 4 2 2) |
| 667 |
(kill-barrier) |
| 668 |
(mapc #'mp:process-join all-processes) |
| 669 |
(setf all-processes nil) |
| 670 |
(check-barrier 5 2 1) |
| 671 |
(check-barrier 6 2 2) |
| 672 |
(check-barrier 7 5 0) |
| 673 |
;; clean up |
| 674 |
(mapc #'mp:process-join all-processes)) |
| 675 |
|
| 676 |
(test-with-timeout mp.barrier.unblock-2 |
| 677 |
(setf barrier (mp:make-barrier 3) |
| 678 |
before-barrier 0 |
| 679 |
after-barrier 0 |
| 680 |
all-processes nil |
| 681 |
mutex (mp:make-lock)) |
| 682 |
(mp:barrier-unblock barrier :disable t) |
| 683 |
(check-barrier 1 1 0) |
| 684 |
(check-barrier 2 2 0) |
| 685 |
(check-barrier 3 3 0) |
| 686 |
(check-barrier 4 4 0) |
| 687 |
;; clean up |
| 688 |
(mapc #'mp:process-join all-processes)) |
| 689 |
|
| 690 |
(test-with-timeout mp.barrier.unblock-3 |
| 691 |
(setf barrier (mp:make-barrier 3) |
| 692 |
before-barrier 0 |
| 693 |
after-barrier 0 |
| 694 |
all-processes nil |
| 695 |
mutex (mp:make-lock)) |
| 696 |
(mp:barrier-unblock barrier :reset-count 4) |
| 697 |
(check-barrier 1 0 1) |
| 698 |
(check-barrier 2 0 2) |
| 699 |
(check-barrier 3 0 3) |
| 700 |
(check-barrier 4 4 0) |
| 701 |
(mapc #'mp:process-join all-processes) |
| 702 |
(setf all-processes nil) |
| 703 |
(check-barrier 5 4 1) |
| 704 |
(check-barrier 6 4 2) |
| 705 |
(check-barrier 7 4 3) |
| 706 |
(check-barrier 8 8 0) |
| 707 |
;; clean up |
| 708 |
(mapc #'mp:process-join all-processes)))) |
| 709 |
|
| 710 |
|
| 711 |
;; Condition variables |
| 712 |
(test-with-timeout mp.cv.wait-and-signal |
| 713 |
(let* ((mutex (mp:make-lock :name "cv.wait-and-signal")) |
| 714 |
(cv (mp:make-condition-variable)) |
| 715 |
(counter-before 0) |
| 716 |
(counter-after 0) |
| 717 |
(n-threads 10) |
| 718 |
(waiting-processes |
| 719 |
(loop repeat n-threads collect |
| 720 |
(mp:process-run-function |
| 721 |
"cv.wait-and-signal" |
| 722 |
(lambda () |
| 723 |
(mp:get-lock mutex) |
| 724 |
(incf counter-before) |
| 725 |
(mp:condition-variable-wait cv mutex) |
| 726 |
(when (not (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 727 |
(error "Wrong lock owner")) |
| 728 |
(incf counter-after) |
| 729 |
(mp:giveup-lock mutex)))))) |
| 730 |
(loop until (mp:with-lock (mutex) (= counter-before n-threads))) |
| 731 |
;; signal wakes up _at least_ one thread each time it is called (for |
| 732 |
;; efficiency reasons, the posix specification explicitely allows waking |
| 733 |
;; up more than one thread) |
| 734 |
(loop with n-signaled = 0 ; minimum number of threads that have woken up |
| 735 |
until (= n-signaled n-threads) |
| 736 |
do (mp:get-lock mutex) |
| 737 |
(is (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 738 |
(mp:condition-variable-signal cv) |
| 739 |
(mp:giveup-lock mutex) |
| 740 |
(is (not (eq (mp:lock-owner mutex) mp:*current-process*))) |
| 741 |
(loop until (mp:with-lock (mutex) |
| 742 |
(and (> counter-after n-signaled) |
| 743 |
(setf n-signaled counter-after))))))) |
| 744 |
|
| 745 |
(test-with-timeout mp.cv.wait-and-broadcast |
| 746 |
(let* ((mutex (mp:make-lock :name "cv.wait-and-broadcast")) |
| 747 |
(cv (mp:make-condition-variable)) |
| 748 |
(counter-before 0) |
| 749 |
(counter-after 0) |
| 750 |
(wakeup nil) |
| 751 |
(n-threads 10) |
| 752 |
(waiting-processes |
| 753 |
(loop repeat n-threads collect |
| 754 |
(mp:process-run-function |
| 755 |
"cv.wait-and-broadcast" |
| 756 |
(lambda () |
| 757 |
(mp:get-lock mutex) |
| 758 |
(incf counter-before) |
| 759 |
(loop until wakeup ; ignore spurious wakeups (allowed by posix) |
| 760 |
do (mp:condition-variable-wait cv mutex)) |
| 761 |
(when (not (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 762 |
(error "Wrong lock owner")) |
| 763 |
(incf counter-after) |
| 764 |
(mp:giveup-lock mutex)))))) |
| 765 |
(loop until (mp:with-lock (mutex) (= counter-before n-threads))) |
| 766 |
;; broadcast wakes up all threads |
| 767 |
(mp:get-lock mutex) |
| 768 |
(is (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 769 |
(setf wakeup t) |
| 770 |
(mp:condition-variable-broadcast cv) |
| 771 |
(mp:giveup-lock mutex) |
| 772 |
(is (not (eq (mp:lock-owner mutex) mp:*current-process*))) |
| 773 |
(mapc #'mp:process-join waiting-processes) |
| 774 |
(is (= counter-after n-threads)))) |
| 775 |
|
| 776 |
(test-with-timeout (mp.cv.timedwait-timeout 30) ; whole test times out after 30 seconds |
| 777 |
(let* ((mutex (mp:make-lock :name "cv.timedwait-timeout")) |
| 778 |
(cv (mp:make-condition-variable)) |
| 779 |
(flag 0) |
| 780 |
(waiting-process |
| 781 |
(mp:process-run-function |
| 782 |
"cv.timedwait-timeout" |
| 783 |
(lambda () |
| 784 |
(mp:get-lock mutex) |
| 785 |
(setf flag 1) |
| 786 |
;; condition variable times out after 1 second, before |
| 787 |
;; whole test times out |
| 788 |
(mp:condition-variable-timedwait cv mutex 1) |
| 789 |
(when (not (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 790 |
(error "Wrong lock owner")) |
| 791 |
(setf flag 2) |
| 792 |
(mp:giveup-lock mutex))))) |
| 793 |
(loop until (mp:with-lock (mutex) (/= flag 0))) |
| 794 |
(mp:process-join waiting-process) |
| 795 |
(is (null (mp:lock-owner mutex))) |
| 796 |
(is (= flag 2)))) |
| 797 |
|
| 798 |
(test-with-timeout (mp.cv.timedwait-signal 30) ; whole test times out after 30 seconds |
| 799 |
(let* ((mutex (mp:make-lock :name "cv.timedwait-signal")) |
| 800 |
(cv (mp:make-condition-variable)) |
| 801 |
(flag 0) |
| 802 |
(waiting-process |
| 803 |
(mp:process-run-function |
| 804 |
"cv.timedwait-signal" |
| 805 |
(lambda () |
| 806 |
(mp:get-lock mutex) |
| 807 |
(setf flag 1) |
| 808 |
;; condition variable times out after 60 seconds, after |
| 809 |
;; whole test times out |
| 810 |
(mp:condition-variable-timedwait cv mutex 60) |
| 811 |
(when (not (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 812 |
(error "Wrong lock owner")) |
| 813 |
(setf flag 2) |
| 814 |
(mp:giveup-lock mutex))))) |
| 815 |
(loop until (mp:with-lock (mutex) (/= flag 0))) |
| 816 |
(mp:get-lock mutex) |
| 817 |
(is (eq (mp:lock-owner mutex) mp:*current-process*)) |
| 818 |
(mp:condition-variable-signal cv) |
| 819 |
(mp:giveup-lock mutex) |
| 820 |
(is (not (eq (mp:lock-owner mutex) mp:*current-process*))) |
| 821 |
(mp:process-join waiting-process) |
| 822 |
(is (= flag 2)))) |
| 823 |
|