2 * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
5 * Copyright 2001-2002, Travis Geiselbrecht. All rights reserved.
6 * Distributed under the terms of the NewOS License.
8 #ifndef _LIBROOT_LOCK_H
9 #define _LIBROOT_LOCK_H
14 // TODO: Copied from the kernel private lock.h/lock.c. We should somehow make
15 // that code reusable.
21 typedef struct benaphore
{
27 static inline status_t
28 benaphore_init(benaphore
*ben
, const char *name
)
30 if (ben
== NULL
|| name
== NULL
)
34 ben
->sem
= create_sem(0, name
);
42 static inline status_t
43 benaphore_lock_etc(benaphore
*ben
, uint32 flags
, bigtime_t timeout
)
45 // TODO: This function really shouldn't be used, since timeouts screw the
46 // benaphore behavior.
47 if (atomic_add(&ben
->count
, -1) <= 0)
48 return acquire_sem_etc(ben
->sem
, 1, flags
, timeout
);
54 static inline status_t
55 benaphore_lock(benaphore
*ben
)
57 if (atomic_add(&ben
->count
, -1) <= 0) {
60 error
= acquire_sem(ben
->sem
);
61 } while (error
== B_INTERRUPTED
);
70 static inline status_t
71 benaphore_unlock(benaphore
*ben
)
73 if (atomic_add(&ben
->count
, 1) < 0)
74 return release_sem(ben
->sem
);
80 } // namespace BPrivate
83 using BPrivate::benaphore
;
84 using BPrivate::benaphore_init
;
85 using BPrivate::benaphore_lock_etc
;
86 using BPrivate::benaphore_lock
;
87 using BPrivate::benaphore_unlock
;
90 #endif // _LIBROOT_LOCK_H