From 4bdbb6ae90238834229ac20184a1c8c7bcb71ce0 Mon Sep 17 00:00:00 2001 From: Siarhei Vishniakou Date: Thu, 11 Apr 2019 09:42:09 -0700 Subject: Initialize MotionClassifier from a separate thread We are currently calling getService directly from the thread that calls InputManager constructor. That means, the function call directly affects the start-up time of the system. But InputClassifier HAL is not mission-critical, and the system can function perfectly fine without it. Moreover, we are already allowing for mMotionClassifier to be null in our code. In this change, mMotionClassifier will become non-null right away, but may not be initialized when it is first created. The initialization will happen in a separate thread. Once initialized, MotionClassifier may become null inside InputClassifier if the latter receives a HAL death callback. To account for the possibility of the MotionClassifier not being valid right away, we are allowing mService variable in the MotionClassifier to be nullable. The contract for mService is as follows: mService is null by default. If MotionClassifier initializes successfully (may take ~ 100 ms), then mService will become non-null. Once non-null, it stays non-null. This allows MotionClassifier's separate thread to not have to check the nullness of mService. Other threads, however, must assume that mService can be null. They are checking the value with a lock held. If MotionClassifier init fails, it will also call the deathRecipient's serviceDied call, to signal that it is no longer useful. This should prompt the owner of MotionClassifier to dispose it. Bug: 130184032 Test: atest google/perf/boottime/boottime-test Test: atest libinput_tests inputflinger_tests Change-Id: Id4f9c316ef6725d96abebc55a96079946647eb39 --- services/inputflinger/InputClassifier.h | 42 ++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 8 deletions(-) (limited to 'services/inputflinger/InputClassifier.h') diff --git a/services/inputflinger/InputClassifier.h b/services/inputflinger/InputClassifier.h index 0b1483fdd5..47e20dbf75 100644 --- a/services/inputflinger/InputClassifier.h +++ b/services/inputflinger/InputClassifier.h @@ -114,15 +114,22 @@ protected: class MotionClassifier final : public MotionClassifierInterface { public: /** - * The provided pointer to the service cannot be null. + * The deathRecipient will be subscribed to the HAL death. If the death recipient + * owns MotionClassifier and receives HAL death, it should delete its copy of it. + * The callback serviceDied will also be sent if the MotionClassifier itself fails + * to initialize. If the MotionClassifier fails to initialize, it is not useful, and + * should be deleted. + * If no death recipient is supplied, then the registration step will be skipped, so there will + * be no listeners registered for the HAL death. This is useful for testing + * MotionClassifier in isolation. */ - MotionClassifier(sp service); + explicit MotionClassifier(sp deathRecipient = nullptr); ~MotionClassifier(); + /** * Classifies events asynchronously; that is, it doesn't block events on a classification, - * but instead sends them over to the classifier HAL - * and after a classification is determined, - * it then marks the next event it sees in the stream with it. + * but instead sends them over to the classifier HAL and after a classification is + * determined, it then marks the next event it sees in the stream with it. * * Therefore, it is acceptable to have the classifications be delayed by 1-2 events * in a particular gesture. @@ -134,6 +141,16 @@ public: virtual void dump(std::string& dump) override; private: + /** + * Initialize MotionClassifier. + * Return true if initializaion is successful. + */ + bool init(); + /** + * Entity that will be notified of the HAL death (most likely InputClassifier). + */ + wp mDeathRecipient; + // The events that need to be sent to the HAL. BlockingQueue mEvents; /** @@ -148,7 +165,7 @@ private: std::thread mHalThread; /** * Print an error message if the caller is not on the InputClassifier thread. - * Caller must supply the name of the calling function as __function__ + * Caller must supply the name of the calling function as __func__ */ void ensureHalThread(const char* function); /** @@ -156,9 +173,14 @@ private: */ void callInputClassifierHal(); /** - * Access to the InputClassifier HAL. Can always be safely dereferenced. + * Access to the InputClassifier HAL. May be null if init() hasn't completed yet. + * When init() successfully completes, mService is guaranteed to remain non-null and to not + * change its value until MotionClassifier is destroyed. + * This variable is *not* guarded by mLock in the InputClassifier thread, because + * that thread knows exactly when this variable is initialized. + * When accessed in any other thread, mService is checked for nullness with a lock. */ - const sp mService; + sp mService; std::mutex mLock; /** * Per-device input classifications. Should only be accessed using the @@ -195,6 +217,10 @@ private: * Useful for tests to ensure proper cleanup. */ void requestExit(); + /** + * Return string status of mService + */ + const char* getServiceStatus() REQUIRES(mLock); }; -- cgit v1.2.3-59-g8ed1b