xref: /haiku/src/apps/installer/SemaphoreLocker.h (revision 8e3916f8ef7f29c7fc15250c8d3f5ac510988116)
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 
12 class SemaphoreLocking {
13 public:
Lock(sem_id * lockable)14 	inline bool Lock(sem_id* lockable)
15 	{
16 		return acquire_sem(*lockable) == B_OK;
17 	}
18 
Unlock(sem_id * lockable)19 	inline void Unlock(sem_id* lockable)
20 	{
21 		release_sem(*lockable);
22 	}
23 };
24 
25 
26 class SemaphoreLocker : public AutoLocker<sem_id, SemaphoreLocking> {
27 public:
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 
37 private:
38 	sem_id	fSem;
39 };
40 
41 #endif // _SEMAPHORE_LOCKER_H
42