Home | History | Annotate | Download | only in libexynosutils
      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 (at) samsung.com)
     21  * \date      2011/06/15
     22  *
     23  * <b>Revision History: </b>
     24  * - 2010/06/15 : Sangwoo, Park(sw5771.park (at) 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 {
     47     m_mutex = NULL;
     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 
     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:
     77         ALOGE("%s::unmatched type(%d) fail", __func__, type);
     78         return false;
     79     }
     80 
     81     m_mutex = new Mutex(androidMutexType, name);
     82     if (m_mutex == NULL) {
     83         ALOGE("%s::Mutex create fail", __func__);
     84         return false;
     85     }
     86 
     87     m_type = type;
     88     strcpy(m_name, name);
     89 
     90     m_flagCreate = true;
     91 
     92     return true;
     93 }
     94 
     95 void ExynosMutex::destroy(void)
     96 {
     97     if (m_flagCreate == false) {
     98         ALOGE("%s::Not yet created", __func__);
     99         return;
    100     }
    101 
    102     if (m_mutex)
    103         delete ((Mutex *)m_mutex);
    104     m_mutex = NULL;
    105 
    106     m_flagCreate = false;
    107 }
    108 
    109 bool ExynosMutex::getCreatedStatus(void)
    110 {
    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 
    121 #ifdef EXYNOS_MUTEX_DEBUG
    122     ALOGD("%s::%s'lock() start", __func__, m_name);
    123 #endif
    124 
    125     if (((Mutex *)m_mutex)->lock() != 0) {
    126         ALOGE("%s::m_core->lock() fail", __func__);
    127         return false;
    128     }
    129 
    130 #ifdef EXYNOS_MUTEX_DEBUG
    131     ALOGD("%s::%s'lock() end", __func__, m_name);
    132 #endif
    133 
    134     return true;
    135 }
    136 
    137 bool ExynosMutex::unLock(void)
    138 {
    139     if (m_flagCreate == false) {
    140         ALOGE("%s::Not yet created", __func__);
    141         return false;
    142     }
    143 
    144 #ifdef EXYNOS_MUTEX_DEBUG
    145     ALOGD("%s::%s'unlock() start", __func__, m_name);
    146 #endif
    147 
    148     ((Mutex *)m_mutex)->unlock();
    149 
    150 #ifdef EXYNOS_MUTEX_DEBUG
    151     ALOGD("%s::%s'unlock() end", __func__, m_name);
    152 #endif
    153 
    154     return true;
    155 }
    156 
    157 bool ExynosMutex::tryLock(void)
    158 {
    159     if (m_flagCreate == false) {
    160         ALOGE("%s::Not yet created", __func__);
    161         return false;
    162     }
    163 
    164     int ret = 0;
    165 
    166 #ifdef EXYNOS_MUTEX_DEBUG
    167     ALOGD("%s::%s'trylock() start", __func__, m_name);
    168 #endif
    169 
    170     ret = ((Mutex *)m_mutex)->tryLock();
    171 
    172 #ifdef EXYNOS_MUTEX_DEBUG
    173     ALOGD("%s::%s'trylock() end", __func__, m_name);
    174 #endif
    175 
    176     return (ret == 0) ? true : false;
    177 }
    178 
    179 int ExynosMutex::getType(void)
    180 {
    181     return m_type;
    182 }
    183 
    184 void *exynos_mutex_create(
    185     int type,
    186     char *name)
    187 {
    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     }
    195 
    196     return (void*)mutex;
    197 }
    198 
    199 bool exynos_mutex_destroy(
    200     void *handle)
    201 {
    202     if (handle == NULL) {
    203         ALOGE("%s::handle is null", __func__);
    204         return false;
    205     }
    206 
    207     if (((ExynosMutex *)handle)->getCreatedStatus() == true)
    208         ((ExynosMutex *)handle)->destroy();
    209 
    210     delete (ExynosMutex *)handle;
    211 
    212     return true;
    213 }
    214 
    215 bool exynos_mutex_lock(
    216     void *handle)
    217 {
    218     if (handle == NULL) {
    219         ALOGE("%s::handle is null", __func__);
    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) {
    231         ALOGE("%s::handle is null", __func__);
    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) {
    243         ALOGE("%s::handle is null", __func__);
    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) {
    255         ALOGE("%s::handle is null", __func__);
    256         return false;
    257     }
    258 
    259     return ((ExynosMutex *)handle)->getType();
    260 }
    261 
    262 bool exynos_mutex_get_created_status(
    263     void *handle)
    264 {
    265     if (handle == NULL) {
    266         ALOGE("%s::handle is null", __func__);
    267         return false;
    268     }
    269 
    270     return ((ExynosMutex *)handle)->getCreatedStatus();
    271 }
    272 
    273