yuandaimaahao / AndroidFrameworkTutorial

写给应用开发的 Android Framework 教程
Apache License 2.0
734 stars 190 forks source link

binder驱动中binder_ioctl方法会先获取一个thread,为啥根据binder_thread->pid在红黑树寻找thread?难道同一个进程的不同线程的pid不一样吗? #43

Open zhuzhibin1991 opened 1 year ago

zhuzhibin1991 commented 1 year ago

`static struct binder_thread binder_get_thread_ilocked( struct binder_proc proc, struct binder_thread new_thread) { 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
        return thread;
}
if (!new_thread)
    return NULL;
thread = new_thread;
binder_stats_created(BINDER_STAT_THREAD);
thread->proc = proc;
thread->pid = current->pid;
get_task_struct(current);
thread->task = current;
atomic_set(&thread->tmp_ref, 0);
init_waitqueue_head(&thread->wait);
INIT_LIST_HEAD(&thread->todo);
rb_link_node(&thread->rb_node, parent, p);
rb_insert_color(&thread->rb_node, &proc->threads);
thread->looper_need_return = true;
thread->return_error.work.type = BINDER_WORK_RETURN_ERROR;
thread->return_error.cmd = BR_OK;
thread->reply_error.work.type = BINDER_WORK_RETURN_ERROR;
thread->reply_error.cmd = BR_OK;
spin_lock_init(&thread->prio_lock);
thread->prio_state = BINDER_PRIO_SET;
INIT_LIST_HEAD(&new_thread->waiting_thread_node);
return thread;

}` 以上代码中可以看到赋值语句thread->pid = current->pid;

define get_current() (current_thread_info()->task)

define current get_current()

thread->pid拿到的其实是当前线程对应的 task_struct中的pid,这个pid其实是线程id,不同线程的id是不同的

yuandaimaahao commented 1 year ago

主线程pid 和进程 pid 相同