1ck_brlock(3) BSD Library Functions Manual ck_brlock(3)
2
4 ck_brlock_init, ck_brlock_write_lock, ck_brlock_write_unlock,
5 ck_brlock_write_trylock, ck_brlock_read_register,
6 ck_brlock_read_unregister, ck_brlock_read_lock, ck_brlock_read_trylock,
7 ck_brlock_read_unlock — big-reader locks
8
10 Concurrency Kit (libck, -lck)
11
13 #include <ck_brlock.h>
14
15 ck_brlock_t brlock = CK_BRLOCK_INITIALIZER;
16
17 ck_brlock_reader_t reader = CK_BRLOCK_READER_INITIALIZER;
18
19 void
20 ck_brlock_init(ck_brlock_t *br);
21
22 void
23 ck_brlock_write_lock(ck_brlock_t *br);
24
25 void
26 ck_brlock_write_unlock(ck_brlock_t *br);
27
28 bool
29 ck_brlock_write_trylock(ck_brlock_t *br, unsigned int factor);
30
31 void
32 ck_brlock_read_register(ck_brlock_t *br, ck_brlock_reader_t *reader);
33
34 void
35 ck_brlock_read_unregister(ck_brlock_t *br, ck_brlock_reader_t *reader);
36
37 void
38 ck_brlock_read_lock(ck_brlock_t *br, ck_brlock_reader_t *reader);
39
40 bool
41 ck_brlock_read_trylock(ck_brlock_t *br, ck_brlock_reader_t *reader,
42 unsigned int factor);
43
44 void
45 ck_brlock_read_unlock(ck_brlock_reader_t *reader);
46
48 Big reader locks are distributed reader-writer locks with low latency
49 constant time reader acquisition (with respect to number of concurrent
50 readers). On the other hand, writer acquisitions are a relatively expen‐
51 sive O(n) operation. This is a write-biased lock.
52
54 static ck_brlock_t lock = CK_BRLOCK_INITIALIZER;
55 static __thread ck_brlock_reader_t reader;
56
57 static void
58 reader(void)
59 {
60
61 /* Add our thread as a lock participant. */
62 ck_brlock_read_register(&lock, &reader);
63
64 for (;;) {
65 ck_brlock_read_lock(&lock, &reader);
66 /* Read-side critical section. */
67 ck_brlock_read_unlock(&reader);
68
69 if (ck_brlock_read_trylock(&lock, &reader, 1) == true) {
70 /* Read-side critical section. */
71 ck_brlock_read_unlock(&reader);
72 }
73 }
74
75 return;
76 }
77
78 static void
79 writer(void)
80 {
81
82 for (;;) {
83 ck_brlock_write_lock(&lock);
84 /* Write-side critical section. */
85 ck_brlock_write_unlock(&lock);
86
87 if (ck_brlock_write_trylock(&lock, 1) == true) {
88 /* Write-side critical section. */
89 ck_brlock_write_unlock(&lock);
90 }
91 }
92
93 return;
94 }
95
97 ck_bytelock(3), ck_rwlock(3)
98
99 Additional information available at http://concurrencykit.org/
100
101 July 26, 2013.