Android本地: 如何正确处理没有android_native_app_glue.h的输入?

huangapple go评论80阅读模式
英文:

Android Native: How to properly handle input without android_native_app_glue.h?

问题

我正在尝试创建一个原生Android应用程序,而不使用NDK的粘合代码,以避免创建新线程,而是像普通应用程序一样保持单线程。这在文档中有描述,但据我所知,没有示例。

我已经使用粘合代码的帮助创建了一些代码,它可以工作,但一旦我在滑动并释放时触发了ANR。慢速滑动和点击可以正常工作。

在单线程中处理输入的最佳方法是什么?

我的当前代码:

static int on_input_queue_event(int fd, int events, void* data) {
  LOGI("got events: %d", events);
  App* app = data;
  for (int i=0; i<events; i++) {
    uint64_t stuff;
    size_t count = read(fd, &stuff, 8);
    LOGI("%llu <-%d->", stuff, count);
    AInputEvent* event;
    int result = AInputQueue_getEvent(app->input_queue, &event);
    if (result < 0) {
      LOGE("something went wrong");
      break;
    }
    AInputQueue_finishEvent(app->input_queue, event, 1);
  }
  // 0: unregister callback; 1: keep doing work
  return 1;
}

static void on_input_queue_created(ANativeActivity* activity, AInputQueue* queue) {
  LOGI("on_input_queue_created");
  App* app = activity->instance;
  app->input_queue = queue;
  ALooper* looper = ALooper_prepare(0);
  AInputQueue_attachLooper(queue, looper, 1, on_input_queue_event, app);
}

static void on_input_queue_destroyed(ANativeActivity* activity, AInputQueue* queue) {
  LOGI("on_input_queue_destroyed");
  App* app = activity->instance;
  app->input_queue = NULL;
  AInputQueue_detachLooper(queue);
}

Logcat:

got events: 1
0 <-1->
[...]
got events: 1
0 <-1->
[mouse button up while still swiping]
[new events not popping up any more]

Stacktraces:

DALVIK THREADS (14):
"main" prio=5 tid=1 Native
  | group="main" sCount=1 ucsCount=0 flags=1 obj=0x71d9bcd8 self=0x777a3acd66f0
  | sysTid=11513 nice=-10 cgrp=top-app sched=0/0 handle=0x777b071c24f8
  | state=S schedstat=( 488289842 15752972 1719 ) utm=36 stm=12 core=1 HZ=100
  | stack=0x7fff6020b000-0x7fff6020d000 stackSize=8188KB
  | held mutexes=
  native: #00 pc 000b947a  /apex/com.android.runtime/lib64/bionic/libc.so (__epoll_pwait+10) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
  native: #01 pc 0001780b  /system/lib64/libutils.so (android::Looper::pollOnce+315) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
  native: #02 pc 00178f23  /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce+35) (BuildId: 81dafef743b2c95b83dbef61bb32d02a)
  native: #03 pc 003919cb  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+219) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
  native: #04 pc 00378444  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
  native: #05 pc 003c52dc  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
  native: #06 pc 0056d5a0  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall<false>+2080)

<details>
<summary>英文:</summary>

I&#39;m trying to do create a native Android app without using the NDK&#39;s glue code, to avoid creating a new thread but instead staying single-threaded like normal apps do. This is described in the 
[1], however, there is no example for it I&#39;m aware of.
I&#39;ve put something together with the help of the glue code which works but once I swipe and release while doing that it triggers an ANR. Slow swipes and clicks are working fine. What is the best way to handle input in a single thread? My current code: ```c static int on_input_queue_event(int fd, int events, void* data) { LOGI(&quot;got events: %d&quot;, events); App* app = data; for (int i=0; i&lt;events; i++) { uint64_t stuff; size_t count = read(fd, &amp;stuff, 8); LOGI(&quot;%llu &lt;-%d-&gt;&quot;, stuff, count); AInputEvent* event; int result = AInputQueue_getEvent(app-&gt;input_queue, &amp;event); if (result &lt; 0) { LOGE(&quot;something went wrong&quot;); break; } AInputQueue_finishEvent(app-&gt;input_queue, event, 1); } // 0: unregister callback; 1: keep doing work return 1; } static void on_input_queue_created(ANativeActivity* activity, AInputQueue* queue) { LOGI(&quot;on_input_queue_created&quot;); App* app = activity-&gt;instance; app-&gt;input_queue = queue; ALooper* looper = ALooper_prepare(0); AInputQueue_attachLooper(queue, looper, 1, on_input_queue_event, app); } static void on_input_queue_destroyed(ANativeActivity* activity, AInputQueue* queue) { LOGI(&quot;on_input_queue_destroyed&quot;); App* app = activity-&gt;instance; app-&gt;input_queue = NULL; AInputQueue_detachLooper(queue); }

