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
18#ifndef ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
19#define ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
20
21#include <utils/Timers.h>
22#include <utils/Condition.h>
23#include <utils/Errors.h>
24#include <utils/Mutex.h>
25
26#include <memory>
27
28namespace android {
29
30/**
31 * WaitableMutexWrapper can be used with AutoConditionLock to construct scoped locks for the
32 * wrapped Mutex with timeouts for lock acquisition.
33 */
34class WaitableMutexWrapper {
35    friend class AutoConditionLock;
36public:
37    /**
38     * Construct the ConditionManger with the given Mutex.
39     */
40    WaitableMutexWrapper(Mutex* mutex);
41
42    virtual ~WaitableMutexWrapper();
43private:
44    Mutex* mMutex;
45    bool mState;
46    Condition mCondition;
47};
48
49/**
50 * AutoConditionLock is a scoped lock similar to Mutex::Autolock, but allows timeouts to be
51 * specified for lock acquisition.
52 *
53 * AutoConditionLock is used with a WaitableMutexWrapper to lock/unlock the WaitableMutexWrapper's
54 * wrapped Mutex, and wait/set/signal the WaitableMutexWrapper's wrapped condition. To use this,
55 * call AutoConditionLock::waitAndAcquire to get an instance.  This will:
56 *      - Lock the given WaitableMutexWrapper's mutex.
57 *      - Wait for the WaitableMutexWrapper's condition to become false, or timeout.
58 *      - Set the WaitableMutexWrapper's condition to true.
59 *
60 * When the AutoConditionLock goes out of scope and is destroyed, this will:
61 *      - Set the WaitableMutexWrapper's condition to false.
62 *      - Signal threads waiting on this condition to wakeup.
63 *      - Release WaitableMutexWrapper's mutex.
64 */
65class AutoConditionLock final {
66public:
67    AutoConditionLock() = delete;
68    AutoConditionLock(const AutoConditionLock& other) = delete;
69    AutoConditionLock & operator=(const AutoConditionLock&) = delete;
70
71    ~AutoConditionLock();
72
73    /**
74     * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting up to waitTime
75     * nanoseconds to acquire the WaitableMutexWrapper's wrapped lock.
76     *
77     * Return an empty unique_ptr if this fails, or a timeout occurs.
78     */
79    static std::unique_ptr<AutoConditionLock> waitAndAcquire(
80            const std::shared_ptr<WaitableMutexWrapper>& manager, nsecs_t waitTime);
81
82    /**
83     * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting indefinitely to
84     * acquire the WaitableMutexWrapper's wrapped lock.
85     *
86     * Return an empty unique_ptr if this fails.
87     */
88    static std::unique_ptr<AutoConditionLock> waitAndAcquire(
89            const std::shared_ptr<WaitableMutexWrapper>& manager);
90private:
91    AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper>& manager);
92
93    std::shared_ptr<WaitableMutexWrapper> mManager;
94    Mutex::Autolock mAutoLock;
95    bool mAcquired;
96};
97
98}; // namespace android
99
100#endif // ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
101