1*1fb62fb0SOlivier Houchard /*
2*1fb62fb0SOlivier Houchard * Copyright 2014 Jaidev Sridhar.
3*1fb62fb0SOlivier Houchard * Copyright 2014 Samy Al Bahra.
4*1fb62fb0SOlivier Houchard * All rights reserved.
5*1fb62fb0SOlivier Houchard *
6*1fb62fb0SOlivier Houchard * Redistribution and use in source and binary forms, with or without
7*1fb62fb0SOlivier Houchard * modification, are permitted provided that the following conditions
8*1fb62fb0SOlivier Houchard * are met:
9*1fb62fb0SOlivier Houchard * 1. Redistributions of source code must retain the above copyright
10*1fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer.
11*1fb62fb0SOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
12*1fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer in the
13*1fb62fb0SOlivier Houchard * documentation and/or other materials provided with the distribution.
14*1fb62fb0SOlivier Houchard *
15*1fb62fb0SOlivier Houchard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16*1fb62fb0SOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17*1fb62fb0SOlivier Houchard * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18*1fb62fb0SOlivier Houchard * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19*1fb62fb0SOlivier Houchard * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20*1fb62fb0SOlivier Houchard * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21*1fb62fb0SOlivier Houchard * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22*1fb62fb0SOlivier Houchard * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23*1fb62fb0SOlivier Houchard * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24*1fb62fb0SOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25*1fb62fb0SOlivier Houchard * SUCH DAMAGE.
26*1fb62fb0SOlivier Houchard */
27*1fb62fb0SOlivier Houchard
28*1fb62fb0SOlivier Houchard #ifndef CK_SWLOCK_H
29*1fb62fb0SOlivier Houchard #define CK_SWLOCK_H
30*1fb62fb0SOlivier Houchard
31*1fb62fb0SOlivier Houchard #include <ck_elide.h>
32*1fb62fb0SOlivier Houchard #include <ck_limits.h>
33*1fb62fb0SOlivier Houchard #include <ck_pr.h>
34*1fb62fb0SOlivier Houchard #include <ck_stdbool.h>
35*1fb62fb0SOlivier Houchard #include <ck_stddef.h>
36*1fb62fb0SOlivier Houchard
37*1fb62fb0SOlivier Houchard struct ck_swlock {
38*1fb62fb0SOlivier Houchard uint32_t value;
39*1fb62fb0SOlivier Houchard };
40*1fb62fb0SOlivier Houchard typedef struct ck_swlock ck_swlock_t;
41*1fb62fb0SOlivier Houchard
42*1fb62fb0SOlivier Houchard #define CK_SWLOCK_INITIALIZER {0}
43*1fb62fb0SOlivier Houchard #define CK_SWLOCK_WRITER_BIT (1UL << 31)
44*1fb62fb0SOlivier Houchard #define CK_SWLOCK_LATCH_BIT (1UL << 30)
45*1fb62fb0SOlivier Houchard #define CK_SWLOCK_WRITER_MASK (CK_SWLOCK_LATCH_BIT | CK_SWLOCK_WRITER_BIT)
46*1fb62fb0SOlivier Houchard #define CK_SWLOCK_READER_MASK (UINT32_MAX ^ CK_SWLOCK_WRITER_MASK)
47*1fb62fb0SOlivier Houchard
48*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_init(struct ck_swlock * rw)49*1fb62fb0SOlivier Houchard ck_swlock_init(struct ck_swlock *rw)
50*1fb62fb0SOlivier Houchard {
51*1fb62fb0SOlivier Houchard
52*1fb62fb0SOlivier Houchard rw->value = 0;
53*1fb62fb0SOlivier Houchard ck_pr_barrier();
54*1fb62fb0SOlivier Houchard return;
55*1fb62fb0SOlivier Houchard }
56*1fb62fb0SOlivier Houchard
57*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_write_unlock(ck_swlock_t * rw)58*1fb62fb0SOlivier Houchard ck_swlock_write_unlock(ck_swlock_t *rw)
59*1fb62fb0SOlivier Houchard {
60*1fb62fb0SOlivier Houchard
61*1fb62fb0SOlivier Houchard ck_pr_fence_unlock();
62*1fb62fb0SOlivier Houchard ck_pr_and_32(&rw->value, CK_SWLOCK_READER_MASK);
63*1fb62fb0SOlivier Houchard return;
64*1fb62fb0SOlivier Houchard }
65*1fb62fb0SOlivier Houchard
66*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_swlock_locked_writer(ck_swlock_t * rw)67*1fb62fb0SOlivier Houchard ck_swlock_locked_writer(ck_swlock_t *rw)
68*1fb62fb0SOlivier Houchard {
69*1fb62fb0SOlivier Houchard bool r;
70*1fb62fb0SOlivier Houchard
71*1fb62fb0SOlivier Houchard r = ck_pr_load_32(&rw->value) & CK_SWLOCK_WRITER_BIT;
72*1fb62fb0SOlivier Houchard ck_pr_fence_acquire();
73*1fb62fb0SOlivier Houchard return r;
74*1fb62fb0SOlivier Houchard }
75*1fb62fb0SOlivier Houchard
76*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_write_downgrade(ck_swlock_t * rw)77*1fb62fb0SOlivier Houchard ck_swlock_write_downgrade(ck_swlock_t *rw)
78*1fb62fb0SOlivier Houchard {
79*1fb62fb0SOlivier Houchard
80*1fb62fb0SOlivier Houchard ck_pr_inc_32(&rw->value);
81*1fb62fb0SOlivier Houchard ck_swlock_write_unlock(rw);
82*1fb62fb0SOlivier Houchard return;
83*1fb62fb0SOlivier Houchard }
84*1fb62fb0SOlivier Houchard
85*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_swlock_locked(ck_swlock_t * rw)86*1fb62fb0SOlivier Houchard ck_swlock_locked(ck_swlock_t *rw)
87*1fb62fb0SOlivier Houchard {
88*1fb62fb0SOlivier Houchard bool r;
89*1fb62fb0SOlivier Houchard
90*1fb62fb0SOlivier Houchard r = ck_pr_load_32(&rw->value);
91*1fb62fb0SOlivier Houchard ck_pr_fence_acquire();
92*1fb62fb0SOlivier Houchard return r;
93*1fb62fb0SOlivier Houchard }
94*1fb62fb0SOlivier Houchard
95*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_swlock_write_trylock(ck_swlock_t * rw)96*1fb62fb0SOlivier Houchard ck_swlock_write_trylock(ck_swlock_t *rw)
97*1fb62fb0SOlivier Houchard {
98*1fb62fb0SOlivier Houchard bool r;
99*1fb62fb0SOlivier Houchard
100*1fb62fb0SOlivier Houchard r = ck_pr_cas_32(&rw->value, 0, CK_SWLOCK_WRITER_BIT);
101*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
102*1fb62fb0SOlivier Houchard return r;
103*1fb62fb0SOlivier Houchard }
104*1fb62fb0SOlivier Houchard
CK_ELIDE_TRYLOCK_PROTOTYPE(ck_swlock_write,ck_swlock_t,ck_swlock_locked,ck_swlock_write_trylock)105*1fb62fb0SOlivier Houchard CK_ELIDE_TRYLOCK_PROTOTYPE(ck_swlock_write, ck_swlock_t,
106*1fb62fb0SOlivier Houchard ck_swlock_locked, ck_swlock_write_trylock)
107*1fb62fb0SOlivier Houchard
108*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
109*1fb62fb0SOlivier Houchard ck_swlock_write_lock(ck_swlock_t *rw)
110*1fb62fb0SOlivier Houchard {
111*1fb62fb0SOlivier Houchard
112*1fb62fb0SOlivier Houchard ck_pr_or_32(&rw->value, CK_SWLOCK_WRITER_BIT);
113*1fb62fb0SOlivier Houchard while (ck_pr_load_32(&rw->value) & CK_SWLOCK_READER_MASK)
114*1fb62fb0SOlivier Houchard ck_pr_stall();
115*1fb62fb0SOlivier Houchard
116*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
117*1fb62fb0SOlivier Houchard return;
118*1fb62fb0SOlivier Houchard }
119*1fb62fb0SOlivier Houchard
120*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_write_latch(ck_swlock_t * rw)121*1fb62fb0SOlivier Houchard ck_swlock_write_latch(ck_swlock_t *rw)
122*1fb62fb0SOlivier Houchard {
123*1fb62fb0SOlivier Houchard
124*1fb62fb0SOlivier Houchard /* Publish intent to acquire lock. */
125*1fb62fb0SOlivier Houchard ck_pr_or_32(&rw->value, CK_SWLOCK_WRITER_BIT);
126*1fb62fb0SOlivier Houchard
127*1fb62fb0SOlivier Houchard /* Stall until readers have seen the writer and cleared. */
128*1fb62fb0SOlivier Houchard while (ck_pr_cas_32(&rw->value, CK_SWLOCK_WRITER_BIT,
129*1fb62fb0SOlivier Houchard CK_SWLOCK_WRITER_MASK) == false) {
130*1fb62fb0SOlivier Houchard do {
131*1fb62fb0SOlivier Houchard ck_pr_stall();
132*1fb62fb0SOlivier Houchard } while (ck_pr_load_32(&rw->value) != CK_SWLOCK_WRITER_BIT);
133*1fb62fb0SOlivier Houchard }
134*1fb62fb0SOlivier Houchard
135*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
136*1fb62fb0SOlivier Houchard return;
137*1fb62fb0SOlivier Houchard }
138*1fb62fb0SOlivier Houchard
139*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_write_unlatch(ck_swlock_t * rw)140*1fb62fb0SOlivier Houchard ck_swlock_write_unlatch(ck_swlock_t *rw)
141*1fb62fb0SOlivier Houchard {
142*1fb62fb0SOlivier Houchard
143*1fb62fb0SOlivier Houchard ck_pr_fence_unlock();
144*1fb62fb0SOlivier Houchard ck_pr_store_32(&rw->value, 0);
145*1fb62fb0SOlivier Houchard return;
146*1fb62fb0SOlivier Houchard }
147*1fb62fb0SOlivier Houchard
CK_ELIDE_PROTOTYPE(ck_swlock_write,ck_swlock_t,ck_swlock_locked,ck_swlock_write_lock,ck_swlock_locked_writer,ck_swlock_write_unlock)148*1fb62fb0SOlivier Houchard CK_ELIDE_PROTOTYPE(ck_swlock_write, ck_swlock_t,
149*1fb62fb0SOlivier Houchard ck_swlock_locked, ck_swlock_write_lock,
150*1fb62fb0SOlivier Houchard ck_swlock_locked_writer, ck_swlock_write_unlock)
151*1fb62fb0SOlivier Houchard
152*1fb62fb0SOlivier Houchard CK_ELIDE_TRYLOCK_PROTOTYPE(ck_swlock_read, ck_swlock_t,
153*1fb62fb0SOlivier Houchard ck_swlock_locked_writer, ck_swlock_read_trylock)
154*1fb62fb0SOlivier Houchard
155*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
156*1fb62fb0SOlivier Houchard ck_swlock_read_trylock(ck_swlock_t *rw)
157*1fb62fb0SOlivier Houchard {
158*1fb62fb0SOlivier Houchard uint32_t l = ck_pr_load_32(&rw->value);
159*1fb62fb0SOlivier Houchard
160*1fb62fb0SOlivier Houchard if (l & CK_SWLOCK_WRITER_BIT)
161*1fb62fb0SOlivier Houchard return false;
162*1fb62fb0SOlivier Houchard
163*1fb62fb0SOlivier Houchard l = ck_pr_faa_32(&rw->value, 1) & CK_SWLOCK_WRITER_MASK;
164*1fb62fb0SOlivier Houchard if (l == CK_SWLOCK_WRITER_BIT)
165*1fb62fb0SOlivier Houchard ck_pr_dec_32(&rw->value);
166*1fb62fb0SOlivier Houchard
167*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
168*1fb62fb0SOlivier Houchard return l == 0;
169*1fb62fb0SOlivier Houchard }
170*1fb62fb0SOlivier Houchard
171*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_read_lock(ck_swlock_t * rw)172*1fb62fb0SOlivier Houchard ck_swlock_read_lock(ck_swlock_t *rw)
173*1fb62fb0SOlivier Houchard {
174*1fb62fb0SOlivier Houchard uint32_t l;
175*1fb62fb0SOlivier Houchard
176*1fb62fb0SOlivier Houchard for (;;) {
177*1fb62fb0SOlivier Houchard while (ck_pr_load_32(&rw->value) & CK_SWLOCK_WRITER_BIT)
178*1fb62fb0SOlivier Houchard ck_pr_stall();
179*1fb62fb0SOlivier Houchard
180*1fb62fb0SOlivier Houchard l = ck_pr_faa_32(&rw->value, 1) & CK_SWLOCK_WRITER_MASK;
181*1fb62fb0SOlivier Houchard if (l == 0)
182*1fb62fb0SOlivier Houchard break;
183*1fb62fb0SOlivier Houchard
184*1fb62fb0SOlivier Houchard /*
185*1fb62fb0SOlivier Houchard * If the latch bit has not been set, then the writer would
186*1fb62fb0SOlivier Houchard * have observed the reader and will wait to completion of
187*1fb62fb0SOlivier Houchard * read-side critical section.
188*1fb62fb0SOlivier Houchard */
189*1fb62fb0SOlivier Houchard if (l == CK_SWLOCK_WRITER_BIT)
190*1fb62fb0SOlivier Houchard ck_pr_dec_32(&rw->value);
191*1fb62fb0SOlivier Houchard }
192*1fb62fb0SOlivier Houchard
193*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
194*1fb62fb0SOlivier Houchard return;
195*1fb62fb0SOlivier Houchard }
196*1fb62fb0SOlivier Houchard
197*1fb62fb0SOlivier Houchard CK_CC_INLINE static bool
ck_swlock_locked_reader(ck_swlock_t * rw)198*1fb62fb0SOlivier Houchard ck_swlock_locked_reader(ck_swlock_t *rw)
199*1fb62fb0SOlivier Houchard {
200*1fb62fb0SOlivier Houchard
201*1fb62fb0SOlivier Houchard ck_pr_fence_load();
202*1fb62fb0SOlivier Houchard return ck_pr_load_32(&rw->value) & CK_SWLOCK_READER_MASK;
203*1fb62fb0SOlivier Houchard }
204*1fb62fb0SOlivier Houchard
205*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_swlock_read_unlock(ck_swlock_t * rw)206*1fb62fb0SOlivier Houchard ck_swlock_read_unlock(ck_swlock_t *rw)
207*1fb62fb0SOlivier Houchard {
208*1fb62fb0SOlivier Houchard
209*1fb62fb0SOlivier Houchard ck_pr_fence_unlock();
210*1fb62fb0SOlivier Houchard ck_pr_dec_32(&rw->value);
211*1fb62fb0SOlivier Houchard return;
212*1fb62fb0SOlivier Houchard }
213*1fb62fb0SOlivier Houchard
214*1fb62fb0SOlivier Houchard CK_ELIDE_PROTOTYPE(ck_swlock_read, ck_swlock_t,
215*1fb62fb0SOlivier Houchard ck_swlock_locked_writer, ck_swlock_read_lock,
216*1fb62fb0SOlivier Houchard ck_swlock_locked_reader, ck_swlock_read_unlock)
217*1fb62fb0SOlivier Houchard
218*1fb62fb0SOlivier Houchard #endif /* CK_SWLOCK_H */
219