blob: 22c832e0689c64754842dff4c6bd72ca94685fe4 [file] [log] [blame]
Dianne Hackborn8d044e82013-04-30 17:24:15 -07001/*
2 * Copyright (C) 2013 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.internal.os;
18
19import android.os.Handler;
Makoto Onuki83415262019-02-14 15:37:20 -080020import android.os.HandlerExecutor;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070021import android.os.HandlerThread;
Makoto Onuki712886f2018-04-27 15:22:50 -070022import android.os.Looper;
Jeff Sharkey74cd3de2016-04-06 17:40:54 -060023import android.os.Trace;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070024
Makoto Onuki83415262019-02-14 15:37:20 -080025import java.util.concurrent.Executor;
26
Dianne Hackborn8d044e82013-04-30 17:24:15 -070027/**
28 * Shared singleton background thread for each process.
29 */
30public final class BackgroundThread extends HandlerThread {
Makoto Onuki712886f2018-04-27 15:22:50 -070031 private static final long SLOW_DISPATCH_THRESHOLD_MS = 10_000;
32 private static final long SLOW_DELIVERY_THRESHOLD_MS = 30_000;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070033 private static BackgroundThread sInstance;
34 private static Handler sHandler;
Makoto Onuki83415262019-02-14 15:37:20 -080035 private static HandlerExecutor sHandlerExecutor;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070036
37 private BackgroundThread() {
38 super("android.bg", android.os.Process.THREAD_PRIORITY_BACKGROUND);
39 }
40
41 private static void ensureThreadLocked() {
42 if (sInstance == null) {
43 sInstance = new BackgroundThread();
44 sInstance.start();
Makoto Onuki712886f2018-04-27 15:22:50 -070045 final Looper looper = sInstance.getLooper();
46 looper.setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
47 looper.setSlowLogThresholdMs(
48 SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070049 sHandler = new Handler(sInstance.getLooper());
Makoto Onuki83415262019-02-14 15:37:20 -080050 sHandlerExecutor = new HandlerExecutor(sHandler);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070051 }
52 }
53
54 public static BackgroundThread get() {
55 synchronized (BackgroundThread.class) {
56 ensureThreadLocked();
57 return sInstance;
58 }
59 }
60
61 public static Handler getHandler() {
62 synchronized (BackgroundThread.class) {
63 ensureThreadLocked();
64 return sHandler;
65 }
66 }
Makoto Onuki83415262019-02-14 15:37:20 -080067
68 public static Executor getExecutor() {
69 synchronized (BackgroundThread.class) {
70 ensureThreadLocked();
71 return sHandlerExecutor;
72 }
73 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -070074}