好记性不如铅笔头

kernel, linux, 操作系统

Linux waitqueue简单笔记

作者最近还在研究waitqueue,本文可能还有点问题,没搞全。这里先简单的笔记下吧,占个坑~

CONTENTS

wait.h

#ifndef _LINUX_WAIT_H
#define _LINUX_WAIT_H

#define WNOHANG        0x00000001
#define WUNTRACED    0x00000002
#define WSTOPPED    WUNTRACED
#define WEXITED        0x00000004
#define WCONTINUED    0x00000008
#define WNOWAIT        0x01000000    /* Don't reap, just poll status.  */

#define __WNOTHREAD    0x20000000    /* Don't wait on children of other threads in this group */
#define __WALL        0x40000000    /* Wait on all children, regardless of type */
#define __WCLONE    0x80000000    /* Wait only on non-SIGCHLD children */

/* First argument to waitid: */
#define P_ALL        0
#define P_PID        1
#define P_PGID        2

#ifdef __KERNEL__

#include <linux/config.h>
#include <linux/list.h>
#include <linux/stddef.h>
#include <linux/spinlock.h>
#include <asm/system.h>
#include <asm/current.h>

typedef struct __wait_queue wait_queue_t;
typedef int (*wait_queue_func_t)(wait_queue_t *wait, unsigned mode, int sync, void *key);
/*默认的唤醒回调函数*/
int default_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);

/* 等待队列中的单个元素 */
struct __wait_queue {
    unsigned int flags;
#define WQ_FLAG_EXCLUSIVE    0x01
    struct task_struct * task; /* 等待的任务 */
    wait_queue_func_t func; /* 唤醒后的回调函数 */
    struct list_head task_list;/* 元素在链表中 */
};

/* 等待某一位时使用的等待子元素 */
struct wait_bit_key {
    void *flags; /*该位所在的地址*/
    int bit_nr;   /* 该位的偏移量 */
};

/* 等待队列中的单个元素 */
struct wait_bit_queue {
    struct wait_bit_key key;
    wait_queue_t wait;
};

/* 等待队列头 */
struct __wait_queue_head {
    spinlock_t lock; /* 等待队列是一个链表,该锁用来锁定链表操作 */
    struct list_head task_list; /* 链表头 */
};
typedef struct __wait_queue_head wait_queue_head_t;


/*
 * Macros for declaration and initialisaton of the datatypes
 */
/* 初始化一个等待队列元素 */
#define __WAITQUEUE_INITIALIZER(name, tsk) {                \
    .task        = tsk,                        \
    .func        = default_wake_function,            \ /* 默认唤醒回调函数 */
    .task_list    = { NULL, NULL } }  /* 元素不在任何一个链表中 */

#define DECLARE_WAITQUEUE(name, tsk)                    \
    wait_queue_t name = __WAITQUEUE_INITIALIZER(name, tsk)

/* 使用一个等待队列元素初始化一个等待队列,其实是初始化一个链表头 */
#define __WAIT_QUEUE_HEAD_INITIALIZER(name) {                \
    .lock        = SPIN_LOCK_UNLOCKED,                \
    .task_list    = { &(name).task_list, &(name).task_list } }

#define DECLARE_WAIT_QUEUE_HEAD(name) \
    wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)

#define __WAIT_BIT_KEY_INITIALIZER(word, bit)                \
    { .flags = word, .bit_nr = bit, }

/* 初始化一个等待队列 */
static inline void init_waitqueue_head(wait_queue_head_t *q)
{
    q->lock = SPIN_LOCK_UNLOCKED;
    INIT_LIST_HEAD(&q->task_list);
}

/* 初始化一个等待队列元素指针 */
static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)
{
    q->flags = 0;
    q->task = p;
    q->func = default_wake_function;/* 使用默认回调函数 */
}

/* 使用自定义回调函数初始化一个等待队列元素 */
static inline void init_waitqueue_func_entry(wait_queue_t *q,
                    wait_queue_func_t func)
{
    q->flags = 0;
    q->task = NULL;
    q->func = func;
}

/* 判断一个等待队列是否有效,通过判断里面是否还有等待元素 */
static inline int waitqueue_active(wait_queue_head_t *q)
{
    return !list_empty(&q->task_list);
}

