-
Notifications
You must be signed in to change notification settings - Fork 0
/
lock_if.h
526 lines (452 loc) · 14.5 KB
/
lock_if.h
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/*
* File: lock_if.h
* Author: Vasileios Trigonakis <[email protected]>
* Description:
* lock_if.h is part of ASCYLIB
*
* Copyright (c) 2014 Vasileios Trigonakis <[email protected]>,
* Tudor David <[email protected]>
* Distributed Programming Lab (LPD), EPFL
*
* ASCYLIB is free software: you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, version 2
* of the License.
*
* This program 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 General Public License for more details.
*
*/
#ifndef _LOCK_IF_H_
#define _LOCK_IF_H_
#include "utils.h"
#include "latency.h"
#define PREFETCHW_LOCK(lock) PREFETCHW(lock)
#if defined(MUTEX)
typedef pthread_mutex_t ptlock_t;
# define LOCK_LOCAL_DATA
# define PTLOCK_SIZE sizeof(ptlock_t)
# define INIT_LOCK(lock) pthread_mutex_init((pthread_mutex_t *) lock, NULL)
# define DESTROY_LOCK(lock) pthread_mutex_destroy((pthread_mutex_t *) lock)
# define LOCK(lock) pthread_mutex_lock((pthread_mutex_t *) lock)
# define TRYLOCK(lock) pthread_mutex_trylock((pthread_mutex_t *) lock)
# define UNLOCK(lock) pthread_mutex_unlock((pthread_mutex_t *) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) pthread_mutex_init((pthread_mutex_t *) lock, NULL)
# define GL_DESTROY_LOCK(lock) pthread_mutex_destroy((pthread_mutex_t *) lock)
# define GL_LOCK(lock) pthread_mutex_lock((pthread_mutex_t *) lock)
# define GL_TRYLOCK(lock) pthread_mutex_trylock((pthread_mutex_t *) lock)
# define GL_UNLOCK(lock) pthread_mutex_unlock((pthread_mutex_t *) lock)
#elif defined(SPIN) /* pthread spinlock */
typedef pthread_spinlock_t ptlock_t;
# define LOCK_LOCAL_DATA
# define PTLOCK_SIZE sizeof(ptlock_t)
# define INIT_LOCK(lock) pthread_spin_init((pthread_spinlock_t *) lock, PTHREAD_PROCESS_PRIVATE);
# define DESTROY_LOCK(lock) pthread_spin_destroy((pthread_spinlock_t *) lock)
# define LOCK(lock) pthread_spin_lock((pthread_spinlock_t *) lock)
# define UNLOCK(lock) pthread_spin_unlock((pthread_spinlock_t *) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) pthread_spin_init((pthread_spinlock_t *) lock, PTHREAD_PROCESS_PRIVATE);
# define GL_DESTROY_LOCK(lock) pthread_spin_destroy((pthread_spinlock_t *) lock)
# define GL_LOCK(lock) pthread_spin_lock((pthread_spinlock_t *) lock)
# define GL_UNLOCK(lock) pthread_spin_unlock((pthread_spinlock_t *) lock)
#elif defined(TAS) /* TAS */
# if RETRY_STATS == 1
extern RETRY_STATS_VARS;
# define PTLOCK_SIZE 32 /* choose 8, 16, 32, 64 */
typedef struct tticket
{
union
{
volatile uint32_t whole;
struct
{
volatile uint16_t tick;
volatile uint16_t curr;
};
};
} tticket_t;
# else
# define PTLOCK_SIZE 32 /* choose 8, 16, 32, 64 */
# endif
# define PASTER(x, y, z) x ## y ## z
# define EVALUATE(sz) PASTER(uint, sz, _t)
# define UTYPE EVALUATE(PTLOCK_SIZE)
# define PASTER2(x, y) x ## y
# define EVALUATE2(sz) PASTER2(CAS_U, sz)
# define CAS_UTYPE EVALUATE2(PTLOCK_SIZE)
typedef volatile UTYPE ptlock_t;
# define LOCK_LOCAL_DATA
# define INIT_LOCK(lock) tas_init(lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) tas_lock(lock)
# define TRYLOCK(lock) tas_trylock(lock)
# define UNLOCK(lock) tas_unlock(lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) tas_init(lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) tas_lock(lock)
# define GL_TRYLOCK(lock) tas_trylock(lock)
# define GL_UNLOCK(lock) tas_unlock(lock)
# define TAS_FREE 0
# define TAS_LCKD 1
static inline void
tas_init(ptlock_t* l)
{
*l = TAS_FREE;
# if defined(__tile__)
MEM_BARRIER;
# endif
}
static inline uint32_t
tas_lock(ptlock_t* l)
{
LOCK_TRY();
#if RETRY_STATS == 1
volatile tticket_t* t = (volatile tticket_t*) l;
volatile uint16_t tick = FAI_U16(&t->tick);
uint16_t cur = t->curr;
int16_t distance = tick - cur;
if (distance < 0 ) { printf("distatnce is %d / %u\n", distance, distance); }
LOCK_QUEUE(distance);
while (t->curr != tick)
{
PAUSE;
}
#else
while (CAS_UTYPE(l, TAS_FREE, TAS_LCKD) == TAS_LCKD)
{
PAUSE;
}
#endif
return 0;
}
static inline uint32_t
tas_trylock(ptlock_t* l)
{
#if RETRY_STATS == 1
LOCK_TRY_ONCE();
volatile uint32_t tc = *(volatile uint32_t*) l;
volatile tticket_t* tp = (volatile tticket_t*) &tc;
if (tp->curr == tp->tick)
{
COMPILER_NO_REORDER(uint64_t tc_old = tc;);
tp->tick++;
int res = CAS_U32((uint32_t*) l, tc_old, tc) == tc_old;
if (!res)
{
LOCK_QUEUE_ONCE(tp->tick - tp->curr);
}
return res;
}
else
{
LOCK_QUEUE_ONCE(tp->tick - tp->curr);
return 0;
}
#else
return (CAS_UTYPE(l, TAS_FREE, TAS_LCKD) == TAS_FREE);
#endif
}
static inline uint32_t
tas_unlock(ptlock_t* l)
{
# if defined(__tile__)
MEM_BARRIER;
# endif
#if RETRY_STATS == 1
volatile tticket_t* t = (volatile tticket_t*) l;
PREFETCHW(t);
COMPILER_NO_REORDER(t->curr++;);
#else
COMPILER_NO_REORDER(*l = TAS_FREE;);
#endif
return 0;
}
#elif defined(TTAS) /* TTAS */
# define PTLOCK_SIZE 32 /* choose 8, 16, 32, 64 */
# define PASTER(x, y, z) x ## y ## z
# define EVALUATE(sz) PASTER(uint, sz, _t)
# define UTYPE EVALUATE(PTLOCK_SIZE)
# define PASTER2(x, y) x ## y
# define EVALUATE2(sz) PASTER2(CAS_U, sz)
# define CAS_UTYPE EVALUATE2(PTLOCK_SIZE)
typedef volatile UTYPE ptlock_t;
# define LOCK_LOCAL_DATA
# define INIT_LOCK(lock) ttas_init(lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) ttas_lock(lock)
# define TRYLOCK(lock) ttas_trylock(lock)
# define UNLOCK(lock) ttas_unlock(lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) ttas_init(lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) ttas_lock(lock)
# define GL_TRYLOCK(lock) ttas_trylock(lock)
# define GL_UNLOCK(lock) ttas_unlock(lock)
# define TTAS_FREE 0
# define TTAS_LCKD 1
static inline void
ttas_init(ptlock_t* l)
{
*l = TTAS_FREE;
# if defined(__tile__)
MEM_BARRIER;
# endif
}
static inline uint32_t
ttas_lock(ptlock_t* l)
{
while (1)
{
while (*l == TTAS_LCKD)
{
PAUSE;
}
if (CAS_UTYPE(l, TTAS_FREE, TTAS_LCKD) == TTAS_FREE)
{
break;
}
}
return 0;
}
static inline uint32_t
ttas_trylock(ptlock_t* l)
{
return (CAS_UTYPE(l, TTAS_FREE, TTAS_LCKD) == TTAS_FREE);
}
static inline uint32_t
ttas_unlock(ptlock_t* l)
{
# if defined(__tile__)
MEM_BARRIER;
# endif
COMPILER_NO_REORDER(*l = TTAS_FREE;);
return 0;
}
#elif defined(TICKET) /* ticket lock */
struct ticket_st
{
uint32_t ticket;
uint32_t curr;
/* char padding[40]; */
};
typedef struct ticket_st ptlock_t;
# define LOCK_LOCAL_DATA
# define INIT_LOCK(lock) ticket_init((volatile ptlock_t*) lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) ticket_lock((volatile ptlock_t*) lock)
# define TRYLOCK(lock) ticket_trylock((volatile ptlock_t*) lock)
# define UNLOCK(lock) ticket_unlock((volatile ptlock_t*) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) ticket_init((volatile ptlock_t*) lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) ticket_lock((volatile ptlock_t*) lock)
# define GL_TRYLOCK(lock) ticket_trylock((volatile ptlock_t*) lock)
# define GL_UNLOCK(lock) ticket_unlock((volatile ptlock_t*) lock)
static inline void
ticket_init(volatile ptlock_t* l)
{
l->ticket = l->curr = 0;
# if defined(__tile__)
MEM_BARRIER;
# endif
}
# define TICKET_BASE_WAIT 512
# define TICKET_MAX_WAIT 4095
# define TICKET_WAIT_NEXT 128
static inline uint32_t
ticket_lock(volatile ptlock_t* l)
{
uint32_t ticket = FAI_U32(&l->ticket);
# if defined(OPTERON_OPTIMIZE)
uint32_t wait = TICKET_BASE_WAIT;
uint32_t distance_prev = 1;
while (1)
{
PREFETCHW(l);
uint32_t cur = l->curr;
if (cur == ticket)
{
break;
}
uint32_t distance = (ticket > cur) ? (ticket - cur) : (cur - ticket);
if (distance > 1)
{
if (distance != distance_prev)
{
distance_prev = distance;
wait = TICKET_BASE_WAIT;
}
nop_rep(distance * wait);
wait = (wait + TICKET_BASE_WAIT) & TICKET_MAX_WAIT;
}
else
{
nop_rep(TICKET_WAIT_NEXT);
}
if (distance > 20)
{
sched_yield();
}
}
# else /* !OPTERON_OPTIMIZE */
PREFETCHW(l);
while (ticket != l->curr)
{
PREFETCHW(l);
PAUSE;
}
# endif
return 0;
}
static inline uint32_t
ticket_trylock(volatile ptlock_t* l)
{
volatile uint64_t tc = *(volatile uint64_t*) l;
volatile struct ticket_st* tp = (volatile struct ticket_st*) &tc;
if (tp->curr == tp->ticket)
{
COMPILER_NO_REORDER(uint64_t tc_old = tc;);
tp->ticket++;
return CAS_U64((uint64_t*) l, tc_old, tc) == tc_old;
}
else
{
return 0;
}
}
static inline uint32_t
ticket_unlock(volatile ptlock_t* l)
{
# if defined(__tile__)
MEM_BARRIER;
# endif
PREFETCHW(l);
COMPILER_NO_REORDER(l->curr++;);
return 0;
}
#elif defined(HTICKET) /* Hierarchical ticket lock */
# include "htlock.h"
# define INIT_LOCK(lock) init_alloc_htlock((htlock_t*) lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) htlock_lock((htlock_t*) lock)
# define UNLOCK(lock) htlock_release((htlock_t*) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) init_alloc_htlock((htlock_t*) lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) htlock_lock((htlock_t*) lock)
# define GL_UNLOCK(lock) htlock_release((htlock_t*) lock)
#elif defined(CLH) /* CLH lock */
# include "clh.h"
# define INIT_LOCK(lock) init_alloc_clh((clh_lock_t*) lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) clh_local_p.my_pred = \
clh_acquire((volatile struct clh_qnode **) lock, clh_local_p.my_qnode);
# define UNLOCK(lock) clh_local_p.my_qnode = \
clh_release(clh_local_p.my_qnode, clh_local_p.my_pred);
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) init_alloc_clh((clh_lock_t*) lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) clh_local_p.my_pred = \
clh_acquire((volatile struct clh_qnode **) lock, clh_local_p.my_qnode);
# define GL_UNLOCK(lock) clh_local_p.my_qnode = \
clh_release(clh_local_p.my_qnode, clh_local_p.my_pred);
#elif defined(MCS) /* MCS lock */
# include "mcs.h"
typedef mcs_lock_t ptlock_t;
#define LOCK_LOCAL_DATA __thread mcs_lock_local_t __mcs_local
# define INIT_LOCK(lock) mcs_lock_init((mcs_lock_t*) lock, NULL)
# define DESTROY_LOCK(lock) mcs_lock_destroy((mcs_lock_t*) lock)
# define LOCK(lock) mcs_lock_lock((mcs_lock_t*) lock)
# define UNLOCK(lock) mcs_lock_unlock((mcs_lock_t*) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) mcs_lock_init((mcs_lock_t*) lock, NULL)
# define GL_DESTROY_LOCK(lock) mcs_lock_destroy((mcs_lock_t*) lock)
# define GL_LOCK(lock) mcs_lock_lock((mcs_lock_t*) lock)
# define GL_UNLOCK(lock) mcs_lock_unlock((mcs_lock_t*) lock)
#elif defined(NONE) /* no locking */
struct none_st
{
uint32_t nothing;
};
typedef struct none_st ptlock_t;
# define LOCK_LOCAL_DATA
# define INIT_LOCK(lock) none_init((volatile ptlock_t*) lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock) none_lock((volatile ptlock_t*) lock)
# define TRYLOCK(lock) none_trylock((volatile ptlock_t*) lock)
# define UNLOCK(lock) none_unlock((volatile ptlock_t*) lock)
/* GLOBAL lock */
# define GL_INIT_LOCK(lock) none_init((volatile ptlock_t*) lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock) none_lock((volatile ptlock_t*) lock)
# define GL_TRYLOCK(lock) none_trylock((volatile ptlock_t*) lock)
# define GL_UNLOCK(lock) none_unlock((volatile ptlock_t*) lock)
static inline void
none_init(volatile ptlock_t* l)
{
}
static inline uint32_t
none_lock(volatile ptlock_t* l)
{
return 0;
}
static inline uint32_t
none_trylock(volatile ptlock_t* l)
{
return 1;
}
static inline uint32_t
none_unlock(volatile ptlock_t* l)
{
return 0;
}
#endif
/* --------------------------------------------------------------------------------------------------- */
/* GLOBAL LOCK --------------------------------------------------------------------------------------- */
/* --------------------------------------------------------------------------------------------------- */
#if defined(LL_GLOBAL_LOCK)
# define ND_GET_LOCK(nd) nd /* LOCK / UNLOCK are not defined in any case ;-) */
# undef INIT_LOCK
# undef DESTROY_LOCK
# undef LOCK
# undef UNLOCK
# undef PREFETCHW_LOCK
# define INIT_LOCK(lock)
# define DESTROY_LOCK(lock)
# define LOCK(lock)
# define UNLOCK(lock)
# define PREFETCHW_LOCK(lock)
# define INIT_LOCK_A(lock) GL_INIT_LOCK(lock)
# define DESTROY_LOCK_A(lock) GL_DESTROY_LOCK(lock)
# define LOCK_A(lock) GL_LOCK(lock)
# define TRYLOCK_A(lock) GL_TRYLOCK(lock)
# define UNLOCK_A(lock) GL_UNLOCK(lock)
# define PREFETCHW_LOCK_A(lock)
/* optik */
# define OPTIK_WITHOUT_GL_DO(a)
# define OPTIK_WITH_GL_DO(a) a
#else /* !LL_GLOBAL_LOCK */
# define ND_GET_LOCK(nd) &nd->lock
# undef GL_INIT_LOCK
# undef GL_DESTROY_LOCK
# undef GL_LOCK
# undef GL_UNLOCK
# define GL_INIT_LOCK(lock)
# define GL_DESTROY_LOCK(lock)
# define GL_LOCK(lock)
# define GL_UNLOCK(lock)
# define INIT_LOCK_A(lock) INIT_LOCK(lock)
# define DESTROY_LOCK_A(lock) DESTROY_LOCK(lock)
# define LOCK_A(lock) LOCK(lock)
# define TRYLOCK_A(lock) TRYLOCK(lock)
# define UNLOCK_A(lock) UNLOCK(lock)
# define PREFETCHW_LOCK_A(lock) PREFETCHW_LOCK(lock)
/* optik */
# define OPTIK_WITHOUT_GL_DO(a) a
# define OPTIK_WITH_GL_DO(a)
#endif
#endif /* _LOCK_IF_H_ */