1 /* 2 * Copyright 2024 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 // Keeps track of groups of channels that all use the same EventFlag 18 19 #pragma once 20 21 #include <android-base/thread_annotations.h> 22 23 #include <array> 24 #include <mutex> 25 #include <optional> 26 #include <thread> 27 28 #include "AdpfTypes.h" 29 #include "PowerHintSession.h" 30 #include "PowerSessionManager.h" 31 #include "SessionChannel.h" 32 33 namespace aidl::google::hardware::power::impl::pixel { 34 // This manages a group of FMQ channels that are watched by a single thread. This class manages the 35 // channels, the thread that waits on their callbacks, and the mutex for that thread. 36 // 37 // We have the channels guarded by a lock in this class and the channel count guarded by 38 // the manager, because adding/removing is only done by the manager while locked. 39 // Thus, only the manager lock is required to count the group size when figuring out where 40 // to insert a new channel. 41 template <class PowerSessionManagerT = PowerSessionManager<>, 42 class PowerHintSessionT = PowerHintSession<>> 43 class ChannelGroup : public Immobile { 44 public: 45 ~ChannelGroup(); 46 ChannelGroup(int32_t id); 47 bool removeChannel(int32_t slot) EXCLUDES(mGroupMutex); 48 int32_t getChannelCount() const; 49 // Returns the channel ID 50 std::shared_ptr<SessionChannel> createChannel(int32_t tgid, int32_t uid) EXCLUDES(mGroupMutex); 51 std::shared_ptr<SessionChannel> getChannel(int32_t slot) EXCLUDES(mGroupMutex); 52 void getFlagDesc(std::optional<FlagQueueDesc> *_return_desc) const; 53 54 private: 55 void runChannelGroup() EXCLUDES(mGroupMutex); 56 57 // Guard the number of channels with the global lock, so we only need one 58 // lock in order to figure out where to insert new sessions, instead of getting 59 // a lock for each channelgroup. 60 int32_t mLiveChannels = 0; 61 const int32_t mGroupId; 62 63 // Tracks whether the group is currently being destructed, used to kill the helper thread 64 bool mDestructing = false; 65 // Used to guard items internal to the FMQ thread 66 std::mutex mGroupMutex; GUARDED_BY(mGroupMutex)67 std::array<std::shared_ptr<SessionChannel>, kMaxChannels> mChannels GUARDED_BY(mGroupMutex){}; 68 const std::shared_ptr<FlagQueue> mFlagQueue; 69 70 std::thread mGroupThread; 71 }; 72 73 } // namespace aidl::google::hardware::power::impl::pixel 74