彭浩翔 彭浩翔 - 1 month ago 20
C Question

How "save_and_cli(flag)" really works?

I was tracing a piece of code for a program works in embedded Linux and encountered this. I couldn't google out useful information to show me how it really works.

This piece of code is written like:

unsigned char foo(*someaddress){
unsigned long flags = 0; //why use a long?
flags = save_and_cli(); //what is this?
spin_lock_irqsave(&someaddress, flags);//ok, this is a spin lock

...do something...

restore_flags(flags); //release something?
spin_unlock_irqrestore(&someaddress, flags);//release lock
}


It seems this LONG flags' value is never changed and what is save_and_cli() function?

Thanks if you reading this.

LPs LPs
Answer

IIRC save_and_cli is an "ancient" version of local_irq_save

It saves the current state of local interrupt delivery and then disable it.

Function local_irq_restore is used to restore irq state saved by local_irq_save

Here for reference