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 FBaseRt_SemaphoreImpl.cpp
20 * @brief This is the implementation file for the _SemaphoreImpl class.
24 #include <semaphore.h>
29 #include "FBase_NativeError.h"
30 #include <FBaseSysLog.h>
31 #include "FBaseRt_SemaphoreImpl.h"
33 namespace Tizen { namespace Base { namespace Runtime
36 _SemaphoreImpl::_SemaphoreImpl(void)
44 _SemaphoreImpl::~_SemaphoreImpl(void)
46 if (__pSemaphore != null)
48 sem_destroy(__pSemaphore);
56 _SemaphoreImpl::Create(int count)
58 return Create(L"", count);
62 _SemaphoreImpl::Create(const Tizen::Base::String& name, int count)
66 sem_t* pSemaphore = null;
68 SysTryReturnResult(NID_BASE_RT, count >= 0, E_INVALID_ARG,
69 "The count(%d) MUST be greater than or equal to 0.", count);
71 pSemaphore = (sem_t*) malloc(sizeof(sem_t));
72 SysTryReturnResult(NID_BASE_RT, pSemaphore != null, E_OUT_OF_MEMORY,
73 "Semaphore memory allocation failed.");
75 ret = sem_init(pSemaphore, 0, count);
76 SysTryCatch(NID_BASE_RT, ret == 0, r = E_SYSTEM, E_SYSTEM,
77 "[E_SYSTEM] Semaphore intialization failed : [%s]", __ConvertNativeErrorToMessage(errno));
79 if (name.GetLength() > 0)
85 __pSemaphore = pSemaphore;
96 _SemaphoreImpl::Acquire(long timeout)
98 SysTryReturnResult(NID_BASE_RT, __pSemaphore != null, E_INVALID_STATE,
99 "Sempahore is not initialized.");
105 if (timeout == (long) INFINITE)
107 rval = sem_wait((sem_t*) __pSemaphore);
113 rval = clock_gettime(CLOCK_REALTIME, &tm);
114 SysTryReturn(NID_BASE_RT, rval == 0, E_SYSTEM, E_SYSTEM,
115 "[%s] Sempahore acquire timeout setting failed.", __ConvertNativeErrorToMessage(errno));
117 tm.tv_sec += timeout / 1000; // convert ms to s
118 tm.tv_nsec += (timeout % 1000) * (1000 * 1000); // convert ms to ns
120 rval = sem_timedwait((sem_t*) __pSemaphore, &tm);
131 continue; // interrupted due to some signal, continue wait
134 SysLogException(NID_BASE_RT, E_TIMEOUT, "[E_TIMEOUT] Semaphore acquire timedout");
138 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Semaphore acquire failed due to system error.");
145 _SemaphoreImpl::TryToAcquire(void)
151 rval = sem_trywait((sem_t*) __pSemaphore);
161 continue; // interrupted due to some signal, continue wait
164 SysLogException(NID_BASE_RT, E_OBJECT_LOCKED, "[E_OBJECT_LOCKED] Semaphore is already locked");
165 return E_OBJECT_LOCKED;
168 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Semaphore acquire failed : [%s]", __ConvertNativeErrorToMessage(errno));
175 _SemaphoreImpl::Release(void)
177 SysTryReturnResult(NID_BASE_RT, __pSemaphore != null, E_INVALID_STATE,
178 "Sempahore is not initialized.");
180 int rval = sem_post((sem_t*) __pSemaphore);
181 SysTryReturnResult(NID_BASE_RT, rval == 0, E_SYSTEM,
182 "Sempahore release failed : [%s]", __ConvertNativeErrorToMessage(errno));
187 } } } // Tizen::Runtime