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 FBaseRtSemaphoreGuard.h
19 * @brief This is the header file for the %SemaphoreGuard class.
21 * This header file contains the declarations of the %SemaphoreGuard class.
24 #ifndef _FBASE_RT_SEMAPHORE_GUARD_H_
25 #define _FBASE_RT_SEMAPHORE_GUARD_H_
27 #include <FBaseRtSemaphore.h>
28 #include <FBaseRtTypes.h>
30 namespace Tizen { namespace Base { namespace Runtime
34 * @class SemaphoreGuard
35 * @brief This class is the RAII style class for %Semaphore class.
43 * This constructor acquires a semaphore count in a blocking way.
47 * @param[in] s The %Semaphore instance to be manipulated
48 * @remarks The specific error code can be accessed using the GetLastResult() method.
49 * @see Semaphore::Acquire() for detailed exceptions
51 SemaphoreGuard(Semaphore& s)
55 SetLastResult(Acquire());
59 * This constructor acquires a semaphore count in a non-blocking way.
63 * @param[in] s The %Semaphore instance to be manipulated
64 * @remarks The specific error code can be accessed using the GetLastResult() method.
65 * @see Semaphore::TryToAcquire() for detailed exceptions
67 SemaphoreGuard(Semaphore& s, TryTag)
71 SetLastResult(TryToAcquire());
75 * This destructor releases the acquired semaphore count when going out of a scope
79 * @remarks The specific error code can be accessed using the GetLastResult() method.
80 * @see Semaphore::Release() for detailed exceptions
84 SetLastResult(Release());
88 * Returns whether this instance owns a semaphore count on the given semaphore at constructor.
92 * @return true if a semaphore is owned, @n
95 bool IsAcquired(void) const
101 * Returns whether this instance owns a semaphore count on the given semaphore at constructor. @n
102 * Have same effects to calling IsAcquired().
106 operator bool() const
112 * Acquires a semaphore count manually on the given semaphore at constructor in a blocking way
116 * @return An error code.
117 * @see Semaphore::Acquire() for detailed exceptions
121 return SetAcquiredAndReturn(__s.Acquire());
125 * Acquires a semaphore count manually on the given semaphore at constructor in a non-blocking way
129 * @return An error code.
130 * @see Semaphore::TryToAcquire() for detailed exceptions
132 result TryToAcquire(void)
134 return SetAcquiredAndReturn(__s.TryToAcquire());
138 * Releases the acquired semaphore count manually
142 * @return An error code.
143 * @see Semaphore::Release() for detailed exceptions
147 result r = E_SUCCESS;
158 * The implementation of this copy constructor is intentionally blank and declared as private
159 * to prohibit copying of objects.
163 SemaphoreGuard(const SemaphoreGuard& rhs);
166 * The implementation of this copy assignment operator is intentionally blank and declared as private
167 * to prohibit copying of objects.
171 SemaphoreGuard& operator =(const SemaphoreGuard& rhs);
174 result SetAcquiredAndReturn(result r)
176 __acquired = (r == E_SUCCESS);
185 }}} // Tizen::Base::Runtime
187 #endif // _FBASE_RT_SEMAPHORE_GUARD_H_