File: rtai_sem.h

package info (click to toggle)
rtai 3.1.0-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 23,560 kB
  • ctags: 19,698
  • sloc: ansic: 88,861; cpp: 31,340; tcl: 14,684; sh: 10,652; xml: 760; yacc: 575; lex: 537; makefile: 394; asm: 310; php: 300; perl: 108
file content (377 lines) | stat: -rw-r--r-- 9,790 bytes parent folder | download
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
 * @ingroup lxrt
 * @file
 *
 * @author Paolo Mantegazza
 *
 * @note Copyright &copy; 1999-2003 Paolo Mantegazza <mantegazza@aero.polimi.it>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#ifndef _RTAI_SEM_H
#define _RTAI_SEM_H

#include <rtai_types.h>
#include <rtai_nam2num.h>
#include <rtai_sched.h>

#define RT_SEM_MAGIC 0xaabcdeff

#define SEM_TIMOUT (0xFffe)

#define SEM_ERR (0xFfff)

#if defined(__KERNEL__) && !defined(__cplusplus)

typedef struct rt_semaphore {
    struct rt_queue queue; /* <= Must be first in struct. */
    int magic;
    int type;
    int count;
    struct rt_task_struct *owndby;
    int qtype;
} SEM;

#else /* !__KERNEL__ || __cplusplus */

typedef struct rt_semaphore {
    int opaque;
} SEM;

#endif /* __KERNEL__ && !__cplusplus */

typedef SEM CND;

#ifdef __KERNEL__

#include <linux/errno.h>

typedef SEM psem_t;

typedef SEM pmutex_t;

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

int __rtai_sem_init(void);

void __rtai_sem_exit(void);

void rt_typed_sem_init(SEM *sem,
		       int value,
		       int type);

SEM *_rt_typed_named_sem_init(unsigned long sem_name,
			     int value,
			     int type);

static inline SEM *rt_typed_named_sem_init(const char *sem_name,
					   int value,
					   int type) {
    return _rt_typed_named_sem_init(nam2num(sem_name), value, type);
}

void rt_sem_init(SEM *sem,
		 int value);

int rt_sem_delete(SEM *sem);

int rt_sem_signal(SEM *sem);

int rt_sem_broadcast(SEM *sem);

int rt_sem_wait(SEM *sem);

int rt_sem_wait_if(SEM *sem);

int rt_cntsem_wait_if_and_lock(SEM *sem);

int rt_sem_wait_until(SEM *sem,
		      RTIME time);

int rt_sem_wait_timed(SEM *sem,
		      RTIME delay);

int rt_sem_wait_barrier(SEM *sem);

int rt_sem_count(SEM *sem);

int rt_cond_signal(CND *cnd);

int rt_cond_wait(CND *cnd,
		 SEM *mtx);

int rt_cond_wait_until(CND *cnd,
		       SEM *mtx,
		       RTIME time);

int rt_cond_wait_timed(CND *cnd,
		       SEM *mtx,
		       RTIME delay);

#define rt_named_sem_init(sem_name, value)  rt_typed_named_sem_init(sem_name, value, CNT_SEM)

int rt_named_sem_delete(SEM *sem);

static inline int rt_psem_init(psem_t *sem, int pshared, unsigned int value)
{
	if (value < SEM_TIMOUT) {
		rt_typed_sem_init(sem, value, pshared | PRIO_Q);
		return 0;
	}
	return -EINVAL;
}

static inline int rt_psem_destroy(psem_t *sem)
{
	if (rt_sem_wait_if(sem) >= 0) {
		rt_sem_signal(sem);
		return rt_sem_delete(sem);
	}
	return -EBUSY;
}

static inline int rt_psem_wait(psem_t *sem) {
    return rt_sem_wait(sem) < SEM_TIMOUT ? 0 : -1;
}

static inline int rt_psem_timedwait(psem_t *sem, struct timespec *abstime) {
    return rt_sem_wait_until(sem, timespec2count(abstime)) < SEM_TIMOUT ? 0 : -1;
}

static inline int rt_psem_trywait(psem_t *sem) {
    return rt_sem_wait_if(sem) > 0 ? 0 : -EAGAIN;
}

static inline int rt_psem_post(psem_t *sem) {
    return rt_sem_signal(sem);
}

static inline int rt_psem_getvalue(psem_t *sem, int *sval)
{
	if ((*sval = rt_sem_wait_if(sem)) > 0) {
		rt_sem_signal(sem);
	}
	return 0;
}

static inline int rt_pmutex_init(pmutex_t *mutex, void *mutexattr)
{
	rt_typed_sem_init(mutex, 1, RES_SEM);
	return 0;
}

static inline int rt_pmutex_destroy(pmutex_t *mutex)
{
	if (rt_sem_wait_if(mutex) > 0) {
		rt_sem_signal(mutex);
		return rt_sem_delete(mutex);
	}
	return -EBUSY;
}

static inline int rt_pmutex_lock(pmutex_t *mutex) {
    return rt_sem_wait(mutex) < SEM_TIMOUT ? 0 : -EINVAL;
}

static inline int rt_pmutex_trylock(pmutex_t *mutex) {
    return rt_sem_wait_if(mutex) > 0 ? 0 : -EBUSY;
}

static inline int rt_pmutex_timedlock(pmutex_t *sem, struct timespec *abstime) {
    return rt_sem_wait_until(sem, timespec2count(abstime)) < SEM_TIMOUT ? 0 : -1;
}

static inline int rt_pmutex_unlock(pmutex_t *mutex) {
    return rt_sem_signal(mutex);
}

