Home | History | Annotate | Download | only in jni
      1 /*
      2  * Copyright (C) 2012 The Android Open Source Project
      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  *  Encapsulate a mutex for thread synchronization.
     19  */
     20 
     21 #include "Mutex.h"
     22 #include "NfcJniUtil.h"
     23 
     24 #include <cutils/log.h>
     25 #include <errno.h>
     26 
     27 /*******************************************************************************
     28 **
     29 ** Function:        Mutex
     30 **
     31 ** Description:     Initialize member variables.
     32 **
     33 ** Returns:         None.
     34 **
     35 *******************************************************************************/
     36 Mutex::Mutex ()
     37 {
     38     memset (&mMutex, 0, sizeof(mMutex));
     39     int res = pthread_mutex_init (&mMutex, NULL);
     40     if (res != 0)
     41     {
     42         ALOGE ("Mutex::Mutex: fail init; error=0x%X", res);
     43     }
     44 }
     45 
     46 
     47 /*******************************************************************************
     48 **
     49 ** Function:        ~Mutex
     50 **
     51 ** Description:     Cleanup all resources.
     52 **
     53 ** Returns:         None.
     54 **
     55 *******************************************************************************/
     56 Mutex::~Mutex ()
     57 {
     58     int res = pthread_mutex_destroy (&mMutex);
     59     if (res != 0)
     60     {
     61         ALOGE ("Mutex::~Mutex: fail destroy; error=0x%X", res);
     62     }
     63 }
     64 
     65 
     66 /*******************************************************************************
     67 **
     68 ** Function:        lock
     69 **
     70 ** Description:     Block the thread and try lock the mutex.
     71 **
     72 ** Returns:         None.
     73 **
     74 *******************************************************************************/
     75 void Mutex::lock ()
     76 {
     77     int res = pthread_mutex_lock (&mMutex);
     78     if (res != 0)
     79     {
     80         ALOGE ("Mutex::lock: fail lock; error=0x%X", res);
     81     }
     82 }
     83 
     84 
     85 /*******************************************************************************
     86 **
     87 ** Function:        unlock
     88 **
     89 ** Description:     Unlock a mutex to unblock a thread.
     90 **
     91 ** Returns:         None.
     92 **
     93 *******************************************************************************/
     94 void Mutex::unlock ()
     95 {
     96     int res = pthread_mutex_unlock (&mMutex);
     97     if (res != 0)
     98     {
     99         ALOGE ("Mutex::unlock: fail unlock; error=0x%X", res);
    100     }
    101 }
    102 
    103 
    104 /*******************************************************************************
    105 **
    106 ** Function:        tryLock
    107 **
    108 ** Description:     Try to lock the mutex.
    109 **
    110 ** Returns:         True if the mutex is locked.
    111 **
    112 *******************************************************************************/
    113 bool Mutex::tryLock ()
    114 {
    115     int res = pthread_mutex_trylock (&mMutex);
    116     if ((res != 0) && (res != EBUSY))
    117     {
    118         ALOGE ("Mutex::tryLock: error=0x%X", res);
    119     }
    120     return res == 0;
    121 }
    122 
    123 
    124 /*******************************************************************************
    125 **
    126 ** Function:        nativeHandle
    127 **
    128 ** Description:     Get the handle of the mutex.
    129 **
    130 ** Returns:         Handle of the mutex.
    131 **
    132 *******************************************************************************/
    133 pthread_mutex_t* Mutex::nativeHandle ()
    134 {
    135     return &mMutex;
    136 }
    137 
    138 
    139