程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)

linux同步机制-completion

一、completion

1.1 什么是completion

linux内核中,完成量completion是一种代码同步机制。如果有一个或多个线程必须等待某个内核活动操作达到某个点或某个特定状态,那么completion完成量可以提供一个无竞争的解决方案。

1.2 completion的使用

1.2.1 定义并初始化完成量
// 方式一
struct completion mycompletion;
init_completion(&mycompletion)
// 方式二
DECLARE_COMPLETION(mycompletion);

以下两种方式是等效的,都实现了定义和初始化完成量mycompletion的功能。

1.2.2 等待完成量
wait_for_completion(&mycompletion);
wait_for_completion_interruptible(&mycompletion);
wait_for_completion_timeout(&mycompletion,timeout);
1.2.3 唤醒完成量
complete(&mycompletion);

二、completion实现源码

2.1 struct completion结构体

struct completion结构体定义位于include/linux/completion.h文件中:

/*
 * struct completion - structure used to maintain state for a "completion"
 *
 * This is the opaque structure used to maintain the state for a "completion".
 * Completions currently use a FIFO to queue threads that have to wait for
 * the "completion" event.
 *
 * See also:  complete(), wait_for_completion() (and friends _timeout,
 * _interruptible, _interruptible_timeout, and _killable), init_completion(),
 * reinit_completion(), and macros DECLARE_COMPLETION(),
 * DECLARE_COMPLETION_ONSTACK().
 */
struct completion {
        unsigned int done;
        wait_queue_head_t wait;
};

wait等待队列头,用来放置需要等到的任务,done用来指示任务是否完成。

可以看到其内部是通过等待队列来实现的。

2.2 init_completion

init_completion用于初始化完成量:

#define init_completion(x) __init_completion(x)

/**
 * init_completion - Initialize a dynamically allocated completion
 * @x:  pointer to completion structure that is to be initialized
 *
 * This inline function will initialize a dynamically created completion
 * structure.
 */
static inline void __init_completion(struct completion *x)
{
        x->done = 0;
    	// 初始化等待队列头
        init_waitqueue_head(&x->wait);  
}

2.3 等待完成量

2.3.1wait_for_completion

wait_for_completion用于等待完成量的释放,定义在kernel/sched/completion.c;

do_wait_for_common(struct completion *x,
                   long (*action)(long), long timeout, int state)
{
        if (!x->done) {
                DECLARE_WAITQUEUE(wait, current);

                __add_wait_queue_entry_tail_exclusive(&x->wait, &wait);
                do {
                        if (signal_pending_state(state, current)) {
                                timeout = -ERESTARTSYS;
                                break;
                        }
                        __set_current_state(state);
                        spin_unlock_irq(&x->wait.lock);
                        timeout = action(timeout);
                        spin_lock_irq(&x->wait.lock);
                } while (!x->done && timeout);
                __remove_wait_queue(&x->wait, &wait);
                if (!x->done)
                        return timeout;
        }
        if (x->done != UINT_MAX)
                x->done--;
        return timeout ?: 1;
}


/**
 * wait_for_completion: - waits for completion of a task
 * @x:  holds the state of this particular completion
 *
 * This waits to be signaled for completion of a specific task. It is NOT
 * interruptible and there is no timeout.
 *
 * See also similar routines (i.e. wait_for_completion_timeout()) with timeout
 * and interrupt capability. Also see complete().
 */
void __sched wait_for_completion(struct completion *x)
{
        wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_UNINTERRUPTIBLE);
}

wait_for_completion函数的主要作用是等待某个特定任务的完成。当调用该函数时,如果任务的完成状态没有被设置为完成,当前线程将被阻塞,直到任务的完成状态被设置为完成。

需要注意的是,wait_for_completion函数是不可中断的(non-interruptible),这意味着无论是否收到中断信号,当前线程都会一直等待任务的完成。此外,该函数也没有超时功能,即使等待时间很长,也不会自动超时返回。

2.3.2 wait_for_completion_interruptible

wait_for_completion_interruptible同样用于等待完成量的释放,只不过该函数是可以中断的,定义在kernel/sched/completion.c;

/**
 * wait_for_completion_interruptible: - waits for completion of a task (w/intr)
 * @x:  holds the state of this particular completion
 *
 * This waits for completion of a specific task to be signaled. It is
 * interruptible.
 *
 * Return: -ERESTARTSYS if interrupted, 0 if completed.
 */
