Home | History | Annotate | Download | only in logcat
      1 /*
      2  * Copyright (C) 2011 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 package com.android.ddmuilib.logcat;
     17 
     18 import com.android.ddmlib.AndroidDebugBridge;
     19 import com.android.ddmlib.AndroidDebugBridge.IDeviceChangeListener;
     20 import com.android.ddmlib.IDevice;
     21 
     22 import org.eclipse.jface.preference.IPreferenceStore;
     23 
     24 import java.util.HashMap;
     25 import java.util.Map;
     26 
     27 /**
     28  * A factory for {@link LogCatReceiver} objects. Its primary objective is to cache
     29  * constructed {@link LogCatReceiver}'s per device and hand them back when requested.
     30  */
     31 public class LogCatReceiverFactory {
     32     /** Singleton instance. */
     33     public static final LogCatReceiverFactory INSTANCE = new LogCatReceiverFactory();
     34 
     35     private Map<String, LogCatReceiver> mReceiverCache = new HashMap<String, LogCatReceiver>();
     36 
     37     /** Private constructor: cannot instantiate. */
     38     private LogCatReceiverFactory() {
     39         AndroidDebugBridge.addDeviceChangeListener(new IDeviceChangeListener() {
     40             @Override
     41             public void deviceDisconnected(final IDevice device) {
     42                 // The deviceDisconnected() is called from DDMS code that holds
     43                 // multiple locks regarding list of clients, etc.
     44                 // It so happens that #newReceiver() below adds a clientChangeListener
     45                 // which requires those locks as well. So if we call
     46                 // #removeReceiverFor from a DDMS/Monitor thread, we could end up
     47                 // in a deadlock. As a result, we spawn a separate thread that
     48                 // doesn't hold any of the DDMS locks to remove the receiver.
     49                 Thread t = new Thread(new Runnable() {
     50                         @Override
     51                         public void run() {
     52                             removeReceiverFor(device);                        }
     53                     }, "Remove logcat receiver for " + device.getSerialNumber());
     54                 t.start();
     55             }
     56 
     57             @Override
     58             public void deviceConnected(IDevice device) {
     59             }
     60 
     61             @Override
     62             public void deviceChanged(IDevice device, int changeMask) {
     63             }
     64         });
     65     }
     66 
     67     /**
     68      * Remove existing logcat receivers. This method should not be called from a DDMS thread
     69      * context that might be holding locks. Doing so could result in a deadlock with the following
     70      * two threads locked up: <ul>
     71      * <li> {@link #removeReceiverFor(IDevice)} waiting to lock {@link LogCatReceiverFactory},
     72      * while holding a DDMS monitor internal lock. </li>
     73      * <li> {@link #newReceiver(IDevice, IPreferenceStore)} holding {@link LogCatReceiverFactory}
     74      * while attempting to obtain a DDMS monitor lock. </li>
     75      * </ul>
     76      */
     77     private synchronized void removeReceiverFor(IDevice device) {
     78         LogCatReceiver r = mReceiverCache.get(device.getSerialNumber());
     79         if (r != null) {
     80             r.stop();
     81             mReceiverCache.remove(device.getSerialNumber());
     82         }
     83     }
     84 
     85     public synchronized LogCatReceiver newReceiver(IDevice device, IPreferenceStore prefs) {
     86         LogCatReceiver r = mReceiverCache.get(device.getSerialNumber());
     87         if (r != null) {
     88             return r;
     89         }
     90 
     91         r = new LogCatReceiver(device, prefs);
     92         mReceiverCache.put(device.getSerialNumber(), r);
     93         return r;
     94     }
     95 }
     96