Use std::condition_variable in InputDispatcher
To complete the thread safety annotation work, switch over the usage of
AutoMutex to std::mutex.
Bug: 123097103
Test: interact with booted device
Change-Id: I722acd97c4cab285b0439f3c5d173c814936bce0
diff --git a/services/inputflinger/BlockingQueue.h b/services/inputflinger/BlockingQueue.h
index 4b16027..db9f26e 100644
--- a/services/inputflinger/BlockingQueue.h
+++ b/services/inputflinger/BlockingQueue.h
@@ -43,7 +43,7 @@
* Blocks execution while queue is empty.
*/
T pop() {
- std::unique_lock<std::mutex> lock(mLock);
+ std::unique_lock lock(mLock);
android::base::ScopedLockAssertion assumeLock(mLock);
mHasElements.wait(lock, [this]{
android::base::ScopedLockAssertion assumeLock(mLock);
diff --git a/services/inputflinger/InputDispatcher.cpp b/services/inputflinger/InputDispatcher.cpp
index d288736..381bfe8 100644
--- a/services/inputflinger/InputDispatcher.cpp
+++ b/services/inputflinger/InputDispatcher.cpp
@@ -46,6 +46,7 @@
#include "InputDispatcher.h"
#include <errno.h>
+#include <inttypes.h>
#include <limits.h>
#include <sstream>
#include <stddef.h>
@@ -262,7 +263,7 @@
InputDispatcher::~InputDispatcher() {
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
resetKeyRepeatLocked();
releasePendingEventLocked();
@@ -277,8 +278,8 @@
void InputDispatcher::dispatchOnce() {
nsecs_t nextWakeupTime = LONG_LONG_MAX;
{ // acquire lock
- AutoMutex _l(mLock);
- mDispatcherIsAliveCondition.broadcast();
+ std::scoped_lock _l(mLock);
+ mDispatcherIsAlive.notify_all();
// Run a dispatch loop if there are no pending commands.
// The dispatch loop might enqueue commands to run afterwards.
@@ -2261,7 +2262,7 @@
InputDispatcher* d = static_cast<InputDispatcher*>(data);
{ // acquire lock
- AutoMutex _l(d->mLock);
+ std::scoped_lock _l(d->mLock);
ssize_t connectionIndex = d->mConnectionsByFd.indexOfKey(fd);
if (connectionIndex < 0) {
@@ -2509,7 +2510,7 @@
bool needWake;
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
ConfigurationChangedEntry* newEntry =
new ConfigurationChangedEntry(args->sequenceNum, args->eventTime);
@@ -2537,7 +2538,7 @@
newKeyCode = AKEYCODE_HOME;
}
if (newKeyCode != AKEYCODE_UNKNOWN) {
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
struct KeyReplacement replacement = {keyCode, deviceId};
mReplacedKeys.add(replacement, newKeyCode);
keyCode = newKeyCode;
@@ -2547,7 +2548,7 @@
// In order to maintain a consistent stream of up and down events, check to see if the key
// going up is one we've replaced in a down event and haven't yet replaced in an up event,
// even if the modifier was released between the down and the up events.
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
struct KeyReplacement replacement = {keyCode, deviceId};
ssize_t index = mReplacedKeys.indexOfKey(replacement);
if (index >= 0) {
@@ -2742,7 +2743,7 @@
bool needWake;
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
DeviceResetEntry* newEntry =
new DeviceResetEntry(args->sequenceNum, args->eventTime, args->deviceId);
@@ -2896,7 +2897,7 @@
int32_t injectionResult;
{ // acquire lock
- AutoMutex _l(mLock);
+ std::unique_lock _l(mLock);
if (syncMode == INPUT_EVENT_INJECTION_SYNC_NONE) {
injectionResult = INPUT_EVENT_INJECTION_SUCCEEDED;
@@ -2917,7 +2918,7 @@
break;
}
- mInjectionResultAvailableCondition.waitRelative(mLock, remainingTimeout);
+ mInjectionResultAvailable.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
}
if (injectionResult == INPUT_EVENT_INJECTION_SUCCEEDED
@@ -2937,7 +2938,7 @@
break;
}
- mInjectionSyncFinishedCondition.waitRelative(mLock, remainingTimeout);
+ mInjectionSyncFinished.wait_for(_l, std::chrono::nanoseconds(remainingTimeout));
}
}
}
@@ -2988,7 +2989,7 @@
}
injectionState->injectionResult = injectionResult;
- mInjectionResultAvailableCondition.broadcast();
+ mInjectionResultAvailable.notify_all();
}
}
@@ -3005,7 +3006,7 @@
injectionState->pendingForegroundDispatches -= 1;
if (injectionState->pendingForegroundDispatches == 0) {
- mInjectionSyncFinishedCondition.broadcast();
+ mInjectionSyncFinished.notify_all();
}
}
}
@@ -3077,7 +3078,7 @@
ALOGD("setInputWindows displayId=%" PRId32, displayId);
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
// Copy old handles for release if they are no longer present.
const Vector<sp<InputWindowHandle>> oldWindowHandles = getWindowHandlesLocked(displayId);
@@ -3234,7 +3235,7 @@
ALOGD("setFocusedApplication displayId=%" PRId32, displayId);
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
sp<InputApplicationHandle> oldFocusedApplicationHandle =
getValueByKey(mFocusedApplicationHandlesByDisplay, displayId);
@@ -3276,7 +3277,7 @@
ALOGD("setFocusedDisplay displayId=%" PRId32, displayId);
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
if (mFocusedDisplayId != displayId) {
sp<InputWindowHandle> oldFocusedWindowHandle =
@@ -3328,7 +3329,7 @@
bool changed;
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
if (mDispatchEnabled != enabled || mDispatchFrozen != frozen) {
if (mDispatchFrozen && !frozen) {
@@ -3363,7 +3364,7 @@
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
if (mInputFilterEnabled == enabled) {
return;
@@ -3386,7 +3387,7 @@
}
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
sp<InputWindowHandle> fromWindowHandle = getWindowHandleLocked(fromToken);
sp<InputWindowHandle> toWindowHandle = getWindowHandleLocked(toToken);
@@ -3730,7 +3731,7 @@
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
// If InputWindowHandle is null and displayId is not ADISPLAY_ID_NONE,
// treat inputChannel as monitor channel for displayId.
@@ -3772,7 +3773,7 @@
#endif
{ // acquire lock
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
status_t status = unregisterInputChannelLocked(inputChannel, false /*notify*/);
if (status) {
@@ -4271,7 +4272,7 @@
}
void InputDispatcher::dump(std::string& dump) {
- AutoMutex _l(mLock);
+ std::scoped_lock _l(mLock);
dump += "Input Dispatcher State:\n";
dumpDispatchStateLocked(dump);
@@ -4284,10 +4285,9 @@
void InputDispatcher::monitor() {
// Acquire and release the lock to ensure that the dispatcher has not deadlocked.
- mLock.lock();
+ std::unique_lock _l(mLock);
mLooper->wake();
- mDispatcherIsAliveCondition.wait(mLock);
- mLock.unlock();
+ mDispatcherIsAlive.wait(_l);
}
diff --git a/services/inputflinger/InputDispatcher.h b/services/inputflinger/InputDispatcher.h
index 9d8919b..9c5a00a 100644
--- a/services/inputflinger/InputDispatcher.h
+++ b/services/inputflinger/InputDispatcher.h
@@ -17,6 +17,7 @@
#ifndef _UI_INPUT_DISPATCHER_H
#define _UI_INPUT_DISPATCHER_H
+#include <condition_variable>
#include <input/Input.h>
#include <input/InputApplication.h>
#include <input/InputTransport.h>
@@ -884,9 +885,9 @@
sp<InputDispatcherPolicyInterface> mPolicy;
InputDispatcherConfiguration mConfig;
- Mutex mLock;
+ std::mutex mLock;
- Condition mDispatcherIsAliveCondition;
+ std::condition_variable mDispatcherIsAlive;
sp<Looper> mLooper;
@@ -944,11 +945,11 @@
GUARDED_BY(mLock);
// Event injection and synchronization.
- Condition mInjectionResultAvailableCondition;
+ std::condition_variable mInjectionResultAvailable;
bool hasInjectionPermission(int32_t injectorPid, int32_t injectorUid);
void setInjectionResultLocked(EventEntry* entry, int32_t injectionResult);
- Condition mInjectionSyncFinishedCondition;
+ std::condition_variable mInjectionSyncFinished;
void incrementPendingForegroundDispatches(EventEntry* entry);
void decrementPendingForegroundDispatchesLocked(EventEntry* entry);