首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么在tasklet_action()函数中调用BUG?

为什么在tasklet_action()函数中调用BUG?
EN

Stack Overflow用户
提问于 2021-04-25 02:51:06
回答 1查看 589关注 0票数 5
代码语言:javascript
复制
static void tasklet_action(struct softirq_action *a)
{
    // ...

    while (list) {
        struct tasklet_struct *t = list;

        list = list->next;

        if (tasklet_trylock(t)) {
            if (!atomic_read(&t->count)) {
                if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                            &t->state))
                    BUG();

                // ...
    }
}

我所理解的是,如果一个微线程已经被调度,那么这段代码会抛出一个BUG()。这是否意味着同一个微线程既不能同时运行,也不能被调度?

EN

回答 1

Stack Overflow用户

发布于 2021-04-25 22:44:47

这只是对微线程的一个有保证的属性进行的健全性检查。您可以查看a comment in include/linux/interrupt.h中列出的微线程的属性

代码语言:javascript
复制
   Properties:
   * If tasklet_schedule() is called, then tasklet is guaranteed
     to be executed on some cpu at least once after this.
   * If the tasklet is already scheduled, but its execution is still not
     started, it will be executed only once.
   * If this tasklet is already running on another CPU (or schedule is called
     from tasklet itself), it is rescheduled for later.
   * Tasklet is strictly serialized wrt itself, but not
     wrt another tasklets. If client needs some intertask synchronization,
     he makes it with spinlocks.

根据定义,微线程保证在调度后至少运行一次。这段代码:

代码语言:javascript
复制
    if (!atomic_read(&t->count)) {
        if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                    &t->state))
            BUG();

确保此属性有效,否则会出现错误,并使用BUG()停止执行并导致运行时死机。

以下是上述代码的注释版本,以使其更清晰:

代码语言:javascript
复制
    // If the tasklet never ran (t->count == 0)
    if (!atomic_read(&t->count)) {
        // And the tasklet is not scheduled for running (bit TASKLET_STATE_SCHED of t->state is 0)
        if (!test_and_clear_bit(TASKLET_STATE_SCHED,
                    &t->state))
            // There's something wrong, this should never happen!
            BUG();

换句话说,你不能在t->count == 0t->state & (1<<TASKLET_STATE_SCHED) == 0中使用微线程。如果发生这种情况,则存在错误。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67246357

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档