1 /*
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>
34 using 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 
ExynosMutex()45 ExynosMutex::ExynosMutex()
46 {
47     m_mutex = NULL;
48     m_flagCreate = false;
49     m_type = TYPE_BASE;
50     memset(m_name, 0, 128);
51 }
52 
~ExynosMutex()53 ExynosMutex::~ExynosMutex()
54 {
55     if (m_flagCreate == true)
56         this->destroy();
57 }
58 
create(int type,char * name)59 bool ExynosMutex::create(int type, char* name)
60 {
61     if (m_flagCreate == true) {
62         ALOGE("%s::Already created", __func__);
63         return false;
64     }
65 
66     int androidMutexType = 0;
67 
68     m_type = TYPE_BASE;
69 
70     switch (type) {
71     case TYPE_PRIVATE:
72         androidMutexType = Mutex::PRIVATE;
73         break;
74     case TYPE_SHARED:
75         androidMutexType = Mutex::SHARED;
76         break;
77     default:
78         ALOGE("%s::unmatched type(%d) fail", __func__, type);
79         return false;
80     }
81 
82     m_mutex = new Mutex(androidMutexType, name);
83     if (m_mutex == NULL) {
84         ALOGE("%s::Mutex create fail", __func__);
85         return false;
86     }
87 
88     m_type = type;
89     strncpy(m_name, name, 128 - 1);
90 
91     m_flagCreate = true;
92 
93     return true;
94 }
95 
destroy(void)96 void ExynosMutex::destroy(void)
97 {
98     if (m_flagCreate == false) {
99         ALOGE("%s::Not yet created", __func__);
100         return;
101     }
102 
103     if (m_mutex)
104         delete ((Mutex *)m_mutex);
105     m_mutex = NULL;
106 
107     m_flagCreate = false;
108 }
109 
getCreatedStatus(void)110 bool ExynosMutex::getCreatedStatus(void)
111 {
112     return m_flagCreate;
113 }
114 
lock(void)115 bool ExynosMutex::lock(void)
116 {
117     if (m_flagCreate == false) {
118         ALOGE("%s::Not yet created", __func__);
119         return false;
120     }
121 
122 #ifdef EXYNOS_MUTEX_DEBUG
123     ALOGD("%s::%s'lock() start", __func__, m_name);
124 #endif
125 
126     if (((Mutex *)m_mutex)->lock() != 0) {
127         ALOGE("%s::m_core->lock() fail", __func__);
128         return false;
129     }
130 
131 #ifdef EXYNOS_MUTEX_DEBUG
132     ALOGD("%s::%s'lock() end", __func__, m_name);
133 #endif
134 
135     return true;
136 }
137 
unLock(void)138 bool ExynosMutex::unLock(void)
139 {
140     if (m_flagCreate == false) {
141         ALOGE("%s::Not yet created", __func__);
142         return false;
143     }
144 
145 #ifdef EXYNOS_MUTEX_DEBUG
146     ALOGD("%s::%s'unlock() start", __func__, m_name);
147 #endif
148 
149     ((Mutex *)m_mutex)->unlock();
150 
151 #ifdef EXYNOS_MUTEX_DEBUG
152     ALOGD("%s::%s'unlock() end", __func__, m_name);
153 #endif
154 
155     return true;
156 }
157 
tryLock(void)158 bool ExynosMutex::tryLock(void)
159 {
160     if (m_flagCreate == false) {
161         ALOGE("%s::Not yet created", __func__);
162         return false;
163     }
164 
165     int ret = 0;
166 
167 #ifdef EXYNOS_MUTEX_DEBUG
168     ALOGD("%s::%s'trylock() start", __func__, m_name);
169 #endif
170 
171     ret = ((Mutex *)m_mutex)->tryLock();
172 
173 #ifdef EXYNOS_MUTEX_DEBUG
174     ALOGD("%s::%s'trylock() end", __func__, m_name);
175 #endif
176 
177     return (ret == 0) ? true : false;
178 }
179 
getType(void)180 int ExynosMutex::getType(void)
181 {
182     return m_type;
183 }
184 
exynos_mutex_create(int type,char * name)185 void *exynos_mutex_create(
186     int type,
187     char *name)
188 {
189     ExynosMutex *mutex = new ExynosMutex();
190 
191     if (mutex->create(type, name) == false) {
192         ALOGE("%s::mutex->create() fail", __func__);
193         delete mutex;
194         mutex = NULL;
195     }
196 
197     return (void*)mutex;
198 }
199 
exynos_mutex_destroy(void * handle)200 bool exynos_mutex_destroy(
201     void *handle)
202 {
203     if (handle == NULL) {
204         ALOGE("%s::handle is null", __func__);
205         return false;
206     }
207 
208     if (((ExynosMutex *)handle)->getCreatedStatus() == true)
209         ((ExynosMutex *)handle)->destroy();
210 
211     delete (ExynosMutex *)handle;
212 
213     return true;
214 }
215 
exynos_mutex_lock(void * handle)216 bool exynos_mutex_lock(
217     void *handle)
218 {
219     if (handle == NULL) {
220         ALOGE("%s::handle is null", __func__);
221         return false;
222     }
223 
224     return ((ExynosMutex *)handle)->lock();
225 
226 }
227 
exynos_mutex_unlock(void * handle)228 bool exynos_mutex_unlock(
229     void *handle)
230 {
231     if (handle == NULL) {
232         ALOGE("%s::handle is null", __func__);
233         return false;
234     }
235 
236     return ((ExynosMutex *)handle)->unLock();
237 
238 }
239 
exynos_mutex_trylock(void * handle)240 bool exynos_mutex_trylock(
241     void *handle)
242 {
243     if (handle == NULL) {
244         ALOGE("%s::handle is null", __func__);
245         return false;
246     }
247 
248     return ((ExynosMutex *)handle)->tryLock();
249 
250 }
251 
exynos_mutex_get_type(void * handle)252 int exynos_mutex_get_type(
253     void *handle)
254 {
255     if (handle == NULL) {
256         ALOGE("%s::handle is null", __func__);
257         return false;
258     }
259 
260     return ((ExynosMutex *)handle)->getType();
261 }
262 
exynos_mutex_get_created_status(void * handle)263 bool exynos_mutex_get_created_status(
264     void *handle)
265 {
266     if (handle == NULL) {
267         ALOGE("%s::handle is null", __func__);
268         return false;
269     }
270 
271     return ((ExynosMutex *)handle)->getCreatedStatus();
272 }
273 
274