Revert "lshal: use std::async"
This reverts commit 9e509aba9efba4de1253acd332745521d571a474.
Reason for revert: broke adb shell lshal debug
Bug: 323268003
Change-Id: I005ae901dc347a0f4d3ad617ab148c8023786e7b
diff --git a/cmds/lshal/Timeout.h b/cmds/lshal/Timeout.h
index 59ca3bf..805e8dc 100644
--- a/cmds/lshal/Timeout.h
+++ b/cmds/lshal/Timeout.h
@@ -16,44 +16,83 @@
#pragma once
+#include <condition_variable>
#include <chrono>
-#include <future>
+#include <functional>
+#include <mutex>
+#include <thread>
#include <hidl/Status.h>
-#include <utils/Errors.h>
namespace android {
namespace lshal {
-// Call function on interfaceObject and wait for result until the given timeout has reached.
-// Callback functions pass to timeoutIPC() may be executed after the this function
-// has returned, especially if deadline has been reached. Hence, care must be taken when passing
-// data between the background thread and the main thread. See b/311143089.
+class BackgroundTaskState {
+public:
+ explicit BackgroundTaskState(std::function<void(void)> &&func)
+ : mFunc(std::forward<decltype(func)>(func)) {}
+ void notify() {
+ std::unique_lock<std::mutex> lock(mMutex);
+ mFinished = true;
+ lock.unlock();
+ mCondVar.notify_all();
+ }
+ template<class C, class D>
+ bool wait(std::chrono::time_point<C, D> end) {
+ std::unique_lock<std::mutex> 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<void(void)> mFunc;
+};
+
+void *callAndNotify(void *data) {
+ BackgroundTaskState &state = *static_cast<BackgroundTaskState *>(data);
+ state();
+ state.notify();
+ return nullptr;
+}
+
+template<class R, class P>
+bool timeout(std::chrono::duration<R, P> delay, std::function<void(void)> &&func) {
+ auto now = std::chrono::system_clock::now();
+ BackgroundTaskState state{std::forward<decltype(func)>(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<class R, class P, class Function, class I, class... Args>
typename std::result_of<Function(I *, Args...)>::type
timeoutIPC(std::chrono::duration<R, P> wait, const sp<I> &interfaceObject, Function &&func,
Args &&... args) {
using ::android::hardware::Status;
-
- // Execute on a background thread but do not defer execution.
- auto future =
- std::async(std::launch::async, func, interfaceObject, std::forward<Args>(args)...);
- auto status = future.wait_for(wait);
- if (status == std::future_status::ready) {
- return future.get();
- }
-
- // This future belongs to a background thread that we no longer care about.
- // Putting this in the global list avoids std::future::~future() that may wait for the
- // result to come back.
- // This leaks memory, but lshal is a debugging tool, so this is fine.
- static std::vector<decltype(future)> gDeadPool{};
- gDeadPool.emplace_back(std::move(future));
-
- if (status == std::future_status::timeout) {
+ typename std::result_of<Function(I *, Args...)>::type ret{Status::ok()};
+ auto boundFunc = std::bind(std::forward<Function>(func),
+ interfaceObject.get(), std::forward<Args>(args)...);
+ bool success = timeout(wait, [&ret, &boundFunc] {
+ ret = std::move(boundFunc());
+ });
+ if (!success) {
return Status::fromStatusT(TIMED_OUT);
}
- return Status::fromExceptionCode(Status::Exception::EX_ILLEGAL_STATE, "Illegal future_status");
+ return ret;
}
-} // namespace lshal
-} // namespace android
+
+} // namespace lshal
+} // namespace android