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 <errno.h>
     25 #include <string.h>
     26 
     27 #include <android-base/stringprintf.h>
     28 #include <base/logging.h>
     29 
     30 using android::base::StringPrintf;
     31 
     32 /*******************************************************************************
     33 **
     34 ** Function:        Mutex
     35 **
     36 ** Description:     Initialize member variables.
     37 **
     38 ** Returns:         None.
     39 **
     40 *******************************************************************************/
     41 Mutex::Mutex() {
     42   memset(&mMutex, 0, sizeof(mMutex));
     43   int res = pthread_mutex_init(&mMutex, NULL);
     44   if (res != 0) {
     45     LOG(ERROR) << StringPrintf("Mutex::Mutex: fail init; error=0x%X", res);
     46   }
     47 }
     48 
     49 /*******************************************************************************
     50 **
     51 ** Function:        ~Mutex
     52 **
     53 ** Description:     Cleanup all resources.
     54 **
     55 ** Returns:         None.
     56 **
     57 *******************************************************************************/
     58 Mutex::~Mutex() {
     59   int res = pthread_mutex_destroy(&mMutex);
     60   if (res != 0) {
     61     LOG(ERROR) << StringPrintf("Mutex::~Mutex: fail destroy; error=0x%X", res);
     62   }
     63 }
     64 
     65 /*******************************************************************************
     66 **
     67 ** Function:        lock
     68 **
     69 ** Description:     Block the thread and try lock the mutex.
     70 **
     71 ** Returns:         None.
     72 **
     73 *******************************************************************************/
     74 void Mutex::lock() {
     75   int res = pthread_mutex_lock(&mMutex);
     76   if (res != 0) {
     77     LOG(ERROR) << StringPrintf("Mutex::lock: fail lock; error=0x%X", res);
     78   }
     79 }
     80 
     81 /*******************************************************************************
     82 **
     83 ** Function:        unlock
     84 **
     85 ** Description:     Unlock a mutex to unblock a thread.
     86 **
     87 ** Returns:         None.
     88 **
     89 *******************************************************************************/
     90 void Mutex::unlock() {
     91   int res = pthread_mutex_unlock(&mMutex);
     92   if (res != 0) {
     93     LOG(ERROR) << StringPrintf("Mutex::unlock: fail unlock; error=0x%X", res);
     94   }
     95 }
     96 
     97 /*******************************************************************************
     98 **
     99 ** Function:        tryLock
    100 **
    101 ** Description:     Try to lock the mutex.
    102 **
    103 ** Returns:         True if the mutex is locked.
    104 **
    105 *******************************************************************************/
    106 bool Mutex::tryLock() {
    107   int res = pthread_mutex_trylock(&mMutex);
    108   if ((res != 0) && (res != EBUSY)) {
    109     LOG(ERROR) << StringPrintf("Mutex::tryLock: error=0x%X", res);
    110   }
    111   return res == 0;
    112 }
    113 
    114 /*******************************************************************************
    115 **
    116 ** Function:        nativeHandle
    117 **
    118 ** Description:     Get the handle of the mutex.
    119 **
    120 ** Returns:         Handle of the mutex.
    121 **
    122 *******************************************************************************/
    123 pthread_mutex_t* Mutex::nativeHandle() { return &mMutex; }
    124