blob: 5d0e308f6649ff37e8a4960c443f0e8deb631fbe [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.server;
18
19import android.os.Handler;
Makoto Onuki83415262019-02-14 15:37:20 -080020import android.os.HandlerExecutor;
Makoto Onuki712886f2018-04-27 15:22:50 -070021import android.os.Looper;
Jeff Sharkey74cd3de2016-04-06 17:40:54 -060022import android.os.Trace;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070023
Makoto Onuki83415262019-02-14 15:37:20 -080024import java.util.concurrent.Executor;
25
Dianne Hackborn8d044e82013-04-30 17:24:15 -070026/**
27 * Shared singleton foreground thread for the system. This is a thread for regular
28 * foreground service operations, which shouldn't be blocked by anything running in
29 * the background. In particular, the shared background thread could be doing
30 * relatively long-running operations like saving state to disk (in addition to
31 * simply being a background priority), which can cause operations scheduled on it
32 * to be delayed for a user-noticeable amount of time.
33 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080034public final class FgThread extends ServiceThread {
Makoto Onuki712886f2018-04-27 15:22:50 -070035 private static final long SLOW_DISPATCH_THRESHOLD_MS = 100;
36 private static final long SLOW_DELIVERY_THRESHOLD_MS = 200;
37
Dianne Hackborn8d044e82013-04-30 17:24:15 -070038 private static FgThread sInstance;
39 private static Handler sHandler;
Makoto Onuki83415262019-02-14 15:37:20 -080040 private static HandlerExecutor sHandlerExecutor;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070041
42 private FgThread() {
Jeff Brown4ccb8232014-01-16 22:16:42 -080043 super("android.fg", android.os.Process.THREAD_PRIORITY_DEFAULT, true /*allowIo*/);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070044 }
45
46 private static void ensureThreadLocked() {
47 if (sInstance == null) {
48 sInstance = new FgThread();
49 sInstance.start();
Makoto Onuki712886f2018-04-27 15:22:50 -070050 final Looper looper = sInstance.getLooper();
51 looper.setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
52 looper.setSlowLogThresholdMs(
53 SLOW_DISPATCH_THRESHOLD_MS, SLOW_DELIVERY_THRESHOLD_MS);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070054 sHandler = new Handler(sInstance.getLooper());
Makoto Onuki83415262019-02-14 15:37:20 -080055 sHandlerExecutor = new HandlerExecutor(sHandler);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070056 }
57 }
58
59 public static FgThread get() {
yangbingqian6ecf9af2017-05-10 20:31:15 +080060 synchronized (FgThread.class) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070061 ensureThreadLocked();
62 return sInstance;
63 }
64 }
65
66 public static Handler getHandler() {
yangbingqian6ecf9af2017-05-10 20:31:15 +080067 synchronized (FgThread.class) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070068 ensureThreadLocked();
69 return sHandler;
70 }
71 }
Makoto Onuki83415262019-02-14 15:37:20 -080072
73 public static Executor getExecutor() {
74 synchronized (FgThread.class) {
75 ensureThreadLocked();
76 return sHandlerExecutor;
77 }
78 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -070079}