Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NDK/JNI: identifying current thread

In a JNI native method, is there a way to know the ID of the current thread without calling Java back? Thread-local storage would also work.

EDIT: pthread.h is present in the NDK include folder... Does anyone know if Java threads correspond to POSIX ones in the NDK implementation?

like image 917
Seva Alekseyev Avatar asked Apr 21 '11 21:04

Seva Alekseyev


1 Answers

Which ID are you interested in? A Dalvik thread dump includes this:

"main" prio=5 tid=1 TIMED_WAIT
  | group="main" sCount=1 dsCount=0 obj=0x40017730 self=0x12798
  | sysTid=3167 nice=0 sched=0/0 cgrp=default handle=-2146114456
  | schedstat=( 358850000 275073000 869 ) utm=23 stm=12 core=0

"tid" is the VM's ID. "handle" is the pthread_t. "sysTid" is the result of gettid() (the Linux process ID).

The libcore thread ID (obtained from java.lang.Thread.getId()) is not shown.

(You can obtain the above with "adb shell kill -3 ". The output goes to a common file, defined by the dalvik.vm.stack-trace-file property -- usually /data/anr/traces.txt, but it varies by device.)

EDIT: Every Dalvik VM thread is a Linux pthread. The gettid() syscall will give you a unique ID for each thread. Also, you can add identifying information to TLS in java.lang.Thread or pthread_key.

like image 164
fadden Avatar answered Nov 07 '22 21:11

fadden