Android Wakelock mechanism

Source: Internet
Author: User

Wake Lock is a mechanism of locking, as long as someone holding this lock, the system will not go into hibernation, can be obtained by the user-state program and the kernel. The lock can be timed out or not timed out, and the timeout lock will automatically unlock after the time has elapsed. If there is no lock or timeout, the kernel will start the dormant mechanism to enter hibernation .

Powermanager.wakelock There are two states of lock and unlock, lock The way there are two, one is permanent lock, such a lock, unless the explicit release, is not unlocked, so this lock used to be very careful. The second type of lock is a time-out lock that is unlocked for a period of time after it has been locked.

When you create aPowermanager.wakelock, there are two mechanisms, the first is not counting the lock mechanism, and the other is the counting lock mechanism. can be done bySetreferencecounted (BooleanValueacquire ()   How many times, as long as through a  release () --count = = 0 isheld powermanager.wakelock 

  (i). The kernel maintains:
1). Two linked list, Active_wake_locks[wake_lock_type_count]
Active_wake_locks[0] Suspend lock is maintained.
ACTIVE_WAKE_LOCKS[1] is the maintenance of the idle lock.
2). A linked list, inactive_locks to record all locks in the inactive state.

(b). The following describes how the application layer application lock is uploaded to kernel below to understand
The whole wakelock frame.
Like the Powermanagerservice under/sys/power/wake_lock.
The build process.

1). Android provides a ready-made Android.os.PowerManager class, in class
The Newwakelock (int flags, String tag) method is provided to obtain the corresponding
Hierarchy of locks, the definition of this function
Frameworks/base/core/java/android/os/powermanager.java
Below, the application will have a call when applying for Wake_lock.
Instance:
PowerManager pm = (powermanager) getsystemservice (Context.power_service);
Powermanager.wakelock WL = Pm.newwakelock
(Powermanager.screen_dim_wake_lock, "My Tag");
Wl.acquire ();//Application lock This inside will call Powermanagerservice inside Acquirewakelock ()

***********************
Wl.release (); Release the lock, showing the release, if the requested lock is not released in this system will not go into hibernation.


2). Frameworks Layer
/frameworks/base/services/java/com/android/server/
Powermanagerservice.java
This class is the Wakelock to manage all application applications. such as the audio-visual
Frequency players, camera applications such as Wakelock are managed through this class.
Static final String partial_name = "Powermanagerservice"
Power.acquirewakelock (Power.partial_wake_lock,
Partial_name);
This function calls the Acquirewakelock () inside the Power class, at which point the
Partial_name is passed as a parameter to the underlying.




/frameworks/base/core/java/android/os/power.java
public static native void Acquirewakelock (int lock, String ID);
Note: The native declaration method is not implemented in the Power class and its implementation is
In the Frameworks/base/core/jni/android_os_power.cpp, the
To invoke the Acquirewakelock () method of the Power class will invoke the JNI
The implementation method below.






3). Implementation of the JNI layer
Path: 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 Acquirewakelock () is called in the
Under HARDWARE/LIBHARDWARE_LEGACY/POWER/POWER.C below the path
The Acquire_wake_lock (lock, ID)




4). Interaction with the kernel layer
The Acquire_wake_lock (lock, id) function under POWER.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, which means "/sys/power/wake_lock"
The ID is the parameter passed down from the Powermanagerservice class:
Partial_name = "Powermanagerservice"
This is where the file system interacts with the kernel layer.

Android Wakelock mechanism

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.