#define rt_mutex_init(mtx)             rt_typed_sem_init(mtx, 1, RES_SEM)
#define rt_mutex_delete(mtx)           rt_sem_delete(mtx)
#define rt_mutex_destroy(mtx)          rt_sem_delete(mtx)
#define rt_mutex_trylock(mtx)          rt_sem_wait_if(mtx)
#define rt_mutex_lock(mtx)             rt_sem_wait(mtx)
#define rt_mutex_timedlock(mtx, time)  rt_sem_wait_until(mtx, time)
#define rt_mutex_unlock(mtx)           rt_sem_signal(mtx)

#define rt_cond_init(cnd)                  rt_typed_sem_init(cnd, 0, BIN_SEM | PRIO_Q)
#define rt_cond_delete(cnd)                rt_sem_delete(cnd)
#define rt_cond_destroy(cnd)               rt_sem_delete(cnd)
#define rt_cond_broadcast(cnd)             rt_sem_broadcast(cnd)

static inline int rt_cond_timedwait(CND *cnd, SEM *mtx, RTIME time) {
    return rt_cond_wait_until(cnd, mtx, time) < SEM_TIMOUT ? 0 : -1;
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#else /* !__KERNEL__ */

#include <rtai_lxrt.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

RTAI_PROTO(SEM *, rt_typed_sem_init,(int name, int value, int type))
{
	struct { int name, value, type; } arg = { name, value, type };
	return (SEM *)rtai_lxrt(BIDX, SIZARG, LXRT_SEM_INIT, &arg).v[LOW];
}

/**
 * @ingroup lxrt
 * Initialize a counting semaphore.
 *
 * Allocates and initializes a semaphore to be referred by @a name.
 *
 * @param name name of the semaphore.
 *
 * @param value is the initial value of the semaphore
 *
 * It is important to remark that the returned task pointer cannot be used
 * directly, they are for kernel space data, but just passed as arguments when
 * needed.
 *
 * @return a pointer to the semaphore to be used in related calls or 0 if an
 * error has occured.
 */ 
#define rt_sem_init(name, value) rt_typed_sem_init(name, value, CNT_SEM)

#define rt_named_sem_init(sem_name, value) \
	rt_typed_named_sem_init(sem_name, value, CNT_SEM)

RTAI_PROTO(int, rt_sem_delete,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, LXRT_SEM_DELETE, &arg).i[LOW];
}

RTAI_PROTO(SEM *, rt_typed_named_sem_init,(const char *name, int value, int type))
{
	struct { unsigned long name; int value, type; } arg = { nam2num(name), value, type };
	return (SEM *)rtai_lxrt(BIDX, SIZARG, NAMED_SEM_INIT, &arg).v[LOW];
}

RTAI_PROTO(int, rt_named_sem_delete,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, NAMED_SEM_DELETE, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_signal,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_SIGNAL, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_broadcast,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_BROADCAST, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_wait,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_WAIT, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_wait_if,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_WAIT_IF, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_wait_until,(SEM *sem, RTIME time))
{
	struct { SEM *sem; RTIME time; } arg = { sem, time };
	return rtai_lxrt(BIDX, SIZARG, SEM_WAIT_UNTIL, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_wait_timed,(SEM *sem, RTIME delay))
{
	struct { SEM *sem; RTIME delay; } arg = { sem, delay };
	return rtai_lxrt(BIDX, SIZARG, SEM_WAIT_TIMED, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_wait_barrier,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_WAIT_BARRIER, &arg).i[LOW];
}

RTAI_PROTO(int, rt_sem_count,(SEM *sem))
{
	struct { SEM *sem; } arg = { sem };
	return rtai_lxrt(BIDX, SIZARG, SEM_COUNT, &arg).i[LOW];
}

/**
 * @ingroup lxrt
 * Initialize a condition variable.
 *
 * Allocates and initializes a condition variable to be referred by @a name.
 *
 * @param name name of the condition variable.
 *
 * It is important to remark that the returned pointer cannot be used
 * directly, it is for kernel space data, but just passed as arguments when
 * needed.
 *
 * @return a pointer to the condition variable to be used in related calls or 0
 * if an error has occured.
 */ 
#define rt_cond_init(name)                 rt_typed_sem_init(name, 0, BIN_SEM)
#define rt_cond_delete(cnd)                rt_sem_delete(cnd)
#define rt_cond_destroy(cnd)               rt_sem_delete(cnd)
#define rt_cond_broadcast(cnd)             rt_sem_broadcast(cnd)
#define rt_cond_timedwait(cnd, mtx, time)  rt_cond_wait_until(cnd, mtx, time)

RTAI_PROTO(int, rt_cond_signal,(CND *cnd))
{
	struct { CND *cnd; } arg = { cnd };
	return rtai_lxrt(BIDX, SIZARG, COND_SIGNAL, &arg).i[LOW];
}

RTAI_PROTO(int, rt_cond_wait,(CND *cnd, SEM  *mutex))
{
	struct { CND *cnd; SEM *mutex; } arg = { cnd, mutex };
	return rtai_lxrt(BIDX, SIZARG, COND_WAIT, &arg).i[LOW];
}

RTAI_PROTO(int, rt_cond_wait_until,(CND *cnd, SEM *mutex, RTIME time))
{
	struct { CND *cnd; SEM *mutex; RTIME time; } arg = { cnd, mutex, time };
	return rtai_lxrt(BIDX, SIZARG, COND_WAIT_UNTIL, &arg).i[LOW];
}

RTAI_PROTO(int, rt_cond_wait_timed,(CND *cnd, SEM *mutex, RTIME delay))
{
	struct { CND *cnd; SEM *mutex; RTIME delay; } arg = { cnd, mutex, delay };
	return rtai_lxrt(BIDX, SIZARG, COND_WAIT_TIMED, &arg).i[LOW];
}

#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __KERNEL__ */

#endif /* !_RTAI_SEM_H */