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. This lock can be timed out or not timed out,A timeout lock is automatically unlocked after the time has elapsed. If there is no lock or timeout, the kernelIt starts the sleep mechanism to go into hibernation.(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 understandthe 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 classthe Newwakelock (int flags, String tag) method is provided to obtain the correspondingHierarchy of locks, the definition of this functionFrameworks/base/core/java/android/os/powermanager.javabelow, the application will have a call when applying for Wake_lock. Example: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 lock, show the release, if the requested lock is not released here the system will not go into hibernation. 2). Frameworks Layer/frameworks/base/services/java/com/android/server/Powermanagerservice.javaThis class is the Wakelock to manage all application applications. such as the audio-visualfrequency 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 thePartial_name is passed as a parameter to the underlying.
/frameworks/base/core/java/android/os/power.javaPublic static native void Acquirewakelock (int lock, String ID);Note: The native declaration method is not implemented in the Power class and its implementation isin the Frameworks/base/core/jni/android_os_power.cpp, theto invoke the Acquirewakelock () method of the Power class will invoke the JNIthe implementation method below.
3). Implementation of the JNI layerPath: Frameworks/base/core/jni/android_os_power.cppstatic 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 theunder HARDWARE/LIBHARDWARE_LEGACY/POWER/POWER.C below the paththe Acquire_wake_lock (lock, id)
4). Interaction with the kernel layerThe 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.
Introduction to Android Wake_lock