Linux Wait Queue and Completion Summary

Completion
==========

struct completion {
    unsigned int done;
    wait_queue_head_t wait;
};


/* Initializer */

#define COMPLETION_INITIALIZER(work) \
    { 0, __WAIT_QUEUE_HEAD_INITIALIZER((work).wait) }

#define COMPLETION_INITIALIZER_ONSTACK(work) \
    ({ init_completion(&work); work; })

static inline void init_completion(struct completion *x)

/* Re-init */

#define INIT_COMPLETION(x)  ((x).done = 0)

/* Declare and initialize */

#define DECLARE_COMPLETION(work)
#define DECLARE_COMPLETION_ONSTACK(work)

/* TBD - Why the defination of DECLARE_COMPLETION_ONSTACK() is different when
lockdep is enabled or disabled ? */

/* Wait for completion variations */

extern void wait_for_completion(struct completion *);
extern void wait_for_completion_io(struct completion *);
extern int wait_for_completion_interruptible(struct completion *x);
extern int wait_for_completion_killable(struct completion *x);
extern unsigned long wait_for_completion_timeout(struct completion *x,
                           unsigned long timeout);
extern unsigned long wait_for_completion_io_timeout(struct completion *x,
                            unsigned long timeout);
extern long wait_for_completion_interruptible_timeout(
    struct completion *x, unsigned long timeout);
extern long wait_for_completion_killable_timeout(
    struct completion *x, unsigned long timeout);
extern bool try_wait_for_completion(struct completion *x);
extern bool completion_done(struct completion *x);

/* Complete variations */

extern void complete(struct completion *);
extern void complete_all(struct completion *);



Wait Queue
==========

There are two types of "wait", exclusive and non-exclusive. It impacts the behavior of wake_up(), wake_up_nr() and similar APIs.
For example, wake_up() wakes up at most one exclusive task, wake_up_nr() wakes up at most @nr tasks.

How to use wait queue?
---------------------------
The task to be waiting shall:
1. Initialize one wait structure.
2. Call the appropriate wait_* API  or Call add_wait_queue_* API and schedule() or Call prepare_to_wait() and finish_wait()

The task wakes up others shall call:
1. The appropriate wake_up_* API.


Structure
-----------

struct __wait_queue {
    unsigned int flags;
#define WQ_FLAG_EXCLUSIVE   0x01
    void *private;               /* points to task_struct waiting, generally */
    wait_queue_func_t func;     /* called during wake up process, usually default_wakeup_function() or  autoremove_wake_function() */
    struct list_head task_list;     /* link to other wait_queue_t structure */
};

typedef struct __wait_queue wait_queue_t;

struct __wait_queue_head {
    spinlock_t lock;
    struct list_head task_list;
};

typedef struct __wait_queue_head wait_queue_head_t;


Wait queue head initialize
-----------------------------
#define __WAIT_QUEUE_HEAD_INITIALIZER(name)
#define __WAIT_QUEUE_HEAD_INIT_ONSTACK(name)
#define init_waitqueue_head(q)

#define DECLARE_WAIT_QUEUE_HEAD(name)
#define DECLARE_WAIT_QUEUE_HEAD_ONSTACK(name)


API Group 1 - waitqueue is NOT auto removed from waitqueue_head at wakeup time
----------------------------------------------------------------

/* Initializer - wait_queue_t.func is initialized to default_wakeup_function() */

#define __WAITQUEUE_INITIALIZER(name, tsk)
static inline void init_waitqueue_entry(wait_queue_t *q, struct task_struct *p)

#define DECLARE_WAITQUEUE(name, tsk)

/* Add/remove wait_queue_t to wait_queue_head_t */

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


API Group 2 - Waitqueues which are removed from the waitqueue_head at wakeup time
----------------------------------------------------------------

/* Initializer -  wait_queue_t.func is initialized to autoremove_wake_function()

#define DEFINE_WAIT_FUNC(name, function)
#define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function)
#define init_wait(wait)

void prepare_to_wait(wait_queue_head_t *q, wait_queue_t *wait, int state);
void prepare_to_wait_exclusive(wait_queue_head_t *q, wait_queue_t *wait, int state);
void finish_wait(wait_queue_head_t *q, wait_queue_t *wait);


High Level API
-----------------

/*
* Wake up variations
* *_locked - wait queue head lock is held
* *_interruptible - wake up processes sleeping in interruptible state only
* *_all - wait up all processes sleeping
*/

#define wake_up(x)          __wake_up(x, TASK_NORMAL, 1, NULL)
#define wake_up_nr(x, nr)       __wake_up(x, TASK_NORMAL, nr, NULL)
#define wake_up_all(x)          __wake_up(x, TASK_NORMAL, 0, NULL)
#define wake_up_locked(x)       __wake_up_locked((x), TASK_NORMAL, 1)
#define wake_up_all_locked(x)       __wake_up_locked((x), TASK_NORMAL, 0)

