GetStringCritical可以导致死锁吗?

时间:2017-05-24 07:38:36

标签: java android java-native-interface deadlock

system_server中发生了死锁,并且PackageManager保持锁定。

"PackageManager" prio=5 tid=27 WaitingForGcToComplete
  | group="main" sCount=1 dsCount=0 obj=0x12d3e7b0 self=0xb865c1a0
  | sysTid=579 nice=0 cgrp=default sched=0/0 handle=0xb865c788
  | state=S schedstat=( 82300981 68593861 146 ) utm=6 stm=2 core=1 HZ=100
  | stack=0xa0fdb000-0xa0fdd000 stackSize=1036KB
  | held mutexes=
  native: #00 pc 00012960  /system/lib/libc.so (syscall+28)
  native: #01 pc 000a88ad  /system/lib/libart.so(art::Mutex::ExclusiveLock(art::Thread*)+364)
  native: #02 pc 0013aa7b  /system/lib/libart.so (art::gc::Heap::IncrementDisableMovingGC(art::Thread*)+90)
  native: #03 pc 001c0329  /system/lib/libart.so (art::JNI::GetStringCritical(_JNIEnv*, _jstring*, unsigned char*)+392)
  native: #04 pc 00082223  /system/lib/libandroid_runtime.so (???)
  native: #05 pc 00082291  /system/lib/libandroid_runtime.so (???)
  native: #06 pc 00263595  /data/dalvik-cache/arm/system@framework@boot.oat (Java_android_os_Parcel_nativeWriteString__JLjava_lang_String_2+120)
  at android.os.Parcel.nativeWriteString(Native method)
  at android.os.Parcel.writeString(Parcel.java:542)
  at android.content.ComponentName.writeToParcel(ComponentName.java:267)
  at android.content.ComponentName.writeToParcel(ComponentName.java:282)
  at android.content.Intent.writeToParcel(Intent.java:7486)
  at android.app.ApplicationThreadProxy.scheduleUnbindService(ApplicationThreadNative.java:929)
  at com.android.server.am.ActiveServices.removeConnectionLocked(ActiveServices.java:1842)
  at com.android.server.am.ActiveServices.unbindServiceLocked(ActiveServices.java:943)
  at com.android.server.am.ActivityManagerService.unbindService(ActivityManagerService.java:15787)
  - locked <0x1d3e13e9> (a com.android.server.am.ActivityManagerService)

所以我谷歌并查看GetStringCritical和ReleaseStringCritical之间是否还有其他JNI调用,如果GC被阻止,可能会发生死锁。 有关详情,请参阅The Java Native Interface: Programmer's Guide and Specification

这是frameworks \ base \ core \ jni \ android_os_Parcel.cpp中方法android_os_Parcel_writeString的一部分。

    const jchar* str = env->GetStringCritical(val, 0);
    if (str) {
        err = parcel->writeString16(str, env->GetStringLength(val));
        env->ReleaseStringCritical(val, str);
    }

我无法确定死锁是由android_os_Parcel_writeString引起的,因为android_os_Parcel_writeString是一种非常常用的方法。

所以我问的是android_os_Parcel_writeString可能导致死锁吗?

感谢您的回答并忘记了我笨拙的英语。

0 个答案:

没有答案