在C语言中,中断可以在哪个地方中断功能?

在C语言中,中断可以在哪个地方中断功能?,c,linux-kernel,interrupt-handling,spinlock,C,Linux Kernel,Interrupt Handling,Spinlock,我正在用ISOC90编写代码,它禁止混合声明和代码。 所以我有这样的想法: int function(int something) { int foo, ret; int bar = function_to_get_bar(); some_typedef_t foobar = get_this_guy(something); spin_lock_irqsave(lock); /* Here is code that does a lot of

我正在用ISOC90编写代码,它禁止混合声明和代码。 所以我有这样的想法:

int function(int something)
{
    int foo, ret;
    int bar = function_to_get_bar();
    some_typedef_t foobar = get_this_guy(something);

    spin_lock_irqsave(lock);

    /*
    Here is code that does a lot of things
    */

    spin_unlock_irqrestore(lock);

    return ret;
}
问题是硬件或软件中断是否发生,在哪个地方它可以中断我的功能,是否也能发生在变量声明的中间?


我之所以问这个问题,是因为我需要这个函数不被中断中断。我想使用spin_lock_irqsave()来确保这一点,但我想知道中断是否可以在变量声明中中断我的函数?

中断是高度特定于硬件平台的。但处理器运行的代码中没有“变量声明”。变量只是预先确定的内存(或寄存器,如果编译器选择)中的位置

如果您的意思是分配给变量,那么是的,通常会发生中断。如果您需要
函数\u以获得\u bar()
不被中断和
旋转锁定\u irqsave(锁定)保证它不会,然后将赋值移到其中

int function(int something)
{
    int foo, ret;
    int bar; // This is declaration, just for the compiler
    some_typedef_t foobar;

    spin_lock_irqsave(lock);

    bar = function_to_get_bar(); // This is assignment, will actually run some code on the CPU
    foobar = get_this_guy(something);

    /*
    Here is code that does a lot of things
    */

    spin_unlock_irqrestore(lock);

    return ret;
}

中断是高度特定于硬件平台的。但处理器运行的代码中没有“变量声明”。变量只是预先确定的内存(或寄存器,如果编译器选择)中的位置

如果您的意思是分配给变量,那么是的,通常会发生中断。如果您需要
函数\u以获得\u bar()
不被中断和
旋转锁定\u irqsave(锁定)保证它不会,然后将赋值移到其中

int function(int something)
{
    int foo, ret;
    int bar; // This is declaration, just for the compiler
    some_typedef_t foobar;

    spin_lock_irqsave(lock);

    bar = function_to_get_bar(); // This is assignment, will actually run some code on the CPU
    foobar = get_this_guy(something);

    /*
    Here is code that does a lot of things
    */

    spin_unlock_irqrestore(lock);

    return ret;
}

这回答了我的问题。Thanks@PaulOgilvie当然,它将在进入锁之前完成,但这不是重点。这些值来自函数,调用时可能会发生中断,除非锁定。中断可能会影响函数的结果。请注意:在SMP系统上,中断仍然会到达其余的CPU,但在该CPU上运行的代码不会被中断。我们必须记住(特别是在函数中,如1)spin_lock()、2)do_smth()、3)spin_unlock()和4)return)我们保护数据访问,而不是代码本身!这回答了我的问题。Thanks@PaulOgilvie当然,它将在进入锁之前完成,但这不是重点。这些值来自函数,调用时可能会发生中断,除非锁定。中断可能会影响函数的结果。请注意:在SMP系统上,中断仍然会到达其余的CPU,但在该CPU上运行的代码不会被中断。我们必须记住(特别是在函数中,如1)spin_lock()、2)do_smth()、3)spin_unlock()和4)return)我们保护数据访问,而不是代码本身!