xref: /haiku/headers/build/os/support/Autolock.h (revision 4f00613311d0bd6b70fa82ce19931c41f071ea4e)
1 //
2 //	$Id: Autolock.h 3246 2003-05-14 17:21:46Z haydentech $
3 //
4 //	This is the BAutolock interface for OpenBeOS.  It has been created to
5 //	be source and binary compatible with the BeOS version of BAutolock.
6 //  To that end, all members are inline just as with the BeOS version.
7 //
8 
9 
10 #ifndef	_OPENBEOS_AUTOLOCK_H
11 #define	_OPENBEOS_AUTOLOCK_H
12 
13 
14 #include <Locker.h>
15 #include <Looper.h>
16 
17 
18 #ifdef USE_OPENBEOS_NAMESPACE
19 namespace OpenBeOS {
20 #endif
21 
22 class BAutolock {
23 public:
24 	inline BAutolock(BLooper *looper);
25 	inline BAutolock(BLocker *locker);
26 	inline BAutolock(BLocker &locker);
27 
28 	inline ~BAutolock();
29 
30 	inline bool IsLocked(void);
31 
32 private:
33 	BLocker *fTheLocker;
34 	BLooper *fTheLooper;
35 	bool	fIsLocked;
36 };
37 
38 
39 inline BAutolock::BAutolock(BLooper *looper) :
40 	fTheLocker(NULL), fTheLooper(looper), fIsLocked(looper->Lock())
41 {
42 }
43 
44 
45 inline BAutolock::BAutolock(BLocker *locker) :
46 	fTheLocker(locker), fTheLooper(NULL), fIsLocked(locker->Lock())
47 {
48 }
49 
50 
51 inline BAutolock::BAutolock(BLocker &locker) :
52 	fTheLocker(&locker), fTheLooper(NULL), fIsLocked(locker.Lock())
53 {
54 }
55 
56 
57 inline BAutolock::~BAutolock()
58 {
59 	if (fIsLocked) {
60 		if (fTheLooper != NULL) {
61 			fTheLooper->Unlock();
62 		} else {
63 			fTheLocker->Unlock();
64 		}
65 	}
66 }
67 
68 
69 inline bool BAutolock::IsLocked()
70 {
71 	return fIsLocked;
72 }
73 
74 
75 #ifdef USE_OPENBEOS_NAMESPACE
76 }
77 #endif
78 
79 #endif // _OPENBEOS_LOCKER_H
80