wrflock.h
4.42 KB
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
#ifndef _WRFLOCK_H
#define _WRFLOCK_H 1
#include <atomic>
#include <linux/futex.h>
#define WRFLOCK_WWAITBLOCK 1
#define WRFLOCK_WWAITYIELD 2
#define WRFLOCK_RWAITBLOCK 4
#define WRFLOCK_RWAITYIELD 8
#define WRFLOCK_FWAITBLOCK 16
#define WRFLOCK_FWAITYIELD 32
struct wrflock_t
{
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define WRFLOCK_COUNTERS_OFFSET 0
# define WRFLOCK_STATE_OFFSET 1
# define MAKEFLAGS64(flags, le) (le?((((uint64_t)(flags))<<32)):(((uint64_t)(flags))))
# define MAKESHIFT64(shift, le) (le?(shift+32):(shift))
# elif __BYTE_ORDER == __BIG_ENDIAN
# define WRFLOCK_COUNTERS_OFFSET 1
# define WRFLOCK_STATE_OFFSET 0
# define MAKEFLAGS64(flags, le) (le?((((uint64_t)(flags)))):((((uint64_t)(flags))<<32)))
# define MAKESHIFT64(shift, le) (le?(shift):(shift+32))
# else
# error Unsupported byte order.
# endif
# define WRFLOCK_PRIVATE_MASK_32 ((uint32_t)0x04000000)
# define WRFLOCK_PRIVATE_MASK_64 (MAKEFLAGS64(WRFLOCK_PRIVATE_MASK_32,0) | MAKEFLAGS64(WRFLOCK_PRIVATE_MASK_32,1))
# define WRFLOCK_WWAITYIELD_MASK_32 ((uint32_t)0x00010000)
# define WRFLOCK_WWAITYIELD_MASK_64 MAKEFLAGS64(WRFLOCK_WWAITYIELD_MASK_32,1)
# define WRFLOCK_RWAITYIELD_MASK_32 ((uint32_t)0x00020000)
# define WRFLOCK_RWAITYIELD_MASK_64 MAKEFLAGS64(WRFLOCK_RWAITYIELD_MASK_32,1)
# define WRFLOCK_FWAITYIELD_MASK_32 ((uint32_t)0x00040000)
# define WRFLOCK_FWAITYIELD_MASK_64 MAKEFLAGS64(WRFLOCK_FWAITYIELD_MASK_32,1)
# define WRACQUIRE_VALUE_SHIFT_32 28
# define RDACQUIRE_VALUE_SHIFT_32 29
# define FRACQUIRE_VALUE_SHIFT_32 30
# define RDACQUIRE_COUNTER_SHIFT_32 0
# define WRACQUIRE_VALUE_SHIFT_64 MAKESHIFT64(WRACQUIRE_VALUE_SHIFT_32,1)
# define RDACQUIRE_VALUE_SHIFT_64 MAKESHIFT64(RDACQUIRE_VALUE_SHIFT_32,1)
# define FRACQUIRE_VALUE_SHIFT_64 MAKESHIFT64(FRACQUIRE_VALUE_SHIFT_32,1)
# define RDACQUIRE_COUNTER_SHIFT_64 MAKESHIFT64(RDACQUIRE_COUNTER_SHIFT_32,0)
# define WRACQUIRE_VALUE_MASK_32 ((uint32_t)0x10000000)
# define RDACQUIRE_VALUE_MASK_32 ((uint32_t)0x20000000)
# define FRACQUIRE_VALUE_MASK_32 ((uint32_t)0x40000000)
# define RDACQUIRE_COUNTER_MASK_32 ((uint32_t)0x0000FFFF)
# define RDNXTLOOP_FLAG_MASK_32 ((uint32_t)0x02000000)
# define WRACQUIRE_VALUE_MASK_64 MAKEFLAGS64(WRACQUIRE_VALUE_MASK_32,1)
# define RDACQUIRE_VALUE_MASK_64 MAKEFLAGS64(RDACQUIRE_VALUE_MASK_32,1)
# define FRACQUIRE_VALUE_MASK_64 MAKEFLAGS64(FRACQUIRE_VALUE_MASK_32,1)
# define RDACQUIRE_COUNTER_MASK_64 MAKEFLAGS64(RDACQUIRE_COUNTER_MASK_32,0)
# define RDNXTLOOP_FLAG_MASK_64 MAKEFLAGS64(RDNXTLOOP_FLAG_MASK_32,1)
# define NEXTSTATE_WRITE_MASK_32 ((uint32_t)0x00000010)
# define NEXTSTATE_READFREE_MASK_32 ((uint32_t)0x00000020)
# define NEXTSTATE_VALUE_MASK_32 (NEXTSTATE_WRITE_MASK_32|NEXTSTATE_READFREE_MASK_32|NEXTSTATE_FREE_MASK_32)
# define NEXTSTATE_WRITE_MASK_64 MAKEFLAGS64(NEXTSTATE_WRITE_MASK_32,1)
# define NEXTSTATE_READFREE_MASK_64 MAKEFLAGS64(NEXTSTATE_READFREE_MASK_32,1)
# define NEXTSTATE_VALUE_MASK_64 (NEXTSTATE_WRITE_MASK_64|NEXTSTATE_READFREE_MASK_64|NEXTSTATE_FREE_MASK_64)
# define CURRSTATE_WRITE_MASK_32 ((uint32_t)0x00000001)
# define CURRSTATE_READ_MASK_32 ((uint32_t)0x00000002)
# define CURRSTATE_FREE_MASK_32 ((uint32_t)0x00000004)
# define CURRSTATE_VALUE_MASK_32 (CURRSTATE_WRITE_MASK_32|CURRSTATE_READ_MASK_32|CURRSTATE_FREE_MASK_32)
# define CURRSTATE_WRITE_MASK_64 MAKEFLAGS64(CURRSTATE_WRITE_MASK_32,1)
# define CURRSTATE_READ_MASK_64 MAKEFLAGS64(CURRSTATE_READ_MASK_32,1)
# define CURRSTATE_FREE_MASK_64 MAKEFLAGS64(CURRSTATE_FREE_MASK_32,1)
# define CURRSTATE_VALUE_MASK_64 (CURRSTATE_WRITE_MASK_64|CURRSTATE_READ_MASK_64|CURRSTATE_FREE_MASK_64)
union {
std::atomic<uint64_t> data64;
std::atomic<uint32_t> data32[2];
};
};
int wrflock_init (wrflock_t *lock, int waittype, int pshared);
int wrflock_destroy (wrflock_t *lock);
int wrflock_wacquire (wrflock_t *lock);
int wrflock_racquire (wrflock_t *lock);
int wrflock_facquire (wrflock_t *lock);
int wrflock_wrelease (wrflock_t *lock);
int wrflock_rrelease (wrflock_t *lock);
int wrflock_frelease (wrflock_t *lock);
int wrflock_wwait (wrflock_t *lock);
int wrflock_rwait (wrflock_t *lock);
int wrflock_fwait (wrflock_t *lock);
int wrflock_wtrywait (wrflock_t *lock);
int wrflock_rtrywait (wrflock_t *lock);
int wrflock_ftrywait (wrflock_t *lock);
int wrflock_wtimewait (wrflock_t *lock, const struct timespec *abstime);
int wrflock_rtimewait (wrflock_t *lock, const struct timespec *abstime);
int wrflock_ftimewait (wrflock_t *lock, const struct timespec *abstime);
#endif /* rmwlock.h */