This repository was archived by the owner on May 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlockable.lisp
More file actions
57 lines (39 loc) · 1.29 KB
/
lockable.lisp
File metadata and controls
57 lines (39 loc) · 1.29 KB
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
(defpackage :net.mwatters.lockable
(:nicknames :lockable)
(:use :common-lisp)
(:import-from :struct-like-classes
:define-struct-like-class)
(:export
:make-lock
:with-lock
:lockable
:with-thing-locked
:make-lockable-hash-table
:with-lockable-hash-table-locked))
(in-package :net.mwatters.lockable)
(defun make-lock (&key name)
#+single-threaded (declare (ignore name))
#-single-threaded (bt:make-recursive-lock name))
#+single-threaded
(defmacro with-lock ((lock) &body forms)
(declare (ignore lock))
`(progn ,@forms))
#-single-threaded
(defmacro with-lock ((lock) &body forms)
`(bt:with-recursive-lock-held (,lock) ,@forms))
(define-struct-like-class lockable ()
(lock (make-lock)))
(defmacro with-thing-locked (what &body forms)
`(with-lock ((lockable-lock ,what))
,@forms))
(define-struct-like-class (lockable-hash-table
(:constructor make-lockable-hash-table-1)) (lockable)
actual)
(defun make-lockable-hash-table (&rest args)
(make-lockable-hash-table-1 :actual (apply #'make-hash-table args)))
(defmacro with-lockable-hash-table-locked ((var ht) &body forms)
(let ((v (gensym "V")))
`(let ((,v ,ht))
(with-thing-locked ,v
(let ((,var (lockable-hash-table-actual ,v)))
,@forms)))))