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