Android Input (7) -- 模块的锁检测

Link:

Android Input (1) -- InputManagerService启动

Android Input (2) -- inputReader

Android Input (3) -- inputDispatcher

Android Input (4) -- inputDispatcher到ViewRootImpl

Android Input (5) -- ViewRootImpl 的事件分发(Activity ViewGroup View )

Android Input (6) -- PhoneWindowManager中Power,BACK等按键处理流程

Android Input (7) -- 模块的锁检测

Android Input (8) -- ANR input event原理


目录

1.1 InputManagerService watchdog锁检测

1.2 InputDispatcher锁检测

1.3 inputReader锁检测

1.4 EventHub锁检测


 

1.1 InputManagerService watchdog锁检测

//IMS被systemserver启动时添加watchdog

@frameworks/base/services/core/java/com/android/server/input/InputManagerService.java

public class InputManagerService extends IInputManager.Stub
        implements Watchdog.Monitor {   //1 implements Watchdog.Monitor

    public void start() {
        Slog.i(TAG, "Starting input manager");
        nativeStart(mPtr);

        // Add ourself to the Watchdog monitors.
        Watchdog.getInstance().addMonitor(this);   //2 watchdog添加检测
    }
    
    @Override
    public void monitor() {
        synchronized (mInputFilterLock) { }   //3 override watchdog的monitor方法,通过 synchronized获取锁,来判断当前线程是否死锁
        nativeMonitor(mPtr);
    }
}

 

1.2 InputDispatcher锁检测

#IMS.nativeMonitor检测native层thread

 

@frameworks/base/services/core/jni/com_android_server_input_InputManagerService.cpp
static void nativeMonitor(JNIEnv* /* env */, jclass /* clazz */, jlong ptr) {
    NativeInputManager* im = reinterpret_cast(ptr);

    im->getInputManager()->getReader()->monitor();
    im->getInputManager()->getDispatcher()->monitor();
}

im->getInputManager()->getDispatcher()获取 的分别是inputReader和inputDispatcher

 

@frameworks/native/services/inputflinger/InputManager.cpp
sp InputManager::getReader() {
    return mReader;
}

sp InputManager::getDispatcher() {
    return mDispatcher;
}

mDispatcher = new InputDispatcher(dispatcherPolicy);
mReader = new InputReader(eventHub, readerPolicy, mDispatcher);

native层这里inputDispatcher通过mLock.lock和unlock来测试锁是否可用

@frameworks/native/services/inputflinger/InputDispatcher.cpp
void InputDispatcher::monitor() {
    // Acquire and release the lock to ensure that the dispatcher has not deadlocked.
    mLock.lock();
    mLooper->wake();
    mDispatcherIsAliveCondition.wait(mLock);
    mLock.unlock();
}

1.3 inputReader锁检测

@frameworks/native/services/inputflinger/InputReader.cpp
void InputReader::monitor() {
    // Acquire and release the lock to ensure that the reader has not deadlocked.
    mLock.lock();
    mEventHub->wake();
    mReaderIsAliveCondition.wait(mLock);
    mLock.unlock();

    // Check the EventHub
    mEventHub->monitor();
}

1.4 EventHub锁检测

@frameworks/native/services/inputflinger/EventHub.cpp
void EventHub::monitor() {
    // Acquire and release the lock to ensure that the event hub has not deadlocked.
    mLock.lock();
    mLock.unlock();
}

 

 

 

你可能感兴趣的:(Input)