/*
 * Used to distinguish between sync and async io wait context:
 * sync i/o typically specifies a NULL wait queue entry or a wait
 * queue entry bound to a task (current task) to wake up.
 * aio specifies a wait queue entry with an async notification
 * callback routine, not associated with any task.
 */
#define is_sync_wait(wait)    (!(wait) || ((wait)->task))

extern void FASTCALL(add_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));
extern void FASTCALL(add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t * wait));
extern void FASTCALL(remove_wait_queue(wait_queue_head_t *q, wait_queue_t * wait));

/* 将一个元素新加入到等待队列中 */
static inline void __add_wait_queue(wait_queue_head_t *head, wait_queue_t *new)
{
    list_add(&new->task_list, &head->task_list);
}

/*
 * Used for wake-one threads:
 */
/* 将一个元素新加入到等待队列中,加入队尾 */
static inline void __add_wait_queue_tail(wait_queue_head_t *head,
                        wait_queue_t *new)
{
    list_add_tail(&new->task_list, &head->task_list);
}

/*将一个等待队列元素从队列中移除*/
static inline void __remove_wait_queue(wait_queue_head_t *head,
                            wait_queue_t *old)
{
    list_del(&old->task_list);
}

void FASTCALL(__wake_up(wait_queue_head_t *q, unsigned int mode, int nr, void *key));
extern void FASTCALL(__wake_up_locked(wait_queue_head_t *q, unsigned int mode));
extern void FASTCALL(__wake_up_sync(wait_queue_head_t *q, unsigned int mode, int nr));
void FASTCALL(__wake_up_bit(wait_queue_head_t *, void *, int));
int FASTCALL(__wait_on_bit(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
int FASTCALL(__wait_on_bit_lock(wait_queue_head_t *, struct wait_bit_queue *, int (*)(void *), unsigned));
void FASTCALL(wake_up_bit(void *, int));
int FASTCALL(out_of_line_wait_on_bit(void *, int, int (*)(void *), unsigned));
int FASTCALL(out_of_line_wait_on_bit_lock(void *, int, int (*)(void *), unsigned));
wait_queue_head_t *FASTCALL(bit_waitqueue(void *, int));

#define wake_up(x)            __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 1, NULL)
#define wake_up_nr(x, nr)        __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, nr, NULL)
#define wake_up_all(x)            __wake_up(x, TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE, 0, NULL)
#define wake_up_interruptible(x)    __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
#define wake_up_interruptible_nr(x, nr)    __wake_up(x, TASK_INTERRUPTIBLE, nr, NULL)
#define wake_up_interruptible_all(x)    __wake_up(x, TASK_INTERRUPTIBLE, 0, NULL)
#define    wake_up_locked(x)        __wake_up_locked((x), TASK_UNINTERRUPTIBLE | TASK_INTERRUPTIBLE)
#define wake_up_interruptible_sync(x)   __wake_up_sync((x),TASK_INTERRUPTIBLE, 1)

/* 等待某一事件响应 */
#define __wait_event(wq, condition)                     \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                            \ /* 这里的实现没有超时 */

        /* 等待时不可被中断打断 */
        prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
        if (condition)                        \ /* 条件满足才会跳出 */
            break;                        \
        schedule();                        \  /* 这里就开始休眠了,如果条件不满足,而且被意外唤醒(??),那么继续循环 */
    }                                \
    finish_wait(&wq, &__wait);                    \  /* 完成等待 */
} while (0)

#define wait_event(wq, condition)                     \
do {                                    \
    if (condition)                             \ /* 在进入等待前先判断下,如果条件满足就不用等了 */
        break;                            \
    __wait_event(wq, condition);                    \
} while (0)

/* 加入超时后的等待事件响应 */
#define __wait_event_timeout(wq, condition, ret)            \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                            \
        prepare_to_wait(&wq, &__wait, TASK_UNINTERRUPTIBLE);    \
        if (condition)                        \
            break;                        \
        ret = schedule_timeout(ret);                \ /* 让出CPU */
        if (!ret)                        \
            break;                        \
    }                                \
    finish_wait(&wq, &__wait);                    \
} while (0)

