blob: 37f41beea95cdf7d87fb8f887dcf12ed9d0f4fbd [file] [log] [blame]
Yifan Honge2dadf02017-02-14 15:43:31 -08001/*
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 Moreland0b8e3872019-06-25 08:54:34 -070017#pragma once
18
Yifan Hong2cfbc512024-02-01 07:32:00 +000019#include <condition_variable>
Yifan Honge2dadf02017-02-14 15:43:31 -080020#include <chrono>
Yifan Hong2cfbc512024-02-01 07:32:00 +000021#include <functional>
22#include <mutex>
23#include <thread>
Yifan Honge2dadf02017-02-14 15:43:31 -080024
25#include <hidl/Status.h>
26
27namespace android {
28namespace lshal {
29
Yifan Hong2cfbc512024-02-01 07:32:00 +000030class BackgroundTaskState {
31public:
32 explicit BackgroundTaskState(std::function<void(void)> &&func)
33 : mFunc(std::forward<decltype(func)>(func)) {}
34 void notify() {
35 std::unique_lock<std::mutex> lock(mMutex);
36 mFinished = true;
37 lock.unlock();
38 mCondVar.notify_all();
39 }
40 template<class C, class D>
41 bool wait(std::chrono::time_point<C, D> end) {
42 std::unique_lock<std::mutex> lock(mMutex);
43 mCondVar.wait_until(lock, end, [this](){ return this->mFinished; });
44 return mFinished;
45 }
46 void operator()() {
47 mFunc();
48 }
49private:
50 std::mutex mMutex;
51 std::condition_variable mCondVar;
52 bool mFinished = false;
53 std::function<void(void)> mFunc;
54};
55
56void *callAndNotify(void *data) {
57 BackgroundTaskState &state = *static_cast<BackgroundTaskState *>(data);
58 state();
59 state.notify();
60 return nullptr;
61}
62
63template<class R, class P>
64bool timeout(std::chrono::duration<R, P> delay, std::function<void(void)> &&func) {
65 auto now = std::chrono::system_clock::now();
66 BackgroundTaskState state{std::forward<decltype(func)>(func)};
67 pthread_t thread;
68 if (pthread_create(&thread, nullptr, callAndNotify, &state)) {
69 std::cerr << "FATAL: could not create background thread." << std::endl;
70 return false;
71 }
72 bool success = state.wait(now + delay);
73 if (!success) {
74 pthread_kill(thread, SIGINT);
75 }
76 pthread_join(thread, nullptr);
77 return success;
78}
79
Yifan Hongf2d557b2017-05-24 19:45:02 -070080template<class R, class P, class Function, class I, class... Args>
Tomasz Wasilczyk2ed90a52024-01-09 15:44:58 -080081typename std::invoke_result<Function, I *, Args...>::type
Yifan Hongf2d557b2017-05-24 19:45:02 -070082timeoutIPC(std::chrono::duration<R, P> wait, const sp<I> &interfaceObject, Function &&func,
83 Args &&... args) {
Yifan Honge2dadf02017-02-14 15:43:31 -080084 using ::android::hardware::Status;
Yifan Hong2cfbc512024-02-01 07:32:00 +000085 typename std::result_of<Function(I *, Args...)>::type ret{Status::ok()};
86 auto boundFunc = std::bind(std::forward<Function>(func),
87 interfaceObject.get(), std::forward<Args>(args)...);
88 bool success = timeout(wait, [&ret, &boundFunc] {
89 ret = std::move(boundFunc());
90 });
91 if (!success) {
Yifan Honge2dadf02017-02-14 15:43:31 -080092 return Status::fromStatusT(TIMED_OUT);
93 }
Yifan Hong2cfbc512024-02-01 07:32:00 +000094 return ret;
Yifan Honge2dadf02017-02-14 15:43:31 -080095}
Yifan Hong2cfbc512024-02-01 07:32:00 +000096
97} // namespace lshal
98} // namespace android