summaryrefslogtreecommitdiff
path: root/include/asterisk/spinlock.h
blob: dacb58299c1dbb5261552b8842f80fa1d9ffa93a (plain)
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
/*
 * Asterisk -- An open source telephony toolkit.
 *
 * Copyright (C) 2014, Fairview 5 Engineering, LLC
 *
 * George Joseph <george.joseph@fairview5.com>
 *
 * See http://www.asterisk.org for more information about
 * the Asterisk project. Please do not directly contact
 * any of the maintainers of this project for assistance;
 * the project provides a web site, mailing lists and IRC
 * channels for your use.
 *
 * This program is free software, distributed under the terms of
 * the GNU General Public License Version 2. See the LICENSE file
 * at the top of the source tree.
 */

/*! \file
 * \brief Spin Locks.
 *
 * In some atomic operation circumstances the __atomic calls are not quite
 * flexible enough but a full fledged mutex or rwlock is too expensive.
 *
 * Spin locks should be used only for protecting short blocks of critical
 * code such as simple compares and assignments.  Operations that may block,
 * hold a lock, or cause the thread to give up it's timeslice should NEVER
 * be attempted in a spin lock.
 *
 * Because spinlocks must be as lightweight as possible, there are no
 * recursion or deadlock checks.
 *
 */

#ifndef _ASTERISK_SPINLOCK_H
#define _ASTERISK_SPINLOCK_H

#include <pthread.h>
#include "asterisk/compiler.h"

/*!
 * \brief Spinlock Implementation Types
 *
 * Not all implementations will be available on all platforms.
 *
 */
enum ast_spinlock_type {
	AST_SPINLOCK_TYPE_GCC_ATOMICS,
	AST_SPINLOCK_TYPE_GAS_X86,
	AST_SPINLOCK_TYPE_GAS_ARM,
	AST_SPINLOCK_TYPE_GAS_SPARC,
	AST_SPINLOCK_TYPE_OSX_ATOMICS,
	AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK,
	AST_SPINLOCK_TYPE_PTHREAD_MUTEX,
};

/*!
 * \brief Implementation using GCC Atomics
 *
 * Specifically, __sync_lock_test_and_set is used to atomically
 * set/unset the lock variable.
 *
 * Most recent gcc implementations support this method and it's performance
 * is equal to or better than the assembly implementations hence it is the
 * most preferred implementation on all platforms.
 *
 */
#ifdef HAVE_GCC_ATOMICS
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GCC_ATOMICS
#define AST_SPINLOCK_TYPE_LABEL "gcc_atomics"
typedef volatile unsigned int ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	*lock = 0;
	return 0;
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	while (__sync_lock_test_and_set(lock, 1)) {
		while(*lock) {
		}
	}
	return 0;
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	return __sync_lock_test_and_set(lock, 1);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	__sync_lock_release(lock);
	return 0;
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return 0;
}
#endif

/*!
 * \brief Implementation using x86 Assembly
 *
 * For x86 implementations that don't support gcc atomics,
 * this is the next best method.
 *
 */
#if (defined(__x86_64__) || defined(__i386__)) && !defined(AST_SPINLOCK_TYPE)
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_X86
#define AST_SPINLOCK_TYPE_LABEL "gas_x86"
typedef volatile unsigned int ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	*lock = 0;
	return 0;
}

static force_inline int x86chgl(ast_spinlock_t *p, unsigned int v)
{
	__asm __volatile (
		"	xchg   %0, %1 ;"
		: "+r" (v), "=m" (*p)
		: "m" (*p)
	);

	return (v);
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	while (x86chgl(lock, 1)) {
		while(*lock) {
		}
	}
	return 0;
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	return x86chgl(lock, 1);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	x86chgl(lock, 0);
	return 0;
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return 0;
}
#endif

/*!
 * \brief Implementation using ARM Assembly
 *
 * For ARM implementations that don't support gcc atomics,
 * this is the next best method.
 *
 */
#if defined(__arm__) && !defined(AST_SPINLOCK_TYPE)
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_ARM
#define AST_SPINLOCK_TYPE_LABEL "gas_arm"
typedef volatile unsigned int ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	*lock = 0;
	return 0;
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	unsigned int tmp;

	__asm __volatile (
		"1:	ldrex %[tmp], %[lock];"
		"	teq %[tmp], #0;"
#if defined __ARM_ARCH && __ARM_ARCH >= 7
		"	wfene;"
#endif
		"	strexeq %[tmp], %[c1], %[lock];"
		"	teqeq %[tmp], #0;"
		"	bne 1b;"
		: [tmp] "=&r" (tmp)
		: [lock] "m" (*lock) [c1] "r" (1)
		: "cc"
	);

	return tmp;
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	unsigned int tmp;

	__asm __volatile (
		"	ldrex %[tmp], %[lock];"
		"	teq %[tmp], #0;"
#if defined __ARM_ARCH && __ARM_ARCH >= 7
		"	wfene;"
#endif
		"	strexeq %[tmp], %[c1], %[lock];"
		: [tmp] "=&r" (tmp)
		: [lock] "m" (*lock) [c1] "r" (1)
		: "cc"
	);

	return tmp;
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	__asm __volatile (
		"	dmb;"
		"	str %[c0], %[lock];"
#if defined __ARM_ARCH && __ARM_ARCH >= 7
		"	dsb;"
		"	sev;"
#endif
		:
		: [lock] "m" (*lock) [c0] "r" (0)
		: "cc"
	);

	return 0;
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return 0;
}
#endif

