Android wakelock application and release

Source: Internet
Author: User

Android wakelock can be applied for and released by kernel space and user space. The applied non-Timeout lock requires the corresponding call of wake_unlock to release the lock, and the timeout lock does not need to be released manually (you can also release it manually). After the timeout, the kernel system will automatically release the lock.

In the kernel space, you can directly call wake_lock and wake_lock_timeout to apply for a lock.

Android Kernel provides an interface for users to apply for and release wakelock, which is implemented in kernel/power/userwakelock. in C, the following is a simple analysis of this file. userwakelock only involves the locks for user space operations. All locks are managed through the red/black tree, here we will not analyze the operations on the red and black trees.

User space access interface:

/Sys/power/wake_lock

/Sys/power/wake_unlock

129 ssize_t wake_lock_show (
130 struct kobject * kobj, struct kobj_attribute * ATTR, char * BUF)
131 {
132 char * s = Buf;
133 char * end = BUF + page_size;
134 struct rb_node * N;
135 struct user_wake_lock * l;
136
137 mutex_lock (& tree_lock );
138
139 For (n = rb_first (& user_wake_locks); n! = NULL; n = rb_next (N )){
140 L = rb_entry (n, struct user_wake_lock, node );
141 If (wake_lock_active (& L-> wake_lock ))
142 S + = scnprintf (S, end-S, "% s", L-> name );
143}
144 S + = scnprintf (S, end-S, "\ n ");
145
146 mutex_unlocks (& tree_lock );
147 return (S-BUF );
148}

DisplayApplication SpaceAll applied activation locks (including timeout locks and non-Timeout locks)

150 ssize_t wake_lock_store (
151 struct kobject * kobj, struct kobj_attribute * ATTR,
152 const char * Buf, size_t N)
153 {
154 long timeout;
155 struct user_wake_lock * l;
156
157 mutex_lock (& tree_lock );
158 L = lookup_wake_lock_name (BUF, 1, & timeout );
159 If (is_err (L )){
160 N = ptr_err (L );
161 goto bad_name;
162}
163
164 If (debug_mask & debug_access)
165 pr_info ("wake_lock_store: % s, timeout % LD \ n", L-> name, timeout );
166
167 If (timeout)
168 wake_lock_timeout (& L-> wake_lock, timeout );
169 else
170 wake_lock (& L-> wake_lock );
171 bad_name:
172 mutex_unlocks (& tree_lock );
173 return N;
174}

Apply for a lock. The name and timeout value are included in the @ Buf parameter. lookup_wake_lock_name resolves the name and timeout value. If the timeout value does not exist, the applied lock is a non-Timeout lock. Otherwise, the lock is a timeout lock.

167 ~ 170 call wake_lock to apply for a non-Timeout lock; call wake_lock_timeout to apply for a non-Timeout lock.

177 ssize_t wake_unlock_show (
178 struct kobject * kobj, struct kobj_attribute * ATTR, char * BUF)
179 {
180 char * s = Buf;
181 char * end = BUF + page_size;
182 struct rb_node * N;
183 struct user_wake_lock * l;
184
185 mutex_lock (& tree_lock );
186
187 for (n = rb_first (& user_wake_locks); n! = NULL; n = rb_next (N )){
188 L = rb_entry (n, struct user_wake_lock, node );
189 if (! Wake_lock_active (& L-> wake_lock ))
190 s + = scnprintf (S, end-S, "% s", L-> name );
191}
192 S + = scnprintf (S, end-S, "\ n ");
193
194 mutex_unlocks (& tree_lock );
195 return (S-BUF );
196}

Show this functionYou can apply for the current application space.All inactive locks (including time-out locks and non-Time-out locks) of, print out the lock name, and return it to the user space.

187 traverse the entire RB tree,

198 ssize_t wake_unlock_store (
199 struct kobject * kobj, struct kobj_attribute * ATTR,
200 const char * Buf, size_t N)
201 {
202 struct user_wake_lock * l;
203
204 mutex_lock (& tree_lock );
205 L = lookup_wake_lock_name (BUF, 0, null );
206 if (is_err (L )){
207 n = ptr_err (L );
208 goto not_found;
209}
210
211 If (debug_mask & debug_access)
212 pr_info ("wake_unlock_store: % s \ n", L-> name );
213
214 wake_unlock (& L-> wake_lock );
215 not_found:
216 mutex_unlocks (& tree_lock );
217 return N;
218}

205 the user space interface organizes the wakelock lock according to the Rb (Red/black tree), which can speed up the search. The third parameter is null, because the wake_unlock_store is only applicable to non-Timeout locks and does not require unlock for the timeout lock.

214 call wake_unlock to release non-Timeout locks

If you use a command

# Echo "Kaka 12">/sys/power/wake_lock

Apply for a timeout lock.

# Cat/sys/power/wake_lock

Kaka

The timeout lock still exists and the interface is faulty.

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.