#define wait_event_timeout(wq, condition, timeout)            \
({                                    \
    long __ret = timeout;                        \
    if (!(condition))                         \
        __wait_event_timeout(wq, condition, __ret);        \
    __ret;                                \
})

/* 可被中断打断的等待事件响应 */
#define __wait_event_interruptible(wq, condition, ret)            \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                            \
        prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);    \
        if (condition)                        \
            break;                        \
        if (!signal_pending(current)) {                \
            schedule();                    \
            continue;                    \
        }                            \
        ret = -ERESTARTSYS;                    \
        break;                            \
    }                                \
    finish_wait(&wq, &__wait);                    \
} while (0)

#define wait_event_interruptible(wq, condition)                \
({                                    \
    int __ret = 0;                            \
    if (!(condition))                        \
        __wait_event_interruptible(wq, condition, __ret);    \
    __ret;                                \
})

 /* 加入超时后的等待事件响应,也可被中断打断 */
#define __wait_event_interruptible_timeout(wq, condition, ret)        \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                            \
        prepare_to_wait(&wq, &__wait, TASK_INTERRUPTIBLE);    \
        if (condition)                        \
            break;                        \
        if (!signal_pending(current)) {                \
            ret = schedule_timeout(ret);            \
            if (!ret)                    \
                break;                    \
            continue;                    \
        }                            \
        ret = -ERESTARTSYS;                    \
        break;                            \
    }                                \
    finish_wait(&wq, &__wait);                    \
} while (0)

#define wait_event_interruptible_timeout(wq, condition, timeout)    \
({                                    \
    long __ret = timeout;                        \
    if (!(condition))                        \
        __wait_event_interruptible_timeout(wq, condition, __ret); \
    __ret;                                \
})

/* 加入互斥唤醒的等待,如果有多个任务在等待,这里就唤醒一个 */
#define __wait_event_interruptible_exclusive(wq, condition, ret)    \
do {                                    \
    DEFINE_WAIT(__wait);                        \
                                    \
    for (;;) {                            \
        prepare_to_wait_exclusive(&wq, &__wait,            \
                    TASK_INTERRUPTIBLE);        \
        if (condition)                        \
            break;                        \
        if (!signal_pending(current)) {                \
            schedule();                    \
            continue;                    \
        }                            \
        ret = -ERESTARTSYS;                    \
        break;                            \
    }                                \
    finish_wait(&wq, &__wait);                    \
} while (0)

#define wait_event_interruptible_exclusive(wq, condition)        \
({                                    \
    int __ret = 0;                            \
    if (!(condition))                        \
        __wait_event_interruptible_exclusive(wq, condition, __ret);\
    __ret;                                \
})

/*
 * Must be called with the spinlock in the wait_queue_head_t held.
 */
/*
将任务加入等待队列,注意这里是互斥加入
*/
static inline void add_wait_queue_exclusive_locked(wait_queue_head_t *q,
                           wait_queue_t * wait)
{
    wait->flags |= WQ_FLAG_EXCLUSIVE;
    __add_wait_queue_tail(q,  wait);
}

/*
 * Must be called with the spinlock in the wait_queue_head_t held.
 */
static inline void remove_wait_queue_locked(wait_queue_head_t *q,
                        wait_queue_t * wait)
{
    __remove_wait_queue(q,  wait);
}

/*
 * These are the old interfaces to sleep waiting for an event.
 * They are racy.  DO NOT use them, use the wait_event* interfaces above.  
 * We plan to remove these interfaces during 2.7.
 */
extern void FASTCALL(sleep_on(wait_queue_head_t *q));
extern long FASTCALL(sleep_on_timeout(wait_queue_head_t *q,
                      signed long timeout));
extern void FASTCALL(interruptible_sleep_on(wait_queue_head_t *q));
extern long FASTCALL(interruptible_sleep_on_timeout(wait_queue_head_t *q,
                            signed long timeout));

/*
 * Waitqueues which are removed from the waitqueue_head at wakeup time
 */
void FASTCALL(prepare_to_wait(wait_queue_head_t *q,
                wait_queue_t *wait, int state));
void FASTCALL(prepare_to_wait_exclusive(wait_queue_head_t *q,
                wait_queue_t *wait, int state));
