-
Notifications
You must be signed in to change notification settings - Fork 4
/
spinlock.h
66 lines (53 loc) · 1.35 KB
/
spinlock.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
/*
* Copyright (c) 2011 The Regents of the University of California
* Barret Rhoden <[email protected]>
* Kevin Klues <[email protected]>
*
* This file 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
* Lesser GNU General Public License for more details.
*
* See COPYING.LESSER for details on the GNU Lesser General Public License.
* See COPYING for details on the GNU General Public License.
*/
#ifndef SPINLOCK_H
#define SPINLOCK_H
#include <errno.h>
#ifdef __cplusplus
extern "C" {
#endif
static __inline void
cpu_relax(void)
{
asm volatile("pause" : : : "memory");
}
#define SPINLOCK_INITIALIZER {0}
typedef struct spinlock {
int lock;
} spinlock_t;
static void spinlock_init(spinlock_t *lock)
{
lock->lock = 0;
}
static int spinlock_trylock(spinlock_t *lock)
{
return __sync_lock_test_and_set(&lock->lock, EBUSY);
}
static void spinlock_lock(spinlock_t *lock)
{
while (spinlock_trylock(lock))
cpu_relax();
}
static inline void spinlock_unlock(spinlock_t *lock)
{
__sync_lock_release(&lock->lock, 0);
}
#define spin_pdr_lock_t spinlock_t
#define spin_pdr_init spinlock_init
#define spin_pdr_lock spinlock_lock
#define spin_pdr_unlock spinlock_unlock
#ifdef __cplusplus
}
#endif
#endif // SPINLOCK_H