Re: Cortex M0 interrupts question
Posted: Tue Apr 07, 2015 1:50 am
Okay, so my real problem here is that some FreeRTOS api functions use ENTER_CRITICAL/EXIT_CRITICAL internally, but depend on interrupts being re-enabled so that the caller is rescheduled within the function. By mapping osalSysLock/osalSysUnlock to ENTER_CRITICAL/EXIT_CRITICAL, they are nested which means tasks aren't rescheduled properly since interrupts get enabled too late.
If I map osalSysLock/osalSysUnlock to ENABLE_INTERRUPTS/DISABLE_INTERRUPTS, I get around the nesting problem but then I break the contract of osalSysLock (interrupts will sometimes be re-enabled before the return of the S-class function call). The subsequent call to osalSysUnlock won't do anything, but any code between the S-class function and osalSysUnlock will not be safe.
Unfortunately I think without modifying FreeRTOS I can not have the critical section handling that ChibiOS/HAL expects. I implemented osalThreadSuspendTimeoutS and osalThreadResume{S/I} using FreeRTOS task notifications. In xTaskNotifyWait() if I removed the first call to taskENTER_CRITICAL() and the last call to taskEXIT_CRITICAL(), I believe the system will behave as expected. There would be a critical section from osalSysLock() until after the thread is rescheduled within xTaskNotifyWait(), then another critical section from that point (after the thread was resumed) until the call to osalSysUnlock in the calling thread. This would avoid the potential race condition you identified.
I would just be prohibited from using xTaskNotifyWait elsewhere in my code, which is fine because the feature is essentially reserved for OSAL use (tasks only have room for one 32-bit message). I really wanted to avoid modifying FreeRTOS though.
Sorry for the wall of text, I'm just thinking aloud here.
If I map osalSysLock/osalSysUnlock to ENABLE_INTERRUPTS/DISABLE_INTERRUPTS, I get around the nesting problem but then I break the contract of osalSysLock (interrupts will sometimes be re-enabled before the return of the S-class function call). The subsequent call to osalSysUnlock won't do anything, but any code between the S-class function and osalSysUnlock will not be safe.
Unfortunately I think without modifying FreeRTOS I can not have the critical section handling that ChibiOS/HAL expects. I implemented osalThreadSuspendTimeoutS and osalThreadResume{S/I} using FreeRTOS task notifications. In xTaskNotifyWait() if I removed the first call to taskENTER_CRITICAL() and the last call to taskEXIT_CRITICAL(), I believe the system will behave as expected. There would be a critical section from osalSysLock() until after the thread is rescheduled within xTaskNotifyWait(), then another critical section from that point (after the thread was resumed) until the call to osalSysUnlock in the calling thread. This would avoid the potential race condition you identified.
I would just be prohibited from using xTaskNotifyWait elsewhere in my code, which is fine because the feature is essentially reserved for OSAL use (tasks only have room for one 32-bit message). I really wanted to avoid modifying FreeRTOS though.
Sorry for the wall of text, I'm just thinking aloud here.