void FASTCALL(finish_wait(wait_queue_head_t *q, wait_queue_t *wait));
int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key);
int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *key);

/*
 * 定义一个等待队列元素,这里可以看到task为current,函数为默认的自动移除唤醒函数,
 * 唤醒后该元素即被移除出队列。
*/
#define DEFINE_WAIT(name)                        \
    wait_queue_t name = {                        \
        .task        = current,                \
        .func        = autoremove_wake_function,        \
        .task_list    = {    .next = &(name).task_list,    \
                    .prev = &(name).task_list,    \
                },                    \
    }

#define DEFINE_WAIT_BIT(name, word, bit)                \
    struct wait_bit_queue name = {                    \
        .key = __WAIT_BIT_KEY_INITIALIZER(word, bit),        \
        .wait    = {                        \
            .task        = current,            \
            .func        = wake_bit_function,        \
            .task_list    =                \
                LIST_HEAD_INIT((name).wait.task_list),    \
        },                            \
    }

#define init_wait(wait)                            \
    do {                                \
        (wait)->task = current;                    \
        (wait)->func = autoremove_wake_function;        \
        INIT_LIST_HEAD(&(wait)->task_list);            \
    } while (0)

/**
 * wait_on_bit - wait for a bit to be cleared
 * @word: the word being waited on, a kernel virtual address
 * @bit: the bit of the word being waited on
 * @action: the function used to sleep, which may take special actions
 * @mode: the task state to sleep in
 *
 * There is a standard hashed waitqueue table for generic use. This
 * is the part of the hashtable's accessor API that waits on a bit.
 * For instance, if one were to have waiters on a bitflag, one would
 * call wait_on_bit() in threads waiting for the bit to clear.
 * One uses wait_on_bit() where one is waiting for the bit to clear,
 * but has no intention of setting it.
 */
static inline int wait_on_bit(void *word, int bit,
                int (*action)(void *), unsigned mode)
{
    if (!test_bit(bit, word))
        return 0;
    return out_of_line_wait_on_bit(word, bit, action, mode);
}

/**
 * wait_on_bit_lock - wait for a bit to be cleared, when wanting to set it
 * @word: the word being waited on, a kernel virtual address
 * @bit: the bit of the word being waited on
 * @action: the function used to sleep, which may take special actions
 * @mode: the task state to sleep in
 *
 * There is a standard hashed waitqueue table for generic use. This
 * is the part of the hashtable's accessor API that waits on a bit
 * when one intends to set it, for instance, trying to lock bitflags.
 * For instance, if one were to have waiters trying to set bitflag
 * and waiting for it to clear before setting it, one would call
 * wait_on_bit() in threads waiting to be able to set the bit.
 * One uses wait_on_bit_lock() where one is waiting for the bit to
 * clear with the intention of setting it, and when done, clearing it.
 */
static inline int wait_on_bit_lock(void *word, int bit,
                int (*action)(void *), unsigned mode)
{
    if (!test_and_set_bit(bit, word))
        return 0;
    return out_of_line_wait_on_bit_lock(word, bit, action, mode);
}
    
#endif /* __KERNEL__ */

#endif

wait.c

/*
 * Generic waiting primitives.
 *
 * (C) 2004 William Irwin, Oracle
 */
#include <linux/config.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/sched.h>
#include <linux/mm.h>
#include <linux/wait.h>
#include <linux/hash.h>

/* 将一个等待队列元素添加到等待队列中 */
void fastcall add_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
{
    unsigned long flags;

    wait->flags &= ~WQ_FLAG_EXCLUSIVE;
    spin_lock_irqsave(&q->lock, flags);/* 关中断 加锁 */
    __add_wait_queue(q, wait); /* 调用内部实现函数 */
    spin_unlock_irqrestore(&q->lock, flags);
}
EXPORT_SYMBOL(add_wait_queue);

/* 用互斥方式将一个等待队列元素加入到等待队列中 */
void fastcall add_wait_queue_exclusive(wait_queue_head_t *q, wait_queue_t *wait)
{
    unsigned long flags;

    wait->flags |= WQ_FLAG_EXCLUSIVE;
    spin_lock_irqsave(&q->lock, flags);
    __add_wait_queue_tail(q, wait);
    spin_unlock_irqrestore(&q->lock, flags);
}
EXPORT_SYMBOL(add_wait_queue_exclusive);

