Get Input device information in locked context
Call getInputDevicesLocked with mutex locked, annotate InputReader member
variables and method functions to enforce the mutex lock check.
Bug: 173326051
Test: atest inputflinger_tests
Change-Id: I3009fbfd15d72b92a5691167037af9ea0197028f
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index c5210b5..9671dec 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -475,25 +475,25 @@
}
InputDeviceIdentifier EventHub::getDeviceIdentifier(int32_t deviceId) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
return device != nullptr ? device->identifier : InputDeviceIdentifier();
}
Flags<InputDeviceClass> EventHub::getDeviceClasses(int32_t deviceId) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
return device != nullptr ? device->classes : Flags<InputDeviceClass>(0);
}
int32_t EventHub::getDeviceControllerNumber(int32_t deviceId) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
return device != nullptr ? device->controllerNumber : 0;
}
void EventHub::getConfiguration(int32_t deviceId, PropertyMap* outConfiguration) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->configuration) {
*outConfiguration = *device->configuration;
@@ -507,7 +507,7 @@
outAxisInfo->clear();
if (axis >= 0 && axis <= ABS_MAX) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
@@ -534,7 +534,7 @@
bool EventHub::hasRelativeAxis(int32_t deviceId, int axis) const {
if (axis >= 0 && axis <= REL_MAX) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
return device != nullptr ? device->relBitmask.test(axis) : false;
}
@@ -542,7 +542,7 @@
}
bool EventHub::hasInputProperty(int32_t deviceId, int property) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
return property >= 0 && property <= INPUT_PROP_MAX && device != nullptr
@@ -552,7 +552,7 @@
int32_t EventHub::getScanCodeState(int32_t deviceId, int32_t scanCode) const {
if (scanCode >= 0 && scanCode <= KEY_MAX) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd() && device->keyBitmask.test(scanCode)) {
@@ -565,7 +565,7 @@
}
int32_t EventHub::getKeyCodeState(int32_t deviceId, int32_t keyCode) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd() && device->keyMap.haveKeyLayout()) {
@@ -588,7 +588,7 @@
int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const {
if (sw >= 0 && sw <= SW_MAX) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd() && device->swBitmask.test(sw)) {
@@ -604,7 +604,7 @@
*outValue = 0;
if (axis >= 0 && axis <= ABS_MAX) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd() && device->absBitmask.test(axis)) {
@@ -624,7 +624,7 @@
bool EventHub::markSupportedKeyCodes(int32_t deviceId, size_t numCodes, const int32_t* keyCodes,
uint8_t* outFlags) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->keyMap.haveKeyLayout()) {
@@ -652,7 +652,7 @@
status_t EventHub::mapKey(int32_t deviceId, int32_t scanCode, int32_t usageCode, int32_t metaState,
int32_t* outKeycode, int32_t* outMetaState, uint32_t* outFlags) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
status_t status = NAME_NOT_FOUND;
@@ -692,7 +692,7 @@
}
status_t EventHub::mapAxis(int32_t deviceId, int32_t scanCode, AxisInfo* outAxisInfo) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->keyMap.haveKeyLayout()) {
@@ -706,13 +706,13 @@
}
void EventHub::setExcludedDevices(const std::vector<std::string>& devices) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
mExcludedDevices = devices;
}
bool EventHub::hasScanCode(int32_t deviceId, int32_t scanCode) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && scanCode >= 0 && scanCode <= KEY_MAX) {
return device->keyBitmask.test(scanCode);
@@ -721,7 +721,7 @@
}
bool EventHub::hasLed(int32_t deviceId, int32_t led) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
int32_t sc;
if (device != nullptr && device->mapLed(led, &sc) == NO_ERROR) {
@@ -731,7 +731,7 @@
}
void EventHub::setLedState(int32_t deviceId, int32_t led, bool on) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd()) {
device->setLedStateLocked(led, on);
@@ -742,7 +742,7 @@
std::vector<VirtualKeyDefinition>& outVirtualKeys) const {
outVirtualKeys.clear();
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->virtualKeyMap) {
const std::vector<VirtualKeyDefinition> virtualKeys =
@@ -752,7 +752,7 @@
}
const std::shared_ptr<KeyCharacterMap> EventHub::getKeyCharacterMap(int32_t deviceId) const {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr) {
return device->getKeyCharacterMap();
@@ -761,7 +761,7 @@
}
bool EventHub::setKeyboardLayoutOverlay(int32_t deviceId, std::shared_ptr<KeyCharacterMap> map) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && map != nullptr && device->keyMap.keyCharacterMap != nullptr) {
device->keyMap.keyCharacterMap->combine(*map);
@@ -822,7 +822,7 @@
}
void EventHub::vibrate(int32_t deviceId, const VibrationElement& element) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd()) {
ff_effect effect;
@@ -857,7 +857,7 @@
}
void EventHub::cancelVibrate(int32_t deviceId) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device != nullptr && device->hasValidFd()) {
if (device->ffEffectPlaying) {
@@ -930,7 +930,7 @@
size_t EventHub::getEvents(int timeoutMillis, RawEvent* buffer, size_t bufferSize) {
ALOG_ASSERT(bufferSize >= 1);
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
struct input_event readBuffer[bufferSize];
@@ -1184,7 +1184,7 @@
}
std::vector<TouchVideoFrame> EventHub::getVideoFrames(int32_t deviceId) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device == nullptr || !device->videoDevice) {
@@ -1593,7 +1593,7 @@
}
bool EventHub::isDeviceEnabled(int32_t deviceId) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device == nullptr) {
ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
@@ -1603,7 +1603,7 @@
}
status_t EventHub::enableDevice(int32_t deviceId) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device == nullptr) {
ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
@@ -1625,7 +1625,7 @@
}
status_t EventHub::disableDevice(int32_t deviceId) {
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
Device* device = getDeviceLocked(deviceId);
if (device == nullptr) {
ALOGE("Invalid device id=%" PRId32 " provided to %s", deviceId, __func__);
@@ -1809,7 +1809,7 @@
void EventHub::requestReopenDevices() {
ALOGV("requestReopenDevices() called");
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
mNeedToReopenDevices = true;
}
@@ -1817,7 +1817,7 @@
dump += "Event Hub State:\n";
{ // acquire lock
- AutoMutex _l(mLock);
+ std::lock_guard<std::mutex> lock(mLock);
dump += StringPrintf(INDENT "BuiltInKeyboardId: %d\n", mBuiltInKeyboardId);
@@ -1868,8 +1868,7 @@
void EventHub::monitor() {
// Acquire and release the lock to ensure that the event hub has not deadlocked.
- mLock.lock();
- mLock.unlock();
+ std::unique_lock<std::mutex> lock(mLock);
}
}; // namespace android