I'm learning binder driver. It has a struct binder_thread
for saving thread info. I find that it uses binder_thread.pid
that is assigned with current->pid
to distinguish each others. for example, in binder_get_thread
method, that field is used to judge whether current thread has been add to the tree.
static struct binder_thread *binder_get_thread(struct binder_proc *proc)
{
struct binder_thread *thread = NULL;
struct rb_node *parent = NULL;
struct rb_node **p = &proc->threads.rb_node;
while (*p) {
parent = *p;
thread = rb_entry(parent, struct binder_thread, rb_node);
if (current->pid < thread->pid)
p = &(*p)->rb_left;
else if (current->pid > thread->pid)
p = &(*p)->rb_right;
else
break;
}
.....
}
But as I known, current->pid
is the current process id, how can it be used for distinguishing threads?
It is a bit of confusing terminology, but at the kernel level each thread is assigned its own pid
value which uniquely identifies it. When a process is started, its first thread is given a unique pid
and the thread group ID is set to be the same pid
. When new threads are created, they are given unique pid
values but the same thread group ID so they have linkage back to their parent. So this rb tree search code you are looking at will work because current->pid
is the currently executing thread ID and it is comparing against the thread->pid
values for the thread entries in the tree.
The answer from paxdiablo in this SO question explains the thread / process ID in some more detail, if you are interested: If threads share the same PID, how can they be identified?