2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FBaseRtMutexGuard.h
20 * @brief This is the header file for the %MutexGuard class.
22 * This header file contains the declarations of the %MutexGuard class.
25 #ifndef _FBASE_RT_MUTEX_GUARD_H_
26 #define _FBASE_RT_MUTEX_GUARD_H_
28 #include <FBaseRtMutex.h>
29 #include <FBaseRtTypes.h>
31 namespace Tizen { namespace Base { namespace Runtime
36 * @brief This class is the RAII style class for %Mutex class.
40 * The following example demonstrates how to use this %MutexGuard class
45 * #include <FBaseRt.h>
47 * using namespace std;
48 * using namespace Tizen::Base::Runtime;
50 * class SynchronizedCounter
53 * explicit SynchronizedCounter(long long initialValue = 0)
55 * , __count(initialValue)
60 * SynchronizedCounter(const SynchronizedCounter& rhs)
62 * , __count(rhs.__count)
67 * SynchronizedCounter& operator =(SynchronizedCounter rhs)
69 * __count = rhs.__count;
73 * SynchronizedCounter& operator ++()
79 * SynchronizedCounter& operator --()
85 * bool TryToIncrement(void)
87 * return TryToIncrementImpl(1);
90 * bool TryToDecrement(void)
92 * return TryToIncrementImpl(-1);
95 * long long GetCount() const
101 * void IncrementImpl(int amount)
103 * MutexGuard lock(__m);
105 * } // The acquired lock will be released when going out of scope
107 * bool TryToIncrementImpl(int amount)
109 * MutexGuard lock(__m, Try); // Uses predefined Try const object for non-blocking mode locking
110 * TryReturn(lock.IsLocked(), false, “Failed to lock mutex”);
123 * static SynchronizedCounter counter;
124 * static const long long LOOP_COUNT = 10000000;
126 * virtual Object* Run(void)
128 * for (long long i = 0; i < LOOP_COUNT; ++i)
137 * SynchronizedCounter MyThread::counter;
140 * ButtonPanel::OnActionPerformed(const Tizen::Ui::Control& source, int actionId)
146 * static const int NUM_THREADS = 10;
147 * MyThread thrs[NUM_THREADS];
149 * __pLabel->SetText(L"Button is clicked!");
150 * AppLog("Button is pressed!");
152 * for (int i = 0; i < NUM_THREADS; ++i)
154 * thrs[i].Construct();
158 * for (int i = 0; i < NUM_THREADS; ++i)
163 * AppLog("Total Count = [%lld]", MyThread::counter.GetCount());
164 * AppAssertf(MyThread::counter.GetCount() == NUM_THREADS * MyThread::LOOP_COUNT,
165 * "[Assert] Count is wrong");
179 * This constructor acquires the lock in a blocking way.
183 * @param[in] m The %Mutex instance to be manipulated
184 * @remarks The specific error code can be accessed using the GetLastResult() method.
185 * @see Mutex::Acquire() for detailed exceptions
191 SetLastResult(Lock());
195 * This constructor acquires the lock in a non-blocking way.
199 * @param[in] m The %Mutex instance to be manipulated
200 * @remarks The specific error code can be accessed using the GetLastResult() method.
201 * @see Mutex::TryToAcquire() for detailed exceptions
203 MutexGuard(Mutex& m, TryTag)
207 SetLastResult(TryToLock());
211 * This destructor releases the lock if acquired when going out of a scope
215 * @remarks The specific error code can be accessed using the GetLastResult() method.
216 * @see Mutex::Release() for detailed exceptions
220 SetLastResult(Unlock());
224 * Returns whether this instance owns the lock on the given mutex at constructor.
228 * @return true if the lock is owned, @n
231 bool IsLocked(void) const
237 * Returns whether this instance owns the lock on the given mutex at constructor. @n
238 * Have same effects to calling IsLocked().
242 operator bool() const
248 * Acquires the lock manually on the given mutex at constructor in a blocking way
252 * @return An error code.
253 * @see Mutex::Acquire() for detailed exceptions
257 return SetLockedAndReturn(__m.Acquire());
261 * Acquires the lock manually on the given mutex at constructor in a non-blocking way
265 * @return An error code.
266 * @see Mutex::TryToAcquire() for detailed exceptions
268 result TryToLock(void)
270 return SetLockedAndReturn(__m.TryToAcquire());
274 * Releases the lock manually
278 * @return An error code.
279 * @see Mutex::Release() for detailed exceptions
283 result r = E_SUCCESS;
294 * The implementation of this copy constructor is intentionally blank and declared as private
295 * to prohibit copying of objects.
299 MutexGuard(const MutexGuard& rhs);
302 * The implementation of this copy assignment operator is intentionally blank and declared as private
303 * to prohibit copying of objects.
307 MutexGuard& operator =(const MutexGuard& rhs);
310 result SetLockedAndReturn(result r)
312 __locked = (r == E_SUCCESS);
321 }}} // Tizen::Base::Runtime
323 #endif // _FBASE_RT_MUTEX_GUARD_H_