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
13typedef struct lock lock;
14typedef struct mlock mlock;
15
16struct lock {
17	sem_id		s;
18	long		c;
19};
20
21struct mlock {
22	sem_id		s;
23};
24
25extern _IMPEXP_KERNEL int	new_lock(lock *l, const char *name);
26extern _IMPEXP_KERNEL int	free_lock(lock *l);
27
28static 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
35static 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
46extern _IMPEXP_KERNEL int	new_mlock(mlock *l, long c, const char *name);
47extern _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