Hello, the question is in the thread name.
Warning : I'll shoot without notice anyone saying "C pointers". C pointers are basically trivial, as long as there are no '*' charter in your C codebase
I start first: the scope is a context switch in my toy operating system. The issue was a dependency inversion where a lower layer directly calls a higher one. The solution was to implement a callback and to do this cleanly I added a functional interface, so every layer has a common fixed interface.
The implementation in C language, I show only relevant code snippets:
lower layer API header:
lower layer code:
higher layer code:
Please be kind, it's the first time I've implemented something this complicated, and I know there's still a lot of testing to do.
Anyone have something to show?
Warning : I'll shoot without notice anyone saying "C pointers". C pointers are basically trivial, as long as there are no '*' charter in your C codebase
I start first: the scope is a context switch in my toy operating system. The issue was a dependency inversion where a lower layer directly calls a higher one. The solution was to implement a callback and to do this cleanly I added a functional interface, so every layer has a common fixed interface.
The implementation in C language, I show only relevant code snippets:
lower layer API header:
C:
typedef hal_stack_word_t *hal_timerSchedCallback_func_t(hal_stack_word_t *stack_pointer);
typedef hal_timerSchedCallback_func_t *hal_timerSchedCallback_ptr_t;
void hal_timerSchedSetCallback(hal_timerSchedCallback_ptr_t func_ptr);
lower layer code:
C:
static hal_timerSchedCallback_ptr_t sched_callback = NULL;
void hal_timerSchedSetCallback(hal_timerSchedCallback_ptr_t func_ptr)
{
sched_callback = func_ptr;
}
ISR(TIMER1_COMPA_vect, ISR_NAKED)
{
[...]
// scheduler callback
if( sched_callback != NULL ) { sp_next = sched_callback(sp_current); }
[...]
}
higher layer code:
C:
static hal_timerSchedCallback_func_t tm_schedulerRR;
void tm_schedulerInit(void) { hal_timerSchedSetCallback(tm_schedulerRR); }
[...]
hal_stack_word_t *tm_schedulerRR(hal_stack_word_t *stack_pointer)
{
mod_thread_item_t *thread;
// save current thread context
thread = mod_threadGetPointer(mod_threadGetCurrent());
thread->stack_pointer = stack_pointer;
// switch thread and context
[...]
thread = mod_threadGetPointer(mod_threadGetCurrent());
return thread->stack_pointer;
}
Please be kind, it's the first time I've implemented something this complicated, and I know there's still a lot of testing to do.
Anyone have something to show?