/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include namespace android { namespace lshal { static constexpr std::chrono::milliseconds IPC_CALL_WAIT{500}; class BackgroundTaskState { public: explicit BackgroundTaskState(std::function &&func) : mFunc(std::forward(func)) {} void notify() { std::unique_lock lock(mMutex); mFinished = true; lock.unlock(); mCondVar.notify_all(); } template bool wait(std::chrono::time_point end) { std::unique_lock lock(mMutex); mCondVar.wait_until(lock, end, [this](){ return this->mFinished; }); return mFinished; } void operator()() { mFunc(); } private: std::mutex mMutex; std::condition_variable mCondVar; bool mFinished = false; std::function mFunc; }; void *callAndNotify(void *data) { BackgroundTaskState &state = *static_cast(data); state(); state.notify(); return nullptr; } template bool timeout(std::chrono::duration delay, std::function &&func) { auto now = std::chrono::system_clock::now(); BackgroundTaskState state{std::forward(func)}; pthread_t thread; if (pthread_create(&thread, nullptr, callAndNotify, &state)) { std::cerr << "FATAL: could not create background thread." << std::endl; return false; } bool success = state.wait(now + delay); if (!success) { pthread_kill(thread, SIGINT); } pthread_join(thread, nullptr); return success; } template typename std::result_of::type timeoutIPC(std::chrono::duration wait, const sp &interfaceObject, Function &&func, Args &&... args) { using ::android::hardware::Status; typename std::result_of::type ret{Status::ok()}; auto boundFunc = std::bind(std::forward(func), interfaceObject.get(), std::forward(args)...); bool success = timeout(wait, [&ret, &boundFunc] { ret = std::move(boundFunc()); }); if (!success) { return Status::fromStatusT(TIMED_OUT); } return ret; } template typename std::result_of::type timeoutIPC(const sp &interfaceObject, Function &&func, Args &&... args) { return timeoutIPC(IPC_CALL_WAIT, interfaceObject, func, args...); } } // namespace lshal } // namespace android