Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Steven Moreland | 0b8e387 | 2019-06-25 08:54:34 -0700 | [diff] [blame] | 17 | #pragma once |
| 18 | |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 19 | #include <chrono> |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 20 | #include <future> |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 21 | |
| 22 | #include <hidl/Status.h> |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 23 | #include <utils/Errors.h> |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | namespace lshal { |
| 27 | |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 28 | // Call function on interfaceObject and wait for result until the given timeout has reached. |
| 29 | // Callback functions pass to timeoutIPC() may be executed after the this function |
| 30 | // has returned, especially if deadline has been reached. Hence, care must be taken when passing |
| 31 | // data between the background thread and the main thread. See b/311143089. |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 32 | template<class R, class P, class Function, class I, class... Args> |
Tomasz Wasilczyk | 2ed90a5 | 2024-01-09 15:44:58 -0800 | [diff] [blame] | 33 | typename std::invoke_result<Function, I *, Args...>::type |
Yifan Hong | f2d557b | 2017-05-24 19:45:02 -0700 | [diff] [blame] | 34 | timeoutIPC(std::chrono::duration<R, P> wait, const sp<I> &interfaceObject, Function &&func, |
| 35 | Args &&... args) { |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 36 | using ::android::hardware::Status; |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 37 | |
| 38 | // Execute on a background thread but do not defer execution. |
| 39 | auto future = |
| 40 | std::async(std::launch::async, func, interfaceObject, std::forward<Args>(args)...); |
| 41 | auto status = future.wait_for(wait); |
| 42 | if (status == std::future_status::ready) { |
| 43 | return future.get(); |
| 44 | } |
| 45 | |
| 46 | // This future belongs to a background thread that we no longer care about. |
| 47 | // Putting this in the global list avoids std::future::~future() that may wait for the |
| 48 | // result to come back. |
| 49 | // This leaks memory, but lshal is a debugging tool, so this is fine. |
| 50 | static std::vector<decltype(future)> gDeadPool{}; |
| 51 | gDeadPool.emplace_back(std::move(future)); |
| 52 | |
| 53 | if (status == std::future_status::timeout) { |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 54 | return Status::fromStatusT(TIMED_OUT); |
| 55 | } |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 56 | return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_STATE, "Illegal future_status"); |
Yifan Hong | e2dadf0 | 2017-02-14 15:43:31 -0800 | [diff] [blame] | 57 | } |
Yifan Hong | 1513627 | 2024-02-01 16:36:37 -0800 | [diff] [blame] | 58 | } // namespace lshal |
| 59 | } // namespace android |