blob: c3b3aa983e5accc218afa05b4a1cd4e194ca5934 [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
45ExynosMutex::ExynosMutex(
46 int type,
47 char* name)
48{
49 int androidMutexType = 0;
50 m_mutex = NULL;
51 m_type = TYPE_BASE;
52
53 switch (type) {
54 case TYPE_PRIVATE:
55 androidMutexType = Mutex::PRIVATE;
56 break;
57 case TYPE_SHARED:
58 androidMutexType = Mutex::SHARED;
59 break;
60 default:
Dima Zavin53b41972012-04-02 10:29:34 -070061 ALOGE("%s::unmatched type(%d) fail", __func__, type);
Jiho Chang61bc1542012-03-24 05:52:01 +090062 break;
63 }
64
65 m_mutex = new Mutex(androidMutexType, name);
66 if (m_mutex == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -070067 ALOGE("%s::Mutex create fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +090068 }
69
70 m_type = type;
71 strcpy(m_name, name);
72}
73
74ExynosMutex::~ExynosMutex()
75{
76 if (m_mutex)
77 delete ((Mutex *)m_mutex);
78 m_mutex = NULL;
79}
80
81bool ExynosMutex::lock(
82 void)
83{
84#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -070085 ALOGD("%s::%s'lock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +090086#endif
87
88 if (m_mutex == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -070089 ALOGE("%s::Mutex create fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +090090 return false;
91 }
92
93 if (((Mutex *)m_mutex)->lock() != 0) {
Dima Zavin53b41972012-04-02 10:29:34 -070094 ALOGE("%s::m_core->lock() fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +090095 return false;
96 }
97
98#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -070099 ALOGD("%s::%s'lock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900100#endif
101
102 return true;
103}
104
105bool ExynosMutex::unLock(
106 void)
107{
108#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700109 ALOGD("%s::%s'unlock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900110#endif
111
112 if (m_mutex == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700113 ALOGE("%s::Mutex create fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900114 return false;
115 }
116
117 ((Mutex *)m_mutex)->unlock();
118
119#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700120 ALOGD("%s::%s'unlock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900121#endif
122
123 return true;
124}
125
126bool ExynosMutex::tryLock(
127 void)
128{
129 int ret = 0;
130
131#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700132 ALOGD("%s::%s'trylock() start", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900133#endif
134
135 if (m_mutex == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700136 ALOGE("%s::Mutex create fail", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900137 return false;
138 }
139
140 ret = ((Mutex *)m_mutex)->tryLock();
141
142#ifdef EXYNOS_MUTEX_DEBUG
Dima Zavin53b41972012-04-02 10:29:34 -0700143 ALOGD("%s::%s'trylock() end", __func__, m_name);
Jiho Chang61bc1542012-03-24 05:52:01 +0900144#endif
145
146 return (ret == 0) ? true : false;
147}
148
149int ExynosMutex::getType(
150 void)
151{
152 return m_type;
153}
154
155int ExynosMutex::getCreatedStatus(
156 void)
157{
158 if (m_mutex == NULL)
159 return STATUS_NOT_CREATED;
160 else
161 return STATUS_CREATED;
162}
163
164void *exynos_mutex_create(
165 int type,
166 char *name)
167{
168 ExynosMutex *mutex = new ExynosMutex(type, name);
169
170 return (void*)mutex;
171}
172
173bool exynos_mutex_destroy(
174 void *handle)
175{
176 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700177 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900178 return false;
179 }
180
181 delete (ExynosMutex *)handle;
182
183 return true;
184}
185
186bool exynos_mutex_lock(
187 void *handle)
188{
189 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700190 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900191 return false;
192 }
193
194 return ((ExynosMutex *)handle)->lock();
195
196}
197
198bool exynos_mutex_unlock(
199 void *handle)
200{
201 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700202 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900203 return false;
204 }
205
206 return ((ExynosMutex *)handle)->unLock();
207
208}
209
210bool exynos_mutex_trylock(
211 void *handle)
212{
213 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700214 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900215 return false;
216 }
217
218 return ((ExynosMutex *)handle)->tryLock();
219
220}
221
222int exynos_mutex_get_type(
223 void *handle)
224{
225 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700226 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900227 return false;
228 }
229
230 return ((ExynosMutex *)handle)->getType();
231}
232
233int exynos_mutex_get_created_status(
234 void *handle)
235{
236 if (handle == NULL) {
Dima Zavin53b41972012-04-02 10:29:34 -0700237 ALOGE("%s::handle is null", __func__);
Jiho Chang61bc1542012-03-24 05:52:01 +0900238 return false;
239 }
240
241 return ((ExynosMutex *)handle)->getCreatedStatus();
242}
243