Logcat:

got events: 1
0 &lt;-1-&gt;
[...]
got events: 1
0 &lt;-1-&gt;
[mouse button up while still swiping]
[new events not popping up any more]

Stacktraces:

DALVIK THREADS (14):
&quot;main&quot; prio=5 tid=1 Native
| group=&quot;main&quot; sCount=1 ucsCount=0 flags=1 obj=0x71d9bcd8 self=0x777a3acd66f0
| sysTid=11513 nice=-10 cgrp=top-app sched=0/0 handle=0x777b071c24f8
| state=S schedstat=( 488289842 15752972 1719 ) utm=36 stm=12 core=1 HZ=100
| stack=0x7fff6020b000-0x7fff6020d000 stackSize=8188KB
| held mutexes=
native: #00 pc 000b947a  /apex/com.android.runtime/lib64/bionic/libc.so (__epoll_pwait+10) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0001780b  /system/lib64/libutils.so (android::Looper::pollOnce+315) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #02 pc 00178f23  /system/lib64/libandroid_runtime.so (android::android_os_MessageQueue_nativePollOnce+35) (BuildId: 81dafef743b2c95b83dbef61bb32d02a)
native: #03 pc 003919cb  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+219) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #04 pc 00378444  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #05 pc 003c52dc  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #06 pc 0056d5a0  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2080) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #07 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #08 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #09 pc 001fc324  /system/framework/framework.jar (android.os.MessageQueue.next)
native: #10 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #11 pc 0056c90e  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+110) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #12 pc 0056d586  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2054) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #13 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #14 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #15 pc 001fb308  /system/framework/framework.jar (android.os.Looper.loopOnce)
native: #16 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #17 pc 0056c90e  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+110) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #18 pc 0056d586  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2054) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #19 pc 0039b021  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+17169) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #20 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #21 pc 001fba7c  /system/framework/framework.jar (android.os.Looper.loop)
native: #22 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #23 pc 0056c90e  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+110) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #24 pc 0056d586  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2054) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #25 pc 0039b021  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+17169) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #26 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #27 pc 001c36f4  /system/framework/framework.jar (android.app.ActivityThread.main)
native: #28 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #29 pc 0091e575  /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+901) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #30 pc 00391b5c  /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+140) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #31 pc 003787a6  /apex/com.android.art/lib64/libart.so (art_quick_invoke_static_stub+806) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #32 pc 003c530f  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+255) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #33 pc 007f00c4  /apex/com.android.art/lib64/libart.so (_jobject* art::InvokeMethod&lt;8&gt;+1476) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #34 pc 00759c77  /apex/com.android.art/lib64/libart.so (art::Method_invoke +39) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #35 pc 003919cb  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+219) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #36 pc 00378444  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #37 pc 003c52dc  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #38 pc 0056d5a0  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2080) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #39 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #40 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #41 pc 0050bf00  /system/framework/framework.jar (com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run)
native: #42 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #43 pc 0091e575  /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+901) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #44 pc 00391b5c  /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+140) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
at android.os.MessageQueue.nativePollOnce(Native method)
at android.os.MessageQueue.next(MessageQueue.java:335)
at android.os.Looper.loopOnce(Looper.java:162)
at android.os.Looper.loop(Looper.java:294)
at android.app.ActivityThread.main(ActivityThread.java:8176)
at java.lang.reflect.Method.invoke(Native method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
&quot;ReferenceQueueDaemon&quot; daemon prio=5 tid=9 Waiting
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f002f0 self=0x777a3ad01e40
| sysTid=11524 nice=4 cgrp=top-app sched=0/0 handle=0x7777de95fcf0
| state=S schedstat=( 853658 772115 5 ) utm=0 stm=0 core=5 HZ=100
| stack=0x7777de85d000-0x7777de85f000 stackSize=1035KB
| held mutexes=
at java.lang.Object.wait(Native method)
- waiting on &lt;0x0da62c46&gt; (a java.lang.Class&lt;java.lang.ref.ReferenceQueue&gt;)
at java.lang.Object.wait(Object.java:386)
at java.lang.Object.wait(Object.java:524)
at java.lang.Daemons$ReferenceQueueDaemon.runInternal(Daemons.java:237)
- locked &lt;0x0da62c46&gt; (a java.lang.Class&lt;java.lang.ref.ReferenceQueue&gt;)
at java.lang.Daemons$Daemon.run(Daemons.java:145)
at java.lang.Thread.run(Thread.java:1012)
&quot;FinalizerDaemon&quot; daemon prio=5 tid=11 Waiting
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00368 self=0x777a3acfe6a0
| sysTid=11525 nice=4 cgrp=top-app sched=0/0 handle=0x7777de856cf0
| state=S schedstat=( 1200728 0 2 ) utm=0 stm=0 core=4 HZ=100
| stack=0x7777de754000-0x7777de756000 stackSize=1035KB
| held mutexes=
at java.lang.Object.wait(Native method)
- waiting on &lt;0x05625e07&gt; (a java.lang.Object)
at java.lang.Object.wait(Object.java:386)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:203)
- locked &lt;0x05625e07&gt; (a java.lang.Object)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:224)
at java.lang.Daemons$FinalizerDaemon.runInternal(Daemons.java:305)
at java.lang.Daemons$Daemon.run(Daemons.java:145)
at java.lang.Thread.run(Thread.java:1012)
&quot;FinalizerWatchdogDaemon&quot; daemon prio=5 tid=13 Waiting
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00458 self=0x777a3acfcad0
| sysTid=11526 nice=4 cgrp=top-app sched=0/0 handle=0x7777de74dcf0
| state=S schedstat=( 237374 853338 3 ) utm=0 stm=0 core=0 HZ=100
| stack=0x7777de64b000-0x7777de64d000 stackSize=1035KB
| held mutexes=
at java.lang.Object.wait(Native method)
- waiting on &lt;0x0432dd5d&gt; (a java.lang.Daemons$FinalizerWatchdogDaemon)
at java.lang.Object.wait(Object.java:386)
at java.lang.Object.wait(Object.java:524)
at java.lang.Daemons$FinalizerWatchdogDaemon.sleepUntilNeeded(Daemons.java:424)
- locked &lt;0x0432dd5d&gt; (a java.lang.Daemons$FinalizerWatchdogDaemon)
at java.lang.Daemons$FinalizerWatchdogDaemon.runInternal(Daemons.java:404)
at java.lang.Daemons$Daemon.run(Daemons.java:145)
at java.lang.Thread.run(Thread.java:1012)
&quot;Signal Catcher&quot; daemon prio=10 tid=6 Runnable
| group=&quot;system&quot; sCount=0 ucsCount=0 flags=0 obj=0x12f00200 self=0x777a3acdba60
| sysTid=11519 nice=-20 cgrp=top-app sched=0/0 handle=0x77782e9fbcf0
| state=R schedstat=( 87318858 1261005 318 ) utm=5 stm=3 core=0 HZ=100
| stack=0x77782e905000-0x77782e907000 stackSize=987KB
| held mutexes= &quot;mutator lock&quot;(shared held)
native: #00 pc 0071caec  /apex/com.android.art/lib64/libart.so (art::DumpNativeStack+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #01 pc 0085b40a  /apex/com.android.art/lib64/libart.so (art::Thread::DumpStack const+346) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #02 pc 0087c80b  /apex/com.android.art/lib64/libart.so (art::DumpCheckpoint::Run+1163) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #03 pc 00875f54  /apex/com.android.art/lib64/libart.so (art::ThreadList::RunCheckpoint+980) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #04 pc 00874e06  /apex/com.android.art/lib64/libart.so (art::ThreadList::Dump+1782) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #05 pc 00874684  /apex/com.android.art/lib64/libart.so (art::ThreadList::DumpForSigQuit+1620) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #06 pc 0080d5ed  /apex/com.android.art/lib64/libart.so (art::Runtime::DumpForSigQuit+45) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #07 pc 00830129  /apex/com.android.art/lib64/libart.so (art::SignalCatcher::HandleSigQuit+2201) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #08 pc 0082eed5  /apex/com.android.art/lib64/libart.so (art::SignalCatcher::Run+565) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #09 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #10 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;perfetto_hprof_listener&quot; prio=10 tid=7 Native (still starting up)
| group=&quot;&quot; sCount=1 ucsCount=0 flags=1 obj=0x0 self=0x777a3ace4570
| sysTid=11520 nice=-20 cgrp=top-app sched=0/0 handle=0x77782e8fecf0
| state=S schedstat=( 403014 644798 6 ) utm=0 stm=0 core=0 HZ=100
| stack=0x77782e808000-0x77782e80a000 stackSize=987KB
| held mutexes=
native: #00 pc 000b8195  /apex/com.android.runtime/lib64/bionic/libc.so (read+5) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 00025ed0  /apex/com.android.art/lib64/libperfetto_hprof.so (void* std::__1::__thread_proxy&lt;std::__1::tuple&lt;std::__1::unique_ptr&lt;std::__1::__thread_struct, std::__1::default_delete&lt;std::__1::__thread_struct&gt; &gt;, ArtPlugin_Initialize::$_7&gt; &gt;+320) (BuildId: 4c01f0d4df1271c9a7a47f2678762ef5)
native: #02 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #03 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;Jit thread pool worker thread 0&quot; daemon prio=5 tid=8 Native
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00278 self=0x777a3acf5b90
| sysTid=11522 nice=9 cgrp=top-app sched=0/0 handle=0x777822978cf0
| state=S schedstat=( 270465 7744 4 ) utm=0 stm=0 core=0 HZ=100
| stack=0x77782287a000-0x77782287c000 stackSize=1019KB
| held mutexes=
native: #00 pc 0005dae6  /apex/com.android.runtime/lib64/bionic/libc.so (syscall+22) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 003cf36b  /apex/com.android.art/lib64/libart.so (art::ConditionVariable::WaitHoldingLocks+107) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #02 pc 0087e737  /apex/com.android.art/lib64/libart.so (art::ThreadPool::GetTask+103) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #03 pc 0087d9c0  /apex/com.android.art/lib64/libart.so (art::ThreadPoolWorker::Run+64) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #04 pc 0087d488  /apex/com.android.art/lib64/libart.so (art::ThreadPoolWorker::Callback+152) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #05 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #06 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;HeapTaskDaemon&quot; daemon prio=5 tid=10 WaitingForTaskProcessor
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00790 self=0x777a3acfaf00
| sysTid=11523 nice=4 cgrp=top-app sched=0/0 handle=0x7777dea68cf0
| state=S schedstat=( 7569749 995173 22 ) utm=0 stm=0 core=2 HZ=100
| stack=0x7777de966000-0x7777de968000 stackSize=1035KB
| held mutexes=
native: #00 pc 0005dae6  /apex/com.android.runtime/lib64/bionic/libc.so (syscall+22) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 003cf36b  /apex/com.android.art/lib64/libart.so (art::ConditionVariable::WaitHoldingLocks+107) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #02 pc 005422d1  /apex/com.android.art/lib64/libart.so (art::gc::TaskProcessor::GetTask+465) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #03 pc 00542ae3  /apex/com.android.art/lib64/libart.so (art::gc::TaskProcessor::RunAllTasks+67) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #04 pc 003919cb  /apex/com.android.art/lib64/libart.so (art_quick_generic_jni_trampoline+219) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #05 pc 00378444  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #06 pc 003c52dc  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #07 pc 0056d5a0  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2080) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #08 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #09 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #10 pc 0002bac4  /apex/com.android.art/javalib/core-libart.jar (java.lang.Daemons$HeapTaskDaemon.runInternal)
native: #11 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #12 pc 0056c90e  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+110) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #13 pc 0056d586  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2054) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #14 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #15 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #16 pc 0002ae30  /apex/com.android.art/javalib/core-libart.jar (java.lang.Daemons$Daemon.run)
native: #17 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #18 pc 0056c90e  /apex/com.android.art/lib64/libart.so (art::interpreter::ArtInterpreterToInterpreterBridge+110) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #19 pc 0056d586  /apex/com.android.art/lib64/libart.so (bool art::interpreter::DoCall&lt;false&gt;+2054) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #20 pc 0039adb2  /apex/com.android.art/lib64/libart.so (void art::interpreter::ExecuteSwitchImplCpp&lt;false&gt;+16546) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #21 pc 00393725  /apex/com.android.art/lib64/libart.so (ExecuteSwitchImplAsm+5) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #22 pc 0010edcc  /apex/com.android.art/javalib/core-oj.jar (java.lang.Thread.run)
native: #23 pc 005657e7  /apex/com.android.art/lib64/libart.so (art::interpreter::Execute +647) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #24 pc 0091e575  /apex/com.android.art/lib64/libart.so (artQuickToInterpreterBridge+901) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #25 pc 00391b5c  /apex/com.android.art/lib64/libart.so (art_quick_to_interpreter_bridge+140) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #26 pc 00378444  /apex/com.android.art/lib64/libart.so (art_quick_invoke_stub+756) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #27 pc 003c52dc  /apex/com.android.art/lib64/libart.so (art::ArtMethod::Invoke+204) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #28 pc 00852686  /apex/com.android.art/lib64/libart.so (art::Thread::CreateCallback+1510) (BuildId: c2a5d0e70b21c5c725569bad41f1b195)
native: #29 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #30 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
at dalvik.system.VMRuntime.runHeapTasks(Native method)
at java.lang.Daemons$HeapTaskDaemon.runInternal(Daemons.java:678)
at java.lang.Daemons$Daemon.run(Daemons.java:145)
at java.lang.Thread.run(Thread.java:1012)
&quot;ADB-JDWP Connection Control Thread&quot; daemon prio=0 tid=12 WaitingInMainDebuggerLoop
| group=&quot;system&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f003e0 self=0x777a3ad00270
| sysTid=11521 nice=-20 cgrp=top-app sched=0/0 handle=0x77782e801cf0
| state=S schedstat=( 1707457 3546731 17 ) utm=0 stm=0 core=2 HZ=100
| stack=0x77782e70b000-0x77782e70d000 stackSize=987KB
| held mutexes=
native: #00 pc 000b957a  /apex/com.android.runtime/lib64/bionic/libc.so (__ppoll+10) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0006da0a  /apex/com.android.runtime/lib64/bionic/libc.so (poll+74) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #02 pc 0000b601  /apex/com.android.art/lib64/libadbconnection.so (adbconnection::AdbConnectionState::RunPollLoop+833) (BuildId: 00b98692de2230b124260270cb1358f8)
native: #03 pc 00009bc0  /apex/com.android.art/lib64/libadbconnection.so (adbconnection::CallbackFunction+1616) (BuildId: 00b98692de2230b124260270cb1358f8)
native: #04 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #05 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;binder:11513_1&quot; prio=5 tid=14 Native
| group=&quot;main&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f004d0 self=0x777a3ad03a10
| sysTid=11527 nice=0 cgrp=top-app sched=0/0 handle=0x7777de546cf0
| state=S schedstat=( 340768 13826 6 ) utm=0 stm=0 core=1 HZ=100
| stack=0x7777de450000-0x7777de452000 stackSize=987KB
| held mutexes=
native: #00 pc 000b84b7  /apex/com.android.runtime/lib64/bionic/libc.so (__ioctl+7) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0006b378  /apex/com.android.runtime/lib64/bionic/libc.so (ioctl+216) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #02 pc 0009040f  /system/lib64/libbinder.so (android::IPCThreadState::joinThreadPool+303) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #03 pc 000902d7  /system/lib64/libbinder.so (android::PoolThread::threadLoop+23) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #04 pc 000111d2  /system/lib64/libutils.so (android::Thread::_threadLoop+178) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #05 pc 000eb6a2  /system/lib64/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell+146) (BuildId: 81dafef743b2c95b83dbef61bb32d02a)
native: #06 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #07 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;binder:11513_2&quot; prio=5 tid=15 Native
| group=&quot;main&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00548 self=0x777a3ad055e0
| sysTid=11528 nice=0 cgrp=top-app sched=0/0 handle=0x7777de449cf0
| state=S schedstat=( 6424318 505756 43 ) utm=0 stm=0 core=1 HZ=100
| stack=0x7777de353000-0x7777de355000 stackSize=987KB
| held mutexes=
native: #00 pc 000b84b7  /apex/com.android.runtime/lib64/bionic/libc.so (__ioctl+7) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0006b378  /apex/com.android.runtime/lib64/bionic/libc.so (ioctl+216) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #02 pc 0009040f  /system/lib64/libbinder.so (android::IPCThreadState::joinThreadPool+303) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #03 pc 000902d7  /system/lib64/libbinder.so (android::PoolThread::threadLoop+23) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #04 pc 000111d2  /system/lib64/libutils.so (android::Thread::_threadLoop+178) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #05 pc 000eb6a2  /system/lib64/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell+146) (BuildId: 81dafef743b2c95b83dbef61bb32d02a)
native: #06 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #07 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;binder:11513_3&quot; prio=5 tid=16 Native
| group=&quot;main&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f005c0 self=0x777a3ad071b0
| sysTid=11529 nice=0 cgrp=top-app sched=0/0 handle=0x7777de34ccf0
| state=S schedstat=( 3328384 71333 19 ) utm=0 stm=0 core=4 HZ=100
| stack=0x7777de256000-0x7777de258000 stackSize=987KB
| held mutexes=
native: #00 pc 000b84b7  /apex/com.android.runtime/lib64/bionic/libc.so (__ioctl+7) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0006b378  /apex/com.android.runtime/lib64/bionic/libc.so (ioctl+216) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #02 pc 0009040f  /system/lib64/libbinder.so (android::IPCThreadState::joinThreadPool+303) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #03 pc 000902d7  /system/lib64/libbinder.so (android::PoolThread::threadLoop+23) (BuildId: 36c70419238237b253dceef0acc47cb5)
native: #04 pc 000111d2  /system/lib64/libutils.so (android::Thread::_threadLoop+178) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #05 pc 000eb6a2  /system/lib64/libandroid_runtime.so (android::AndroidRuntime::javaThreadShell+146) (BuildId: 81dafef743b2c95b83dbef61bb32d02a)
native: #06 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #07 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
&quot;RenderThread&quot; daemon prio=7 tid=17 Native
| group=&quot;main&quot; sCount=1 ucsCount=0 flags=1 obj=0x12f00638 self=0x777a3ad08d80
| sysTid=11532 nice=-4 cgrp=top-app sched=0/0 handle=0x7777d88b3cf0
| state=S schedstat=( 1724650 182811 12 ) utm=0 stm=0 core=5 HZ=100
| stack=0x7777d87bd000-0x7777d87bf000 stackSize=987KB
| held mutexes=
native: #00 pc 000b947a  /apex/com.android.runtime/lib64/bionic/libc.so (__epoll_pwait+10) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #01 pc 0001780b  /system/lib64/libutils.so (android::Looper::pollOnce+315) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #02 pc 0070ac6c  /system/lib64/libhwui.so (android::uirenderer::renderthread::RenderThread::threadLoop+268) (BuildId: 7b99cf67d77fc23fc7bd5c04e05f8f6c)
native: #03 pc 000111d2  /system/lib64/libutils.so (android::Thread::_threadLoop+178) (BuildId: 624a8b7c0eabca99824bda237d4324ab)
native: #04 pc 000cd00a  /apex/com.android.runtime/lib64/bionic/libc.so (__pthread_start+58) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
native: #05 pc 00062cd8  /apex/com.android.runtime/lib64/bionic/libc.so (__start_thread+56) (BuildId: 3b932b1f8a953967762bc3b7184185e5)
(no managed stack frames)
[... too long for stackoverflow]

答案1

得分: 0

这似乎有效:

  • 忽略 fd
  • 忽略 events
  • 只需读取输入队列,直到它返回<0
static int on_input_queue_event(int fd, int events, void* data) {
  App* app = data;
  AInputEvent* event;
  while (AInputQueue_getEvent(app->input_queue, &event) >= 0) {
    // 处理事件
    AInputQueue_finishEvent(app->input_queue, event, 1);
  }
  return 1;
}

编辑:我怀疑当我刷了一下然后立刻放开时,两个事件同时弹出,只有一个被读取,因此留下一个未处理,触发 ANR。

英文:

This seems to work:

  • ignore fd
  • ignore events
  • just read input queue until it returns <0
static int on_input_queue_event(int fd, int events, void* data) {
  App* app = data;
  AInputEvent* event;
  while (AInputQueue_getEvent(app-&gt;input_queue, &amp;event) &gt;= 0) {
    // handle event
    AInputQueue_finishEvent(app-&gt;input_queue, event, 1);
  }
  return 1;
}

Edit: I suspect that when I swiped and let go immediately, two events popped up at the same time and only one was read hence leaving one unprocessed, triggering an ANR.

huangapple
  • 本文由 发表于 2023年6月30日 03:09:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76583985.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定