int __sched wait_for_completion_interruptible(struct completion *x)
{
        long t = wait_for_common(x, MAX_SCHEDULE_TIMEOUT, TASK_INTERRUPTIBLE);
        if (t == -ERESTARTSYS)
                return t;
        return 0;
}

2.4 complete

complete函数用于唤醒等待此特定complete事件的单个线程,定义在kernel/sched/completion.c;

/**
 * complete: - signals a single thread waiting on this completion
 * @x:  holds the state of this particular completion
 *
 * This will wake up a single thread waiting on this completion. Threads will be
 * awakened in the same order in which they were queued.
 *
 * See also complete_all(), wait_for_completion() and related routines.
 *
 * If this function wakes up a task, it executes a full memory barrier before
 * accessing the task state.
 */
void complete(struct completion *x)
{
        unsigned long flags;

        spin_lock_irqsave(&x->wait.lock, flags);

        if (x->done != UINT_MAX)
                x->done++;
        __wake_up_locked(&x->wait, TASK_NORMAL, 1);
        spin_unlock_irqrestore(&x->wait.lock, flags);
}

当调用该函数时,如果有线程正在等待该完成状态,它将被唤醒并继续执行。

需要注意的是,complete函数会按照线程入队的顺序依次唤醒等待的线程。

2.5 completion_done

completion_done函数用于检测某个complete是否有正在等待的线程,定义在kernel/sched/completion.c;

/**
 *      completion_done - Test to see if a completion has any waiters
 *      @x:     completion structure
 *
 *      Return: 0 if there are waiters (wait_for_completion() in progress)
 *               1 if there are no waiters.
 *
 *      Note, this will always return true if complete_all() was called on @X.
 */
bool completion_done(struct completion *x)
{
        unsigned long flags;

        if (!READ_ONCE(x->done))
                return false;

        /*
         * If ->done, we need to wait for complete() to release ->wait.lock
         * otherwise we can end up freeing the completion before complete()
         * is done referencing it.
         */
        spin_lock_irqsave(&x->wait.lock, flags);
        spin_unlock_irqrestore(&x->wait.lock, flags);
        return true;
}

当调用该函数时,如果有线程正在等待该完成状态,则返回0;如果没有线程在等待,则返回1。

需要注意的是,如果之前调用了complete_all函数来完成所有等待的线程,则无论如何都会返回1,表示没有等待的线程。

亲爱的读者和支持者们,自动博客加入了打赏功能,陆陆续续收到了各位老铁的打赏。在此,我想由衷地感谢每一位对我们博客的支持和打赏。你们的慷慨与支持,是我们前行的动力与源泉。

日期姓名金额
2023-09-06*源19
2023-09-11*朝科88
2023-09-21*号5
2023-09-16*真60
2023-10-26*通9.9
2023-11-04*慎0.66
2023-11-24*恩0.01
2023-12-30I*B1
2024-01-28*兴20
2024-02-01QYing20
2024-02-11*督6
2024-02-18一*x1
2024-02-20c*l18.88
2024-01-01*I5
2024-04-08*程150
2024-04-18*超20
2024-04-26.*V30
2024-05-08D*W5
2024-05-29*辉20
2024-05-30*雄10
2024-06-08*:10
2024-06-23小狮子666
2024-06-28*s6.66
2024-06-29*炼1
2024-06-30*!1
2024-07-08*方20
2024-07-18A*16.66
2024-07-31*北12
2024-08-13*基1
2024-08-23n*s2
2024-09-02*源50
2024-09-04*J2
2024-09-06*强8.8
2024-09-09*波1
2024-09-10*口1
2024-09-10*波1
2024-09-12*波10
2024-09-18*明1.68
2024-09-26B*h10
2024-09-3010
2024-10-02M*i1
2024-10-14*朋10
2024-10-22*海10
2024-10-23*南10
2024-10-26*节6.66
2024-10-27*o5
2024-10-28W*F6.66
2024-10-29R*n6.66
2024-11-02*球6
2024-11-021*鑫6.66
2024-11-25*沙5
2024-11-29C*n2.88
posted @   大奥特曼打小怪兽  阅读(1333)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
如果有任何技术小问题,欢迎大家交流沟通,共同进步

公告 & 打赏

>>

欢迎打赏支持我 ^_^

最新公告

程序项目代做,有需求私信(小程序、网站、爬虫、电路板设计、驱动、应用程序开发、毕设疑难问题处理等)。

了解更多

点击右上角即可分享
微信分享提示