Make UEFI boot-platform build again
[haiku.git] / headers / private / libroot / libroot_lock.h
blob597821fde79746fa3cbd9f9b2831fb204fd9d0e3
1 /*
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.
7 */
8 #ifndef _LIBROOT_LOCK_H
9 #define _LIBROOT_LOCK_H
11 #include <OS.h>
14 // TODO: Copied from the kernel private lock.h/lock.c. We should somehow make
15 // that code reusable.
18 namespace BPrivate {
21 typedef struct benaphore {
22 sem_id sem;
23 int32 count;
24 } benaphore;
27 static inline status_t
28 benaphore_init(benaphore *ben, const char *name)
30 if (ben == NULL || name == NULL)
31 return B_BAD_VALUE;
33 ben->count = 1;
34 ben->sem = create_sem(0, name);
35 if (ben->sem >= B_OK)
36 return B_OK;
38 return ben->sem;
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);
50 return B_OK;
54 static inline status_t
55 benaphore_lock(benaphore *ben)
57 if (atomic_add(&ben->count, -1) <= 0) {
58 status_t error;
59 do {
60 error = acquire_sem(ben->sem);
61 } while (error == B_INTERRUPTED);
63 return error;
66 return B_OK;
70 static inline status_t
71 benaphore_unlock(benaphore *ben)
73 if (atomic_add(&ben->count, 1) < 0)
74 return release_sem(ben->sem);
76 return B_OK;
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