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 FBaseRtSemaphoreGuard.h
20 * @brief This is the header file for the %SemaphoreGuard class.
22 * This header file contains the declarations of the %SemaphoreGuard class.
25 #ifndef _FBASE_RT_SEMAPHORE_GUARD_H_
26 #define _FBASE_RT_SEMAPHORE_GUARD_H_
28 #include <FBaseRtSemaphore.h>
29 #include <FBaseRtTypes.h>
31 namespace Tizen { namespace Base { namespace Runtime
35 * @class SemaphoreGuard
36 * @brief This class is the RAII style class for %Semaphore class.
44 * This constructor acquires a semaphore count in a blocking way.
48 * @param[in] s The %Semaphore instance to be manipulated
49 * @remarks The specific error code can be accessed using the GetLastResult() method.
50 * @see Semaphore::Acquire() for detailed exceptions
52 SemaphoreGuard(Semaphore& s)
56 SetLastResult(Acquire());
60 * This constructor acquires a semaphore count in a non-blocking way.
64 * @param[in] s The %Semaphore instance to be manipulated
65 * @remarks The specific error code can be accessed using the GetLastResult() method.
66 * @see Semaphore::TryToAcquire() for detailed exceptions
68 SemaphoreGuard(Semaphore& s, TryTag)
72 SetLastResult(TryToAcquire());
76 * This destructor releases the acquired semaphore count when going out of a scope
80 * @remarks The specific error code can be accessed using the GetLastResult() method.
81 * @see Semaphore::Release() for detailed exceptions
85 SetLastResult(Release());
89 * Returns whether this instance owns a semaphore count on the given semaphore at constructor.
93 * @return true if a semaphore is owned, @n
96 bool IsAcquired(void) const
102 * Returns whether this instance owns a semaphore count on the given semaphore at constructor. @n
103 * Have same effects to calling IsAcquired().
107 operator bool() const
113 * Acquires a semaphore count manually on the given semaphore at constructor in a blocking way
117 * @return An error code.
118 * @see Semaphore::Acquire() for detailed exceptions
122 return SetAcquiredAndReturn(__s.Acquire());
126 * Acquires a semaphore count manually on the given semaphore at constructor in a non-blocking way
130 * @return An error code.
131 * @see Semaphore::TryToAcquire() for detailed exceptions
133 result TryToAcquire(void)
135 return SetAcquiredAndReturn(__s.TryToAcquire());
139 * Releases the acquired semaphore count manually
143 * @return An error code.
144 * @see Semaphore::Release() for detailed exceptions
148 result r = E_SUCCESS;
159 * The implementation of this copy constructor is intentionally blank and declared as private
160 * to prohibit copying of objects.
164 SemaphoreGuard(const SemaphoreGuard& rhs);
167 * The implementation of this copy assignment operator is intentionally blank and declared as private
168 * to prohibit copying of objects.
172 SemaphoreGuard& operator =(const SemaphoreGuard& rhs);
175 result SetAcquiredAndReturn(result r)
177 __acquired = (r == E_SUCCESS);
186 }}} // Tizen::Base::Runtime
188 #endif // _FBASE_RT_SEMAPHORE_GUARD_H_