Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 19 | import static android.os.Process.getThreadPriority; |
| 20 | import static android.os.Process.myTid; |
| 21 | import static android.os.Process.setThreadPriority; |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 22 | |
| 23 | /** |
| 24 | * Utility class to boost threads in sections where important locks are held. |
| 25 | */ |
| 26 | public class ThreadPriorityBooster { |
| 27 | |
Makoto Onuki | eed5b5a | 2018-03-13 14:22:23 -0700 | [diff] [blame] | 28 | private static final boolean ENABLE_LOCK_GUARD = false; |
| 29 | |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 30 | private volatile int mBoostToPriority; |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 31 | 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 Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 45 | final int tid = myTid(); |
| 46 | final int prevPriority = getThreadPriority(tid); |
| 47 | final PriorityState state = mThreadState.get(); |
Jorim Jaggi | 7e12b6d | 2017-06-09 12:00:58 -0700 | [diff] [blame] | 48 | if (state.regionCounter == 0) { |
| 49 | state.prevPriority = prevPriority; |
| 50 | if (prevPriority > mBoostToPriority) { |
| 51 | setThreadPriority(tid, mBoostToPriority); |
| 52 | } |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 53 | } |
| 54 | state.regionCounter++; |
Makoto Onuki | eed5b5a | 2018-03-13 14:22:23 -0700 | [diff] [blame] | 55 | if (ENABLE_LOCK_GUARD) { |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 56 | LockGuard.guard(mLockGuardIndex); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | public void reset() { |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 61 | final PriorityState state = mThreadState.get(); |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 62 | state.regionCounter--; |
Jorim Jaggi | 245281c | 2017-06-07 14:33:04 -0700 | [diff] [blame] | 63 | final int currentPriority = getThreadPriority(myTid()); |
| 64 | if (state.regionCounter == 0 && state.prevPriority != currentPriority) { |
| 65 | setThreadPriority(myTid(), state.prevPriority); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Updates the priority we boost the threads to, and updates the current thread's priority if |
| 71 | * necessary. |
| 72 | */ |
| 73 | protected void setBoostToPriority(int priority) { |
| 74 | |
| 75 | // We don't care about the other threads here, as long as they see the update of this |
| 76 | // variable immediately. |
| 77 | mBoostToPriority = priority; |
| 78 | final PriorityState state = mThreadState.get(); |
| 79 | final int tid = myTid(); |
| 80 | final int prevPriority = getThreadPriority(tid); |
| 81 | if (state.regionCounter != 0 && prevPriority != priority) { |
| 82 | setThreadPriority(tid, priority); |
Jorim Jaggi | 36db127 | 2017-03-28 00:43:31 +0200 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | private static class PriorityState { |
| 87 | |
| 88 | /** |
| 89 | * Acts as counter for number of synchronized region that needs to acquire 'this' as a lock |
| 90 | * the current thread is currently in. When it drops down to zero, we will no longer boost |
| 91 | * the thread's priority. |
| 92 | */ |
| 93 | int regionCounter; |
| 94 | |
| 95 | /** |
| 96 | * The thread's previous priority before boosting. |
| 97 | */ |
| 98 | int prevPriority; |
| 99 | } |
| 100 | } |