/*!
 * \brief Implementation using Sparc Assembly
 *
 * For Sparc implementations that don't support gcc atomics,
 * this is the next best method.
 *
 */
#if defined(__sparc__) && !defined(AST_SPINLOCK_TYPE)
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_GAS_SPARC
#define AST_SPINLOCK_TYPE_LABEL "gas_sparc"
typedef volatile unsigned char ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	*lock = 0;
	return 0;
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	unsigned char tmp;

	__asm__ __volatile__(
		"1:	ldstub		%[lock], %[tmp]\n"
		"	brnz,pn		%[tmp], 2f\n"
		"	 nop\n"
		"	.subsection	2\n"
		"2:	ldub		%[lock], %[tmp]\n"
		"	brnz,pt		%[tmp], 2b\n"
		"	 nop\n"
		"	ba,a,pt		%%xcc, 1b\n"
		"	.previous"
		: [tmp] "=&r" (tmp)
		: [lock] "m" (*lock)
		: "memory"
	);

	return 0;
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	unsigned long result = 1;

	__asm__ __volatile__(
		"	ldstub		%[lock], %[result]\n"
		: [result] "=&r" (result)
		: [lock] "m" (*lock)
		: "memory", "cc"
	);

	return (result != 0);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	__asm__ __volatile__(
		"	stb		%%g0, %[lock]"
		:
		: [lock] "m" (*lock)
		: "memory", "cc"
	);

	return 0;
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return 0;
}
#endif

/*!
 * \brief Implementation using pthread_spinlock
 *
 * pthread_spinlocks are not supported on all platforms
 * but if for some reason none of the previous implementations are
 * available, it can be used with reasonable performance.
 *
 */
#if defined (HAVE_PTHREAD_SPINLOCK) && !defined(AST_SPINLOCK_TYPE)
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_SPINLOCK
#define AST_SPINLOCK_TYPE_LABEL "pthread_spinlock"
typedef pthread_spinlock_t ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	return pthread_spin_init(lock, PTHREAD_PROCESS_PRIVATE);
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	return pthread_spin_lock(lock);
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	return pthread_spin_trylock(lock);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	return pthread_spin_unlock(lock);
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return pthread_spin_destroy(lock);
}
#endif

/*!
 * \brief Implementation using OSX Atomics
 *
 * The Darwin/Mac OSX platform has its own atomics
 * implementation but it uses more kernel time than
 * GCC atomics and x86 assembly.  It is included
 * as an unlikely fallback.
 *
 */
#if defined(HAVE_OSX_ATOMICS) && !defined(AST_SPINLOCK_TYPE)
#include <libkern/OSAtomic.h>
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_OSX_ATOMICS
#define AST_SPINLOCK_TYPE_LABEL "osx_atomics"
typedef OSSpinLock ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	*lock = OS_SPINLOCK_INIT;
	return 0;
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	OSSpinLockLock(lock);
	return 0;
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	return !OSSpinLockTry(lock);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	OSSpinLockUnlock(lock);
	return 0;
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return 0;
}
#endif

/*!
 * \brief Implementation using pthread_mutex
 *
 * pthread_mutex is supported on all platforms but
 * it is also the worst performing. It is included
 * as an unlikely fallback.
 *
 */
#if !defined(AST_SPINLOCK_TYPE)
#define AST_SPINLOCK_TYPE AST_SPINLOCK_TYPE_PTHREAD_MUTEX
#define AST_SPINLOCK_TYPE_LABEL "pthread_mutex"
typedef pthread_mutex_t ast_spinlock_t;

static force_inline int ast_spinlock_init(ast_spinlock_t *lock)
{
	pthread_mutex_init(lock, NULL);
	return 0;
}

static force_inline int ast_spinlock_lock(ast_spinlock_t *lock)
{
	return pthread_mutex_lock(lock);
}

static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock)
{
	return pthread_mutex_trylock(lock);
}

static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock)
{
	return pthread_mutex_unlock(lock);
}

static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock)
{
	return pthread_mutex_destroy(lock);
}
#endif

#if !defined(AST_SPINLOCK_TYPE)
#error "No spinlock implementation could be found."
#endif

/* Prototypes are declared here to insure that each implementation provides
 * the same API and to act as placeholders for the documentation.
 */

/*!
 * \brief Initialize a spin lock
 * \param lock Address of the lock
 * \retval 0 Success
 * \retval other Failure
 */
static force_inline int ast_spinlock_init(ast_spinlock_t *lock);

/*!
 * \brief Lock a spin lock
 * \param lock Address of the lock
 * \retval 0 Success
 * \retval other Failure
 */
static force_inline int ast_spinlock_lock(ast_spinlock_t *lock);

/*!
 * \brief Try to lock a spin lock
 *
 * Attempt to gain a lock.  Return immediately
 * regardless of result.
 *
 * \param lock Address of the lock
 * \retval 0 Success
 * \retval other Lock was not obtained
 */
static force_inline int ast_spinlock_trylock(ast_spinlock_t *lock);

/*!
 * \brief Unlock a spin lock
 * \param lock Address of the lock
 * \retval 0 Success
 * \retval other Failure
 */
static force_inline int ast_spinlock_unlock(ast_spinlock_t *lock);

/*!
 * \brief Destroy a spin lock
 * \param lock Address of the lock
 * \retval 0 Success
 * \retval other Failure
 */
static force_inline int ast_spinlock_destroy(ast_spinlock_t *lock);

#endif /* _ASTERISK_SPINLOCK_H */