1 /* 2 * Copyright (C) 2021 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.tare; 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 * Singleton thread for all of TARE. 29 * 30 * @see com.android.internal.os.BackgroundThread 31 */ 32 final class TareHandlerThread extends HandlerThread { 33 34 private static TareHandlerThread sInstance; 35 private static Executor sHandlerExecutor; 36 private static Handler sHandler; 37 TareHandlerThread()38 private TareHandlerThread() { 39 super("tare"); 40 } 41 ensureThreadLocked()42 private static void ensureThreadLocked() { 43 if (sInstance == null) { 44 sInstance = new TareHandlerThread(); 45 sInstance.start(); 46 final Looper looper = sInstance.getLooper(); 47 looper.setTraceTag(Trace.TRACE_TAG_SYSTEM_SERVER); 48 sHandler = new Handler(sInstance.getLooper()); 49 sHandlerExecutor = new HandlerExecutor(sHandler); 50 } 51 } 52 get()53 static TareHandlerThread get() { 54 synchronized (TareHandlerThread.class) { 55 ensureThreadLocked(); 56 } 57 return sInstance; 58 } 59 60 /** Returns the singleton handler executor for TareHandlerThread */ getExecutor()61 public static Executor getExecutor() { 62 synchronized (TareHandlerThread.class) { 63 ensureThreadLocked(); 64 return sHandlerExecutor; 65 } 66 } 67 68 /** Returns the singleton handler for TareHandlerThread. */ getHandler()69 public static Handler getHandler() { 70 synchronized (TareHandlerThread.class) { 71 ensureThreadLocked(); 72 } 73 return sHandler; 74 } 75 } 76