1*38e8c45fSAndroid Build Coastguard Worker /*
2*38e8c45fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project
3*38e8c45fSAndroid Build Coastguard Worker *
4*38e8c45fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License");
5*38e8c45fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License.
6*38e8c45fSAndroid Build Coastguard Worker * You may obtain a copy of the License at
7*38e8c45fSAndroid Build Coastguard Worker *
8*38e8c45fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0
9*38e8c45fSAndroid Build Coastguard Worker *
10*38e8c45fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software
11*38e8c45fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS,
12*38e8c45fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*38e8c45fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and
14*38e8c45fSAndroid Build Coastguard Worker * limitations under the License.
15*38e8c45fSAndroid Build Coastguard Worker */
16*38e8c45fSAndroid Build Coastguard Worker
17*38e8c45fSAndroid Build Coastguard Worker #pragma once
18*38e8c45fSAndroid Build Coastguard Worker
19*38e8c45fSAndroid Build Coastguard Worker #include <chrono>
20*38e8c45fSAndroid Build Coastguard Worker #include <future>
21*38e8c45fSAndroid Build Coastguard Worker
22*38e8c45fSAndroid Build Coastguard Worker #include <hidl/Status.h>
23*38e8c45fSAndroid Build Coastguard Worker #include <utils/Errors.h>
24*38e8c45fSAndroid Build Coastguard Worker
25*38e8c45fSAndroid Build Coastguard Worker namespace android {
26*38e8c45fSAndroid Build Coastguard Worker namespace lshal {
27*38e8c45fSAndroid Build Coastguard Worker
28*38e8c45fSAndroid Build Coastguard Worker // Call function on interfaceObject and wait for result until the given timeout has reached.
29*38e8c45fSAndroid Build Coastguard Worker // Callback functions pass to timeoutIPC() may be executed after the this function
30*38e8c45fSAndroid Build Coastguard Worker // has returned, especially if deadline has been reached. Hence, care must be taken when passing
31*38e8c45fSAndroid Build Coastguard Worker // data between the background thread and the main thread. See b/311143089.
32*38e8c45fSAndroid Build Coastguard Worker template<class R, class P, class Function, class I, class... Args>
33*38e8c45fSAndroid Build Coastguard Worker typename std::invoke_result<Function, I *, Args...>::type
timeoutIPC(std::chrono::duration<R,P> wait,const sp<I> & interfaceObject,Function && func,Args &&...args)34*38e8c45fSAndroid Build Coastguard Worker timeoutIPC(std::chrono::duration<R, P> wait, const sp<I> &interfaceObject, Function &&func,
35*38e8c45fSAndroid Build Coastguard Worker Args &&... args) {
36*38e8c45fSAndroid Build Coastguard Worker using ::android::hardware::Status;
37*38e8c45fSAndroid Build Coastguard Worker
38*38e8c45fSAndroid Build Coastguard Worker // Execute on a background thread but do not defer execution.
39*38e8c45fSAndroid Build Coastguard Worker auto future =
40*38e8c45fSAndroid Build Coastguard Worker std::async(std::launch::async, func, interfaceObject, std::forward<Args>(args)...);
41*38e8c45fSAndroid Build Coastguard Worker auto status = future.wait_for(wait);
42*38e8c45fSAndroid Build Coastguard Worker if (status == std::future_status::ready) {
43*38e8c45fSAndroid Build Coastguard Worker return future.get();
44*38e8c45fSAndroid Build Coastguard Worker }
45*38e8c45fSAndroid Build Coastguard Worker
46*38e8c45fSAndroid Build Coastguard Worker // This future belongs to a background thread that we no longer care about.
47*38e8c45fSAndroid Build Coastguard Worker // Putting this in the global list avoids std::future::~future() that may wait for the
48*38e8c45fSAndroid Build Coastguard Worker // result to come back.
49*38e8c45fSAndroid Build Coastguard Worker // This leaks memory, but lshal is a debugging tool, so this is fine.
50*38e8c45fSAndroid Build Coastguard Worker static std::vector<decltype(future)> gDeadPool{};
51*38e8c45fSAndroid Build Coastguard Worker gDeadPool.emplace_back(std::move(future));
52*38e8c45fSAndroid Build Coastguard Worker
53*38e8c45fSAndroid Build Coastguard Worker if (status == std::future_status::timeout) {
54*38e8c45fSAndroid Build Coastguard Worker return Status::fromStatusT(TIMED_OUT);
55*38e8c45fSAndroid Build Coastguard Worker }
56*38e8c45fSAndroid Build Coastguard Worker return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_STATE, "Illegal future_status");
57*38e8c45fSAndroid Build Coastguard Worker }
58*38e8c45fSAndroid Build Coastguard Worker } // namespace lshal
59*38e8c45fSAndroid Build Coastguard Worker } // namespace android
60