/* 从队列中移除某一个等待元素 */
void fastcall remove_wait_queue(wait_queue_head_t *q, wait_queue_t *wait)
{
    unsigned long flags;

    spin_lock_irqsave(&q->lock, flags);/* 关中断 加锁 */
    __remove_wait_queue(q, wait);
    spin_unlock_irqrestore(&q->lock, flags);
}
EXPORT_SYMBOL(remove_wait_queue);


/*
 * Note: we use "set_current_state()" _after_ the wait-queue add,
 * because we need a memory barrier there on SMP, so that any
 * wake-function that tests for the wait-queue being active
 * will be guaranteed to see waitqueue addition _or_ subsequent
 * tests in this thread will see the wakeup having taken place.
 *
 * The spin_unlock() itself is semi-permeable and only protects
 * one way (it only protects stuff inside the critical region and
 * stops them from bleeding out - it would still allow subsequent
 * loads to move into the the critical region).
 */

/* 当前进程准备进入等待状态 */
void fastcall
prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state)
{
    unsigned long flags;

    wait->flags &= ~WQ_FLAG_EXCLUSIVE;
    spin_lock_irqsave(&q->lock, flags);
    if (list_empty(&wait->task_list))
        __add_wait_queue(q, wait);
    /*
     * don't alter the task state if this is just going to
     * queue an async wait queue callback
     */
    if (is_sync_wait(wait))
        set_current_state(state);
    spin_unlock_irqrestore(&q->lock, flags);
}
EXPORT_SYMBOL(prepare_to_wait);

void fastcall
prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state)
{
    unsigned long flags;

    wait->flags |= WQ_FLAG_EXCLUSIVE;
    spin_lock_irqsave(&q->lock, flags);
    if (list_empty(&wait->task_list))
        __add_wait_queue_tail(q, wait);
    /*
     * don't alter the task state if this is just going to
      * queue an async wait queue callback
     */
    if (is_sync_wait(wait))
        set_current_state(state);
    spin_unlock_irqrestore(&q->lock, flags);
}
EXPORT_SYMBOL(prepare_to_wait_exclusive);


/* 当前进程结束等待 */
void fastcall finish_wait(wait_queue_head_t *q, wait_queue_t *wait)
{
    unsigned long flags;

    __set_current_state(TASK_RUNNING);
    /*
     * We can check for list emptiness outside the lock
     * IFF:
     *  - we use the "careful" check that verifies both
     *    the next and prev pointers, so that there cannot
     *    be any half-pending updates in progress on other
     *    CPU's that we haven't seen yet (and that might
     *    still change the stack area.
     * and
     *  - all other users take the lock (ie we can only
     *    have _one_ other CPU that looks at or modifies
     *    the list).
     */
    if (!list_empty_careful(&wait->task_list)) {
        spin_lock_irqsave(&q->lock, flags);
        list_del_init(&wait->task_list);
        spin_unlock_irqrestore(&q->lock, flags);
    }
}
EXPORT_SYMBOL(finish_wait);

/* 删除自己的回调函数 */
int autoremove_wake_function(wait_queue_t *wait, unsigned mode, int sync, void *key)
{
    /* 首先调用默认的回调函数 */
    int ret = default_wake_function(wait, mode, sync, key);

    if (ret)
        list_del_init(&wait->task_list);/* 然后将该等待队列元素从队列中删除 */
    return ret;
}
EXPORT_SYMBOL(autoremove_wake_function);

/* 使用bit校验的唤醒回调函数 */
int wake_bit_function(wait_queue_t *wait, unsigned mode, int sync, void *arg)
{
    struct wait_bit_key *key = arg;
    struct wait_bit_queue *wait_bit
        = container_of(wait, struct wait_bit_queue, wait);

        /* 如果指定的位校验不通过,就直接返回  */
    if (wait_bit->key.flags != key->flags ||
            wait_bit->key.bit_nr != key->bit_nr ||
            test_bit(key->bit_nr, key->flags))
        return 0;
    else
        return autoremove_wake_function(wait, mode, sync, key);
}
EXPORT_SYMBOL(wake_bit_function);

