blob: f74a438509c26e622fe6f39299719d060c8e86eb [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
Makoto Onukieed5b5a2018-03-13 14:22:23 -070028 private static final boolean ENABLE_LOCK_GUARD = false;
29
Jorim Jaggi245281c2017-06-07 14:33:04 -070030 private volatile int mBoostToPriority;
Jorim Jaggi36db1272017-03-28 00:43:31 +020031 private final int mLockGuardIndex;
32
33 private final ThreadLocal<PriorityState> mThreadState = new ThreadLocal<PriorityState>() {
34 @Override protected PriorityState initialValue() {
35 return new PriorityState();
36 }
37 };
38
39 public ThreadPriorityBooster(int boostToPriority, int lockGuardIndex) {
40 mBoostToPriority = boostToPriority;
41 mLockGuardIndex = lockGuardIndex;
42 }
43
44 public void boost() {
Jorim Jaggi245281c2017-06-07 14:33:04 -070045 final int tid = myTid();
Jorim Jaggi245281c2017-06-07 14:33:04 -070046 final PriorityState state = mThreadState.get();
Jorim Jaggi7e12b6d2017-06-09 12:00:58 -070047 if (state.regionCounter == 0) {
Makoto Onukid8bb71e2019-01-30 14:22:04 -080048 final int prevPriority = getThreadPriority(tid);
Jorim Jaggi7e12b6d2017-06-09 12:00:58 -070049 state.prevPriority = prevPriority;
50 if (prevPriority > mBoostToPriority) {
51 setThreadPriority(tid, mBoostToPriority);
52 }
Jorim Jaggi36db1272017-03-28 00:43:31 +020053 }
54 state.regionCounter++;
Makoto Onukieed5b5a2018-03-13 14:22:23 -070055 if (ENABLE_LOCK_GUARD) {
Jorim Jaggi36db1272017-03-28 00:43:31 +020056 LockGuard.guard(mLockGuardIndex);
57 }
58 }
59
60 public void reset() {
Jorim Jaggi245281c2017-06-07 14:33:04 -070061 final PriorityState state = mThreadState.get();
Jorim Jaggi36db1272017-03-28 00:43:31 +020062 state.regionCounter--;
Makoto Onukid8bb71e2019-01-30 14:22:04 -080063 if (state.regionCounter == 0) {
64 final int currentPriority = getThreadPriority(myTid());
65 if (state.prevPriority != currentPriority) {
66 setThreadPriority(myTid(), state.prevPriority);
67 }
Jorim Jaggi245281c2017-06-07 14:33:04 -070068 }
69 }
70
71 /**
72 * Updates the priority we boost the threads to, and updates the current thread's priority if
73 * necessary.
74 */
75 protected void setBoostToPriority(int priority) {
76
77 // We don't care about the other threads here, as long as they see the update of this
78 // variable immediately.
79 mBoostToPriority = priority;
80 final PriorityState state = mThreadState.get();
81 final int tid = myTid();
Makoto Onukid8bb71e2019-01-30 14:22:04 -080082 if (state.regionCounter != 0) {
83 final int prevPriority = getThreadPriority(tid);
84 if (prevPriority != priority) {
85 setThreadPriority(tid, priority);
86 }
Jorim Jaggi36db1272017-03-28 00:43:31 +020087 }
88 }
89
90 private static class PriorityState {
91
92 /**
93 * Acts as counter for number of synchronized region that needs to acquire 'this' as a lock
94 * the current thread is currently in. When it drops down to zero, we will no longer boost
95 * the thread's priority.
96 */
97 int regionCounter;
98
99 /**
100 * The thread's previous priority before boosting.
101 */
102 int prevPriority;
103 }
104}