1 /*
2  * Copyright (C) 2021 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 #ifndef android_hardware_automotive_vehicle_aidl_impl_vhal_include_PendingRequestPool_H_
18 #define android_hardware_automotive_vehicle_aidl_impl_vhal_include_PendingRequestPool_H_
19 
20 #include <VehicleUtils.h>
21 #include <android-base/result.h>
22 #include <android-base/thread_annotations.h>
23 
24 #include <list>
25 #include <mutex>
26 #include <thread>
27 #include <unordered_map>
28 #include <unordered_set>
29 
30 namespace android {
31 namespace hardware {
32 namespace automotive {
33 namespace vehicle {
34 
35 // A thread-safe pending request pool that tracks whether each request has timed-out.
36 class PendingRequestPool final {
37   public:
38     using TimeoutCallbackFunc = std::function<void(const std::unordered_set<int64_t>&)>;
39 
40     explicit PendingRequestPool(int64_t timeoutInNano);
41 
42     ~PendingRequestPool();
43 
44     // Adds a list of requests to the request pool.
45     // The clientId is the key for all the requests. It could be a number or an address to a data
46     // structure that represents a client. The caller must maintain this data structure.
47     // All the request IDs must be unique for one client, if any of the requestIds is duplicate with
48     // any pending request IDs for the client, this function returns error and no requests would be
49     // added. Otherwise, they would be added to the request pool.
50     // The callback would be called if requests are not finished within {@code mTimeoutInNano}
51     // seconds.
52     VhalResult<void> addRequests(const void* clientId,
53                                  const std::unordered_set<int64_t>& requestIds,
54                                  std::shared_ptr<const TimeoutCallbackFunc> callback);
55 
56     // Checks whether the request is currently pending.
57     bool isRequestPending(const void* clientId, int64_t requestId) const;
58 
59     // Tries to mark the requests as finished and remove them from the pool if the request is
60     // currently pending. Returns the list of request that is pending and has been finished
61     // successfully. This function would try to finish any valid requestIds even though some of the
62     // requestIds are not valid.
63     std::unordered_set<int64_t> tryFinishRequests(const void* clientId,
64                                                   const std::unordered_set<int64_t>& requestIds);
65 
66     // Returns how many pending requests in the pool, for testing purpose.
67     size_t countPendingRequests(const void* clientId) const;
68 
69     size_t countPendingRequests() const;
70 
71   private:
72     // The maximum number of pending requests allowed per client. If exceeds this number, adding
73     // more requests would fail. This is to prevent spamming from client.
74     static constexpr size_t MAX_PENDING_REQUEST_PER_CLIENT = 10000;
75 
76     struct PendingRequest {
77         std::unordered_set<int64_t> requestIds;
78         int64_t timeoutTimestamp;
79         std::shared_ptr<const TimeoutCallbackFunc> callback;
80     };
81 
82     int64_t mTimeoutInNano;
83     mutable std::mutex mLock;
84     std::unordered_map<const void*, std::list<PendingRequest>> mPendingRequestsByClient
85             GUARDED_BY(mLock);
86     std::thread mThread;
87     bool mThreadStop = false;
88     std::condition_variable mCv;
89     std::mutex mCvLock;
90 
91     bool isRequestPendingLocked(const void* clientId, int64_t requestId) const REQUIRES(mLock);
92 
93     // Checks whether the requests in the pool has timed-out, run periodically in a separate thread.
94     void checkTimeout();
95 };
96 
97 }  // namespace vehicle
98 }  // namespace automotive
99 }  // namespace hardware
100 }  // namespace android
101 
102 #endif  // android_hardware_automotive_vehicle_aidl_impl_vhal_include_PendingRequestPool_H_
103