/*
 * To allow interruptible waiting and asynchronous (i.e. nonblocking)
 * waiting, the actions of __wait_on_bit() and __wait_on_bit_lock() are
 * permitted return codes. Nonzero return codes halt waiting and return.
 */
/* 等待某一位为真时进行回调函数响应 */
int __sched fastcall
__wait_on_bit(wait_queue_head_t *wq, struct wait_bit_queue *q,
            int (*action)(void *), unsigned mode)
{
    int ret = 0;

    do {
        prepare_to_wait(wq, &q->wait, mode);
        if (test_bit(q->key.bit_nr, q->key.flags))
            ret = (*action)(q->key.flags);
    } while (test_bit(q->key.bit_nr, q->key.flags) && !ret);/* 不停的循环检测 */
    finish_wait(wq, &q->wait);
    return ret;
}
EXPORT_SYMBOL(__wait_on_bit);

int __sched fastcall out_of_line_wait_on_bit(void *word, int bit,
                    int (*action)(void *), unsigned mode)
{
    wait_queue_head_t *wq = bit_waitqueue(word, bit);
    DEFINE_WAIT_BIT(wait, word, bit);

    return __wait_on_bit(wq, &wait, action, mode);
}
EXPORT_SYMBOL(out_of_line_wait_on_bit);

int __sched fastcall
__wait_on_bit_lock(wait_queue_head_t *wq, struct wait_bit_queue *q,
            int (*action)(void *), unsigned mode)
{
    int ret = 0;

    do {
        prepare_to_wait_exclusive(wq, &q->wait, mode);
        if (test_bit(q->key.bit_nr, q->key.flags)) {
            if ((ret = (*action)(q->key.flags)))
                break;
        }
    } while (test_and_set_bit(q->key.bit_nr, q->key.flags));
    finish_wait(wq, &q->wait);
    return ret;
}
EXPORT_SYMBOL(__wait_on_bit_lock);

int __sched fastcall out_of_line_wait_on_bit_lock(void *word, int bit,
                    int (*action)(void *), unsigned mode)
{
    wait_queue_head_t *wq = bit_waitqueue(word, bit);
    DEFINE_WAIT_BIT(wait, word, bit);

    return __wait_on_bit_lock(wq, &wait, action, mode);
}
EXPORT_SYMBOL(out_of_line_wait_on_bit_lock);

void fastcall __wake_up_bit(wait_queue_head_t *wq, void *word, int bit)
{
    struct wait_bit_key key = __WAIT_BIT_KEY_INITIALIZER(word, bit);
    if (waitqueue_active(wq))
        __wake_up(wq, TASK_INTERRUPTIBLE|TASK_UNINTERRUPTIBLE, 1, &key);
}
EXPORT_SYMBOL(__wake_up_bit);

/**
 * wake_up_bit - wake up a waiter on a bit
 * @word: the word being waited on, a kernel virtual address
 * @bit: the bit of the word being waited on
 *
 * There is a standard hashed waitqueue table for generic use. This
 * is the part of the hashtable's accessor API that wakes up waiters
 * on a bit. For instance, if one were to have waiters on a bitflag,
 * one would call wake_up_bit() after clearing the bit.
 *
 * In order for this to function properly, as it uses waitqueue_active()
 * internally, some kind of memory barrier must be done prior to calling
 * this. Typically, this will be smp_mb__after_clear_bit(), but in some
 * cases where bitflags are manipulated non-atomically under a lock, one
 * may need to use a less regular barrier, such fs/inode.c's smp_mb(),
 * because spin_unlock() does not guarantee a memory barrier.
 */
void fastcall wake_up_bit(void *word, int bit)
{
    __wake_up_bit(bit_waitqueue(word, bit), word, bit);
}
EXPORT_SYMBOL(wake_up_bit);

fastcall wait_queue_head_t *bit_waitqueue(void *word, int bit)
{
    const int shift = BITS_PER_LONG == 32 ? 5 : 6;
    const struct zone *zone = page_zone(virt_to_page(word));
    unsigned long val = (unsigned long)word << shift | bit;

    return &zone->wait_table[hash_long(val, zone->wait_table_bits)];
}
EXPORT_SYMBOL(bit_waitqueue);

 

发表评论

20 − 3 =

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据