1*1fb62fb0SOlivier Houchard /*
2*1fb62fb0SOlivier Houchard * Copyright 2010-2015 Samy Al Bahra.
3*1fb62fb0SOlivier Houchard * All rights reserved.
4*1fb62fb0SOlivier Houchard *
5*1fb62fb0SOlivier Houchard * Redistribution and use in source and binary forms, with or without
6*1fb62fb0SOlivier Houchard * modification, are permitted provided that the following conditions
7*1fb62fb0SOlivier Houchard * are met:
8*1fb62fb0SOlivier Houchard * 1. Redistributions of source code must retain the above copyright
9*1fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer.
10*1fb62fb0SOlivier Houchard * 2. Redistributions in binary form must reproduce the above copyright
11*1fb62fb0SOlivier Houchard * notice, this list of conditions and the following disclaimer in the
12*1fb62fb0SOlivier Houchard * documentation and/or other materials provided with the distribution.
13*1fb62fb0SOlivier Houchard *
14*1fb62fb0SOlivier Houchard * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15*1fb62fb0SOlivier Houchard * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16*1fb62fb0SOlivier Houchard * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17*1fb62fb0SOlivier Houchard * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18*1fb62fb0SOlivier Houchard * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19*1fb62fb0SOlivier Houchard * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20*1fb62fb0SOlivier Houchard * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21*1fb62fb0SOlivier Houchard * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22*1fb62fb0SOlivier Houchard * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23*1fb62fb0SOlivier Houchard * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24*1fb62fb0SOlivier Houchard * SUCH DAMAGE.
25*1fb62fb0SOlivier Houchard */
26*1fb62fb0SOlivier Houchard
27*1fb62fb0SOlivier Houchard #ifndef CK_BYTELOCK_H
28*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_H
29*1fb62fb0SOlivier Houchard
30*1fb62fb0SOlivier Houchard /*
31*1fb62fb0SOlivier Houchard * The implementations here are derived from the work described in:
32*1fb62fb0SOlivier Houchard * Dice, D. and Shavit, N. 2010. TLRW: return of the read-write lock.
33*1fb62fb0SOlivier Houchard * In Proceedings of the 22nd ACM Symposium on Parallelism in Algorithms
34*1fb62fb0SOlivier Houchard * and Architectures (Thira, Santorini, Greece, June 13 - 15, 2010).
35*1fb62fb0SOlivier Houchard * SPAA '10. ACM, New York, NY, 284-293.
36*1fb62fb0SOlivier Houchard */
37*1fb62fb0SOlivier Houchard
38*1fb62fb0SOlivier Houchard #include <ck_cc.h>
39*1fb62fb0SOlivier Houchard #include <ck_md.h>
40*1fb62fb0SOlivier Houchard #include <ck_pr.h>
41*1fb62fb0SOlivier Houchard #include <ck_stdbool.h>
42*1fb62fb0SOlivier Houchard #include <ck_stddef.h>
43*1fb62fb0SOlivier Houchard #include <ck_limits.h>
44*1fb62fb0SOlivier Houchard
45*1fb62fb0SOlivier Houchard struct ck_bytelock {
46*1fb62fb0SOlivier Houchard unsigned int owner;
47*1fb62fb0SOlivier Houchard unsigned int n_readers;
48*1fb62fb0SOlivier Houchard uint8_t readers[CK_MD_CACHELINE - sizeof(unsigned int) * 2] CK_CC_ALIGN(8);
49*1fb62fb0SOlivier Houchard };
50*1fb62fb0SOlivier Houchard typedef struct ck_bytelock ck_bytelock_t;
51*1fb62fb0SOlivier Houchard
52*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_INITIALIZER { 0, 0, {0} }
53*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_UNSLOTTED UINT_MAX
54*1fb62fb0SOlivier Houchard
55*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_bytelock_init(struct ck_bytelock * bytelock)56*1fb62fb0SOlivier Houchard ck_bytelock_init(struct ck_bytelock *bytelock)
57*1fb62fb0SOlivier Houchard {
58*1fb62fb0SOlivier Houchard unsigned int i;
59*1fb62fb0SOlivier Houchard
60*1fb62fb0SOlivier Houchard bytelock->owner = 0;
61*1fb62fb0SOlivier Houchard bytelock->n_readers = 0;
62*1fb62fb0SOlivier Houchard for (i = 0; i < sizeof bytelock->readers; i++)
63*1fb62fb0SOlivier Houchard bytelock->readers[i] = false;
64*1fb62fb0SOlivier Houchard
65*1fb62fb0SOlivier Houchard ck_pr_barrier();
66*1fb62fb0SOlivier Houchard return;
67*1fb62fb0SOlivier Houchard }
68*1fb62fb0SOlivier Houchard
69*1fb62fb0SOlivier Houchard #ifdef CK_F_PR_LOAD_64
70*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_LENGTH sizeof(uint64_t)
71*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_LOAD ck_pr_load_64
72*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_TYPE uint64_t
73*1fb62fb0SOlivier Houchard #elif defined(CK_F_PR_LOAD_32)
74*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_LENGTH sizeof(uint32_t)
75*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_LOAD ck_pr_load_32
76*1fb62fb0SOlivier Houchard #define CK_BYTELOCK_TYPE uint32_t
77*1fb62fb0SOlivier Houchard #else
78*1fb62fb0SOlivier Houchard #error Unsupported platform.
79*1fb62fb0SOlivier Houchard #endif
80*1fb62fb0SOlivier Houchard
81*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_bytelock_write_lock(struct ck_bytelock * bytelock,unsigned int slot)82*1fb62fb0SOlivier Houchard ck_bytelock_write_lock(struct ck_bytelock *bytelock, unsigned int slot)
83*1fb62fb0SOlivier Houchard {
84*1fb62fb0SOlivier Houchard CK_BYTELOCK_TYPE *readers = (void *)bytelock->readers;
85*1fb62fb0SOlivier Houchard unsigned int i;
86*1fb62fb0SOlivier Houchard
87*1fb62fb0SOlivier Houchard /* Announce upcoming writer acquisition. */
88*1fb62fb0SOlivier Houchard while (ck_pr_cas_uint(&bytelock->owner, 0, slot) == false)
89*1fb62fb0SOlivier Houchard ck_pr_stall();
90*1fb62fb0SOlivier Houchard
91*1fb62fb0SOlivier Houchard /* If we are slotted, we might be upgrading from a read lock. */
92*1fb62fb0SOlivier Houchard if (slot <= sizeof bytelock->readers)
93*1fb62fb0SOlivier Houchard ck_pr_store_8(&bytelock->readers[slot - 1], false);
94*1fb62fb0SOlivier Houchard
95*1fb62fb0SOlivier Houchard /*
96*1fb62fb0SOlivier Houchard * Wait for slotted readers to drain out. This also provides the
97*1fb62fb0SOlivier Houchard * lock acquire semantics.
98*1fb62fb0SOlivier Houchard */
99*1fb62fb0SOlivier Houchard ck_pr_fence_atomic_load();
100*1fb62fb0SOlivier Houchard
101*1fb62fb0SOlivier Houchard for (i = 0; i < sizeof(bytelock->readers) / CK_BYTELOCK_LENGTH; i++) {
102*1fb62fb0SOlivier Houchard while (CK_BYTELOCK_LOAD(&readers[i]) != false)
103*1fb62fb0SOlivier Houchard ck_pr_stall();
104*1fb62fb0SOlivier Houchard }
105*1fb62fb0SOlivier Houchard
106*1fb62fb0SOlivier Houchard /* Wait for unslotted readers to drain out. */
107*1fb62fb0SOlivier Houchard while (ck_pr_load_uint(&bytelock->n_readers) != 0)
108*1fb62fb0SOlivier Houchard ck_pr_stall();
109*1fb62fb0SOlivier Houchard
110*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
111*1fb62fb0SOlivier Houchard return;
112*1fb62fb0SOlivier Houchard }
113*1fb62fb0SOlivier Houchard
114*1fb62fb0SOlivier Houchard #undef CK_BYTELOCK_LENGTH
115*1fb62fb0SOlivier Houchard #undef CK_BYTELOCK_LOAD
116*1fb62fb0SOlivier Houchard #undef CK_BYTELOCK_TYPE
117*1fb62fb0SOlivier Houchard
118*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_bytelock_write_unlock(struct ck_bytelock * bytelock)119*1fb62fb0SOlivier Houchard ck_bytelock_write_unlock(struct ck_bytelock *bytelock)
120*1fb62fb0SOlivier Houchard {
121*1fb62fb0SOlivier Houchard
122*1fb62fb0SOlivier Houchard ck_pr_fence_unlock();
123*1fb62fb0SOlivier Houchard ck_pr_store_uint(&bytelock->owner, 0);
124*1fb62fb0SOlivier Houchard return;
125*1fb62fb0SOlivier Houchard }
126*1fb62fb0SOlivier Houchard
127*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_bytelock_read_lock(struct ck_bytelock * bytelock,unsigned int slot)128*1fb62fb0SOlivier Houchard ck_bytelock_read_lock(struct ck_bytelock *bytelock, unsigned int slot)
129*1fb62fb0SOlivier Houchard {
130*1fb62fb0SOlivier Houchard
131*1fb62fb0SOlivier Houchard if (ck_pr_load_uint(&bytelock->owner) == slot) {
132*1fb62fb0SOlivier Houchard ck_pr_store_8(&bytelock->readers[slot - 1], true);
133*1fb62fb0SOlivier Houchard ck_pr_fence_strict_store();
134*1fb62fb0SOlivier Houchard ck_pr_store_uint(&bytelock->owner, 0);
135*1fb62fb0SOlivier Houchard return;
136*1fb62fb0SOlivier Houchard }
137*1fb62fb0SOlivier Houchard
138*1fb62fb0SOlivier Houchard /* Unslotted threads will have to use the readers counter. */
139*1fb62fb0SOlivier Houchard if (slot > sizeof bytelock->readers) {
140*1fb62fb0SOlivier Houchard for (;;) {
141*1fb62fb0SOlivier Houchard ck_pr_inc_uint(&bytelock->n_readers);
142*1fb62fb0SOlivier Houchard ck_pr_fence_atomic_load();
143*1fb62fb0SOlivier Houchard if (ck_pr_load_uint(&bytelock->owner) == 0)
144*1fb62fb0SOlivier Houchard break;
145*1fb62fb0SOlivier Houchard ck_pr_dec_uint(&bytelock->n_readers);
146*1fb62fb0SOlivier Houchard
147*1fb62fb0SOlivier Houchard while (ck_pr_load_uint(&bytelock->owner) != 0)
148*1fb62fb0SOlivier Houchard ck_pr_stall();
149*1fb62fb0SOlivier Houchard }
150*1fb62fb0SOlivier Houchard
151*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
152*1fb62fb0SOlivier Houchard return;
153*1fb62fb0SOlivier Houchard }
154*1fb62fb0SOlivier Houchard
155*1fb62fb0SOlivier Houchard slot -= 1;
156*1fb62fb0SOlivier Houchard for (;;) {
157*1fb62fb0SOlivier Houchard #ifdef CK_F_PR_FAA_8
158*1fb62fb0SOlivier Houchard ck_pr_fas_8(&bytelock->readers[slot], true);
159*1fb62fb0SOlivier Houchard ck_pr_fence_atomic_load();
160*1fb62fb0SOlivier Houchard #else
161*1fb62fb0SOlivier Houchard ck_pr_store_8(&bytelock->readers[slot], true);
162*1fb62fb0SOlivier Houchard ck_pr_fence_store_load();
163*1fb62fb0SOlivier Houchard #endif
164*1fb62fb0SOlivier Houchard
165*1fb62fb0SOlivier Houchard /*
166*1fb62fb0SOlivier Houchard * If there is no owner at this point, our slot has
167*1fb62fb0SOlivier Houchard * already been published and it is guaranteed no
168*1fb62fb0SOlivier Houchard * write acquisition will succeed until we drain out.
169*1fb62fb0SOlivier Houchard */
170*1fb62fb0SOlivier Houchard if (ck_pr_load_uint(&bytelock->owner) == 0)
171*1fb62fb0SOlivier Houchard break;
172*1fb62fb0SOlivier Houchard
173*1fb62fb0SOlivier Houchard ck_pr_store_8(&bytelock->readers[slot], false);
174*1fb62fb0SOlivier Houchard while (ck_pr_load_uint(&bytelock->owner) != 0)
175*1fb62fb0SOlivier Houchard ck_pr_stall();
176*1fb62fb0SOlivier Houchard }
177*1fb62fb0SOlivier Houchard
178*1fb62fb0SOlivier Houchard ck_pr_fence_lock();
179*1fb62fb0SOlivier Houchard return;
180*1fb62fb0SOlivier Houchard }
181*1fb62fb0SOlivier Houchard
182*1fb62fb0SOlivier Houchard CK_CC_INLINE static void
ck_bytelock_read_unlock(struct ck_bytelock * bytelock,unsigned int slot)183*1fb62fb0SOlivier Houchard ck_bytelock_read_unlock(struct ck_bytelock *bytelock, unsigned int slot)
184*1fb62fb0SOlivier Houchard {
185*1fb62fb0SOlivier Houchard
186*1fb62fb0SOlivier Houchard ck_pr_fence_unlock();
187*1fb62fb0SOlivier Houchard
188*1fb62fb0SOlivier Houchard if (slot > sizeof bytelock->readers)
189*1fb62fb0SOlivier Houchard ck_pr_dec_uint(&bytelock->n_readers);
190*1fb62fb0SOlivier Houchard else
191*1fb62fb0SOlivier Houchard ck_pr_store_8(&bytelock->readers[slot - 1], false);
192*1fb62fb0SOlivier Houchard
193*1fb62fb0SOlivier Houchard return;
194*1fb62fb0SOlivier Houchard }
195*1fb62fb0SOlivier Houchard
196*1fb62fb0SOlivier Houchard #endif /* CK_BYTELOCK_H */
197