xref: /haiku/headers/private/userlandfs/legacy/lock.h (revision 83812f67529c88d4fb4b942162a9f21142e683fa)
1 /*
2 	Copyright 1999-2001, Be Incorporated.   All Rights Reserved.
3 	This file may be used under the terms of the Be Sample Code License.
4 */
5 
6 #ifndef _LOCK_H
7 #define _LOCK_H
8 
9 #include <BeBuild.h>
10 
11 #include <OS.h>
12 
13 #ifndef _IMPEXP_KERNEL
14 #define _IMPEXP_KERNEL
15 #endif
16 
17 #ifdef __cplusplus
18 	extern "C" {
19 #else
20 	typedef struct lock lock;
21 	typedef struct mlock mlock;
22 #endif
23 
24 
25 struct lock {
26 	sem_id		s;
27 	long		c;
28 };
29 
30 struct mlock {
31 	sem_id		s;
32 };
33 
34 extern _IMPEXP_KERNEL int	new_lock(lock *l, const char *name);
35 extern _IMPEXP_KERNEL int	free_lock(lock *l);
36 
37 #ifdef LOCK
38 #undef LOCK
39 #endif
40 
41 #define	LOCK(l)		if (atomic_add(&l.c, -1) <= 0) acquire_sem(l.s);
42 #define	UNLOCK(l)	if (atomic_add(&l.c, 1) < 0) release_sem(l.s);
43 
44 extern _IMPEXP_KERNEL int	new_mlock(mlock *l, long c, const char *name);
45 extern _IMPEXP_KERNEL int	free_mlock(mlock *l);
46 
47 #define		LOCKM(l,cnt)	acquire_sem_etc(l.s, cnt, 0, 0)
48 #define		UNLOCKM(l,cnt)	release_sem_etc(l.s, cnt, 0)
49 
50 
51 #ifdef __cplusplus
52   } // extern "C"
53 #endif
54 
55 #endif
56