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_MutexImpl.cpp
20 * @brief This is the implementation file for the _MutexImpl class.
28 #include <FBaseSysLog.h>
29 #include "FBase_NativeError.h"
30 #include "FBaseRt_MutexImpl.h"
32 namespace Tizen { namespace Base { namespace Runtime
35 _MutexImpl::_MutexImpl(void)
41 _MutexImpl::~_MutexImpl(void)
45 pthread_mutex_destroy(__pMutex);
52 _MutexImpl::Create(void)
58 _MutexImpl::CreateNonRecursiveMutex(void)
61 pthread_mutex_t* pMutex = null;
64 pthread_mutexattr_t mutexAttr;
66 pMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
67 SysTryReturnResult(NID_BASE_RT, pMutex != null, E_OUT_OF_MEMORY,
68 "Mutex memory allocation failed.")
70 // set mutex attributes
71 pthread_mutexattr_init(&mutexAttr);
72 pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_PRIVATE);
73 pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_ERRORCHECK_NP);
75 // intialize the mutex
76 ret = pthread_mutex_init(pMutex, &mutexAttr);
77 SysTryCatch(NID_BASE_RT, ret == 0, r = E_SYSTEM, E_SYSTEM,
78 "[%s] Mutex intialization failed.", __ConvertNativeErrorToMessage(ret));
80 // destroy the attributes
81 pthread_mutexattr_destroy(&mutexAttr);
99 _MutexImpl::Create(const Tizen::Base::String& name)
101 result r = E_SUCCESS;
102 pthread_mutex_t* pMutex = null;
105 pthread_mutexattr_t mutexAttr;
107 pMutex = (pthread_mutex_t*) malloc(sizeof(pthread_mutex_t));
108 SysTryReturnResult(NID_BASE_RT, pMutex != null, E_OUT_OF_MEMORY,
109 "Mutex memory allocation failed.")
111 // set mutex attributes
112 pthread_mutexattr_init(&mutexAttr);
113 pthread_mutexattr_setpshared(&mutexAttr, PTHREAD_PROCESS_PRIVATE);
114 pthread_mutexattr_settype(&mutexAttr, PTHREAD_MUTEX_RECURSIVE_NP);
116 // intialize the mutex
117 ret = pthread_mutex_init(pMutex, &mutexAttr);
118 SysTryCatch(NID_BASE_RT, ret == 0, r = E_SYSTEM, E_SYSTEM,
119 "[%s] Mutex intialization failed.", __ConvertNativeErrorToMessage(ret));
121 // destroy the attributes
122 pthread_mutexattr_destroy(&mutexAttr);
139 _MutexImpl::Acquire(void)
141 return Acquire(INFINITE);
145 _MutexImpl::Acquire(long timeout)
147 SysTryReturnResult(NID_BASE_RT, __pMutex != null, E_INVALID_STATE, "Mutex is not initialized.");
149 if (timeout == (long) INFINITE)
151 int rval = pthread_mutex_lock((pthread_mutex_t*) __pMutex);
152 SysTryReturnResult(NID_BASE_RT, rval == 0, E_SYSTEM, "[%s] Mutex acquire failed.", __ConvertNativeErrorToMessage(rval));
158 int rval = clock_gettime(CLOCK_REALTIME, &tm);
159 SysTryReturn(NID_BASE_RT, rval == 0, E_SYSTEM, E_SYSTEM,
160 "[%s] Mutex acquire timeout setting failed.", __ConvertNativeErrorToMessage(errno));
162 tm.tv_sec += timeout / 1000; // convert ms to s
163 tm.tv_nsec += (timeout % 1000) * (1000 * 1000); // convert ms to ns
165 rval = pthread_mutex_timedlock((pthread_mutex_t*) __pMutex, &tm);
174 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Mutex acquire failed due to timedout");
177 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Mutex acquire failed : [%s].", __ConvertNativeErrorToMessage(rval));
186 _MutexImpl::TryToAcquire(void)
188 SysTryReturnResult(NID_BASE_RT, __pMutex != null, E_INVALID_STATE, "Mutex is not initialized.");
190 int rval = pthread_mutex_trylock((pthread_mutex_t*) __pMutex);
199 SysLogException(NID_BASE_RT, E_OBJECT_LOCKED, "[E_OBJECT_LOCKED] Mutex acquire failed.");
200 return E_OBJECT_LOCKED;
202 SysLogException(NID_BASE_RT, E_SYSTEM, "[E_SYSTEM] Mutex acquire failed : [%s].", __ConvertNativeErrorToMessage(rval));
208 _MutexImpl::Release(void)
210 SysTryReturnResult(NID_BASE_RT, __pMutex != null, E_INVALID_STATE, "Mutex is not initialized.");
212 int rval = pthread_mutex_unlock((pthread_mutex_t*) __pMutex);
213 SysTryReturnResult(NID_BASE_RT, rval == 0, E_SYSTEM,
214 "Mutex release failed : [%s]", __ConvertNativeErrorToMessage(rval));
219 } } } // Tizen::Runtime