1 krisbash 1.1 #ifndef _pal_lockatomic_h
2 #define _pal_lockatomic_h
3
4 #if !defined(_pal_lock_h)
5 #error "Include pal/lock.h (Atomic version not available)"
6 #endif
7
8 #include "palcommon.h"
9 #include "thread.h"
10
11 PAL_BEGIN_EXTERNC
12
13 /*
14 **==============================================================================
15 **
16 ** ReadWriteLock -- read/write lock (non-recursive)
17 **
18 **==============================================================================
19 */
20
21 #define READWRITELOCK_INITIALIZER { 0 }
22 krisbash 1.1
23 typedef struct _ReadWriteLock
24 {
25 ptrdiff_t state;
26 } ReadWriteLock;
27
28 PAL_INLINE void ReadWriteLock_Init(
29 _Out_ ReadWriteLock* self)
30 {
31 self->state = 0;
32 }
33
34
35 void ReadWriteLock_AcquireRead(
36 _Inout_ ReadWriteLock* self);
37
38 int ReadWriteLock_TryAcquireRead(
39 _Inout_ ReadWriteLock* self);
40
41 void ReadWriteLock_AcquireWrite(
42 _Inout_ ReadWriteLock* self);
43 krisbash 1.1
44 int ReadWriteLock_TryAcquireWrite(
45 _Inout_ ReadWriteLock* self);
46
47 void ReadWriteLock_ReleaseRead(
48 _Inout_ ReadWriteLock* self);
49
50 void ReadWriteLock_ReleaseWrite(
51 _Inout_ ReadWriteLock* self);
52
53 /*
54 **==============================================================================
55 **
56 ** Lock -- simple non-recursive lock
57 **
58 **==============================================================================
59 */
60
61 #define LOCK_INITIALIZER { READWRITELOCK_INITIALIZER }
62
63 typedef struct _Lock
64 krisbash 1.1 {
65 ReadWriteLock lock;
66 } Lock;
67
68 PAL_INLINE void Lock_Init(
69 _Out_ Lock* self)
70 {
71 ReadWriteLock_Init(&self->lock);
72 }
73
74 PAL_INLINE void Lock_Acquire(
75 _Inout_ Lock* self)
76 {
77 ReadWriteLock_AcquireWrite(&self->lock);
78 }
79
80 PAL_INLINE int Lock_TryAcquire(
81 _Inout_ Lock* self)
82 {
83 return ReadWriteLock_TryAcquireWrite(&self->lock);
84 }
85 krisbash 1.1
86 PAL_INLINE void Lock_Release(
87 _Inout_ Lock* self)
88 {
89 ReadWriteLock_ReleaseWrite(&self->lock);
90 }
91
92 /*
93 **==============================================================================
94 **
95 ** RecursiveLock -- recursive lock
96 **
97 **==============================================================================
98 */
99
100 #define RECURSIVELOCK_INITIALIZER {LOCK_INITIALIZER, 0}
101
102 typedef struct _RecursiveLock
103 {
104 Lock lock;
105 ptrdiff_t count;
106 krisbash 1.1 ThreadID owner;
107 } RecursiveLock;
108
109 PAL_INLINE void RecursiveLock_Init(
110 _Out_ RecursiveLock* self)
111 {
112 Lock_Init(&self->lock);
113 self->count = 0;
114 self->owner = Thread_ID_Null();
115 }
116
117 void RecursiveLock_Acquire(
118 _Inout_ RecursiveLock* self);
119
120 PAL_INLINE void RecursiveLock_Release(
121 _Inout_ RecursiveLock* self)
122 {
123 if (--self->count > 0)
124 return;
125
126 self->owner = Thread_ID_Null();
127 krisbash 1.1 Lock_Release(&self->lock);
128 }
129
130 /*
131 **==============================================================================
132 **
133 ** CachedLock -- cached lock
134 **
135 **==============================================================================
136 */
137
138 /* Temporary disable of cached lock */
139 #if 0
140 typedef struct _CachedLock_Pool
141 {
142 ptrdiff_t* latches; /* Aligned set of cache lines. */
143 volatile ptrdiff_t mask; /* Bits indicate used parts of each line. */
144 } CachedLock_Pool;
145
146 typedef struct _CachedLock
147 {
148 krisbash 1.1 CachedLock_Pool* pool; /* Could be shared or not. */
149 ptrdiff_t* latches; /* Set of latches, one cache line apart. */
150 ReadWriteLock lock; /* Used when there are exclusive threads. */
151 volatile ptrdiff_t master; /* Counts exclusive threads; masks latches. */
152 } CachedLock;
153
154 #define CACHEDLOCK_FLAG_SHARED (1 << 0)
155
156 _Return_type_success_(return == 0)
157 int CachedLock_Init_Checked(
158 _Out_ CachedLock* self,
159 unsigned long flags,
160 NitsCallSite cs);
161
162 #define CachedLock_Init(self, flags) \
163 CachedLock_Init_Checked(self, flags, NitsHere())
164
165 void CachedLock_Destroy(
166 _Inout_ CachedLock* self);
167
168 void CachedLock_AcquireRead(
169 krisbash 1.1 _Inout_ CachedLock* self,
170 _Out_ void** token);
171
172 void CachedLock_AcquireWrite(
173 _Inout_ CachedLock* self);
174
175 void CachedLock_ReleaseRead(
176 _Inout_ CachedLock* self,
177 _In_ void* token);
178
179 void CachedLock_ReleaseWrite(
180 _Inout_ CachedLock* self);
181 #else
182
183 typedef ReadWriteLock CachedLock;
184
185 #define CACHEDLOCK_FLAG_SHARED (1 << 0)
186
187 _Return_type_success_(return == 0)
188 PAL_INLINE int CachedLock_Init(
189 _Out_ CachedLock* self,
190 krisbash 1.1 unsigned long flags)
191 {
192 ReadWriteLock_Init(self);
193 return 0;
194 }
195
196 #define CachedLock_Destroy(self)
197
198 #define CachedLock_AcquireRead(self, token) ReadWriteLock_AcquireRead(self)
199
200 #define CachedLock_AcquireWrite(self) ReadWriteLock_AcquireWrite(self)
201
202 #define CachedLock_ReleaseRead(self, token) ReadWriteLock_ReleaseRead(self)
203
204 #define CachedLock_ReleaseWrite(self) ReadWriteLock_ReleaseWrite(self)
205
206 #endif
207 /*
208 **==============================================================================
209 **
210 ** CondLock -- condition variable
211 krisbash 1.1 **
212 **==============================================================================
213 */
214
215 #define CONDLOCK_DEFAULT_SPINCOUNT ((size_t)-1)
216 #define CONDLOCK_HIGH_SPINCOUNT ((size_t)-2)
217 #define CONDLOCK_LOW_SPINCOUNT ((size_t)-3)
218
219 int CondLock_Wait(
220 ptrdiff_t key,
221 _In_ volatile const ptrdiff_t* destination,
222 ptrdiff_t comparand,
223 size_t spinCount);
224
225 void CondLock_Broadcast(
226 ptrdiff_t key);
227
228 void CondLock_BroadcastSpinners(
229 ptrdiff_t key);
230
231 void CondLock_Signal(
232 krisbash 1.1 ptrdiff_t key);
233
234 PAL_END_EXTERNC
235
236 #endif /* _pal_lockatomic_h */
|