From 81e69b85c8bcd9ab8da04c01bc30aaea129b7ff9 Mon Sep 17 00:00:00 2001 From: Uko Kokņevičs Date: Sun, 19 Oct 2025 10:12:04 +0300 Subject: Make the R/W locks somewhat more fair --- test/rw-lock.lisp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'test') 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 @@ ;; SPDX-FileCopyrightText: 2025 Uko Kokņevičs (defpackage :ukkoclot/test/rw-lock (:documentation "Test-suite for :ukkoclot/src/rw-lock.") - (:use :c2cl :fiveam :ukkoclot/src/rw-lock) - (:import-from :bt2 :with-timeout)) + (:use :c2cl :fiveam :iterate :ukkoclot/src/rw-lock) + (:import-from :bt2 :join-thread :make-thread :with-timeout)) (in-package :ukkoclot/test/rw-lock) (def-suite :ukkoclot/rw-lock) @@ -69,6 +69,28 @@ (is-false (acquire-read-lock lock :wait nil)) (is (rw-lock-p (release-write-lock lock))))) +(test read&write-locks.contention + (let ((lock (make-rw-lock)) + (value 0) + reader-threads + writer-threads) + (flet ((reader-fn () (with-read-lock (lock) + (sleep (random 0.1)) + value)) + (writer-fn () (with-write-lock (lock) + (incf value) + (sleep (random 0.1)) + (incf value)))) + (iter (repeat 100) + (push (make-thread #'reader-fn) reader-threads) + (sleep (random 0.02)) + (push (make-thread #'writer-fn) writer-threads))) + (iter (for writer-thread in writer-threads) + (join-thread writer-thread)) + (is (every #'evenp + (iter (for reader-thread in reader-threads) + (collect (join-thread reader-thread))))))) + (test with-read-lock.simple (let ((lock (make-rw-lock))) (with-read-lock (lock) -- cgit v1.2.3