forked from Snapchat/KeyDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastlock.h
58 lines (47 loc) · 1008 Bytes
/
fastlock.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
#pragma once
#include <inttypes.h>
#ifdef __cplusplus
extern "C" {
#endif
/* Begin C API */
struct fastlock;
void fastlock_init(struct fastlock *lock);
void fastlock_lock(struct fastlock *lock);
int fastlock_trylock(struct fastlock *lock);
void fastlock_unlock(struct fastlock *lock);
void fastlock_free(struct fastlock *lock);
uint64_t fastlock_getlongwaitcount(); // this is a global value
/* End C API */
#ifdef __cplusplus
}
#endif
struct ticket
{
uint16_t m_active;
uint16_t m_avail;
};
struct fastlock
{
volatile struct ticket m_ticket;
volatile int m_pidOwner;
volatile int m_depth;
#ifdef __cplusplus
fastlock()
{
fastlock_init(this);
}
void lock()
{
fastlock_lock(this);
}
bool try_lock()
{
return !!fastlock_trylock(this);
}
void unlock()
{
fastlock_unlock(this);
}
bool fOwnLock(); // true if this thread owns the lock, NOTE: not 100% reliable, use for debugging only
#endif
};