免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 4387 | 回复: 5
打印 上一主题 下一主题

单核arm中spin_lock没有被实现,请问这时用什么机制在中断中做同步? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-07-06 16:45 |只看该作者 |倒序浏览
下面代码的运行平台是单核arm,使用spin_lock保证对reg_atomic_1和reg_atomic_2的原子访问,但是查阅一些资料后发现在单核arm平台上spin_lock没有实现!!那请问下面的代码要怎么在中断中做同步?谢谢。

附代码:

int reg_atomic_1 = 0;
int reg_atomic_2 = 0;

void test()
{
    unsigned long iflags;
    spin_lock_irqsave(lock, iflags);

    reg_atomic_1 = 1;
    reg_atomic_2 = 1;

    spin_unlock_irqrestore(lock, iflags);
}

static irqreturn_t irq_func(int irq, void *dev_id)
{
    unsigned long iflags;
    spin_lock_irqsave(lock, iflags);

    reg_atomic_1 = 2;
    reg_atomic_2 = 2;

    spin_unlock_irqrestore(lock, iflags);
}

论坛徽章:
0
2 [报告]
发表于 2010-07-06 17:00 |只看该作者
回复 1# tassard


    不需要修改吧,见这个帖子的讨论:http://linux.chinaunix.net/bbs/thread-1166513-1-1.html

论坛徽章:
0
3 [报告]
发表于 2010-07-06 17:04 |只看该作者
回复 1# tassard


    spin_lock不管在什么平台上都没有实现,因为,没有这个需求(既然都单核了,就不会出现非中断的执行序并行执行了).
但是,spin_lock未实现并不意味着spin_lock_irqsave是个空操作,它至少还有关中断\保护现场的操作,而spin_lock_irqrestore也有恢复现场\开中断的操作.所以,执行序和中断历程之间还是有保护的,赫赫

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
4 [报告]
发表于 2010-07-06 21:52 |只看该作者
在up下,spin_lock_irqsave的功能退化为“关闭本地cpu的中断”,下面这个网址对你的问题应该有所帮助
http://blog.chinaunix.net/u2/73067/showart_2046163.html

论坛徽章:
0
5 [报告]
发表于 2010-07-07 15:58 |只看该作者
谢谢各位,关于spin_lock_irq我明白了,但是还有个问题。
我查了下源代码,发现spin_lock_irq是会关闭中断的,但是spin_lock函数就不会,那既然这样的话,为什么kernel的handle_edge_irq会使用spin_lock函数而不是spin_lock_irq?这样会不会破坏共享数据?谢谢。

void
handle_edge_irq(unsigned int irq, struct irq_desc *desc)
{
        spin_lock(&desc->lock);

        desc->status &= ~(IRQ_REPLAY | IRQ_WAITING);

        /*
         * If we're currently running this IRQ, or its disabled,
         * we shouldn't process the IRQ. Mark it pending, handle
         * the necessary masking and go out
         */
        if (unlikely((desc->status & (IRQ_INPROGRESS | IRQ_DISABLED)) ||
                    !desc->action)) {
                desc->status |= (IRQ_PENDING | IRQ_MASKED);
                mask_ack_irq(desc, irq);
                goto out_unlock;
        }
        kstat_incr_irqs_this_cpu(irq, desc);

        /* Start handling the irq */
        if (desc->chip->ack)
                desc->chip->ack(irq);

        /* Mark the IRQ currently in progress.*/
        desc->status |= IRQ_INPROGRESS;

        do {
                struct irqaction *action = desc->action;
                irqreturn_t action_ret;

                if (unlikely(!action)) {
                        desc->chip->mask(irq);
                        goto out_unlock;
                }

                /*
                 * When another irq arrived while we were handling
                 * one, we could have masked the irq.
                 * Renable it, if it was not disabled in meantime.
                 */
                if (unlikely((desc->status &
                               (IRQ_PENDING | IRQ_MASKED | IRQ_DISABLED)) ==
                              (IRQ_PENDING | IRQ_MASKED))) {
                        desc->chip->unmask(irq);
                        desc->status &= ~IRQ_MASKED;
                }

                desc->status &= ~IRQ_PENDING;
                spin_unlock(&desc->lock);
                action_ret = handle_IRQ_event(irq, action);
                if (!noirqdebug)
                        note_interrupt(irq, desc, action_ret);
                spin_lock(&desc->lock);

        } while ((desc->status & (IRQ_PENDING | IRQ_DISABLED)) == IRQ_PENDING);

        desc->status &= ~IRQ_INPROGRESS;
out_unlock:
        spin_unlock(&desc->lock);
}

论坛徽章:
0
6 [报告]
发表于 2010-07-08 04:23 |只看该作者
spin_lock 和spin_unlock使用的前提是中断处理程序和锁之间没有交互
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP