summaryrefslogtreecommitdiffstats
path: root/services/camera/libcameraservice/utils/AutoConditionLock.h
blob: 9a3eafcf969113a23f7ebadec89b3df979da7ffd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


#ifndef ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H
#define ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H

#include <utils/Timers.h>
#include <utils/Condition.h>
#include <utils/Errors.h>
#include <utils/Mutex.h>

#include <memory>

namespace android {

/**
 * WaitableMutexWrapper can be used with AutoConditionLock to construct scoped locks for the
 * wrapped Mutex with timeouts for lock acquisition.
 */
class WaitableMutexWrapper {
    friend class AutoConditionLock;
public:
    /**
     * Construct the ConditionManger with the given Mutex.
     */
    WaitableMutexWrapper(Mutex* mutex);

    virtual ~WaitableMutexWrapper();
private:
    Mutex* mMutex;
    bool mState;
    Condition mCondition;
};

/**
 * AutoConditionLock is a scoped lock similar to Mutex::Autolock, but allows timeouts to be
 * specified for lock acquisition.
 *
 * AutoConditionLock is used with a WaitableMutexWrapper to lock/unlock the WaitableMutexWrapper's
 * wrapped Mutex, and wait/set/signal the WaitableMutexWrapper's wrapped condition. To use this,
 * call AutoConditionLock::waitAndAcquire to get an instance.  This will:
 *      - Lock the given WaitableMutexWrapper's mutex.
 *      - Wait for the WaitableMutexWrapper's condition to become false, or timeout.
 *      - Set the WaitableMutexWrapper's condition to true.
 *
 * When the AutoConditionLock goes out of scope and is destroyed, this will:
 *      - Set the WaitableMutexWrapper's condition to false.
 *      - Signal threads waiting on this condition to wakeup.
 *      - Release WaitableMutexWrapper's mutex.
 */
class AutoConditionLock final {
public:
    AutoConditionLock() = delete;
    AutoConditionLock(const AutoConditionLock& other) = delete;
    AutoConditionLock & operator=(const AutoConditionLock&) = delete;

    ~AutoConditionLock();

    /**
     * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting up to waitTime
     * nanoseconds to acquire the WaitableMutexWrapper's wrapped lock.
     *
     * Return an empty unique_ptr if this fails, or a timeout occurs.
     */
    static std::unique_ptr<AutoConditionLock> waitAndAcquire(
            const std::shared_ptr<WaitableMutexWrapper>& manager, nsecs_t waitTime);

    /**
     * Make a new AutoConditionLock from a given WaitableMutexWrapper, waiting indefinitely to
     * acquire the WaitableMutexWrapper's wrapped lock.
     *
     * Return an empty unique_ptr if this fails.
     */
    static std::unique_ptr<AutoConditionLock> waitAndAcquire(
            const std::shared_ptr<WaitableMutexWrapper>& manager);
private:
    AutoConditionLock(const std::shared_ptr<WaitableMutexWrapper>& manager);

    std::shared_ptr<WaitableMutexWrapper> mManager;
    Mutex::Autolock mAutoLock;
};

}; // namespace android

#endif // ANDROID_SERVICE_UTILS_SCOPED_CONDITION_H