blob: c0020ed8a09ace575bd9cec4d042f3c7de169d11 [file] [log] [blame]
Jiho Chang61bc1542012-03-24 05:52:01 +09001/*
2 * Copyright@ Samsung Electronics Co. LTD
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15*/
16
17/*!
18 * \file ExynosMutex.cpp
19 * \brief source file for ExynosMutex
20 * \author Sangwoo, Park(sw5771.park@samsung.com)
21 * \date 2011/06/15
22 *
23 * <b>Revision History: </b>
24 * - 2010/06/15 : Sangwoo, Park(sw5771.park@samsung.com) \n
25 * Initial version
26 *
27 */
28
29//#define LOG_NDEBUG 0
30#define LOG_TAG "ExynosMutex"
31#include <utils/Log.h>
32
33#include <utils/threads.h>
34using namespace android;
35
36#include <stdio.h>
37#include <stdarg.h>
38#include <stdlib.h>
39#include <string.h>
40
41#include "ExynosMutex.h"
42
43//#define EXYNOS_MUTEX_DEBUG
44
Jiho Changc3e0af62012-04-26 13:52:52 -070045ExynosMutex::ExynosMutex()
Jiho Chang61bc1542012-03-24 05:52:01 +090046{
Jiho Chang61bc1542012-03-24 05:52:01 +090047 m_mutex = NULL;
Jiho Changc3e0af62012-04-26 13:52:52 -070048 m_flagCreate = false;
49 m_type = TYPE_BASE;
50}
51
52ExynosMutex::~ExynosMutex()
53{
54 if (m_flagCreate == true)
55 this->destroy();
56}
57
58bool ExynosMutex::create(int type, char* name)
59{
60 if (m_flagCreate == true) {
61 ALOGE("%s::Already created", __func__);
62 return false;
63 }
64
65 int androidMutexType = 0;
66
Jiho Chang61bc1542012-03-24 05:52:01 +090067 m_type = TYPE_BASE;
68
69 switch (type) {
70 case TYPE_PRIVATE:
71 androidMutexType = Mutex::PRIVATE;
72 break;
73 case TYPE_SHARED:
74 androidMutexType = Mutex::SHARED;
75 break;
76 default:
Dima Zavin53b41972012-04-02 10:29:34 -070077 ALOGE("%s::unmatched type(%d) fail", __func__, type);
Jiho Changc3e0af62012-04-26 13:52:52 -070078 return false;
Jiho Chang61bc1542012-03-24 05:52:01 +090079 }
80
81 m_mutex = new Mutex(androidMutexType, name);
82 if (m_mutex == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -070083 ALOGE("%s::Mutex create fail", __func__);
Jiho Changc3e0af62012-04-26 13:52:52 -070084 return false;
Jiho Chang61bc1542012-03-24 05:52:01 +090085 }
86
87 m_type = type;
88 strcpy(m_name, name);
Jiho Changc3e0af62012-04-26 13:52:52 -070089
90 m_flagCreate = true;
91
92 return true;
Jiho Chang61bc1542012-03-24 05:52:01 +090093}
94
Jiho Changc3e0af62012-04-26 13:52:52 -070095void ExynosMutex::destroy(void)
Jiho Chang61bc1542012-03-24 05:52:01 +090096{
Jiho Changc3e0af62012-04-26 13:52:52 -070097 if (m_flagCreate == false) {
98 ALOGE("%s::Not yet created", __func__);
99 return;
100 }
101
Jiho Chang61bc1542012-03-24 05:52:01 +0900102 if (m_mutex)
103 delete ((Mutex *)m_mutex);
104 m_mutex = NULL;
Jiho Changc3e0af62012-04-26 13:52:52 -0700105
106 m_flagCreate = false;
Jiho Chang61bc1542012-03-24 05:52:01 +0900107}
108
Jiho Changc3e0af62012-04-26 13:52:52 -0700109bool ExynosMutex::getCreatedStatus(void)
Jiho Chang61bc1542012-03-24 05:52:01 +0900110{
Jiho Changc3e0af62012-04-26 13:52:52 -0700111 return m_flagCreate;
112}
113
114bool ExynosMutex::lock(void)
115{
116 if (m_flagCreate == false) {
117 ALOGE("%s::Not yet created", __func__);
118 return false;
119 }
120
Jiho Chang61bc1542012-03-24 05:52:01 +0900121#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700122 ALOGD("%s::%s'lock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900123#endif
124
Jiho Chang61bc1542012-03-24 05:52:01 +0900125 if (((Mutex *)m_mutex)->lock() != 0) {
Dima Zavin53b41972012-04-02 10:29:34 -0700126 ALOGE("%s::m_core->lock() fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900127 return false;
128 }
129
130#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700131 ALOGD("%s::%s'lock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900132#endif
133
134 return true;
135}
136
Jiho Changc3e0af62012-04-26 13:52:52 -0700137bool ExynosMutex::unLock(void)
Jiho Chang61bc1542012-03-24 05:52:01 +0900138{
Jiho Changc3e0af62012-04-26 13:52:52 -0700139 if (m_flagCreate == false) {
140 ALOGE("%s::Not yet created", __func__);
141 return false;
142 }
143
Jiho Chang61bc1542012-03-24 05:52:01 +0900144#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700145 ALOGD("%s::%s'unlock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900146#endif
147
Jiho Chang61bc1542012-03-24 05:52:01 +0900148 ((Mutex *)m_mutex)->unlock();
149
150#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700151 ALOGD("%s::%s'unlock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900152#endif
153
154 return true;
155}
156
Jiho Changc3e0af62012-04-26 13:52:52 -0700157bool ExynosMutex::tryLock(void)
Jiho Chang61bc1542012-03-24 05:52:01 +0900158{
Jiho Changc3e0af62012-04-26 13:52:52 -0700159 if (m_flagCreate == false) {
160 ALOGE("%s::Not yet created", __func__);
161 return false;
162 }
163
Jiho Chang61bc1542012-03-24 05:52:01 +0900164 int ret = 0;
165
166#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700167 ALOGD("%s::%s'trylock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900168#endif
169
Jiho Chang61bc1542012-03-24 05:52:01 +0900170 ret = ((Mutex *)m_mutex)->tryLock();
171
172#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700173 ALOGD("%s::%s'trylock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900174#endif
175
176 return (ret == 0) ? true : false;
177}
178
Jiho Changc3e0af62012-04-26 13:52:52 -0700179int ExynosMutex::getType(void)
Jiho Chang61bc1542012-03-24 05:52:01 +0900180{
181 return m_type;
182}
183
Jiho Chang61bc1542012-03-24 05:52:01 +0900184void *exynos_mutex_create(
185 int type,
186 char *name)
187{
Jiho Changc3e0af62012-04-26 13:52:52 -0700188 ExynosMutex *mutex = new ExynosMutex();
189
190 if (mutex->create(type, name) == false) {
191 ALOGE("%s::mutex->create() fail", __func__);
192 delete mutex;
193 mutex = NULL;
194 }
Jiho Chang61bc1542012-03-24 05:52:01 +0900195
196 return (void*)mutex;
197}
198
199bool exynos_mutex_destroy(
200 void *handle)
201{
202 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700203 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900204 return false;
205 }
206
Jiho Changc3e0af62012-04-26 13:52:52 -0700207 if (((ExynosMutex *)handle)->getCreatedStatus() == true)
208 ((ExynosMutex *)handle)->destroy();
209
Jiho Chang61bc1542012-03-24 05:52:01 +0900210 delete (ExynosMutex *)handle;
211
212 return true;
213}
214
215bool exynos_mutex_lock(
216 void *handle)
217{
218 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700219 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900220 return false;
221 }
222
223 return ((ExynosMutex *)handle)->lock();
224
225}
226
227bool exynos_mutex_unlock(
228 void *handle)
229{
230 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700231 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900232 return false;
233 }
234
235 return ((ExynosMutex *)handle)->unLock();
236
237}
238
239bool exynos_mutex_trylock(
240 void *handle)
241{
242 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700243 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900244 return false;
245 }
246
247 return ((ExynosMutex *)handle)->tryLock();
248
249}
250
251int exynos_mutex_get_type(
252 void *handle)
253{
254 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700255 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900256 return false;
257 }
258
259 return ((ExynosMutex *)handle)->getType();
260}
261
Jiho Changc3e0af62012-04-26 13:52:52 -0700262bool exynos_mutex_get_created_status(
Jiho Chang61bc1542012-03-24 05:52:01 +0900263 void *handle)
264{
265 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700266 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900267 return false;
268 }
269
270 return ((ExynosMutex *)handle)->getCreatedStatus();
271}
272