在Linux中检查结构互斥的所有者字段

我想检入一个struct mutex,该{_1}已锁定在该互斥锁的所有者的内核中。

struct mutex {
    atomic_long_t       owner;
    spinlock_t      wait_lock;
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
    struct optimistic_spin_queue osq; /* Spinner MCS lock */
#endif
    struct list_head    wait_list;
#ifdef CONFIG_DEBUG_MUTEXES
    void            *magic;
#endif
#ifdef CONFIG_DEBUG_LOCK_ALLOC
    struct lockdep_map  dep_map;
#endif
};

我从here获悉:

  

字段所有者实际上包含struct task_struct *到当前锁   所有者,因此如果当前不拥有,则为NULL。

有没有安全的方法将该字段与current进行比较?

sun7777777 回答:在Linux中检查结构互斥的所有者字段

  

有什么方法可以将该字段与当前字段进行比较?

kernel/locking/mutex.c中,您可以使用此功能:

/*
 * Internal helper function; C doesn't allow us to hide it :/
 *
 * DO NOT USE (outside of mutex code).
 */
static inline struct task_struct *__mutex_owner(struct mutex *lock)
{
    return (struct task_struct *)(atomic_long_read(&lock->owner) & ~MUTEX_FLAGS);
}

那我想:

extern struct mutex *some_mutex;
extern struct task_struct *current;
__mutex_owner(some_mutex) == current;
,

如果要实现递归互斥锁,则可以使用mutex_trylock_recursive函数:其行为类似于mutex_trylock,但是互斥锁的当前所有者可以调用此函数。在这种情况下,它将返回一个特殊值。


__mutex_owner一样,功能mutex_trylock_recursive被标记为已弃用/“请勿使用”。可能有人不喜欢有关内核中的递归互斥锁的想法。

本文链接:https://www.f2er.com/2959663.html

大家都在问