blob: e8d22d9b581fde1a742a0734a9c40a50dd8e3df8 [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 Honge2dadf02017-02-14 15:43:31 -080019#include <condition_variable>
20#include <chrono>
21#include <functional>
22#include <mutex>
23#include <thread>
24
25#include <hidl/Status.h>
26
27namespace android {
28namespace lshal {
29
30static constexpr std::chrono::milliseconds IPC_CALL_WAIT{500};
31
32class BackgroundTaskState {
33public:
Chih-Hung Hsieh45e31c72018-12-20 15:47:01 -080034 explicit BackgroundTaskState(std::function<void(void)> &&func)
Yifan Honga57dffb2017-02-21 14:59:00 -080035 : mFunc(std::forward<decltype(func)>(func)) {}
Yifan Honge2dadf02017-02-14 15:43:31 -080036 void notify() {
37 std::unique_lock<std::mutex> lock(mMutex);
38 mFinished = true;
39 lock.unlock();
40 mCondVar.notify_all();
41 }
42 template<class C, class D>
43 bool wait(std::chrono::time_point<C, D> end) {
44 std::unique_lock<std::mutex> lock(mMutex);
45 mCondVar.wait_until(lock, end, [this](){ return this->mFinished; });
46 return mFinished;
47 }
Yifan Honga57dffb2017-02-21 14:59:00 -080048 void operator()() {
49 mFunc();
50 }
Yifan Honge2dadf02017-02-14 15:43:31 -080051private:
52 std::mutex mMutex;
53 std::condition_variable mCondVar;
54 bool mFinished = false;
Yifan Honga57dffb2017-02-21 14:59:00 -080055 std::function<void(void)> mFunc;
Yifan Honge2dadf02017-02-14 15:43:31 -080056};
57
Yifan Honga57dffb2017-02-21 14:59:00 -080058void *callAndNotify(void *data) {
59 BackgroundTaskState &state = *static_cast<BackgroundTaskState *>(data);
60 state();
61 state.notify();
Yi Kong19d5c002018-07-20 13:39:55 -070062 return nullptr;
Yifan Honga57dffb2017-02-21 14:59:00 -080063}
64
Yifan Honge2dadf02017-02-14 15:43:31 -080065template<class R, class P>
Yifan Honga57dffb2017-02-21 14:59:00 -080066bool timeout(std::chrono::duration<R, P> delay, std::function<void(void)> &&func) {
Yifan Honge2dadf02017-02-14 15:43:31 -080067 auto now = std::chrono::system_clock::now();
Yifan Honga57dffb2017-02-21 14:59:00 -080068 BackgroundTaskState state{std::forward<decltype(func)>(func)};
69 pthread_t thread;
Yi Kong19d5c002018-07-20 13:39:55 -070070 if (pthread_create(&thread, nullptr, callAndNotify, &state)) {
Yifan Honga57dffb2017-02-21 14:59:00 -080071 std::cerr << "FATAL: could not create background thread." << std::endl;
72 return false;
73 }
Yifan Honge2dadf02017-02-14 15:43:31 -080074 bool success = state.wait(now + delay);
Yifan Honga57dffb2017-02-21 14:59:00 -080075 if (!success) {
76 pthread_kill(thread, SIGINT);
77 }
Yi Kong19d5c002018-07-20 13:39:55 -070078 pthread_join(thread, nullptr);
Yifan Honge2dadf02017-02-14 15:43:31 -080079 return success;
80}
81
Yifan Hongf2d557b2017-05-24 19:45:02 -070082template<class R, class P, class Function, class I, class... Args>
Yifan Honge2dadf02017-02-14 15:43:31 -080083typename std::result_of<Function(I *, Args...)>::type
Yifan Hongf2d557b2017-05-24 19:45:02 -070084timeoutIPC(std::chrono::duration<R, P> wait, const sp<I> &interfaceObject, Function &&func,
85 Args &&... args) {
Yifan Honge2dadf02017-02-14 15:43:31 -080086 using ::android::hardware::Status;
87 typename std::result_of<Function(I *, Args...)>::type ret{Status::ok()};
88 auto boundFunc = std::bind(std::forward<Function>(func),
89 interfaceObject.get(), std::forward<Args>(args)...);
Yifan Hongf2d557b2017-05-24 19:45:02 -070090 bool success = timeout(wait, [&ret, &boundFunc] {
Yifan Hong38903c02017-02-17 13:38:47 -080091 ret = std::move(boundFunc());
Yifan Honge2dadf02017-02-14 15:43:31 -080092 });
93 if (!success) {
94 return Status::fromStatusT(TIMED_OUT);
95 }
96 return ret;
97}
98
Yifan Hongf2d557b2017-05-24 19:45:02 -070099template<class Function, class I, class... Args>
100typename std::result_of<Function(I *, Args...)>::type
101timeoutIPC(const sp<I> &interfaceObject, Function &&func, Args &&... args) {
102 return timeoutIPC(IPC_CALL_WAIT, interfaceObject, func, args...);
103}
104
105
Yifan Honge2dadf02017-02-14 15:43:31 -0800106} // namespace lshal
107} // namespace android