forked from efficient/cicada-exp-sigmod2017-silo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
spinlock.h
55 lines (45 loc) · 866 Bytes
/
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
#ifndef _SPINLOCK_H_
#define _SPINLOCK_H_
#include <stdint.h>
#include "amd64.h"
#include "macros.h"
#include "util.h"
class spinlock {
public:
spinlock() : value(0) {}
spinlock(const spinlock &) = delete;
spinlock(spinlock &&) = delete;
spinlock &operator=(const spinlock &) = delete;
inline void
lock()
{
// XXX: implement SPINLOCK_BACKOFF
uint32_t v = value;
while (v || !__sync_bool_compare_and_swap(&value, 0, 1)) {
nop_pause();
v = value;
}
COMPILER_MEMORY_FENCE;
}
inline bool
try_lock()
{
return __sync_bool_compare_and_swap(&value, 0, 1);
}
inline void
unlock()
{
INVARIANT(value);
value = 0;
COMPILER_MEMORY_FENCE;
}
// just for debugging
inline bool
is_locked() const
{
return value;
}
private:
volatile uint32_t value;
};
#endif /* _SPINLOCK_H_ */