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 FBaseRt_SemaphoreImpl.cpp
19 * @brief This is the implementation file for the _SemaphoreImpl class.
23 #include <semaphore.h>
28 #include "FBase_NativeError.h"
29 #include <FBaseSysLog.h>
30 #include "FBaseRt_SemaphoreImpl.h"
32 namespace Tizen { namespace Base { namespace Runtime
35 _SemaphoreImpl::_SemaphoreImpl(void)
43 _SemaphoreImpl::~_SemaphoreImpl(void)
45 if (__pSemaphore != null)
47 sem_destroy(__pSemaphore);
55 _SemaphoreImpl::Create(int count)
57 return Create(L"", count);
61 _SemaphoreImpl::Create(const Tizen::Base::String& name, int count)
65 sem_t* pSemaphore = null;
67 SysTryReturnResult(NID_BASE_RT, count >= 0, E_INVALID_ARG,
68 "The count(%d) MUST be greater than or equal to 0.", count);
70 pSemaphore = (sem_t*) malloc(sizeof(sem_t));
71 SysTryReturnResult(NID_BASE_RT, pSemaphore != null, E_OUT_OF_MEMORY,
72 "Semaphore memory allocation failed.");
74 ret = sem_init(pSemaphore, 0, count);
75 SysTryCatch(NID_BASE_RT, ret == 0, r = E_SYSTEM, E_SYSTEM,
76 "[E_SYSTEM] Semaphore intialization failed : [%s]", __ConvertNativeErrorToMessage(errno));
78 if (name.GetLength() > 0)
84 __pSemaphore = pSemaphore;
95 _SemaphoreImpl::Acquire(long timeout)
97 SysTryReturnResult(NID_BASE_RT, __pSemaphore != null, E_INVALID_STATE,
98 "Sempahore is not initialized.");
104 if (timeout == (long) INFINITE)
106 rval = sem_wait((sem_t*) __pSemaphore);
112 rval = clock_gettime(CLOCK_REALTIME, &tm);
113 SysTryReturn(NID_BASE_RT, rval == 0, E_SYSTEM, E_SYSTEM,
114 "[%s] Sempahore acquire timeout setting failed.", __ConvertNativeErrorToMessage(errno));
116 tm.tv_sec += timeout / 1000; // convert ms to s
117 tm.tv_nsec += (timeout % 1000) * (1000 * 1000); // convert ms to ns
119 rval = sem_timedwait((sem_t*) __pSemaphore, &tm);
130 continue; // interrupted due to some signal, continue wait
133 SysLogException(NID_BASE_RT, E_TIMEOUT, "[E_TIMEOUT] Semaphore acquire timedout");
137 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Semaphore acquire failed due to system error.");
144 _SemaphoreImpl::TryToAcquire(void)
150 rval = sem_trywait((sem_t*) __pSemaphore);
160 continue; // interrupted due to some signal, continue wait
163 SysLogException(NID_BASE_RT, E_OBJECT_LOCKED, "[E_OBJECT_LOCKED] Semaphore is already locked");
164 return E_OBJECT_LOCKED;
167 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Semaphore acquire failed : [%s]", __ConvertNativeErrorToMessage(errno));
174 _SemaphoreImpl::Release(void)
176 SysTryReturnResult(NID_BASE_RT, __pSemaphore != null, E_INVALID_STATE,
177 "Sempahore is not initialized.");
179 int rval = sem_post((sem_t*) __pSemaphore);
180 SysTryReturnResult(NID_BASE_RT, rval == 0, E_SYSTEM,
181 "Sempahore release failed : [%s]", __ConvertNativeErrorToMessage(errno));
186 } } } // Tizen::Runtime