前段时间开发了一个小屏的功能(效果图如下:),果了一段时间,测试的人员说我们的手机有时开机不了:一直停止在开机的界面.刚开始都不知道什么原因.查了各种代码都没有发现问题.到后来感觉问题越来越严重了.
到后来感觉问题越来越严重了.重要的是这个时低概率事件,很难复现一次,所以有点难.好不容易出现,通过data/anr/traces.txt找到问题,下面来看看关键信息:
"WindowManag4 MONITOR | group="main" sCount=1 dsCount=0 obj=0x4275d4d8 self=0x57929cb0 | sysTid=732 nice=-4 sched=0/0 cgrp=apps handle=1469227632 | schedstat=( 1563495873 1380427864 20236 ) - waiting to lock <0x427e72d8> (a Ljava/lang/Object; ) held by tid=1 (main) | [0]:<0006> Landroid/hardware/display/DisplayManagerGlobal;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerGlobal.java:108</span>) | [1]:<0008> Landroid/view/Display;.updateDisplayInfoLocked()V (Display.java:657) | [2]:<0002> Landroid/view/Display;.getMetrics(Landroid/util/DisplayMetrics;)V (Display.java:585) | [3]:<0104> Lcom/android/server/display/xunhuOverlayDisplayAdapter;.<init>(Lcom/android/server/display/DisplayManagerService$SyncRoot;Lan (<span style="color:#FF0000;">xunhuOverlayDisplayAdapter.java:104</span>) | [4]:<0030> Lcom/android/server/display/DisplayManagerService;.register_xunhuOverlayDisplayAdapterLocked()V (DisplayManagerService.java:994)
"main" prio=5 tid=1 MONITOR | group="main" sCount=1 dsCount=0 obj=0x422c6cf8 self=0x4182e500 | sysTid=715 nice=-2 sched=0/0 cgrp=apps handle=1074631044 | schedstat=( 7254851604 2756007452 25767 ) - waiting to lock <0x4277acc8> (a Lcom/android/server/display/DisplayManagerService$SyncRoot; ) held by tid=14 (WindowManager) | [0]:<0020> Lcom/android/server/display/DisplayManagerService;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerService.java:449</span>) | [1]:<0012> Landroid/hardware/display/DisplayManagerGlobal;.getDisplayInfo(I)Landroid/view/DisplayInfo; (<span style="color:#FF0000;">DisplayManagerGlobal.java:117</span>) | [2]:<0000> Landroid/hardware/display/DisplayManagerGlobal;.getCompatibleDisplay(ILandroid/view/DisplayAdjustments;)Landroid/view/Display; (DisplayManagerGlobal.java:176)
public DisplayInfo getDisplayInfo(int displayId) { try { synchronized (mLock) {<span style="color:#FF0000;">//108</span> DisplayInfo info; if (USE_CACHE) { info = mDisplayInfoCache.get(displayId); if (info != null) { return info; } } info = mDm.getDisplayInfo(displayId);<span style="color:#FF0000;">//117</span> if (info == null) { return null; }首先是这里mLock锁竞争.在这个锁里调用mDm.getDisplayInfo(displayId)的时候会回调到DisplayManagerService里面,如下代码:
public DisplayInfo getDisplayInfo(int displayId) { final int callingUid = Binder.getCallingUid(); final long token = Binder.clearCallingIdentity(); try { synchronized (mSyncRoot) {<span style="color:#FF0000;">//449</span> /// M:[SmartBook]Ignore extra display devicee info if (FeatureOption.MTK_SMARTBOOK_SUPPORT) { if (displayId == mExtraDisplayId) { return null; } }很显然,在获取了 DisplayManagerGlobal里面的锁:mLock,还需要DisplayManagerService里面的锁mSyncRoot.
而我开发的小屏功能在DisplayManagerService里面register小屏的DisplayAdapter的时候已经获取了mSyncRoot锁,所以如果在这同时(register小屏的DisplayAdapter的时候)调用下面的代码:
WindowManager wm = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE); wm.getDefaultDisplay().getMetrics(metric);就容易出现死锁. 因为在调用getDefaultDisplay()的时候最后会调用DisplayManagerGlobal的getDisplayInfo这个方法.而在开机的时候有大量其他地方调用DisplayManagerGlobal的getDisplayInfo这个方法.
原文地址:http://blog.csdn.net/green_shing/article/details/45363315