Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vTaskSuspendAll() / xTaskResumeAll() disables IRQs #6

Open
g-mocken opened this issue Mar 22, 2021 · 1 comment
Open

vTaskSuspendAll() / xTaskResumeAll() disables IRQs #6

g-mocken opened this issue Mar 22, 2021 · 1 comment

Comments

@g-mocken
Copy link

g-mocken commented Mar 22, 2021

Regarding this part (and a few more similar occurrences):

#define DRN_ENTER_CRITICAL_SECTION(_usis) vTaskSuspendAll(); // Note: safe to use before FreeRTOS scheduler started, but not in ISR
#define DRN_EXIT_CRITICAL_SECTION(_usis)  xTaskResumeAll();  // Note: safe to use before FreeRTOS scheduler started, but not in ISR

While, as the comment suggests, it may be safe to use before FreeRTOS scheduler is started, it still has a side effect (at least on my M0+ port):
Any call to malloc() before starting the scheduler will then return with interrupts disabled.

I suggest to wrap the above with
if (xTaskGetSchedulerState()!= taskSCHEDULER_NOT_STARTED){ ... }

(Patching FreeRTOS' handling of the variable uxCriticalNesting would be another way to deal with it, but I prefer not to modify these parts.)

@ljluestc
Copy link

ljluestc commented Aug 9, 2023

Your suggestion of wrapping the critical section code with a check for the scheduler state (xTaskGetSchedulerState()) is a reasonable approach to address the issue. By checking whether the scheduler has been started, you can avoid the side effect of interrupts being disabled when calling malloc() before the scheduler initialization.

Here's how your modified code might look with the suggested change:

if (xTaskGetSchedulerState() == taskSCHEDULER_NOT_STARTED) {
    // Before scheduler started
    // Perform critical section without suspending tasks
} else {
    // After scheduler started
    // Enter critical section
    vTaskSuspendAll();
    
    // ... Critical section code ...
    
    // Exit critical section
    xTaskResumeAll();
}

This approach ensures that the critical section is only applied when the scheduler has been started, preventing unwanted side effects when using memory allocation functions like malloc() before the scheduler is up and running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants