blob: 21fd29c3bbef48d9995d9984076743a3ae611aca [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;
Jeff Sharkey74cd3de2016-04-06 17:40:54 -060021import android.os.Trace;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070022
Makoto Onuki83415262019-02-14 15:37:20 -080023import java.util.concurrent.Executor;
24
Dianne Hackborn8d044e82013-04-30 17:24:15 -070025/**
26 * Shared singleton I/O thread for the system. This is a thread for non-background
27 * service operations that can potential block briefly on network IO operations
28 * (not waiting for data itself, but communicating with network daemons).
29 */
Jeff Brown4ccb8232014-01-16 22:16:42 -080030public final class IoThread extends ServiceThread {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070031 private static IoThread sInstance;
32 private static Handler sHandler;
Makoto Onuki83415262019-02-14 15:37:20 -080033 private static HandlerExecutor sHandlerExecutor;
Dianne Hackborn8d044e82013-04-30 17:24:15 -070034
35 private IoThread() {
Jeff Brown4ccb8232014-01-16 22:16:42 -080036 super("android.io", android.os.Process.THREAD_PRIORITY_DEFAULT, true /*allowIo*/);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070037 }
38
39 private static void ensureThreadLocked() {
40 if (sInstance == null) {
41 sInstance = new IoThread();
42 sInstance.start();
Michael Wrightdaf72b12017-11-01 22:24:08 +000043 sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070044 sHandler = new Handler(sInstance.getLooper());
Makoto Onuki83415262019-02-14 15:37:20 -080045 sHandlerExecutor = new HandlerExecutor(sHandler);
Dianne Hackborn8d044e82013-04-30 17:24:15 -070046 }
47 }
48
49 public static IoThread get() {
Dianne Hackbornefa92b22013-05-03 14:11:43 -070050 synchronized (IoThread.class) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070051 ensureThreadLocked();
52 return sInstance;
53 }
54 }
55
56 public static Handler getHandler() {
Dianne Hackbornefa92b22013-05-03 14:11:43 -070057 synchronized (IoThread.class) {
Dianne Hackborn8d044e82013-04-30 17:24:15 -070058 ensureThreadLocked();
59 return sHandler;
60 }
61 }
Makoto Onuki83415262019-02-14 15:37:20 -080062
63 public static Executor getExecutor() {
64 synchronized (IoThread.class) {
65 ensureThreadLocked();
66 return sHandlerExecutor;
67 }
68 }
Dianne Hackborn8d044e82013-04-30 17:24:15 -070069}