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