#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_interruptible_sync(x)   __wake_up_sync((x), TASK_INTERRUPTIBLE, 1)

/* Wait variations
 * Implemented by APIs like prepare_to_wait()/prepare_to_wait_exclusive()/finish_wait().
 */

#define wait_event(wq, condition)          /* uninterruptable */
#define wait_event_timeout(wq, condition, timeout)
#define wait_event_interruptible(wq, condition)
#define wait_event_interruptible_timeout(wq, condition, timeout)
#define wait_event_hrtimeout(wq, condition, timeout)
#define wait_event_interruptible_hrtimeout(wq, condition, timeout)

#define wait_event_interruptible_exclusive(wq, condition)
#define wait_event_interruptible_locked(wq, condition)
#define wait_event_interruptible_locked_irq(wq, condition)
#define wait_event_interruptible_exclusive_locked(wq, condition)
#define wait_event_interruptible_exclusive_locked_irq(wq, condition)

#define wait_event_killable(wq, condition)

#define wait_event_lock_irq_cmd(wq, condition, lock, cmd)
#define wait_event_lock_irq(wq, condition, lock)
#define wait_event_interruptible_lock_irq_cmd(wq, condition, lock, cmd)
#define wait_event_interruptible_lock_irq(wq, condition, lock)





### 回答1: Linux waitqueue 是一种 Linux 内核中的同步机制,它用于控制进程的执行顺序,使进程之间能够协调进行。 它通过让一个进程在等待另一个进程完成某个操作时进入睡眠状态,并在另一个进程完成操作后唤醒等待的进程。这样,它可以避免进程在不同步的情况下同时进行某些操作,从而减少系统资源的浪费。 因此,waitqueue 机制是 Linux 内核中常用的一种机制,它可以保证系统的正确性和高效性。 ### 回答2: 在Linux操作系统中,waitqueue是一种用于进程或线程等待的机制。 当一个进程或线程需要等待某个条件满足时,它可以使用waitqueue机制来挂起自己的执行。等待队列(waitqueue)是一个数据结构,用于维护等待某个事件发生的进程或线程的列表。 当一个条件被满足时,比如某个共享资源变为可用,就会唤醒等待该条件的进程或线程。唤醒的过程是通过使用wake_up函数来实现的。 当一个进程或线程需要等待条件满足时,它会调用wait_event函数,将自己加入到等待队列中,并将自己标记为等待状态。之后,该进程或线程就会进入睡眠状态,并且由调度器决定运行其他进程或线程。 当条件满足时,比如共享资源变为可用,唤醒该条件的进程或线程的时候,会调用wake_up函数来唤醒等待的进程或线程。被唤醒的进程或线程会从wait_event的调用处继续执行,并继续执行后续逻辑。 需要注意的是,使用waitqueue机制需要配合锁机制使用,以避免竞态条件的产生。在加入等待队列和唤醒过程中,需要对共享资源进行加锁保护,以防止并发访问导致的数据不一致性。 总之,waitqueueLinux中一种用于进程或线程等待的机制,它通过等待队列来管理等待某个条件满足的进程或线程,并通过唤醒函数来唤醒等待的进程或线程。它是实现同步和互斥的重要工具之一,能够实现进程或线程之间的协作与同步。 ### 回答3: Linux中的waitqueue(等待队列)是一种用于进程调度的机制。它允许一个或多个进程阻塞并等待某个特定条件的满足。 waitqueue是一个数据结构,类似于一个队列,用于存储等待某个条件满足的进程。当一个进程等待某个条件时,它会将自己添加到waitqueue中,并进入睡眠状态。 在Linux内核中,waitqueue通常与锁(如spinlock或mutex)结合使用。当一个进程需要等待某个条件时,它需要先获取锁,在锁的保护下将自己添加到waitqueue中,然后释放锁并进入睡眠状态。当条件满足时,另一个进程会获取相同的锁,唤醒等待在waitqueue中的进程。 waitqueue的实现依赖于内核调度器。当一个进程被唤醒时,它会从睡眠状态返回到可运行状态,并进入内核调度器的调度队列,等待分配CPU执行。 waitqueue提供了一种线程同步的机制,使得进程可以等待某个条件满足而不需要忙等待。它在很多Linux内核中的子系统中广泛使用,如设备驱动、文件系统等。 总结来说,waitqueueLinux内核中用于进程调度的一种机制,它允许一个或多个进程等待某个条件的满足。它借助锁和睡眠状态实现进程的阻塞和唤醒,依赖于内核调度器进行进程的调度。waitqueue在提供进程同步、避免忙等待等方面发挥了重要作用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值