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