在哪个地方中断可以中断C中的功能?

Sku*_*ull 1 c linux-kernel interrupt-handling spinlock

我正在用ISO C90编写代码,它禁止混合声明和代码.所以我有这样的事情:

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;
}
Run Code Online (Sandbox Code Playgroud)

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

为什么我问这是因为我需要这个函数不被中断打断.我想使用spin_lock_irqsave()来确保,但我想知道中断是否可以在变量声明中中断我的函数?

Sam*_*nen 5

中断是高度硬件平台特定的.但是处理器运行的代码中没有"变量声明".变量只是预定义的内存(或寄存器,如果编译器选择的话).

如果你的意思是分配变量然后是,通常可以发生中断.如果您不需要function_to_get_bar()被打断并spin_lock_irqsave(lock);保证不会被打断,那么只需在其中移动任务即可.

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;
}
Run Code Online (Sandbox Code Playgroud)

  • 请注意:在SMP系统上,中断仍然会进入其余的CPU,但在这个特定CPU上运行的代码不会被中断.我们必须记住(特别是在*1之类的函数中)spin_lock(),2)do_smth(),3)spin_unlock(),以及4)return*)我们保护数据访问,而不是代码本身! (2认同)