1ck_swlock(3) BSD Library Functions Manual ck_swlock(3)
2
4 ck_swlock_init, ck_swlock_write_latch, ck_swlock_write_unlatch,
5 ck_swlock_write_lock, ck_swlock_write_unlock, ck_swlock_write_trylock,
6 ck_swlock_write_downgrade, ck_swlock_locked_writer, ck_swlock_read_lock,
7 ck_swlock_read_trylock, ck_swlock_read_unlock, ck_swlock_locked_reader —
8 centralized copy-safe write-biased single-writer read-write locks
9
11 Concurrency Kit (libck, -lck)
12
14 #include <ck_swlock.h>
15
16 ck_swlock_t lock = CK_SWLOCK_INITIALIZER;
17
18 void
19 ck_swlock_init(ck_swlock_t *lock);
20
21 void
22 ck_swlock_write_lock(ck_swlock_t *lock);
23
24 void
25 ck_swlock_write_unlock(ck_swlock_t *lock);
26
27 void
28 ck_swlatch_write_latch(ck_swlatch_t *latch);
29
30 void
31 ck_swlatch_write_unlatch(ck_swlatch_t *latch);
32
33 bool
34 ck_swlock_write_trylock(ck_swlock_t *lock);
35
36 bool
37 ck_swlock_write_downgrade(ck_swlock_t *lock);
38
39 bool
40 ck_swlock_locked_writer(ck_swlock_t *lock);
41
42 void
43 ck_swlock_read_lock(ck_swlock_t *lock);
44
45 bool
46 ck_swlock_read_trylock(ck_swlock_t *lock);
47
48 void
49 ck_swlock_read_unlock(ck_swlock_t *lock);
50
51 bool
52 ck_swlock_locked_reader(ck_swlock_t *lock);
53
55 This is a centralized write-biased single-writer reader-writer lock. It
56 requires half the space that ck_rwlock does and has a low latency fast
57 path. The lock supports latch and unlatch operations that allow it to be
58 used in a copy-safe manner (reader-bits may be over-written safely).
59
61 #include <ck_swlock.h>
62
63 static ck_swlock_t lock = CK_SWLOCK_INITIALIZER;
64
65 static void
66 reader(void)
67 {
68
69 for (;;) {
70 ck_swlock_read_lock(&lock);
71 /* Read-side critical section. */
72 ck_swlock_read_unlock(&lock);
73
74 if (ck_swlock_read_trylock(&lock) == true) {
75 /* Read-side critical section. */
76 ck_swlock_read_unlock(&lock);
77 }
78 }
79
80 return;
81 }
82
83 static void
84 writer(void)
85 {
86 ck_swlock_t contrived;
87
88 for (;;) {
89 ck_swlock_write_lock(&lock);
90 /* Write-side critical section. */
91 ck_swlock_write_unlock(&lock);
92
93 if (ck_swlock_write_trylock(&lock) == true) {
94 /* Write-side critical section. */
95 ck_swlock_write_unlock(&lock);
96 }
97
98 ck_swlock_write_latch(&lock);
99 /* Write-side critical section. */
100
101 /* This is safe to do with-in a latch. */
102 contrived = lock;
103 lock = contrived;
104 ck_swlock_write_unlatch(&lock);
105 }
106
107 return;
108 }
109
111 ck_brlock(3), ck_elide(3), ck_pflock(3), ck_rwlock(3), ck_tflock(3)
112
113 Additional information available at http://concurrencykit.org/
114
115 April 22, 2014.