Introduction to wake_lock in Android

Source: Internet
Author: User
Wake lock is a lock mechanism. As long as someone holds this lock, the system will not be able to sleep and can be obtained by user State programs and kernels. the lock can be time-out or not time-out. The lock will be automatically unlocked after the time expires. if there is no lock or timeout, the kernel will start the sleep mechanism to enter sleep. (1 ). kernel maintenance: 1 ). two linked lists, active_wake_locks [wake_lock_type_count] active_wake_locks [0], maintain suspend lock. active_wake_locks [1] maintains idle lock.2 ). an inactive_locks is used to record all inactive locks. (2 ). the following describes how to transfer the lock applied at the application layer to the kernel to understand the entire wakelock framework. For example, the powermanagerservice generation process under/sys/power/wake_lock. 1 ). android provides ready-made android. OS. the powermanager class provides the newwakelock (INT flags, string tag) method in the class to obtain the corresponding locks. This function defines frameworks/base/CORE/Java/Android/OS/powermanager. in Java, applications call wae_lock. Instance: powermanager PM = (powermanager) getsystemservice (context. power_service); powermanager. wakelock WL = PM. newwakelock (powermanager. screen_dim_wake_lock, "my tag"); WL. acquire (); // apply for a lock. This will call acquirewakelock () in powermanagerservice () * *********************** WL. release (); // release the lock, indicating the release. If the applied lock is not released here, the system will not enter sleep. 2). The frameworks Layer/frameworks/base/services/Java/COM/Android/Server/powermanagerservice. Java class is used to manage the wakelock applied by all applications. For example, waelock applied for audio and video players and camera are all managed through this class. Static final string partial_name = "powermanagerservice" power. acquirewakelock.

 

 

/Frameworks/base/CORE/Java/Android/OS/power. javapublic static native void acquirewakelock (INT lock, string ID); Note: Native declarative methods are not implemented in the power class, in frameworks/base/CORE/JNI/android_ OS _power.cpp, the implementation method under JNI is called when the acquirewakelock () method of the power class is called.

 

 

 

3 ). implementation path of the JNI layer: Frameworks/base/CORE/JNI/android_ OS _power.cpp static void acquirewakelock (jnienv * ENV, jobject clazz, jint lock, jstring idobj) {************** const char * id = env-> getstringutfchars (idobj, null); acquire_wake_lock (lock, ID ); env-> releasestringutfchars (idobj, ID);} Note: The Path Hardware/libhardware_legacy/power is called in acquirewakelock. acquire_wake_lock (lock, ID) in C)

 

 

4 ). interaction with the kernel layer in power. the acquire_wake_lock (lock, ID) function in C is as follows: int acquire_wake_lock (INT lock, const char * ID) {*************** return write (FD, ID, strlen (ID);} Note: FD is the file descriptor, in this example, "/sys/power/wake_lock" ID is the parameter passed in from the powermanagerservice class: partial_name = "powermanagerservice". This is where the file system interacts with the kernel layer.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.