美文网首页
Binder Driver 3: 打开设备

Binder Driver 3: 打开设备

作者: ColdWave | 来源:发表于2018-06-27 18:19 被阅读0次
    static int binder_open(struct inode *nodp, struct file *filp)
    {
        struct binder_proc *proc;
        struct binder_device *binder_dev;
    
        proc = kzalloc(sizeof(*proc), GFP_KERNEL);     // 为 当前进程 创建 binder_proc 进行描述
        spin_lock_init(&proc->inner_lock);
        spin_lock_init(&proc->outer_lock);
        get_task_struct(current->group_leader);        // 获取当前进程的 task_struct 
        proc->tsk = current->group_leader;
        INIT_LIST_HEAD(&proc->todo);
        
        if (binder_supported_policy(current->policy)) {
            proc->default_priority.sched_policy = current->policy;
            proc->default_priority.prio = current->normal_prio;
        } else {
            proc->default_priority.sched_policy = SCHED_NORMAL;
            proc->default_priority.prio = NICE_TO_PRIO(0);
        }
    
        binder_dev = container_of(filp->private_data, struct binder_device,
                      miscdev);
        proc->context = &binder_dev->context;
        binder_alloc_init(&proc->alloc);              // 这是关键,初始化 binder buffer allocator
    
        binder_stats_created(BINDER_STAT_PROC);       // 维护全局统计信息 binder_stats
        proc->pid = current->group_leader->pid;
        INIT_LIST_HEAD(&proc->delivered_death);
        INIT_LIST_HEAD(&proc->waiting_threads);
        filp->private_data = proc;                    // Driver 的惯用伎俩
    
        mutex_lock(&binder_procs_lock);                   // 全局锁
        hlist_add_head(&proc->proc_node, &binder_procs);  // 将 当前 binder_proc 加入到 全局的 binder_procs 中
        mutex_unlock(&binder_procs_lock);
    
        return 0;
    }
    
    // proc->alloc 初始化
    void binder_alloc_init(struct binder_alloc *alloc)
    {
        alloc->tsk = current->group_leader;
        alloc->pid = current->group_leader->pid;
        mutex_init(&alloc->mutex);
        INIT_LIST_HEAD(&alloc->buffers);
    }
    

    相关文章

      网友评论

          本文标题:Binder Driver 3: 打开设备

          本文链接:https://www.haomeiwen.com/subject/dssiyftx.html