2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
4 // Licensed under the Apache License, Version 2.0 (the License);
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
8 // http://www.apache.org/licenses/LICENSE-2.0
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
18 * @file FBaseRtMutexGuard.h
19 * @brief This is the header file for the %MutexGuard class.
21 * This header file contains the declarations of the %MutexGuard class.
24 #ifndef _FBASE_RT_MUTEX_GUARD_H_
25 #define _FBASE_RT_MUTEX_GUARD_H_
27 #include <FBaseRtMutex.h>
28 #include <FBaseRtTypes.h>
30 namespace Tizen { namespace Base { namespace Runtime
35 * @brief This class is the RAII style class for %Mutex class.
39 * The following example demonstrates how to use this %MutexGuard class
44 * #include <FBaseRt.h>
46 * using namespace std;
47 * using namespace Tizen::Base::Runtime;
49 * class SynchronizedCounter
52 * explicit SynchronizedCounter(long long initialValue = 0)
54 * , __count(initialValue)
59 * SynchronizedCounter(const SynchronizedCounter& rhs)
61 * , __count(rhs.__count)
66 * SynchronizedCounter& operator =(SynchronizedCounter rhs)
68 * __count = rhs.__count;
72 * SynchronizedCounter& operator ++()
78 * SynchronizedCounter& operator --()
84 * bool TryToIncrement(void)
86 * return TryToIncrementImpl(1);
89 * bool TryToDecrement(void)
91 * return TryToIncrementImpl(-1);
94 * long long GetCount() const
100 * void IncrementImpl(int amount)
102 * MutexGuard lock(__m);
104 * } // The acquired lock will be released when going out of scope
106 * bool TryToIncrementImpl(int amount)
108 * MutexGuard lock(__m, Try); // Uses predefined Try const object for non-blocking mode locking
109 * TryReturn(lock.IsLocked(), false, “Failed to lock mutex”);
122 * static SynchronizedCounter counter;
123 * static const long long LOOP_COUNT = 10000000;
125 * virtual Object* Run(void)
127 * for (long long i = 0; i < LOOP_COUNT; ++i)
136 * SynchronizedCounter MyThread::counter;
139 * ButtonPanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
145 * static const int NUM_THREADS = 10;
146 * MyThread thrs[NUM_THREADS];
148 * __pLabel->SetText(L"Button is clicked!");
149 * AppLog("Button is pressed!");
151 * for (int i = 0; i < NUM_THREADS; ++i)
153 * thrs[i].Construct();
157 * for (int i = 0; i < NUM_THREADS; ++i)
162 * AppLog("Total Count = [%lld]", MyThread::counter.GetCount());
163 * AppAssertf(MyThread::counter.GetCount() == NUM_THREADS * MyThread::LOOP_COUNT,
164 * "[Assert] Count is wrong");
178 * This constructor acquires the lock in a blocking way.
182 * @param[in] m The %Mutex instance to be manipulated
183 * @remarks The specific error code can be accessed using the GetLastResult() method.
184 * @see Mutex::Acquire() for detailed exceptions
190 SetLastResult(Lock());
194 * This constructor acquires the lock in a non-blocking way.
198 * @param[in] m The %Mutex instance to be manipulated
199 * @remarks The specific error code can be accessed using the GetLastResult() method.
200 * @see Mutex::TryToAcquire() for detailed exceptions
202 MutexGuard(Mutex& m, TryTag)
206 SetLastResult(TryToLock());
210 * This destructor releases the lock if acquired when going out of a scope
214 * @remarks The specific error code can be accessed using the GetLastResult() method.
215 * @see Mutex::Release() for detailed exceptions
219 SetLastResult(Unlock());
223 * Returns whether this instance owns the lock on the given mutex at constructor.
227 * @return true if the lock is owned, @n
230 bool IsLocked(void) const
236 * Returns whether this instance owns the lock on the given mutex at constructor. @n
237 * Have same effects to calling IsLocked().
241 operator bool() const
247 * Acquires the lock manually on the given mutex at constructor in a blocking way
251 * @return An error code.
252 * @see Mutex::Acquire() for detailed exceptions
256 return SetLockedAndReturn(__m.Acquire());
260 * Acquires the lock manually on the given mutex at constructor in a non-blocking way
264 * @return An error code.
265 * @see Mutex::TryToAcquire() for detailed exceptions
267 result TryToLock(void)
269 return SetLockedAndReturn(__m.TryToAcquire());
273 * Releases the lock manually
277 * @return An error code.
278 * @see Mutex::Release() for detailed exceptions
282 result r = E_SUCCESS;
293 * The implementation of this copy constructor is intentionally blank and declared as private
294 * to prohibit copying of objects.
298 MutexGuard(const MutexGuard& rhs);
301 * The implementation of this copy assignment operator is intentionally blank and declared as private
302 * to prohibit copying of objects.
306 MutexGuard& operator =(const MutexGuard& rhs);
309 result SetLockedAndReturn(result r)
311 __locked = (r == E_SUCCESS);
320 }}} // Tizen::Base::Runtime
322 #endif // _FBASE_RT_MUTEX_GUARD_H_