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 | |
| 45 | ExynosMutex::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 Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 61 | ALOGE("%s::unmatched type(%d) fail", __func__, type); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 62 | break; |
| 63 | } |
| 64 | |
| 65 | m_mutex = new Mutex(androidMutexType, name); |
| 66 | if (m_mutex == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 67 | ALOGE("%s::Mutex create fail", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | m_type = type; |
| 71 | strcpy(m_name, name); |
| 72 | } |
| 73 | |
| 74 | ExynosMutex::~ExynosMutex() |
| 75 | { |
| 76 | if (m_mutex) |
| 77 | delete ((Mutex *)m_mutex); |
| 78 | m_mutex = NULL; |
| 79 | } |
| 80 | |
| 81 | bool ExynosMutex::lock( |
| 82 | void) |
| 83 | { |
| 84 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 85 | ALOGD("%s::%s'lock() start", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 86 | #endif |
| 87 | |
| 88 | if (m_mutex == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 89 | ALOGE("%s::Mutex create fail", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (((Mutex *)m_mutex)->lock() != 0) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 94 | ALOGE("%s::m_core->lock() fail", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
| 98 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 99 | ALOGD("%s::%s'lock() end", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 100 | #endif |
| 101 | |
| 102 | return true; |
| 103 | } |
| 104 | |
| 105 | bool ExynosMutex::unLock( |
| 106 | void) |
| 107 | { |
| 108 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 109 | ALOGD("%s::%s'unlock() start", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 110 | #endif |
| 111 | |
| 112 | if (m_mutex == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 113 | ALOGE("%s::Mutex create fail", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
| 117 | ((Mutex *)m_mutex)->unlock(); |
| 118 | |
| 119 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 120 | ALOGD("%s::%s'unlock() end", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 121 | #endif |
| 122 | |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | bool ExynosMutex::tryLock( |
| 127 | void) |
| 128 | { |
| 129 | int ret = 0; |
| 130 | |
| 131 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 132 | ALOGD("%s::%s'trylock() start", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 133 | #endif |
| 134 | |
| 135 | if (m_mutex == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 136 | ALOGE("%s::Mutex create fail", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 137 | return false; |
| 138 | } |
| 139 | |
| 140 | ret = ((Mutex *)m_mutex)->tryLock(); |
| 141 | |
| 142 | #ifdef EXYNOS_MUTEX_DEBUG |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 143 | ALOGD("%s::%s'trylock() end", __func__, m_name); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 144 | #endif |
| 145 | |
| 146 | return (ret == 0) ? true : false; |
| 147 | } |
| 148 | |
| 149 | int ExynosMutex::getType( |
| 150 | void) |
| 151 | { |
| 152 | return m_type; |
| 153 | } |
| 154 | |
| 155 | int ExynosMutex::getCreatedStatus( |
| 156 | void) |
| 157 | { |
| 158 | if (m_mutex == NULL) |
| 159 | return STATUS_NOT_CREATED; |
| 160 | else |
| 161 | return STATUS_CREATED; |
| 162 | } |
| 163 | |
| 164 | void *exynos_mutex_create( |
| 165 | int type, |
| 166 | char *name) |
| 167 | { |
| 168 | ExynosMutex *mutex = new ExynosMutex(type, name); |
| 169 | |
| 170 | return (void*)mutex; |
| 171 | } |
| 172 | |
| 173 | bool exynos_mutex_destroy( |
| 174 | void *handle) |
| 175 | { |
| 176 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 177 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 178 | return false; |
| 179 | } |
| 180 | |
| 181 | delete (ExynosMutex *)handle; |
| 182 | |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | bool exynos_mutex_lock( |
| 187 | void *handle) |
| 188 | { |
| 189 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 190 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 191 | return false; |
| 192 | } |
| 193 | |
| 194 | return ((ExynosMutex *)handle)->lock(); |
| 195 | |
| 196 | } |
| 197 | |
| 198 | bool exynos_mutex_unlock( |
| 199 | void *handle) |
| 200 | { |
| 201 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 202 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return ((ExynosMutex *)handle)->unLock(); |
| 207 | |
| 208 | } |
| 209 | |
| 210 | bool exynos_mutex_trylock( |
| 211 | void *handle) |
| 212 | { |
| 213 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 214 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 215 | return false; |
| 216 | } |
| 217 | |
| 218 | return ((ExynosMutex *)handle)->tryLock(); |
| 219 | |
| 220 | } |
| 221 | |
| 222 | int exynos_mutex_get_type( |
| 223 | void *handle) |
| 224 | { |
| 225 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 226 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 227 | return false; |
| 228 | } |
| 229 | |
| 230 | return ((ExynosMutex *)handle)->getType(); |
| 231 | } |
| 232 | |
| 233 | int exynos_mutex_get_created_status( |
| 234 | void *handle) |
| 235 | { |
| 236 | if (handle == NULL) { |
Dima Zavin | 53b4197 | 2012-04-02 10:29:34 -0700 | [diff] [blame^] | 237 | ALOGE("%s::handle is null", __func__); |
Jiho Chang | 61bc154 | 2012-03-24 05:52:01 +0900 | [diff] [blame] | 238 | return false; |
| 239 | } |
| 240 | |
| 241 | return ((ExynosMutex *)handle)->getCreatedStatus(); |
| 242 | } |
| 243 | |