Plaster
New
List
Login
common-lisp
default
anonymous
2020.08.16 19:08:45
(defmacro write-with-rw-lock ((lock) &body body) (let ((locked (gensym))) `(let ((,locked nil)) (unwind-protect (progn (begin-write ,lock) (setf ,locked t) (if ,locked (locally ,@body)) (if ,locked (end-write ,lock))) (if ,locked (end-write ,lock))))))
Raw
Annotate
Repaste
Edit
Annotations
text
default
anonymous
2020.08.16 19:14:54
(defmacro write-with-rw-lock ((lock) &body body) (let ((locked (gensym))) `(let ((,locked nil)) (unwind-protect (when (begin-write ,lock) (prog2 (setf ,locked t) (locally ,@body) (when (end-write ,lock) (setf ,locked nil)))) (when ,locked (end-write ,lock))))))
Raw
Repaste
Edit
text
default
anonymous
2020.08.16 20:55:55
;; Don't need active-writer; if a writer has the lock then its active. ;; If writers-waiting > 0 then readers should defer. (defmacro write-with-rw-lock ((reader-writer-lock) &body body) (let ((retval (gensym)) (lock (gensym))) `(let ((,retval nil) (,lock ,reader-writer-lock)) (with-accessors ((g g-lock) (ra readers-active) (ww writers-waiting) (c-var condition-var)) ,lock (bt:with-lock-held (g) (unwind-protect (incf ww) (unwind-protect (progn (while-loop (> ra 0) (bt:condition-wait c-var g)) (setf ,retval (multiple-value-list (progn ,@body)))) (decf ww) (bt:condition-notify c-var))))) (values-list ,retval))))
Raw
Repaste
Edit