1 /*
2  * Copyright (C) 2020 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 
19 import android.os.Handler;
20 import android.os.HandlerExecutor;
21 import android.os.HandlerThread;
22 import android.os.Looper;
23 import android.os.Process;
24 import android.os.Trace;
25 
26 import java.util.concurrent.Executor;
27 
28 /**
29  * Shared singleton default priority thread for the app scheduling module.
30  *
31  * @see com.android.internal.os.BackgroundThread
32  */
33 public final class AppSchedulingModuleThread extends HandlerThread {
34     private static final long SLOW_DISPATCH_THRESHOLD_MS = 10_000;
35     private static final long SLOW_DELIVERY_THRESHOLD_MS = 30_000;
36     private static AppSchedulingModuleThread sInstance;
37     private static Handler sHandler;
38     private static Executor sHandlerExecutor;
39 
AppSchedulingModuleThread()40     private AppSchedulingModuleThread() {
41         super("appscheduling.default", Process.THREAD_PRIORITY_DEFAULT);
42     }
43 
ensureThreadLocked()44     private static void ensureThreadLocked() {
45         if (sInstance == null) {
46             sInstance = new AppSchedulingModuleThread();
47             sInstance.start();
48             final Looper looper = sInstance.getLooper();
49             looper.setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
50             looper.setSlowLogThresholdMs(
51                     SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
52             sHandler = new Handler(sInstance.getLooper());
53             sHandlerExecutor = new HandlerExecutor(sHandler);
54         }
55     }
56 
57     /** Returns the AppSchedulingModuleThread singleton */
get()58     public static AppSchedulingModuleThread get() {
59         synchronized (AppSchedulingModuleThread.class) {
60             ensureThreadLocked();
61             return sInstance;
62         }
63     }
64 
65     /** Returns the singleton handler for AppSchedulingModuleThread */
getHandler()66     public static Handler getHandler() {
67         synchronized (AppSchedulingModuleThread.class) {
68             ensureThreadLocked();
69             return sHandler;
70         }
71     }
72 
73     /** Returns the singleton handler executor for AppSchedulingModuleThread */
getExecutor()74     public static Executor getExecutor() {
75         synchronized (AppSchedulingModuleThread.class) {
76             ensureThreadLocked();
77             return sHandlerExecutor;
78         }
79     }
80 }
81