1/* 2 * Copyright 2007, Haiku, Inc. All Rights Reserved. 3 * Distributed under the terms of the MIT License. 4 * 5 * Documentation by: 6 * Niels Sascha Reedijk <niels.reedijk@gmail.com> 7 * Corresponds to: 8 * /trunk/headers/os/support/Autolock.h rev 19972 9 */ 10 11/*! 12 \file Autolock.h 13 \brief Implements a handy locking utility. 14*/ 15 16/*! 17 \class BAutolock 18 \ingroup support 19 \ingroup libbe 20 \brief Convenient utility to make parts of your code thread-safe easily. 21 22 The autolocker uses a BLooper or a BLocker in order to protect a part 23 of your code. This class is usually used in combination with a BLocker 24 that protects a certain part of your code and data that are being 25 accessed by multiple threads. While BAutolock does not add any features 26 to locking, it provides a mechanism to easily lock and protect a part of your 27 code. 28 29 Normally, when you need to protect data, you would have to make sure that 30 all your locks are paired with unlocks. Below is a simple example, but you 31 can imagine that there are more complex situations where you might spend a 32 lot of time debugging a hang because you didn't pair all the Lock()s with an 33 Unlock(). See the example: 34 35\code 36status_t 37Receiver::HandleCall(Call *call) 38{ 39 ... work on call data ... 40 41 fDataLocker->Lock() 42 43 ... perform changes ... 44 45 if (!success) 46 { 47 fDataLocker->Unlock(); 48 return B_ERROR; 49 } 50 51 fDataLocker->Unlock() 52 return B_OK; 53} 54\endcode 55 56 With the BAutolock this example can be rewritten as follows: 57 58\code 59status_t 60Receiver::HandleCall(Call *call) 61{ 62 ... work on call data ... 63 64 BAutolock autolock(fDataLocker); 65 66 ... perform changes ... 67 68 if (!success) 69 return B_ERROR; 70 71 return B_OK; 72} 73\endcode 74 75 Since the object is created on stack, it is destroyed as soon as we leave 76 the function. Because the destruction of the object causes it to unlock 77 the BLocker or BLooper, you don't have to manually make sure that every 78 exit from the function is properly unlocked. 79*/ 80 81/*! 82 \fn BAutolock::BAutolock(BLooper *looper) 83 \brief Create an object and lock the BLooper 84*/ 85 86/*! 87 \fn BAutolock::BAutolock(BLocker *locker) 88 \brief Create an object and lock the BLocker 89*/ 90 91/*! 92 \fn BAutolock::BAutolock(BLocker &locker) 93 \brief Create an object and lock the BLocker 94*/ 95 96/*! 97 \fn BAutolock::~BAutolock() 98 \brief Destroy the object and unlock the associated BLocker or BLooper 99*/ 100 101/*! 102 \fn bool BAutolock::IsLocked(void) 103 \brief Verify whether the associated BLocker or BLooper are actually locked. 104 105 Basically you may assume that when the object is created, you are 106 almost always sure the actual locking succeeds. It might fail if the 107 BLocker or BLooper are destroyed though. The semaphore will be 108 released and the Lock() call will fail. 109 110 If you expect this to happen, you can use this method to help you 111 protect yourself from any harm. 112 \retval true The lock was acquired. 113 \retval false Failed to acquire the lock. 114*/ 115