btrfs: [] on the end of a struct field is a variable length array.
[haiku.git] / headers / private / graphics / intel_extreme / lock.h
blob255e177f948d1feb89e17f062d978d5165e363f7
1 /*
2 * Copyright 2006, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Axel Dörfler, axeld@pinc-software.de
7 */
8 #ifndef LOCK_H
9 #define LOCK_H
12 #include <OS.h>
15 typedef struct lock {
16 sem_id sem;
17 int32 count;
18 } lock;
21 static inline status_t
22 init_lock(struct lock *lock, const char *name)
24 lock->sem = create_sem(0, name);
25 lock->count = 0;
27 return lock->sem < B_OK ? lock->sem : B_OK;
31 static inline void
32 uninit_lock(struct lock *lock)
34 delete_sem(lock->sem);
38 static inline status_t
39 acquire_lock(struct lock *lock)
41 if (atomic_add(&lock->count, 1) > 0)
42 return acquire_sem(lock->sem);
44 return B_OK;
48 static inline status_t
49 release_lock(struct lock *lock)
51 if (atomic_add(&lock->count, -1) > 1)
52 return release_sem(lock->sem);
54 return B_OK;
58 class Autolock {
59 public:
60 Autolock(struct lock &lock)
62 fLock(&lock)
64 fStatus = acquire_lock(fLock);
67 ~Autolock()
69 if (fStatus == B_OK)
70 release_lock(fLock);
73 bool
74 IsLocked() const
76 return fStatus == B_OK;
79 private:
80 status_t fStatus;
81 struct lock *fLock;
84 #endif /* LOCK_H */