Possible states for native threads on Android?
What 开发者_JAVA百科are all the possible thread states during execution for native (C/C++) threads on an Android device? Are they the same as the Java Thread States? Are they Linux threads? POSIX threads?
Not required, but bonus points for providing examples of what can cause a thread to enter each state.
Edit: As requested, here's the motivation:
I'm designing the interface for a sampling profiler that works with native C/C++ code on Android. The profiler reports will show thread states over time. I need to know what all the states are in order to a) know how many distinct states I will need to possibly visually differentiate, and b) design a color scheme that visually differentiates and groups the desirable states versus the undesirable states.
I've been told that native threads on Android are just lightweight processes. This agrees with what I've found for Linux in general. Quoting this wiki page:
A process (which includes a thread) on a Linux machine can be in any of the following states:
TASK_RUNNING
- The process is either executing on a CPU or waiting to be executed.TASK_INTERRUPTIBLE
- The process is suspended (sleeping) until some condition becomes true. Raising a hardware interrupt, releasing a system resource the process is waiting for, or delivering a signal are examples of conditions that might wake up the process (put its state back to TASK_RUNNING). Typically blocking IO calls (disk/network) will result in the task being marked as TASK_INTERRUPTIBLE. As soon as the data it is waiting on is ready to be read an interrupt is raised by the device and the interrupt handler changes the state of the task to TASK_INTERRUPTIBLE. Also processes in idle mode (ie not performing any task) should be in this state.TASK_UNINTERRUPTIBLE
- LikeTASK_INTERRUPTIBLE
, except that delivering a signal to the sleeping process leaves its state unchanged. This process state is seldom used. It is valuable, however, under certain specific conditions in which a process must wait until a given event occurs without being interrupted. Ideally not too many tasks will be in this state.
- For instance, this state may be used when a process opens a device file and the corresponding device driver starts probing for a corresponding hardware device. The device driver must not be interrupted until the probing is complete, or the hardware device could be left in an unpredictable state.
- Atomic write operations may require a task to be marked as
UNINTERRUPTIBLE
- NFS access sometimes results in access processes being marked as
UNINTERRUPTIBLE
reads/writes from/to disk can be marked thus for a fraction of a second- I/O following a page fault marks a process
UNINTERRUPTIBLE
- I/O to the same disk that is being accessed for page faults can result in a process marked as
UNINTERRUPTIBLE
- Programmers may mark a task as
UNINTERRUPTIBLE
instead of usingINTERRUPTIBLE
TASK_STOPPED
- Process execution has been stopped; the process enters this state after receiving aSIGSTOP
,SIGTSTP
,SIGTTIN
, orSIGTTOU
signal.TASK_TRACED
- Process execution has been stopped by a debugger.EXIT_ZOMBIE
- Process execution is terminated, but the parent process has not yet issued await4()
orwaitpid()
system call. The OS will not clear zombie processes until the parent issues await()
-like call.EXIT_DEAD
- The final state: the process is being removed by the system because the parent process has just issued await4()
orwaitpid()
system call for it. Changing its state fromEXIT_ZOMBIE
toEXIT_DEAD
avoids race conditions due to other threads of execution that executewait()
-like calls on the same process.
Edit: And yet the Dalvik VM Debug Monitor provides different states. From its documentation:
"thread state" must be one of:
1 -
running
(now executing or ready to do so)
2 -sleeping
(in Thread.sleep())
3 -monitor
(blocked on a monitor lock)
4 -waiting
(in Object.wait())
5 -initializing
6 -starting
7 -native
(executing native code)
8 -vmwait
(waiting on a VM resource)"suspended" [a separate flag in the data structure] will be 0 if the thread is running, 1 if not.
If you design a system app that has to work with threads in even more advanced way than usual app, I'd first start by examining what API is available on Android to access threads.
The answer is pthread = POSIX threads, with pthread.h header file, implemented in Bionic C library. So you have starting point for knowing what you can achieve.
Another thing is that Android doesn't implement full pthread interface, only subset needed for Android to run. More on threads + Bionic here, and how they interact with Java and VM is described here. Also I feel that thread is actually a process, as my code uses setpriority(PRIO_PROCESS, gettid(), pr); to set new thread's priority - I don't recall where I got this info, but this works.
I assume that thread may be in running, finished or blocked (e.g. waiting for mutex) state, but that's my a bit limited knowledge since I never needed other thread state.
Now question is if your app can actually retrieve these states using available API in NDK, and if there're more states, if your users would be really interested to know.
Anyway, you may start by displaying possibly incomplete states of threads, and if your users really care, you'd learn about another states from users' feedback and requests.
Google:
Thread.State BLOCKED The thread is blocked and waiting for a lock.
Thread.State NEW The thread has been created, but has never been started.
Thread.State RUNNABLE The thread may be run.
Thread.State TERMINATED The thread has been terminated.
Thread.State TIMED_WAITING The thread is waiting for a specified amount of time.
Thread.State WAITING The thread is waiting.
These states are not very well explained - I don't see the difference between BLOCKED and WAITING, for example.
Interestingly, there is no 'RUNNING' state - do these devices ever do anything?
精彩评论