xref: /haiku/src/add-ons/kernel/file_systems/websearchfs/lock.h (revision ed24eb5ff12640d052171c6a7feba37fab8a75d1)
1 /*
2 	Copyright 1999, 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 typedef struct lock lock;
14 typedef struct mlock mlock;
15 
16 struct lock {
17 	sem_id		s;
18 	long		c;
19 };
20 
21 struct mlock {
22 	sem_id		s;
23 };
24 
25 extern _IMPEXP_KERNEL int	new_lock(lock *l, const char *name);
26 extern _IMPEXP_KERNEL int	free_lock(lock *l);
27 
28 static inline status_t LOCK(lock *l)
29 {
30 	if (atomic_add(&(l->c), -1) <= 0)
31 		return acquire_sem(l->s);
32 	return B_OK;
33 }
34 
35 static inline status_t UNLOCK(lock *l)
36 {
37 	if (atomic_add(&(l->c), 1) < 0)
38 		return release_sem(l->s);
39 	return B_OK;
40 }
41 
42 
43 //#define	LOCK(l)		if (atomic_add(&l.c, -1) <= 0) acquire_sem(l.s);
44 //#define	UNLOCK(l)	if (atomic_add(&l.c, 1) < 0) release_sem(l.s);
45 
46 extern _IMPEXP_KERNEL int	new_mlock(mlock *l, long c, const char *name);
47 extern _IMPEXP_KERNEL int	free_mlock(mlock *l);
48 
49 #define		LOCKM(l,cnt)	acquire_sem_etc(l.s, cnt, 0, 0)
50 #define		UNLOCKM(l,cnt)	release_sem_etc(l.s, cnt, 0)
51 
52 #endif
53