-
Notifications
You must be signed in to change notification settings - Fork 0
/
cexplock.h
297 lines (247 loc) · 7.27 KB
/
cexplock.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
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#ifndef CEXP_LOCK_H
#define CEXP_LOCK_H
/* Wrapper header for mutexes */
/* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*
* Authorship
* ----------
* This software (CEXP - C-expression interpreter and runtime
* object loader/linker) was created by
*
* Till Straumann <[email protected]>, 2002-2008,
* Stanford Linear Accelerator Center, Stanford University.
*
* Acknowledgement of sponsorship
* ------------------------------
* This software was produced by
* the Stanford Linear Accelerator Center, Stanford University,
* under Contract DE-AC03-76SFO0515 with the Department of Energy.
*
* Government disclaimer of liability
* ----------------------------------
* Neither the United States nor the United States Department of Energy,
* nor any of their employees, makes any warranty, express or implied, or
* assumes any legal liability or responsibility for the accuracy,
* completeness, or usefulness of any data, apparatus, product, or process
* disclosed, or represents that its use would not infringe privately owned
* rights.
*
* Stanford disclaimer of liability
* --------------------------------
* Stanford University makes no representations or warranties, express or
* implied, nor assumes any liability for the use of this software.
*
* Stanford disclaimer of copyright
* --------------------------------
* Stanford University, owner of the copyright, hereby disclaims its
* copyright and all other rights in this software. Hence, anyone may
* freely use it for any purpose without restriction.
*
* Maintenance of notices
* ----------------------
* In the interest of clarity regarding the origin and status of this
* SLAC software, this and all the preceding Stanford University notices
* are to remain affixed to any copy or derivative of this software made
* or distributed by the recipient and are to be affixed to any copy of
* software made or distributed by the recipient that contains a copy or
* derivative of this software.
*
* SLAC Software Notices, Set 4 OTT.002a, 2004 FEB 03
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef __cplusplus
extern "C" {
#endif
#ifdef USE_EPICS_OSI
#include <epicsMutex.h>
#include <epicsEvent.h>
typedef epicsMutexId CexpLock;
typedef epicsEventId CexpEvent;
INLINE int
cexpLockCreate(CexpLock *lp)
{
return ! (*lp=epicsMutexCreate());
}
INLINE int
cexpEventCreate(CexpEvent *id)
{
return ! (*id=epicsEventCreate(epicsEventEmpty));
}
#define cexpLock(l) epicsMutexLock((l))
#define cexpUnlock(l) epicsMutexUnlock((l))
#define cexpLockDestroy(l) epicsMutexDestroy((l))
#define cexpEventSend(e) epicsEventSignal(e)
#define cexpEventWait(e) epicsEventWait(e)
#define cexpEventDestroy(e) epicsEventDestroy(e)
#define cexpLockingInitialize() 0
#elif defined(HAVE_PTHREADS)
#include <pthread.h>
typedef pthread_mutex_t *CexpLock;
typedef pthread_cond_t *CexpCond;
typedef pthread_cond_t CexpCondRec;
#define cexpCondInit(c_p) pthread_cond_init(c_p, 0)
#define cexpCondWait(c_p, l_p) pthread_cond_wait(c_p, l_p)
#define cexpCondSignal(c_p) pthread_cond_signal(c_p)
#define cexpLock(l) pthread_mutex_lock(l)
#define cexpUnlock(l) pthread_mutex_unlock(l)
extern pthread_mutexattr_t cexpMutexAttributes;
int
cexpLockCreate(CexpLock *l);
int
cexpLockDestroy(CexpLock l);
int
cexpLockingInitialize();
#elif defined(__rtems__)
#if defined(HAVE_RTEMS_H)
#include <rtems.h>
#else
/* avoid pulling in <rtems.h> until we can do this in a BSP independent way */
#define rtems_id unsigned long
long rtems_semaphore_obtain();
long rtems_semaphore_release();
long rtems_semaphore_create();
long rtems_semaphore_destroy();
#define rtems_build_name( _C1, _C2, _C3, _C4 ) \
( (_C1) << 24 | (_C2) << 16 | (_C3) << 8 | (_C4) )
#define RTEMS_NO_TIMEOUT 0
#define RTEMS_WAIT 0
#define RTEMS_FIFO 0
#define RTEMS_PRIORITY 0x04 /* must be set to get priority inheritance */
#define RTEMS_BINARY_SEMAPHORE 0x10
#define RTEMS_SIMPLE_BINARY_SEMAPHORE 0x20
#define RTEMS_INHERIT_PRIORITY 0x40
#endif
typedef rtems_id CexpLock;
typedef rtems_id CexpEvent;
#define cexpLock(l) rtems_semaphore_obtain((l), RTEMS_WAIT, RTEMS_NO_TIMEOUT)
#define cexpEventWait(l) rtems_semaphore_obtain((l), RTEMS_WAIT, RTEMS_NO_TIMEOUT)
#define cexpUnlock(l) rtems_semaphore_release((l))
#define cexpEventSend(l) rtems_semaphore_release((l))
/* IMPORTANT: use a standard (not simple) binary semaphore that may nest */
static __inline__ int
cexpLockCreate(CexpLock *l)
{
return
rtems_semaphore_create(
rtems_build_name('c','e','x','p'),
1,/*initial count*/
RTEMS_PRIORITY|RTEMS_BINARY_SEMAPHORE|RTEMS_INHERIT_PRIORITY,
0,
l);
}
static __inline__ int
cexpEventCreate(CexpEvent *pe)
{
return
rtems_semaphore_create(
rtems_build_name('c','e','x','p'),
0,/*initial count*/
RTEMS_SIMPLE_BINARY_SEMAPHORE,
0,
pe);
}
#define cexpLockDestroy(l) rtems_semaphore_delete((l))
#define cexpEventDestroy(l) rtems_semaphore_delete((l))
#define cexpLockingInitialize() 0
#elif defined(NO_THREAD_PROTECTION)
typedef void *CexpLock;
typedef void *CexpEvent;
#define cexpLock(l) do {} while(0)
#define cexpUnlock(l) do {} while(0)
#define cexpLockCreate(l) do {} while(0)
#define cexpLockDestroy(l) do {} while(0)
#define cexpEventSend(l) do {} while(0)
#define cexpEventWait(l) do {} while(0)
#define cexpEventCreate(l) do {} while(0)
#define cexpEventDestroy(l) do {} while(0)
#define cexpReadLock(l) do {} while(0)
#define cexpReadUnlock(l) do {} while(0)
#define cexpWriteLock(l) do {} while(0)
#define cexpWriteUnlock(l) do {} while(0)
#define cexpRWLockInit(l) do {} while(0)
#define cexpLockingInitialize() 0
#else
#error "thread protection not implemented for this target system"
#endif
#if ! defined(HAVE_PTHREADS) && ! defined(NO_THREAD_PROTECTION)
typedef CexpEvent CexpCondRec, *CexpCond;
static __inline__ void
cexpCondWait(CexpCond c, CexpLock l)
{
cexpUnlock(l);
cexpEventWait(*c);
cexpLock(l);
}
static __inline__ void
cexpCondSignal(CexpCond c)
{
cexpEventSend(*c);
}
static __inline__ void
cexpCondInit(CexpCond c)
{
cexpEventCreate(c);
}
#endif
typedef struct CexpRWLockRec_ {
CexpLock mutex;
unsigned readers;
CexpCondRec nap;
unsigned sleeping_writers;
} CexpRWLockRec, *CexpRWLock;
#ifndef NO_THREAD_PROTECTION
static __inline__ void
cexpRWLockInit(CexpRWLock pl)
{
cexpLockCreate(&pl->mutex);
cexpCondInit(&pl->nap);
pl->readers=0;
pl->sleeping_writers=0;
}
/* Readers / Writer lock implementation. This
* should be a little more efficient for the common
* case of a single reader which only has to
* acquire one mutex.
* NOTE: A thread holding the write-lock may still
* do nested readLock() readUnlock() calls.
* However, a reader _MUST_NOT_ try to acquire
* the write lock!
*/
static __inline__ void
cexpReadLock(CexpRWLock l)
{
cexpLock(l->mutex);
l->readers++;
cexpUnlock(l->mutex);
}
static __inline__ void
cexpReadUnlock(CexpRWLock l)
{
cexpLock(l->mutex);
if (0 == --l->readers && l->sleeping_writers) {
l->sleeping_writers--;
cexpCondSignal(&l->nap);
}
cexpUnlock(l->mutex);
}
static __inline__ void
cexpWriteLock(CexpRWLock l)
{
cexpLock(l->mutex);
while (l->readers) {
l->sleeping_writers++;
cexpCondWait( &l->nap, l->mutex );
}
}
static __inline__ void
cexpWriteUnlock(CexpRWLock l)
{
cexpUnlock(l->mutex);
}
#endif
#ifdef __cplusplus
}
#endif
#endif