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()来确保,但我想知道中断是否可以在变量声明中中断我的函数?
中断是高度硬件平台特定的.但是处理器运行的代码中没有"变量声明".变量只是预定义的内存(或寄存器,如果编译器选择的话).
如果你的意思是分配变量然后是,通常可以发生中断.如果您不需要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)