Are the PendSV / SVC exceptions raised immediately?

Viewed 4188

I'm working on a context saving and restoring mechanism on the Cortex-M4 so that I can implement simple multitasking. I use arm-none-eabi-g++ for compiling this code. Portability is not a concern for now.

I use this for a form of cooperative multitasking when a task can call a yield function which will make execution return to the kernel by raising a PendSV exception. (The kernel can then schedule another task for running and eventually return to the current one.)

void Task::yield() {
    // ...
    // ... (Context saving code goes here) ...
    // ...

    // Set the PENDSVSET to trigger a PendSV exception
    SCB->ICSR |= SCB_ICSR_PENDSVSET_Msk;
}

I'd like execution to go back to the kernel immediately.

The question is, after I set the PENDSVSET bit in the ICSR, is it guaranteed that execution goes to PendSV_Handler immediately, or does it continue executing instructions after the yield function?

Would I better off using the SVC instruction for this?

void Task::yield() {
    // ...

    // SVC has an 8-bit immediate constant argument, which
    // can be used by the kernel for determining what kind
    // of system call this is.
    asm volatile("SVC 0");
}
1 Answers
Related