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