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 FBaseRtMutex.h
19 * @brief This is the header file for the %Mutex class.
21 * This header file contains the declarations of the %Mutex class.
24 #ifndef _FBASE_RT_MUTEX_H_
25 #define _FBASE_RT_MUTEX_H_
27 #include <FBaseResult.h>
28 #include <FBaseString.h>
32 namespace Tizen { namespace Base { namespace Runtime
35 * @struct NonRecursiveMutexTag
37 * This struct is used only for providing the non-recursive type of the mutex.
38 * A non-recursive mutex is a mutex that cannot be acquired multiple times by the locking thread. It returns an exception if the locking thread tries to lock the same mutex.
44 struct NonRecursiveMutexTag
49 * This is a constant instance of NonRecursiveMutexTag.
53 * result r = m.Create(NonRecursiveMutex);
56 static const NonRecursiveMutexTag NonRecursiveMutex = {};
60 * @brief This class represents a mutex; a type of synchronization mechanism.
64 * @final This class is not intended for extension.
65 * The %Mutex class represents a mutex; a type of synchronization mechanism.
66 * It is a binary semaphore. Only one thread can acquire the %Mutex.
68 * For more information on the class features, see <a href="../org.tizen.native.appprogramming/html/guide/base/mutex_and_semaphore.htm">Mutex and Semaphore</a>.
73 * The following example demonstrates how to use the %Mutex class.
78 * using namespace Tizen::Base::Runtime;
86 * void UpdateCriticalResource();
93 * MyMutexApp::MyMutexApp()
99 * MyMutexApp::~MyMutexApp()
104 * MyMutexApp::UpdateCriticalResource()
115 class _OSP_EXPORT_ Mutex
116 : public Tizen::Base::Object
120 * This is the default constructor for this class.
124 * @remarks After creating an instance of this class, one of
125 * the Create() methods must be called explicitly to initialize this instance.
130 * This is the destructor for this class.
134 virtual ~Mutex(void);
137 * Creates an unnamed mutex which is a recursive mutex that can be acquired multiple times by the locking thread.
138 * It keeps the number of locks and you must unlock it the same number of times.
142 * @return An error code
143 * @exception E_SUCCESS The method is successful.
144 * @exception E_OUT_OF_MEMORY The memory is insufficient.
145 * @exception E_SYSTEM A system error has occurred.
150 * Creates a non-recursive mutex.
154 * @return An error code
155 * @exception E_SUCCESS The method is successful.
156 * @exception E_SYSTEM A system error has occurred.
158 result Create(NonRecursiveMutexTag);
161 * Creates a named mutex which is a recursive mutex that can be acquired multiple times by the locking thread.
162 * It keeps the number of locks and you must unlock it the same number of times. @n
163 * If a mutex with the specified name already exists, this creates a mutex which references that particular mutex.
167 * @return An error code
168 * @param[in] name The name of the mutex
169 * @exception E_SUCCESS The method is successful.
170 * @exception E_OUT_OF_MEMORY The memory is insufficient.
171 * @exception E_SYSTEM A system error has occurred.
173 result Create(const Tizen::Base::String& name);
176 * Releases the mutex.
180 * @return An error code
181 * @exception E_SUCCESS The method is successful.
182 * @exception E_SYSTEM A system error has occurred.
183 * @remarks This method should be called only after acquiring a lock by calling Acquire() or TryToAcquire().
185 result Release(void);
188 * Acquires the mutex if it is not acquired. @n
189 * If the mutex is already acquired by another thread,
190 * the current thread is blocked until the mutex is released.
194 * @return An error code
195 * @exception E_SUCCESS The method is successful.
196 * @exception E_SYSTEM A system error has occurred.
197 * @remarks This method blocks if called on an already locked monitor object until the mutex becomes available.
199 result Acquire(void);
202 * Tries to acquire the mutex if it is not acquired. @n
203 * If the mutex is already acquired by another thread, @c E_OBJECT_LOCKED is returned.
207 * @return An error code
208 * @exception E_SUCCESS The method is successful.
209 * @exception E_OBJECT_LOCKED The mutex is already locked.
210 * @exception E_SYSTEM A system error has occurred.
211 * @remarks This method blocks if called on an already locked monitor object until the mutex becomes available.
213 result TryToAcquire(void);
216 Mutex(const Mutex& value);
217 Mutex& operator =(const Mutex& value);
220 friend class _MutexImpl;
221 class _MutexImpl * __pMutexImpl;
224 } } } // Tizen::Base::Runtime
227 #endif // _FBASE_RT_MUTEX_H_