blob: a151f1566fc602f10ae7aaffff5d7dd2c46174a8 [file] [log] [blame]
Stan Rokitad0cd57d2019-09-17 15:52:51 -07001/*
2 * Copyright (C) 2019 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#pragma once
18
19#include <mutex>
20
21namespace android {
22namespace hardware {
23namespace sensors {
24namespace V2_0 {
25namespace implementation {
26
27class IScopedWakelockRefCounter {
28 public:
29 virtual void incrementRefCountAndMaybeAcquireWakelock() = 0;
30 virtual void decrementRefCountAndMaybeReleaseWakelock() = 0;
31 // Virtual dtor needed for compilation success
32 virtual ~IScopedWakelockRefCounter(){};
33};
34
35/**
36 * Wrapper around wake lock acquisition functions (acquire/release_wake_lock) that provides a
37 * RAII-style mechanism for keeping a wake lock held for the duration of a scoped block.
38 * When a ScopedWakelock is created, it increments the reference count stored in the HalProxy
39 * for the sub-HALs specific wake lock, acquiring the wake lock if necessary. When the object goes
40 * out of scope, the ref count is decremented, potentially releasing the wake lock if no other
41 * references to the wake lock exist.
42 *
43 * This class is allocated through the createScopedWakelock callback inside the IHalProxyCallback
44 * provided to sub-HALs during initialization and should be used for all wake lock acquisition
45 * inside of the sub-HAL to ensure wake locks are not held indefinitely.
46 *
47 * The most prevalent use case for this class will be for posting events to the framework through
48 * the postEvents HalProxy callback. The expectation is that sub-HALs will create this
49 * ScopedWakelock through the createScopedWakelock upon receiving a sensor events. The lock boolean
50 * provided to createScopedWakelock will be set the according to whether the sensor events are
51 * from wakeup sensors. Then, the sub-HAL will perform any processing necessary before invoking the
52 * postEvents callback passing in the previously created ScopedWakelock. At this point, ownership
53 * of the object will be passed to the HalProxy that will then be responsible for ensuring any
54 * wake locks continue to be held, if necessary.
55 */
56class ScopedWakelock {
57 public:
58 ScopedWakelock(ScopedWakelock&&) = default;
59 ScopedWakelock& operator=(ScopedWakelock&&) = default;
60 virtual ~ScopedWakelock();
61
62 bool isLocked() const { return mLocked; }
63
64 private:
65 friend class HalProxyCallback;
66 IScopedWakelockRefCounter* mRefCounter;
67 bool mLocked;
68 ScopedWakelock(IScopedWakelockRefCounter* refCounter, bool locked);
69 ScopedWakelock(const ScopedWakelock&) = delete;
70 ScopedWakelock& operator=(const ScopedWakelock&) = delete;
71};
72
73} // namespace implementation
74} // namespace V2_0
75} // namespace sensors
76} // namespace hardware
77} // namespace android