1a90ebd77SClemens Zeidler /* 2a90ebd77SClemens Zeidler * Copyright 2006, Haiku, Inc. All Rights Reserved. 3a90ebd77SClemens Zeidler * Distributed under the terms of the MIT License. 4a90ebd77SClemens Zeidler * 5a90ebd77SClemens Zeidler * Authors: 6a90ebd77SClemens Zeidler * Axel Dörfler, axeld@pinc-software.de 7a90ebd77SClemens Zeidler */ 8a90ebd77SClemens Zeidler #ifndef LOCK_H 9a90ebd77SClemens Zeidler #define LOCK_H 10a90ebd77SClemens Zeidler 11a90ebd77SClemens Zeidler 12a90ebd77SClemens Zeidler #include <OS.h> 13a90ebd77SClemens Zeidler 14a90ebd77SClemens Zeidler 15a90ebd77SClemens Zeidler typedef struct lock { 16a90ebd77SClemens Zeidler sem_id sem; 17*73ad2473SPawel Dziepak int32 count; 18a90ebd77SClemens Zeidler } lock; 19a90ebd77SClemens Zeidler 20a90ebd77SClemens Zeidler 21a90ebd77SClemens Zeidler static inline status_t init_lock(struct lock * lock,const char * name)22a90ebd77SClemens Zeidlerinit_lock(struct lock *lock, const char *name) 23a90ebd77SClemens Zeidler { 24a90ebd77SClemens Zeidler lock->sem = create_sem(0, name); 25a90ebd77SClemens Zeidler lock->count = 0; 26a90ebd77SClemens Zeidler 27a90ebd77SClemens Zeidler return lock->sem < B_OK ? lock->sem : B_OK; 28a90ebd77SClemens Zeidler } 29a90ebd77SClemens Zeidler 30a90ebd77SClemens Zeidler 31a90ebd77SClemens Zeidler static inline void uninit_lock(struct lock * lock)32a90ebd77SClemens Zeidleruninit_lock(struct lock *lock) 33a90ebd77SClemens Zeidler { 34a90ebd77SClemens Zeidler delete_sem(lock->sem); 35a90ebd77SClemens Zeidler } 36a90ebd77SClemens Zeidler 37a90ebd77SClemens Zeidler 38a90ebd77SClemens Zeidler static inline status_t acquire_lock(struct lock * lock)39a90ebd77SClemens Zeidleracquire_lock(struct lock *lock) 40a90ebd77SClemens Zeidler { 41a90ebd77SClemens Zeidler if (atomic_add(&lock->count, 1) > 0) 42a90ebd77SClemens Zeidler return acquire_sem(lock->sem); 43a90ebd77SClemens Zeidler 44a90ebd77SClemens Zeidler return B_OK; 45a90ebd77SClemens Zeidler } 46a90ebd77SClemens Zeidler 47a90ebd77SClemens Zeidler 48a90ebd77SClemens Zeidler static inline status_t release_lock(struct lock * lock)49a90ebd77SClemens Zeidlerrelease_lock(struct lock *lock) 50a90ebd77SClemens Zeidler { 51a90ebd77SClemens Zeidler if (atomic_add(&lock->count, -1) > 1) 52a90ebd77SClemens Zeidler return release_sem(lock->sem); 53a90ebd77SClemens Zeidler 54a90ebd77SClemens Zeidler return B_OK; 55a90ebd77SClemens Zeidler } 56a90ebd77SClemens Zeidler 57a90ebd77SClemens Zeidler 58a90ebd77SClemens Zeidler class Autolock { 59a90ebd77SClemens Zeidler public: Autolock(struct lock & lock)60a90ebd77SClemens Zeidler Autolock(struct lock &lock) 61a90ebd77SClemens Zeidler : 62a90ebd77SClemens Zeidler fLock(&lock) 63a90ebd77SClemens Zeidler { 64a90ebd77SClemens Zeidler fStatus = acquire_lock(fLock); 65a90ebd77SClemens Zeidler } 66a90ebd77SClemens Zeidler ~Autolock()67a90ebd77SClemens Zeidler ~Autolock() 68a90ebd77SClemens Zeidler { 69a90ebd77SClemens Zeidler if (fStatus == B_OK) 70a90ebd77SClemens Zeidler release_lock(fLock); 71a90ebd77SClemens Zeidler } 72a90ebd77SClemens Zeidler 73a90ebd77SClemens Zeidler bool IsLocked()74a90ebd77SClemens Zeidler IsLocked() const 75a90ebd77SClemens Zeidler { 76a90ebd77SClemens Zeidler return fStatus == B_OK; 77a90ebd77SClemens Zeidler } 78a90ebd77SClemens Zeidler 79a90ebd77SClemens Zeidler private: 80a90ebd77SClemens Zeidler status_t fStatus; 81a90ebd77SClemens Zeidler struct lock *fLock; 82a90ebd77SClemens Zeidler }; 83a90ebd77SClemens Zeidler 84f8af3174SAlexander von Gluck IV 85a90ebd77SClemens Zeidler #endif /* LOCK_H */ 86