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