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