Skull
Skull

Reputation: 61

In which place interrupts can interrupt function in C?

I'm writing code in ISO C90, which forbids mixed declarations and code. So I have something like this:

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;
}

The question is if hardware or software interrupt happens, in which place it can interrupt my function, can it happen also in the middle of variable declaration?

Why I ask this is because I need that this function is not interrupted by interrupts. I want to use spin_lock_irqsave() to ensure that, but I was wondering if interrupt could interrupt my function in variable declarations?

Upvotes: 1

Views: 410

Answers (1)

Sami Kuhmonen
Sami Kuhmonen

Reputation: 31153

Interrupts are highly hardware platform specific. But there is no "variable declaration" in the code the processor runs. The variables are just places in memory (or in registers, if the compiler so chooses) that are predetermined.

If you mean assigning to variables then yes, in general interrupts can happen. If you need function_to_get_bar() not to be interrupted and spin_lock_irqsave(lock); guarantees it won't, then just move the assignment inside it.

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;
}

Upvotes: 5

Related Questions