blob: 0dfecc051a2d5f0bc8a1fbea3d8b37aa4ef3c0f1 [file] [log] [blame]
Jorim Jaggi36db1272017-03-28 00:43:31 +02001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
15 */
16
17package com.android.server;
18
Jorim Jaggi245281c2017-06-07 14:33:04 -070019import static android.os.Process.getThreadPriority;
20import static android.os.Process.myTid;
21import static android.os.Process.setThreadPriority;
Jorim Jaggi36db1272017-03-28 00:43:31 +020022
23/**
24 * Utility class to boost threads in sections where important locks are held.
25 */
26public class ThreadPriorityBooster {
27
Jorim Jaggi245281c2017-06-07 14:33:04 -070028 private volatile int mBoostToPriority;
Jorim Jaggi36db1272017-03-28 00:43:31 +020029 private final int mLockGuardIndex;
30
31 private final ThreadLocal<PriorityState> mThreadState = new ThreadLocal<PriorityState>() {
32 @Override protected PriorityState initialValue() {
33 return new PriorityState();
34 }
35 };
36
37 public ThreadPriorityBooster(int boostToPriority, int lockGuardIndex) {
38 mBoostToPriority = boostToPriority;
39 mLockGuardIndex = lockGuardIndex;
40 }
41
42 public void boost() {
Jorim Jaggi245281c2017-06-07 14:33:04 -070043 final int tid = myTid();
44 final int prevPriority = getThreadPriority(tid);
45 final PriorityState state = mThreadState.get();
Jorim Jaggi5515b122017-04-07 12:16:33 +020046 state.prevPriority = prevPriority;
Jorim Jaggi36db1272017-03-28 00:43:31 +020047 if (state.regionCounter == 0 && prevPriority > mBoostToPriority) {
Jorim Jaggi245281c2017-06-07 14:33:04 -070048 setThreadPriority(tid, mBoostToPriority);
Jorim Jaggi36db1272017-03-28 00:43:31 +020049 }
50 state.regionCounter++;
51 if (LockGuard.ENABLED) {
52 LockGuard.guard(mLockGuardIndex);
53 }
54 }
55
56 public void reset() {
Jorim Jaggi245281c2017-06-07 14:33:04 -070057 final PriorityState state = mThreadState.get();
Jorim Jaggi36db1272017-03-28 00:43:31 +020058 state.regionCounter--;
Jorim Jaggi245281c2017-06-07 14:33:04 -070059 final int currentPriority = getThreadPriority(myTid());
60 if (state.regionCounter == 0 && state.prevPriority != currentPriority) {
61 setThreadPriority(myTid(), state.prevPriority);
62 }
63 }
64
65 /**
66 * Updates the priority we boost the threads to, and updates the current thread's priority if
67 * necessary.
68 */
69 protected void setBoostToPriority(int priority) {
70
71 // We don't care about the other threads here, as long as they see the update of this
72 // variable immediately.
73 mBoostToPriority = priority;
74 final PriorityState state = mThreadState.get();
75 final int tid = myTid();
76 final int prevPriority = getThreadPriority(tid);
77 if (state.regionCounter != 0 && prevPriority != priority) {
78 setThreadPriority(tid, priority);
Jorim Jaggi36db1272017-03-28 00:43:31 +020079 }
80 }
81
82 private static class PriorityState {
83
84 /**
85 * Acts as counter for number of synchronized region that needs to acquire 'this' as a lock
86 * the current thread is currently in. When it drops down to zero, we will no longer boost
87 * the thread's priority.
88 */
89 int regionCounter;
90
91 /**
92 * The thread's previous priority before boosting.
93 */
94 int prevPriority;
95 }
96}