-
Notifications
You must be signed in to change notification settings - Fork 8
/
plasma_spin.c
354 lines (317 loc) · 13 KB
/
plasma_spin.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
* plasma_spin - portable macros for processor-specific spin loops
*
* inline assembly and compiler intrinsics for building spin loops / spinlocks
*
*
* Copyright (c) 2012, Glue Logic LLC. All rights reserved. code()gluelogic.com
*
* This file is part of plasma.
*
* plasma is free software: you can redistribute it and/or modify it under
* the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or
* (at your option) any later version.
*
* plasma is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with plasma. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _XOPEN_SOURCE
#ifdef __cplusplus
#define _XOPEN_SOURCE 500
#else
#define _XOPEN_SOURCE 600
#endif
#endif
#define PLASMA_FEATURE_DISABLE_WIN32_FULLHDRS
#define PLASMA_SPIN_C99INLINE_FUNCS
/* inlined functions defined in header
* (generate external linkage definition in GCC versions earlier than GCC 4.3)*/
#if defined(NO_C99INLINE) \
|| defined(__clang__) || (defined(__GNUC__) && !defined(__GNUC_STDC_INLINE__))
#define PLASMA_SPIN_C99INLINE
#endif
#include "plasma_spin.h"
#include "plasma_membar.h"
#include "plasma_sysconf.h"
/*
* simple spin lock
*/
/* FUTURE: might consider using plasma_spin_pause_yield_adaptive() in place of
* direct call to plasma_spin_pause() in some tight spin loops. Keeping track
* of callcount has some overhead, but on the other hand permits yielding CPU
* if spin continues for too long. TBD. */
/* FUTURE: might experiment with implementing plasma_atomic_lock_acquire()
* using CAS or xchg or plasma_atomic_fetch_add(), which might be slightly more
* performant depending on the platform architecture */
#ifndef plasma_spin_lock_acquire_spinloop
bool
plasma_spin_lock_acquire_spinloop (plasma_spin_lock_t * const spin)
{
uint32_t * const lck = &spin->lck;
do {
while (plasma_atomic_load_explicit(lck, memory_order_relaxed)) {
plasma_spin_pause();
}
} while (!plasma_atomic_lock_acquire(lck)); /*(includes barrier)*/
return true;
}
#endif
bool
plasma_spin_lock_acquire_spindecay (plasma_spin_lock_t * const spin,
int pause1, int pause32, int yield)
{
/* ((uint32_t *) cast also works for Apple OSSpinLock, which is int32_t) */
uint32_t * const lck = (uint32_t *)&spin->lck;
do {
while (plasma_atomic_load_explicit(lck, memory_order_relaxed)) {
if (pause1) {
--pause1;
plasma_spin_pause();
}
else if (pause32) {
int i = 4;
--pause32;
do {
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
plasma_spin_pause();
} while (--i);
}
else if (yield) {
--yield;
plasma_spin_yield();
}
else
return false;
}
} while (!plasma_atomic_lock_acquire(lck)); /*(includes barrier)*/
return true;
}
static uint32_t nshift; /* num bits rotate (shift) for taglock tag batch */
static uint32_t nprocs; /* num procs */
__attribute_cold__
__attribute_noinline__
static int
plasma_spin_nprocs_init (void)
{
long nprocs_onln = plasma_sysconf_nprocessors_onln();
uint32_t nshift_onln;
uint32_t nshift_accum = 0;
if (nprocs_onln < 1)
nprocs_onln = 1;
nshift_onln = (uint32_t)nprocs_onln;
while ((nshift_onln >>= 1))
++nshift_accum;
nshift = nshift_accum; /* power of 2 of available CPUs (0 if only 1 CPU) */
plasma_membar_StoreStore();
return (nprocs = (uint32_t)nprocs_onln);
/* store ordering above is not critical; so our readers below do not acquire
* If there is a race between threads before nprocs is set, then if a thread
* reads nshift = 0, but nprocs != 0, then thread might be slightly slower
* acquiring the spin lock the first time. Not a big deal. Likewise, it
* is not problem if multiple threads run this routine since this routine
* stores results atomically (via int-sized natural alignment) to the
* static variables and the results of initialization are the same. */
}
#if !defined(__GNUC__) || defined(__GNUC_STDC_INLINE__) || defined(__clang__)
C99INLINE
#endif
static void
plasma_spin_pause_yield_adaptive (const uint32_t distance, const int callcount)
{
/* simplistic backoff employing call count and distance to acquire fair lock
* (might be adjusted to pause multiple times for slightly larger input
* distances, especially on machines with larger numbers of CPUs/cores/SMT,
* but be sure to always yield quickly/immediately if distance >= nprocs)
*
* callers must initialize nshift and nprocs prior to calling this routine
* (initialization avoided here since code might be inlined in tight loop)
* (ok if a few threads run the initialization; results will be same)
* if (__builtin_expect( (!nprocs), 0))
* plasma_spin_nprocs_init();
*
* (nshift == 0 for one core; always yield CPU
* nshift == 1 for two cores; test distance <= nshift)
*/
if (callcount < 128 && distance <= nshift) {
plasma_spin_pause(); /* brief pause if almost our turn */
}
else {
plasma_spin_yield(); /* yield CPU if highly contended */
}
}
/*
* ticket lock
*
* A.3 Ticket Lock provides description and sample ticket lock impl for x86:
* http://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf
* (but in sample implementation in paper above, the unlock should have release
* fence prior to the increment, or should perform the increment in a temporary
* variable and then use an atomic store operation with memory_order_release)
*/
bool
plasma_spin_tktlock_acquire_spinloop (plasma_spin_tktlock_t *
const restrict spin, uint32_t tkt)
{
/* spin until ticket num ready to be served matches our position in queue */
uint32_t cmp = PLASMA_SPIN_TKTLOCK_MASK(tkt);
const uint32_t tktnum = PLASMA_SPIN_TKTLOCK_SHIFT(tkt);
int callcount = 0;
if (__builtin_expect( (!nprocs), 0))
plasma_spin_nprocs_init(); /*init plasma_spin_pause_yield_adaptive()*/
do {
cmp = tktnum > cmp /*(reuse cmp to store distance)*/
? tktnum - cmp
: (PLASMA_SPIN_TKTLOCK_TKTMAX + 1 - cmp) + tktnum;
plasma_spin_pause_yield_adaptive(cmp, ++callcount);
#ifndef __ia64__
cmp = PLASMA_SPIN_TKTLOCK_MASK(
plasma_atomic_load_explicit(&spin->lck.u,memory_order_relaxed));
#else /*(Itanium should emit ld4.acq in atomic load below)*/
cmp = PLASMA_SPIN_TKTLOCK_MASK(
plasma_atomic_load_explicit(&spin->lck.u,memory_order_acquire));
#endif
} while (tktnum != cmp);
#ifndef __ia64__ /*(Itanium should emit ld4.acq in atomic load above)*/
atomic_thread_fence(memory_order_acquire);
#endif
return true;
}
/* inlined functions defined in header
* (generate external linkage definition in C99-compliant compilers)
* (need to -duplicate- definition from header for non-C99-compliant compilers)
*/
#if !defined(__GNUC__) || defined(__GNUC_STDC_INLINE__)
extern inline
bool
plasma_spin_tktlock_is_free (const plasma_spin_tktlock_t * const restrict spin);
bool
plasma_spin_tktlock_is_free (const plasma_spin_tktlock_t * const restrict spin);
extern inline
bool
plasma_spin_tktlock_acquire (plasma_spin_tktlock_t * const restrict spin);
bool
plasma_spin_tktlock_acquire (plasma_spin_tktlock_t * const restrict spin);
extern inline
void
plasma_spin_tktlock_release (plasma_spin_tktlock_t * const restrict spin);
void
plasma_spin_tktlock_release (plasma_spin_tktlock_t * const restrict spin);
#endif
/*
* tag lock
*/
/* FUTURE: might experiment with having both tag and lck as 16-bit quantities
* in a single 32-bit word, and using CAS to modify both in a single atomic op.
* Using CAS does require a read prior to the atomic write, which has its own
* costs. Why bother? Currently plasma_spin_taglock_acquire() is implemented
* with two atomics, which in the uncontended case is slower than tktlock, which
* uses a single atomic operation. (Might also look into cmpxchg8 for 64-bit
* CAS of both tag and lck.) Storing tag and lck in 16-bit would limit number
* of potential simultaneous lock contenders to 32767 (SHRT_MAX) plus one with
* the lock. (Traditional ticket lock has limit of 65535 (USHRT_MAX) plus one
* with the lock.) */
#define PLASMA_SPIN_TAGLOCK_BATCH(x) ((x)>>nshift)
bool
plasma_spin_taglock_acquire (plasma_spin_taglock_t * const restrict taglock)
{
if (__builtin_expect( (!nprocs), 0))/*init for PLASMA_SPIN_TAGLOCK_BATCH()*/
plasma_spin_nprocs_init(); /*init plasma_spin_pause_yield_adaptive()*/
uint32_t * const restrict lck = (uint32_t *)&taglock->lck;
const uint32_t tag = PLASMA_SPIN_TAGLOCK_MASK(
plasma_atomic_fetch_add_u32(&taglock->tag, 1,
memory_order_relaxed));
uint32_t cmp = PLASMA_SPIN_TAGLOCK_MASK(*lck);
const uint32_t batch = PLASMA_SPIN_TAGLOCK_BATCH(tag);
int callcount = 0;
/* pause/yield until tag is part of the current batch */
while (PLASMA_SPIN_TAGLOCK_BATCH(cmp) != batch) {
cmp = tag > cmp /*(reuse cmp to store distance)*/
? tag - cmp
: (PLASMA_SPIN_TAGLOCK_TAGMAX + 1 - cmp) + tag;
plasma_spin_pause_yield_adaptive(cmp, ++callcount);
cmp = PLASMA_SPIN_TAGLOCK_MASK(
plasma_atomic_load_explicit(lck, memory_order_relaxed));
}
/* attempt to obtain lock, or else spin and retry
* (above PLASMA_SPIN_TAGLOCK_BATCH(cmp) == batch only when lock is free) */
#ifdef PLASMA_SPIN_TAGLOCK_VIA_FETCH_OR
while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(
plasma_atomic_fetch_or_u32(lck, PLASMA_SPIN_TAGLOCK_ORVAL,
memory_order_relaxed))) {
do { plasma_spin_pause();
} while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(
plasma_atomic_load_explicit(lck, memory_order_relaxed)));
}
#else /* (needs the cmp assignment below for reuse in the CAS) */
while (!plasma_atomic_CAS_32(lck,cmp,PLASMA_SPIN_TAGLOCK_LOCKVAL(cmp))) {
do {
plasma_spin_pause();
cmp = plasma_atomic_load_explicit(lck, memory_order_relaxed);
} while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(cmp));
}
#endif
plasma_membar_atomic_thread_fence_acq_rel();
return true;
}
bool
plasma_spin_taglock_acquire_urgent (plasma_spin_taglock_t *
const restrict taglock)
{
#ifdef PLASMA_SPIN_TAGLOCK_VIA_FETCH_OR
uint32_t * const restrict lck = (uint32_t *)&taglock->lck;
uint32_t cmp;
while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(
(cmp = plasma_atomic_fetch_or_u32(lck, PLASMA_SPIN_TAGLOCK_ORVAL,
memory_order_relaxed)))) {
do { plasma_spin_pause();
} while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(
plasma_atomic_load_explicit(lck, memory_order_relaxed)));
}
/* jumped queue by locking without incrementing tag
* (therefore decrement lck since taglock_release will increment lck) */
*lck = PLASMA_SPIN_TAGLOCK_LOCKVAL(cmp-1);
plasma_membar_atomic_thread_fence_acq_rel();
return true;
#else
uint32_t * const restrict lck = (uint32_t *)&taglock->lck;
uint32_t cmp = *lck;
do {
while (PLASMA_SPIN_TAGLOCK_IS_LOCKED(cmp)) {
plasma_spin_pause();
cmp = plasma_atomic_load_explicit(lck, memory_order_relaxed);
} /*(jump queue:(cmp-1))*/
} while (!plasma_atomic_CAS_32(lck,cmp,PLASMA_SPIN_TAGLOCK_LOCKVAL(cmp-1)));
plasma_membar_atomic_thread_fence_acq_rel();
return true;
#endif
}
/* inlined functions defined in header
* (generate external linkage definition in C99-compliant compilers)
* (need to -duplicate- definition from header for non-C99-compliant compilers)
*/
#if !defined(__GNUC__) || defined(__GNUC_STDC_INLINE__)
extern inline
bool
plasma_spin_taglock_acquire_try (plasma_spin_taglock_t *
const restrict taglock);
bool
plasma_spin_taglock_acquire_try (plasma_spin_taglock_t *
const restrict taglock);
extern inline
void
plasma_spin_taglock_release (plasma_spin_taglock_t * const restrict taglock);
void
plasma_spin_taglock_release (plasma_spin_taglock_t * const restrict taglock);
#endif