Analysis of Android sleep wake mechanism — wake_ lock

Analysis of Android sleep wake mechanism — wake_ lock

Android sleep wake is mainly based on wake_ Lock mechanism, as long as there is any effective wake in the system_ Lock, the system can not enter the deep sleep, but can carry out the shallow sleep operation of the device. wake_ Lock is usually used when the LCD and TP are turned off, but the system still needs to run normally, such as listening to songs and transferring large files.

Wake lock – Wake lock plays a core role in Android power management system. Wake lock is a lock mechanism. As long as a task holds the lock, the system can’t go to sleep and can be obtained by user mode processes and kernel threads. The lock can be timed out or not. The timed out lock will be automatically unlocked after the time has passed. If there is no lock or timeout, the kernel will start the standard Linux hibernation mechanism to enter hibernation.

Wakelock plays an important role in the sleep wake-up mechanism of Android. The main source code is in the file: kernel/kernel/power/wakelock. C, kernel/include/Linux/wakelock. H.

void wake_lock_init(struct wake_lock *lock, int type, const char *name);  
void wake_lock_destroy(struct wake_lock *lock);  
void wake_lock(struct wake_lock *lock);  
void wake_lock_timeout(struct wake_lock *lock, long timeout);  
void wake_unlock(struct wake_lock *lock);  

Among them, wake_ lock_ init () Used to initialize a new lock. The type parameter specifies the type of the lock; wake_ lock_ destroy () Then cancel a lock; wake_ lock () And wake_ lock_ timeout () It is used to activate the initialized lock to be a valid permanent lock or timeout lock; wake_ unlock () Used to unlock and make it an invalid lock. There are also two interfaces:

int wake_lock_active(struct wake_lock *lock);  
long has_wake_lock(int type);  

Among them, wake_ lock_ Active() is used to judge whether the lock is valid or not. If it is valid, a non-zero value will be returned_ wake_ Lock() is used to determine whether there is a valid type lock in the system. If there is a timeout lock, the longest timeout of the lock will be returned. If there is a permanent lock, it will return – 1. If there is no valid lock in the system, it will return 0.

Similar Posts: