1 /*
2  * Copyright (C) 2015 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 #include "AutoConditionLock.h"
18 
19 namespace android {
20 
WaitableMutexWrapper(Mutex * mutex)21 WaitableMutexWrapper::WaitableMutexWrapper(Mutex* mutex) : mMutex{mutex}, mState{false} {}
22 
~WaitableMutexWrapper()23 WaitableMutexWrapper::~WaitableMutexWrapper() {}
24 
25 // Locks manager-owned mutex
AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper> & manager)26 AutoConditionLock::AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper>& manager) :
27         mManager{manager}, mAutoLock{manager->mMutex} {}
28 
29 // Unlocks manager-owned mutex
~AutoConditionLock()30 AutoConditionLock::~AutoConditionLock() {
31     // Unset the condition and wake everyone up before releasing lock
32     mManager->mState = false;
33     mManager->mCondition.broadcast();
34 }
35 
waitAndAcquire(const std::shared_ptr<WaitableMutexWrapper> & manager,nsecs_t waitTime)36 std::unique_ptr<AutoConditionLock> AutoConditionLock::waitAndAcquire(
37         const std::shared_ptr<WaitableMutexWrapper>& manager, nsecs_t waitTime) {
38 
39     if (manager == nullptr || manager->mMutex == nullptr) {
40         // Bad input, return null
41         return std::unique_ptr<AutoConditionLock>{nullptr};
42     }
43 
44     // Acquire scoped lock
45     std::unique_ptr<AutoConditionLock> scopedLock(new AutoConditionLock(manager));
46 
47     // Figure out what time in the future we should hit the timeout
48     nsecs_t failTime = systemTime(SYSTEM_TIME_MONOTONIC) + waitTime;
49 
50     // Wait until we timeout, or success
51     while(manager->mState) {
52         status_t ret = manager->mCondition.waitRelative(*(manager->mMutex), waitTime);
53         if (ret != NO_ERROR) {
54             // Timed out or whatever, return null
55             return std::unique_ptr<AutoConditionLock>{nullptr};
56         }
57         waitTime = failTime - systemTime(SYSTEM_TIME_MONOTONIC);
58     }
59 
60     // Set the condition and return
61     manager->mState = true;
62     return scopedLock;
63 }
64 
waitAndAcquire(const std::shared_ptr<WaitableMutexWrapper> & manager)65 std::unique_ptr<AutoConditionLock> AutoConditionLock::waitAndAcquire(
66         const std::shared_ptr<WaitableMutexWrapper>& manager) {
67 
68     if (manager == nullptr || manager->mMutex == nullptr) {
69         // Bad input, return null
70         return std::unique_ptr<AutoConditionLock>{nullptr};
71     }
72 
73     // Acquire scoped lock
74     std::unique_ptr<AutoConditionLock> scopedLock(new AutoConditionLock(manager));
75 
76     // Wait until we timeout, or success
77     while(manager->mState) {
78         status_t ret = manager->mCondition.wait(*(manager->mMutex));
79         if (ret != NO_ERROR) {
80             // Timed out or whatever, return null
81             return std::unique_ptr<AutoConditionLock>{nullptr};
82         }
83     }
84 
85     // Set the condition and return
86     manager->mState = true;
87     return scopedLock;
88 }
89 
90 }; // namespace android
91