Debian Codegraph @

doc/ck_spinlock

raw
1 .\"
2 .\" Copyright 2013 Samy Al Bahra.
3 .\" All rights reserved.
4 .\"
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
7 .\" are met:
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\"
14 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
15 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
18 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24 .\" SUCH DAMAGE.
25 .\"
26 .\"
27 .Dd July 26, 2013.
28 .Dt ck_spinlock 3
29 .Sh NAME
30 .Nm ck_spinlock_init ,
31 .Nm ck_spinlock_lock ,
32 .Nm ck_spinlock_unlock ,
33 .Nm ck_spinlock_locked ,
34 .Nm ck_spinlock_trylock ,
35 .Nm ck_spinlock_anderson_init ,
36 .Nm ck_spinlock_anderson_locked ,
37 .Nm ck_spinlock_anderson_lock ,
38 .Nm ck_spinlock_anderson_unlock ,
39 .Nm ck_spinlock_cas_init ,
40 .Nm ck_spinlock_cas_locked ,
41 .Nm ck_spinlock_cas_lock ,
42 .Nm ck_spinlock_cas_lock_eb ,
43 .Nm ck_spinlock_cas_trylock ,
44 .Nm ck_spinlock_cas_unlock ,
45 .Nm ck_spinlock_clh_init ,
46 .Nm ck_spinlock_clh_locked ,
47 .Nm ck_spinlock_clh_lock ,
48 .Nm ck_spinlock_clh_unlock ,
49 .Nm ck_spinlock_dec_init ,
50 .Nm ck_spinlock_dec_locked ,
51 .Nm ck_spinlock_dec_lock ,
52 .Nm ck_spinlock_dec_lock_eb ,
53 .Nm ck_spinlock_dec_trylock ,
54 .Nm ck_spinlock_dec_unlock ,
55 .Nm ck_spinlock_fas_init ,
56 .Nm ck_spinlock_fas_lock ,
57 .Nm ck_spinlock_fas_lock_eb ,
58 .Nm ck_spinlock_fas_locked ,
59 .Nm ck_spinlock_fas_trylock ,
60 .Nm ck_spinlock_fas_unlock ,
61 .Nm ck_spinlock_hclh_init ,
62 .Nm ck_spinlock_hclh_locked ,
63 .Nm ck_spinlock_hclh_lock ,
64 .Nm ck_spinlock_hclh_unlock ,
65 .Nm ck_spinlock_mcs_init ,
66 .Nm ck_spinlock_mcs_locked ,
67 .Nm ck_spinlock_mcs_lock ,
68 .Nm ck_spinlock_mcs_trylock ,
69 .Nm ck_spinlock_mcs_unlock ,
70 .Nm ck_spinlock_ticket_init ,
71 .Nm ck_spinlock_ticket_locked ,
72 .Nm ck_spinlock_ticket_lock ,
73 .Nm ck_spinlock_ticket_lock_pb ,
74 .Nm ck_spinlock_ticket_trylock ,
75 .Nm ck_spinlock_ticket_unlock
76 .Nd spinlock implementations
77 .Sh LIBRARY
78 Concurrency Kit (libck, \-lck)
79 .Sh SYNOPSIS
80 .In ck_spinlock.h
81 .Pp
82 .Dv ck_spinlock_t spinlock = CK_SPINLOCK_INITIALIZER;
83 .Ft void
84 .Fn ck_spinlock_init "ck_spinlock_t *lock"
85 .Ft void
86 .Fn ck_spinlock_lock "ck_spinlock_t *lock"
87 .Ft void
88 .Fn ck_spinlock_unlock "ck_spinlock_t *lock"
89 .Ft bool
90 .Fn ck_spinlock_locked "ck_spinlock_t *lock"
91 .Ft bool
92 .Fn ck_spinlock_trylock "ck_spinlock_t *lock"
93 .Ft void
94 .Fn ck_spinlock_anderson_init "ck_spinlock_anderson_t *lock" "ck_spinlock_anderson_thread_t *slots" "unsigned int count"
95 .Ft bool
96 .Fn ck_spinlock_anderson_locked "ck_spinlock_anderson_t *lock"
97 .Ft void
98 .Fn ck_spinlock_anderson_lock "ck_spinlock_anderson_t *lock" "ck_spinlock_anderson_thread_t **slot"
99 .Ft void
100 .Fn ck_spinlock_anderson_unlock "ck_spinlock_anderson_t *lock" "ck_spinlock_anderson_thread_t *slot"
101 .Pp
102 .Dv ck_spinlock_cas_t spinlock = CK_SPINLOCK_CAS_INITIALIZER;
103 .Ft void
104 .Fn ck_spinlock_cas_init "ck_spinlock_cas_t *lock"
105 .Ft bool
106 .Fn ck_spinlock_cas_locked "ck_spinlock_cas_t *lock"
107 .Ft void
108 .Fn ck_spinlock_cas_lock "ck_spinlock_cas_t *lock"
109 .Ft void
110 .Fn ck_spinlock_cas_lock_eb "ck_spinlock_cas_t *lock"
111 .Ft bool
112 .Fn ck_spinlock_cas_trylock "ck_spinlock_cas_t *lock"
113 .Ft void
114 .Fn ck_spinlock_cas_unlock "ck_spinlock_cas_t *lock"
115 .Ft void
116 .Fn ck_spinlock_clh_init "ck_spinlock_clh_t **lock" "ck_spinlock_clh_t *unowned"
117 .Ft bool
118 .Fn ck_spinlock_clh_locked "ck_spinlock_clh_t **lock"
119 .Ft void
120 .Fn ck_spinlock_clh_lock "ck_spinlock_clh_t **lock" "ck_spinlock_clh_t *node"
121 .Ft void
122 .Fn ck_spinlock_clh_unlock "ck_spinlock_clh_t **node"
123 .Pp
124 .Dv ck_spinlock_dec_t spinlock = CK_SPINLOCK_DEC_INITIALIZER;
125 .Ft void
126 .Fn ck_spinlock_dec_init "ck_spinlock_dec_t *lock"
127 .Ft bool
128 .Fn ck_spinlock_dec_locked "ck_spinlock_dec_t *lock"
129 .Ft void
130 .Fn ck_spinlock_dec_lock "ck_spinlock_dec_t *lock"
131 .Ft void
132 .Fn ck_spinlock_dec_lock_eb "ck_spinlock_dec_t *lock"
133 .Ft bool
134 .Fn ck_spinlock_dec_trylock "ck_spinlock_dec_t *lock"
135 .Ft void
136 .Fn ck_spinlock_dec_unlock "ck_spinlock_dec_t *lock"
137 .Pp
138 .Dv ck_spinlock_fas_t spinlock = CK_SPINLOCK_FAS_INITIALIZER;
139 .Ft void
140 .Fn ck_spinlock_fas_init "ck_spinlock_fas_t *lock"
141 .Ft void
142 .Fn ck_spinlock_fas_lock "ck_spinlock_fas_t *lock"
143 .Ft void
144 .Fn ck_spinlock_fas_lock_eb "ck_spinlock_fas_t *lock"
145 .Ft bool
146 .Fn ck_spinlock_fas_locked "ck_spinlock_fas_t *lock"
147 .Ft bool
148 .Fn ck_spinlock_fas_trylock "ck_spinlock_fas_t *lock"
149 .Ft void
150 .Fn ck_spinlock_fas_unlock "ck_spinlock_fas_t *lock"
151 .Pp
152 .Ft void
153 .Fn ck_spinlock_hclh_init "ck_spinlock_hclh_t **lock" "ck_spinlock_hclh_t *unowned"
154 .Ft bool
155 .Fn ck_spinlock_hclh_locked "ck_spinlock_hclh_t **lock"
156 .Ft void
157 .Fn ck_spinlock_hclh_lock "ck_spinlock_hclh_t **lock" "ck_spinlock_hclh_t *node"
158 .Ft void
159 .Fn ck_spinlock_hclh_unlock "ck_spinlock_hclh_t **node"
160 .Pp
161 .Dv ck_spinlock_mcs_t spinlock = CK_SPINLOCK_MCS_INITIALIZER;
162 .Ft void
163 .Fn ck_spinlock_mcs_init "ck_spinlock_mcs_t **lock"
164 .Ft bool
165 .Fn ck_spinlock_mcs_locked "ck_spinlock_mcs_t **lock"
166 .Ft void
167 .Fn ck_spinlock_mcs_lock "ck_spinlock_mcs_t **lock" "ck_spinlock_mcs_t *node"
168 .Ft bool
169 .Fn ck_spinlock_mcs_trylock "ck_spinlock_mcs_t **lock" "ck_spinlock_mcs_t *node"
170 .Ft void
171 .Fn ck_spinlock_mcs_unlock "ck_spinlock_mcs_t **lock" "ck_spinlock_mcs_t *node"
172 .Pp
173 .Dv ck_spinlock_ticket_t spinlock = CK_SPINLOCK_TICKET_INITIALIZER;
174 .Ft void
175 .Fn ck_spinlock_ticket_init "ck_spinlock_ticket_t *lock"
176 .Ft bool
177 .Fn ck_spinlock_ticket_locked "ck_spinlock_ticket_t *lock"
178 .Ft void
179 .Fn ck_spinlock_ticket_lock "ck_spinlock_ticket_t *lock"
180 .Ft void
181 .Fn ck_spinlock_ticket_lock_pb "ck_spinlock_ticket_t *lock" "unsigned int period"
182 .Ft bool
183 .Fn ck_spinlock_ticket_trylock "ck_spinlock_ticket_t *lock"
184 .Ft void
185 .Fn ck_spinlock_ticket_unlock "ck_spinlock_ticket_t *lock"
186 .Sh DESCRIPTION
187 A family of busy-wait spinlock implementations. The ck_spinlock_t implementation is simply
188 a wrapper around the fetch-and-swap (ck_spinlock_fas_t) implementation. The table below
189 provides a summary of the current implementations.
190 .Bd -literal
191 | Namespace | Algorithm | Type | Restrictions | Fair |
192 \'----------------------|-----------------------------|---------------|-------------------------|--------'
193 ck_spinlock_anderson Anderson Array Fixed number of threads Yes
194 ck_spinlock_cas Compare-and-Swap Centralized None No
195 ck_spinlock_clh Craig, Landin and Hagersten Queue Lifetime requirements Yes
196 ck_spinlock_dec Decrement (Linux kernel) Centralized UINT_MAX concurrency No
197 ck_spinlock_fas Fetch-and-store Centralized None No
198 ck_spinlock_hclh Hierarchical CLH Queue Lifetime requirements Yes *
199 ck_spinlock_mcs Mellor-Crummey and Scott Queue None Yes
200 ck_spinlock_ticket Ticket Centralized None Yes
201 .Ed
202 .Pp
203 * Hierarchical CLH only offers weak fairness for threads accross cluster
204 nodes.
205 .Pp
206 If contention is low and there is no hard requirement for starvation-freedom
207 then a centralized greedy (unfair) spinlock is recommended. If contention is
208 high and there is no requirement for starvation-freedom then a centralized
209 greedy spinlock is recommended to be used with an exponential backoff
210 mechanism. If contention is generally low and there is a hard requirement for
211 starvation-freedom then the ticket lock is recommended. If contention is high
212 and there is a hard requirement for starvation-freedom then the Craig and
213 Landin and Hagersten queue spinlock is recommended unless stack allocation is
214 necessary or NUMA factor is high, in which case the Mellor-Crummey and Scott
215 spinlock is recommended. If you cannot afford O(n) space-usage from array
216 or queue spinlocks but still require fairness under high contention then
217 the ticket lock with proportional back-off is recommended.
218 If NUMA factor is high but prefer a greedy lock, then please see
219 .Xr ck_cohort 3 .
220 .Sh EXAMPLE
221 .Bd -literal -offset indent
222 #include <ck_spinlock.h>
223 #include <stdbool.h>
224
225 /*
226 * Alternatively, the mutex may be initialized at run-time with
227 * ck_spinlock_init(&mutex).
228 */
229 ck_spinlock_t mutex = CK_SPINLOCK_INITIALIZER;
230
231 void
232 example(void)
233 {
234
235 ck_spinlock_lock(&mutex);
236 /*
237 * Critical section.
238 */
239 ck_spinlock_unlock(&mutex);
240
241 ck_spinlock_lock_eb(&mutex);
242 /*
243 * Critical section.
244 */
245 ck_spinlock_unlock(&mutex);
246
247 if (ck_spinlock_trylock(&mutex) == true) {
248 /*
249 * Critical section.
250 */
251 ck_spinlock_unlock(&mutex);
252 }
253 }
254 .Ed
255 .Sh SEE ALSO
256 .Xr ck_cohort 3 ,
257 .Xr ck_elide 3
258 .Pp
259 Additional information available at http://concurrencykit.org/