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