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