Linux pi futex锁优先级继承实现原理

1.数据结构
struct task_struct {
    ...
#ifdef CONFIG_RT_MUTEXES
    /* PI waiters blocked on a rt_mutex held by this task: */
    struct rb_root_cached       pi_waiters;
    /* Updated under owner's pi_lock and rq lock */
    struct task_struct      *pi_top_task;
    /* Deadlock detection and priority inheritance handling: */
    struct rt_mutex_waiter      *pi_blocked_on;                                                                                                            
#endif

    struct list_head        pi_state_list;
    struct futex_pi_state       *pi_state_cache;
    ...
}

struct rt_mutex {
    struct rt_mutex_base    rtmutex;
#ifdef CONFIG_DEBUG_LOCK_ALLOC
    struct lockdep_map  dep_map;
#endif
};


struct rt_mutex_base {                                                         
    raw_spinlock_t      wait_lock;
    struct rb_root_cached   waiters;
    struct task_struct  *owner;
};


/*
 * Priority Inheritance state:
 */
struct futex_pi_state {                                                                                                                                    
    /*
     * list of 'owned' pi_state instances - these have to be
     * cleaned up in do_exit() if the task exits prematurely:
     */
    struct list_head list;

    /*
     * The PI object:
     */
    struct rt_mutex_base pi_mutex;

    struct task_struct *owner;
    refcount_t refcount;

    union futex_key key;
};

struct rt_mutex_waiter {
    struct rb_node      tree_entry;
    struct rb_node      pi_tree_entry;
    struct task_struct  *task;
    struct rt_mutex_base    *lock;
    unsigned int        wake_state;
    int         prio;
    u64         deadline;
    struct ww_acquire_ctx   *ww_ctx;
};

2.futex_lock_pi函数
futex_lock_pi:
    --->futex_lock_pi_atomic
        --->attach_to_pi_state
        --->attach_to_pi_owner (futex/core.c)
            --->__attach_to_pi_owner(struct task_struct *p, union futex_key *key,
                struct futex_pi_state **ps) (futex/core.c)
                {
                    struct futex_pi_state *pi_state = alloc_pi_state();

                    //pi_mutex->owner = p;rt_mutex中的owner代表持锁线程的task_struct
                    rt_mutex_init_proxy_locked(&pi_state->pi_mutex, p);
                    list_add(&pi_state->list, &p->pi_state_list);
                    pi_state->owner = p;
                    *ps = pi_state;
                }
    --->__queue_me
    --->rt_mutex_init_waiter
    --->__rt_mutex_start_proxy_lock(struct rt_mutex_base *lock,
        struct rt_mutex_waiter *waiter,struct task_struct *task) //locking/rtmux_api.c
        --->task_blocks_on_rt_mutex(lock, waiter, task); //locking/rmutex.c
        {
            //参数task代表的是马上被block的线程,传入的是current
            waiter->task = task;
            waiter->lock = lock;
            rt_mutex_enqueue(lock, waiter);
            task->pi_blocked_on = waiter;
            //owner:持锁者,task=当前blocked的任务
            rt_mutex_adjust_prio_chain(owner,...,waiter, task);
        }

    --->rt_mutex_wait_proxy_lock

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值