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 FBaseRtMutex.h
20 * @brief This is the header file for the %Mutex class.
22 * This header file contains the declarations of the %Mutex class.
25 #ifndef _FBASE_RT_MUTEX_H_
26 #define _FBASE_RT_MUTEX_H_
28 #include <FBaseResult.h>
29 #include <FBaseString.h>
33 namespace Tizen { namespace Base { namespace Runtime
36 * @struct NonRecursiveMutexTag
38 * This struct is used only for providing the non-recursive type of mutex.
39 * A non-recursive mutex is a mutex that can not be acquired multiple times by the locking thread. It returns an exception if the locking thread tries to lock the same mutex.
45 struct NonRecursiveMutexTag
50 * This is a constant instance of NonRecursiveMutexTag.
54 * result r = m.Create(NonRecursiveMutex);
57 static const NonRecursiveMutexTag NonRecursiveMutex = {};
61 * @brief This class represents a mutex; a type of synchronization mechanism.
65 * @final This class is not intended for extension.
66 * The %Mutex class represents a mutex; a type of synchronization mechanism.
67 * It is a binary semaphore. Only one thread can acquire the %Mutex.
69 * 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>.
74 * The following example demonstrates how to use the %Mutex class.
79 * using namespace Tizen::Base::Runtime;
87 * void UpdateCriticalResource();
94 * MyMutexApp::MyMutexApp()
100 * MyMutexApp::~MyMutexApp()
105 * MyMutexApp::UpdateCriticalResource()
116 class _OSP_EXPORT_ Mutex
117 : public Tizen::Base::Object
121 * This is the default constructor for this class.
125 * @remarks After creating an instance of this class, one of
126 * the Create() methods must be called explicitly to initialize this instance.
131 * This is the destructor for this class.
135 virtual ~Mutex(void);
138 * Creates an unnamed mutex which is a recursive mutex that can be acquired multiple times by the locking thread.
139 * It keeps the number of getting locked and you must unlock it the same number of times.
143 * @return An error code
144 * @exception E_SUCCESS The method is successful.
145 * @exception E_OUT_OF_MEMORY The memory is insufficient.
146 * @exception E_SYSTEM A system error has occurred.
151 * Creates a non-recursive mutex.
155 * @return An error code
156 * @exception E_SUCCESS The method is successful.
157 * @exception E_SYSTEM A system error has occurred.
159 result Create(NonRecursiveMutexTag);
162 * Creates a named mutex which is a recursive mutex that can be acquired multiple times by the locking thread.
163 * It keeps the number of getting locked and you must unlock it the same number of times. @n
164 * If a mutex with the specified name already exists, this creates a mutex which references that particular mutex.
168 * @return An error code
169 * @param[in] name The name of the mutex
170 * @exception E_SUCCESS The method is successful.
171 * @exception E_OUT_OF_MEMORY The memory is insufficient.
172 * @exception E_SYSTEM A system error has occurred.
174 result Create(const Tizen::Base::String& name);
177 * Releases the mutex.
181 * @return An error code
182 * @exception E_SUCCESS The method is successful.
183 * @exception E_SYSTEM A system error has occurred.
184 * @remarks This method should be called only after acquiring lock by Acquire()/ TryToAcquire() call
186 result Release(void);
189 * Acquires the mutex if it is not acquired. @n
190 * If the mutex is already acquired by another thread,
191 * the current thread is blocked until the mutex is released.
195 * @return An error code
196 * @exception E_SUCCESS The method is successful.
197 * @exception E_SYSTEM A system error has occurred.
198 * @remarks This method will block if called on already locked monitor object until mutex becomes availalbe.
200 result Acquire(void);
203 * Tries to acquire the mutex if it is not acquired. @n
204 * If the mutex is already acquired by another thread, E_OBJECT_LOCKED is returned.
208 * @return An error code
209 * @exception E_SUCCESS The method is successful.
210 * @exception E_OBJECT_LOCKED The mutex is already locked.
211 * @exception E_SYSTEM A system error has occurred.
212 * @remarks This method will block if called on already locked monitor object until mutex becomes availalbe.
214 result TryToAcquire(void);
217 Mutex(const Mutex& value);
218 Mutex& operator =(const Mutex& value);
221 friend class _MutexImpl;
222 class _MutexImpl * __pMutexImpl;
225 } } } // Tizen::Base::Runtime
228 #endif // _FBASE_RT_MUTEX_H_