1 /* 2 * Copyright 2001-2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 */ 5 #ifndef _SUPPORT_AUTOLOCK_H 6 #define _SUPPORT_AUTOLOCK_H 7 8 9 #include <Locker.h> 10 #include <Looper.h> 11 12 13 class BAutolock { 14 public: 15 inline BAutolock(BLooper *looper); 16 inline BAutolock(BLocker *locker); 17 inline BAutolock(BLocker &locker); 18 inline ~BAutolock(); 19 20 inline bool IsLocked(void); 21 22 private: 23 BLocker *fLocker; 24 BLooper *fLooper; 25 bool fIsLocked; 26 }; 27 28 29 inline 30 BAutolock::BAutolock(BLooper *looper) 31 : fLocker(NULL), fLooper(looper), fIsLocked(looper->Lock()) 32 { 33 } 34 35 36 inline 37 BAutolock::BAutolock(BLocker *locker) 38 : fLocker(locker), fLooper(NULL), fIsLocked(locker->Lock()) 39 { 40 } 41 42 43 inline 44 BAutolock::BAutolock(BLocker &locker) 45 : fLocker(&locker), fLooper(NULL), fIsLocked(locker.Lock()) 46 { 47 } 48 49 50 inline 51 BAutolock::~BAutolock() 52 { 53 if (fIsLocked) { 54 if (fLooper != NULL) 55 fLooper->Unlock(); 56 else 57 fLocker->Unlock(); 58 } 59 } 60 61 62 inline bool 63 BAutolock::IsLocked() 64 { 65 return fIsLocked; 66 } 67 68 #endif // _SUPPORT_AUTOLOCK_H 69