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.
Cortex M0 interrupts question
Moderators: RoccoMarco, lbednarz, tfAteba
- Giovanni
- Site Admin
- Posts: 14895
- Joined: Wed May 27, 2009 8:48 am
- Has thanked: 1202 times
- Been thanked: 997 times
Re: Cortex M0 interrupts question
Hi,
Couldn't you take the FreeRTOS function, strip the enter-exit calls and put it into the OSAL layer as an S function? you don't need to modify it.
Giovanni
Couldn't you take the FreeRTOS function, strip the enter-exit calls and put it into the OSAL layer as an S function? you don't need to modify it.
Giovanni
Re: Cortex M0 interrupts question
Hi Giovanni,
Unfortunately not. FreeRTOS implements data hiding so the internals of the TCB structure may only be accessed within tasks.c.
However I think I found a decent solution. I mapped osalSysLock/Unlock to ENABLE/DISABLE_INTERRUPTS, then after the call to the FreeRTOS API function I simply disable interrupts again.
The internals of xTaskNotifyWait has two critical sections, and re-enables interrupts right before it returns.
So, interrupts are enabled in the non-suspended task for a very short while, but by the time the S class function returns they are disabled again. I think this should be safe.
Unfortunately not. FreeRTOS implements data hiding so the internals of the TCB structure may only be accessed within tasks.c.
However I think I found a decent solution. I mapped osalSysLock/Unlock to ENABLE/DISABLE_INTERRUPTS, then after the call to the FreeRTOS API function I simply disable interrupts again.
Code: Select all
msg_t osalThreadSuspendTimeoutS(...) {
xTaskNotifyWait(...);
/* Interrupts were enabled immediately before the exit of xTaskNotifyWait */
osalSysLock();
/* do some more bookkeeping*/
return...;
}
void someFunction(void) {
osalSysLock(); // disables interrupts
osalThreadSuspendTimeoutS(...);
/* interrupts were disabled when the above returned */
... more stuff...
osalSysUnlock();
}
The internals of xTaskNotifyWait has two critical sections, and re-enables interrupts right before it returns.
Code: Select all
xTaskNotifyWait(...)
{
entercritical();
// add task to suspended list
// schedule a context switch
exitcritical();
// context switch now occurs due to pendsv interrupt. Thread is suspended.
entercritical();
// get the notification or timeout that woke us up
exitcritical();
return;
}
So, interrupts are enabled in the non-suspended task for a very short while, but by the time the S class function returns they are disabled again. I think this should be safe.