1/* 2 * Copyright 2008-2009, Stephan Aßmus <superstippi@gmx.de> 3 * All rights reserved. Distributed under the terms of the MIT License. 4 */ 5#ifndef _SEMAPHORE_LOCKER_H 6#define _SEMAPHORE_LOCKER_H 7 8 9#include "AutoLocker.h" 10 11 12class SemaphoreLocking { 13public: 14 inline bool Lock(sem_id* lockable) 15 { 16 return acquire_sem(*lockable) == B_OK; 17 } 18 19 inline void Unlock(sem_id* lockable) 20 { 21 release_sem(*lockable); 22 } 23}; 24 25 26class SemaphoreLocker : public AutoLocker<sem_id, SemaphoreLocking> { 27public: 28 inline SemaphoreLocker(sem_id semaphore, bool alreadyLocked = false, 29 bool lockIfNotLocked = true) 30 : 31 AutoLocker<sem_id, SemaphoreLocking>(), 32 fSem(semaphore) 33 { 34 SetTo(&fSem, alreadyLocked, lockIfNotLocked); 35 } 36 37private: 38 sem_id fSem; 39}; 40 41#endif // _SEMAPHORE_LOCKER_H 42