2 * Copyright 2007-2014 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Niels Sascha Reedijk, niels.reedijk@gmail.com
7 * John Scipione, jscipione@gmail.com
10 * headers/os/support/Autolock.h rev 33370
18 \brief Implements a handy locking utility.
26 \brief Convenient utility to make parts of your code thread-safe easily.
28 The autolocker uses a BLooper or a BLocker in order to protect a part
29 of your code. This class is usually used in combination with a BLocker
30 that protects a certain part of your code and data that are being
31 accessed by multiple threads. While BAutolock does not add any features
32 to locking, it provides a mechanism to easily lock and protect a part of
35 Normally, when you need to protect data, you would have to make sure that
36 all your locks are paired with unlocks. Below is a simple example, but you
37 can imagine that there are more complex situations where you might spend a
38 lot of time debugging a hang because you didn't pair all the Lock()s with
39 an Unlock(). See the example:
43 Receiver::HandleCall(Call *call)
45 ... work on call data ...
49 ... perform changes ...
52 fDataLocker->Unlock();
61 With the BAutolock this example can be rewritten as follows:
65 Receiver::HandleCall(Call *call)
67 ... work on call data ...
69 BAutolock autolock(fDataLocker);
71 ... perform changes ...
80 Since the object is created on stack, it is destroyed as soon as we leave
81 the function. Because the destruction of the object causes it to unlock
82 the BLocker or BLooper, you don't have to manually make sure that every
83 exit from the function is properly unlocked.
90 \fn BAutolock::BAutolock(BLooper* looper)
91 \brief Create an object and lock the BLooper
98 \fn BAutolock::BAutolock(BLocker* locker)
99 \brief Create an object and lock the BLocker
106 \fn BAutolock::BAutolock(BLocker& locker)
107 \brief Create an object and lock the BLocker
114 \fn BAutolock::~BAutolock()
115 \brief Destroy the object and unlock the associated BLocker or BLooper
122 \fn bool BAutolock::IsLocked()
123 \brief Verify whether the associated BLocker or BLooper are actually
126 Basically you may assume that when the object is created, you are
127 almost always sure the actual locking succeeds. It might fail if the
128 BLocker or BLooper are destroyed though. The semaphore will be
129 released and the Lock() call will fail.
131 If you expect this to happen, you can use this method to help you
132 protect yourself from any harm.
134 \return Whether or not the BLocker or BLooper is locked.
135 \retval true The lock was acquired.
136 \retval false Failed to acquire the lock.
143 \fn bool BAutolock::Lock()
144 \brief Lock the BAutolock if it has not already happened
146 Note that unlike BLocker, the object is not locked with lock count. That
147 means that if the lock is already taken, this method returns \c true
150 \return Whether or not the BLocker or BLooper was locked.
151 \retval true The lock was acquired (or had already been acquired).
152 \retval false Failed to acquire the lock.
159 \fn void BAutolock::Unlock()
160 \brief Unlock the BAutolock if the lock is being held.
162 If the lock is not held, the method does nothing.