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 condition variable for thread synchronization.
19 */
20
21 #include "CondVar.h"
22
23 #include <android-base/logging.h>
24 #include <android-base/stringprintf.h>
25 #include <errno.h>
26 #include <string.h>
27
28 using android::base::StringPrintf;
29
30 /*******************************************************************************
31 **
32 ** Function: CondVar
33 **
34 ** Description: Initialize member variables.
35 **
36 ** Returns: None.
37 **
38 *******************************************************************************/
CondVar()39 CondVar::CondVar() {
40 pthread_condattr_t attr;
41 pthread_condattr_init(&attr);
42 pthread_condattr_setclock(&attr, CLOCK_MONOTONIC);
43 memset(&mCondition, 0, sizeof(mCondition));
44 int const res = pthread_cond_init(&mCondition, &attr);
45 if (res) {
46 LOG(ERROR) << StringPrintf("CondVar::CondVar: fail init; error=0x%X", res);
47 }
48 }
49
50 /*******************************************************************************
51 **
52 ** Function: ~CondVar
53 **
54 ** Description: Cleanup all resources.
55 **
56 ** Returns: None.
57 **
58 *******************************************************************************/
~CondVar()59 CondVar::~CondVar() {
60 int const res = pthread_cond_destroy(&mCondition);
61 if (res) {
62 LOG(ERROR) << StringPrintf("CondVar::~CondVar: fail destroy; error=0x%X", res);
63 }
64 }
65
66 /*******************************************************************************
67 **
68 ** Function: wait
69 **
70 ** Description: Block the caller and wait for a condition.
71 **
72 ** Returns: None.
73 **
74 *******************************************************************************/
wait(std::mutex & mutex)75 void CondVar::wait(std::mutex& mutex) {
76 int const res = pthread_cond_wait(&mCondition, mutex.native_handle());
77 if (res) {
78 LOG(ERROR) << StringPrintf("CondVar::wait: fail wait; error=0x%X", res);
79 }
80 }
81
82 /*******************************************************************************
83 **
84 ** Function: wait
85 **
86 ** Description: Block the caller and wait for a condition.
87 ** millisec: Timeout in milliseconds.
88 **
89 ** Returns: True if wait is successful; false if timeout occurs.
90 **
91 *******************************************************************************/
wait(std::mutex & mutex,long millisec)92 bool CondVar::wait(std::mutex& mutex, long millisec) {
93 bool retVal = false;
94 struct timespec absoluteTime;
95
96 if (clock_gettime(CLOCK_MONOTONIC, &absoluteTime) == -1) {
97 LOG(ERROR) << StringPrintf("CondVar::wait: fail get time; errno=0x%X", errno);
98 } else {
99 absoluteTime.tv_sec += millisec / 1000;
100 long ns = absoluteTime.tv_nsec + ((millisec % 1000) * 1000000);
101 if (ns > 1000000000) {
102 absoluteTime.tv_sec++;
103 absoluteTime.tv_nsec = ns - 1000000000;
104 } else
105 absoluteTime.tv_nsec = ns;
106 }
107
108 int waitResult = pthread_cond_timedwait(&mCondition, mutex.native_handle(), &absoluteTime);
109 if ((waitResult != 0) && (waitResult != ETIMEDOUT))
110 LOG(ERROR) << StringPrintf("CondVar::wait: fail timed wait; error=0x%X", waitResult);
111 retVal = (waitResult == 0); // waited successfully
112 return retVal;
113 }
114
115 /*******************************************************************************
116 **
117 ** Function: notifyOne
118 **
119 ** Description: Unblock the waiting thread.
120 **
121 ** Returns: None.
122 **
123 *******************************************************************************/
notifyOne()124 void CondVar::notifyOne() {
125 int const res = pthread_cond_signal(&mCondition);
126 if (res) {
127 LOG(ERROR) << StringPrintf("CondVar::notifyOne: fail signal; error=0x%X", res);
128 }
129 }
130