Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package com.android.server; |
| 18 | |
| 19 | import android.os.Handler; |
Makoto Onuki | 8341526 | 2019-02-14 15:37:20 -0800 | [diff] [blame] | 20 | import android.os.HandlerExecutor; |
Jeff Sharkey | 74cd3de | 2016-04-06 17:40:54 -0600 | [diff] [blame] | 21 | import android.os.Trace; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 22 | |
Makoto Onuki | 8341526 | 2019-02-14 15:37:20 -0800 | [diff] [blame] | 23 | import java.util.concurrent.Executor; |
| 24 | |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 25 | /** |
| 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 Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 30 | public final class IoThread extends ServiceThread { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 31 | private static IoThread sInstance; |
| 32 | private static Handler sHandler; |
Makoto Onuki | 8341526 | 2019-02-14 15:37:20 -0800 | [diff] [blame] | 33 | private static HandlerExecutor sHandlerExecutor; |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 34 | |
| 35 | private IoThread() { |
Jeff Brown | 4ccb823 | 2014-01-16 22:16:42 -0800 | [diff] [blame] | 36 | super("android.io", android.os.Process.THREAD_PRIORITY_DEFAULT, true /*allowIo*/); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | private static void ensureThreadLocked() { |
| 40 | if (sInstance == null) { |
| 41 | sInstance = new IoThread(); |
| 42 | sInstance.start(); |
Michael Wright | daf72b1 | 2017-11-01 22:24:08 +0000 | [diff] [blame] | 43 | sInstance.getLooper().setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 44 | sHandler = new Handler(sInstance.getLooper()); |
Makoto Onuki | 8341526 | 2019-02-14 15:37:20 -0800 | [diff] [blame] | 45 | sHandlerExecutor = new HandlerExecutor(sHandler); |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 46 | } |
| 47 | } |
| 48 | |
| 49 | public static IoThread get() { |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 50 | synchronized (IoThread.class) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 51 | ensureThreadLocked(); |
| 52 | return sInstance; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | public static Handler getHandler() { |
Dianne Hackborn | efa92b2 | 2013-05-03 14:11:43 -0700 | [diff] [blame] | 57 | synchronized (IoThread.class) { |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 58 | ensureThreadLocked(); |
| 59 | return sHandler; |
| 60 | } |
| 61 | } |
Makoto Onuki | 8341526 | 2019-02-14 15:37:20 -0800 | [diff] [blame] | 62 | |
| 63 | public static Executor getExecutor() { |
| 64 | synchronized (IoThread.class) { |
| 65 | ensureThreadLocked(); |
| 66 | return sHandlerExecutor; |
| 67 | } |
| 68 | } |
Dianne Hackborn | 8d044e8 | 2013-04-30 17:24:15 -0700 | [diff] [blame] | 69 | } |