内核completion工作机制

本文介绍了Linux内核中的Completion机制,包括wait_queue_entry_t结构、wait_queue_head_t的初始化和操作,以及complete()和wait_for_completion()函数的使用。通过示例展示了如何在固件升级场景中使用Completion进行同步。
摘要由CSDN通过智能技术生成

core.c
int default_wake_function(wait_queue_entry_t *curr, unsigned mode, int wake_flags,
void *key)
{
return try_to_wake_up(curr->private, mode, wake_flags, 1); // wake up a thread
}
EXPORT_SYMBOL(default_wake_function);

wait.h
/*

  • A single wait-queue entry structure:
    */
    struct wait_queue_entry {
    unsigned int flags;
    void *private;
    wait_queue_func_t func;
    struct list_head entry;
    };

struct wait_queue_head {
spinlock_t lock;
struct list_head head;
};
typedef struct wait_queue_head wait_queue_head_t;

/*

  • Macros for declaration and initialisaton of the datatypes
    */

#define __WAITQUEUE_INITIALIZER(name, tsk) {
.private = tsk,
.func = default_wake_function,
.entry = { NULL, NULL } }

#define DECLARE_WAITQUEUE(name, tsk)
struct wait_queue_entry name = __WAITQUEUE_INITIALIZER(name, tsk)

#define init_waitqueue_head(wq_head)
do {
static struct lock_class_key __key;

__init_waitqueue_head((wq_head), #wq_head, &__key);
} while (0)

wait.c
void __init_waitqueue_head(struct wait_queue_head *wq_head, const char *name, struct lock_class_key *key)
{
spin_lock_init(&wq_head->lock); //初始化自旋锁
lockdep_set_class_and_name(&wq_head->lock, key, name);
INIT_LIST_HEAD(&wq_head->head); //初始化链表
}

/*

  • The core wakeup function. Non-exclusive wakeups (nr_exclusive == 0) just

  • wake everything up. If it’s an exclusive wakeup (nr_exclusive == small +ve

  • number) then we wake all the non-exclusive tasks and one exclusive task.

  • There are circumstances in which we can try to wake a task which has already

  • started to run but is not in state TASK_RUNNING. try_to_wake_up() returns

  • zero in this (rare) case, and we handle it by continuing to scan the queue.
    */
    static int __wake_up_co

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值