diff options
| author | 2024-02-05 22:55:46 +0000 | |
|---|---|---|
| committer | 2024-02-06 22:54:00 +0000 | |
| commit | 73ba92019b8508cc7551a1835237b34f7018bd5c (patch) | |
| tree | 5aee81bf3bd2ac1b45c5fc3b59966d3dfc87240d | |
| parent | a2d3cf16815babdcdd528a0c6733b0b0c121323c (diff) | |
InputTracer: Use explicit thread wake conditions
This is a small cleanup to use the stop_predicate for the
std::condition_variable::wait() method to make the waking conditions
explicit and more readable.
Also, move the events buffers out of the thread loop to avoid the need
to reallocate it for each iteration.
Bug: 210460522
Test: atest inputflinger_tests
Change-Id: Ic56a6f2cf42fe2eee752e1d34fd347a3f5421991
| -rw-r--r-- | services/inputflinger/dispatcher/trace/InputTracer.cpp | 16 |
1 files changed, 9 insertions, 7 deletions
diff --git a/services/inputflinger/dispatcher/trace/InputTracer.cpp b/services/inputflinger/dispatcher/trace/InputTracer.cpp index b065729873..8a855c2035 100644 --- a/services/inputflinger/dispatcher/trace/InputTracer.cpp +++ b/services/inputflinger/dispatcher/trace/InputTracer.cpp @@ -154,19 +154,21 @@ std::optional<InputTracer::EventState>& InputTracer::getState(const EventTracker void InputTracer::threadLoop() { androidSetThreadName("InputTracer"); + std::vector<const EventState> eventsToTrace; + std::vector<const WindowDispatchArgs> dispatchEventsToTrace; + while (true) { - std::vector<const EventState> eventsToTrace; - std::vector<const WindowDispatchArgs> dispatchEventsToTrace; - { + { // acquire lock std::unique_lock lock(mLock); base::ScopedLockAssertion assumeLocked(mLock); + + // Wait until we need to process more events or exit. + mThreadWakeCondition.wait(lock, [&]() REQUIRES(mLock) { + return mThreadExit || !mTraceQueue.empty() || !mDispatchTraceQueue.empty(); + }); if (mThreadExit) { return; } - if (mTraceQueue.empty() && mDispatchTraceQueue.empty()) { - // Wait indefinitely until the thread is awoken. - mThreadWakeCondition.wait(lock); - } mTraceQueue.swap(eventsToTrace); mDispatchTraceQueue.swap(dispatchEventsToTrace); |