diff options
Diffstat (limited to 'test/rw-lock.lisp')
| -rw-r--r-- | test/rw-lock.lisp | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/test/rw-lock.lisp b/test/rw-lock.lisp index fdb60bd..834d0fd 100644 --- a/test/rw-lock.lisp +++ b/test/rw-lock.lisp | |||
| @@ -2,8 +2,8 @@ | |||
| 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> | 2 | ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs <perkontevs@gmail.com> |
| 3 | (defpackage :ukkoclot/test/rw-lock | 3 | (defpackage :ukkoclot/test/rw-lock |
| 4 | (:documentation "Test-suite for :ukkoclot/src/rw-lock.") | 4 | (:documentation "Test-suite for :ukkoclot/src/rw-lock.") |
| 5 | (:use :c2cl :fiveam :ukkoclot/src/rw-lock) | 5 | (:use :c2cl :fiveam :iterate :ukkoclot/src/rw-lock) |
| 6 | (:import-from :bt2 :with-timeout)) | 6 | (:import-from :bt2 :join-thread :make-thread :with-timeout)) |
| 7 | (in-package :ukkoclot/test/rw-lock) | 7 | (in-package :ukkoclot/test/rw-lock) |
| 8 | 8 | ||
| 9 | (def-suite :ukkoclot/rw-lock) | 9 | (def-suite :ukkoclot/rw-lock) |
| @@ -69,6 +69,28 @@ | |||
| 69 | (is-false (acquire-read-lock lock :wait nil)) | 69 | (is-false (acquire-read-lock lock :wait nil)) |
| 70 | (is (rw-lock-p (release-write-lock lock))))) | 70 | (is (rw-lock-p (release-write-lock lock))))) |
| 71 | 71 | ||
| 72 | (test read&write-locks.contention | ||
| 73 | (let ((lock (make-rw-lock)) | ||
| 74 | (value 0) | ||
| 75 | reader-threads | ||
| 76 | writer-threads) | ||
| 77 | (flet ((reader-fn () (with-read-lock (lock) | ||
| 78 | (sleep (random 0.1)) | ||
| 79 | value)) | ||
| 80 | (writer-fn () (with-write-lock (lock) | ||
| 81 | (incf value) | ||
| 82 | (sleep (random 0.1)) | ||
| 83 | (incf value)))) | ||
| 84 | (iter (repeat 100) | ||
| 85 | (push (make-thread #'reader-fn) reader-threads) | ||
| 86 | (sleep (random 0.02)) | ||
| 87 | (push (make-thread #'writer-fn) writer-threads))) | ||
| 88 | (iter (for writer-thread in writer-threads) | ||
| 89 | (join-thread writer-thread)) | ||
| 90 | (is (every #'evenp | ||
| 91 | (iter (for reader-thread in reader-threads) | ||
| 92 | (collect (join-thread reader-thread))))))) | ||
| 93 | |||
| 72 | (test with-read-lock.simple | 94 | (test with-read-lock.simple |
| 73 | (let ((lock (make-rw-lock))) | 95 | (let ((lock (make-rw-lock))) |
| 74 | (with-read-lock (lock) | 96 | (with-read-lock (lock) |