@@ -23,7 +23,7 @@ void _timer_tick_handler(void);
2323 */
2424static kcb_t kernel_state = {
2525 .tasks = NULL ,
26- .task_current = NULL ,
26+ .task_current = {} ,
2727 .rt_sched = noop_rtsched ,
2828 .timer_list = NULL , /* Managed by timer.c, but stored here. */
2929 .next_tid = 1 , /* Start from 1 to avoid confusion with invalid ID 0 */
@@ -85,10 +85,10 @@ static void task_stack_check(void)
8585 if (!should_check )
8686 return ;
8787
88- if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data ))
88+ if (unlikely (!kcb || !get_task_current () || !get_task_current () -> data ))
8989 panic (ERR_STACK_CHECK );
9090
91- tcb_t * self = kcb -> task_current -> data ;
91+ tcb_t * self = get_task_current () -> data ;
9292 if (unlikely (!is_valid_task (self )))
9393 panic (ERR_STACK_CHECK );
9494
@@ -205,7 +205,7 @@ void _yield(void) __attribute__((weak, alias("yield")));
205205/* Scheduler with hint-based ready task search */
206206static list_node_t * find_next_ready_task (void )
207207{
208- if (unlikely (!kcb -> task_current ))
208+ if (unlikely (!get_task_current () ))
209209 return NULL ;
210210
211211 list_node_t * node ;
@@ -225,7 +225,7 @@ static list_node_t *find_next_ready_task(void)
225225 }
226226 }
227227
228- node = kcb -> task_current ;
228+ node = get_task_current () ;
229229 while (itcnt ++ < SCHED_IMAX ) {
230230 node = list_cnext (kcb -> tasks , node );
231231 if (unlikely (!node || !node -> data ))
@@ -258,11 +258,11 @@ static list_node_t *find_next_ready_task(void)
258258/* Scheduler with reduced overhead */
259259static uint16_t schedule_next_task (void )
260260{
261- if (unlikely (!kcb -> task_current || !kcb -> task_current -> data ))
261+ if (unlikely (!get_task_current () || !get_task_current () -> data ))
262262 panic (ERR_NO_TASKS );
263263
264264 /* Mark the previously running task as ready for the next cycle. */
265- tcb_t * current_task = kcb -> task_current -> data ;
265+ tcb_t * current_task = get_task_current () -> data ;
266266 if (current_task -> state == TASK_RUNNING )
267267 current_task -> state = TASK_READY ;
268268
@@ -273,7 +273,7 @@ static uint16_t schedule_next_task(void)
273273 }
274274
275275 /* Update scheduler state */
276- kcb -> task_current = next_node ;
276+ set_task_current ( next_node ) ;
277277 tcb_t * next_task = next_node -> data ;
278278 next_task -> state = TASK_RUNNING ;
279279
@@ -297,11 +297,11 @@ void dispatcher(void)
297297/* Top-level context-switch for preemptive scheduling. */
298298void dispatch (void )
299299{
300- if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data ))
300+ if (unlikely (!kcb || !get_task_current () || !get_task_current () -> data ))
301301 panic (ERR_NO_TASKS );
302302
303303 /* Return from longjmp: context is restored, continue task execution. */
304- if (setjmp (((tcb_t * ) kcb -> task_current -> data )-> context ) != 0 )
304+ if (setjmp (((tcb_t * ) get_task_current () -> data )-> context ) != 0 )
305305 return ;
306306
307307 task_stack_check ();
@@ -313,16 +313,16 @@ void dispatch(void)
313313 }
314314
315315 hal_interrupt_tick ();
316- longjmp (((tcb_t * ) kcb -> task_current -> data )-> context , 1 );
316+ longjmp (((tcb_t * ) get_task_current () -> data )-> context , 1 );
317317}
318318
319319/* Cooperative context switch */
320320void yield (void )
321321{
322- if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data ))
322+ if (unlikely (!kcb || !get_task_current () || !get_task_current () -> data ))
323323 return ;
324324
325- if (setjmp (((tcb_t * ) kcb -> task_current -> data )-> context ) != 0 )
325+ if (setjmp (((tcb_t * ) get_task_current () -> data )-> context ) != 0 )
326326 return ;
327327
328328 task_stack_check ();
@@ -332,7 +332,7 @@ void yield(void)
332332 list_foreach (kcb -> tasks , delay_update , NULL );
333333
334334 schedule_next_task ();
335- longjmp (((tcb_t * ) kcb -> task_current -> data )-> context , 1 );
335+ longjmp (((tcb_t * ) get_task_current () -> data )-> context , 1 );
336336}
337337
338338/* Stack initialization with minimal overhead */
@@ -411,8 +411,8 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
411411 tcb -> id = kcb -> next_tid ++ ;
412412 kcb -> task_count ++ ;
413413
414- if (!kcb -> task_current )
415- kcb -> task_current = node ;
414+ if (!get_task_current () )
415+ set_task_current ( node ) ;
416416
417417 spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
418418
@@ -483,12 +483,12 @@ void mo_task_delay(uint16_t ticks)
483483 return ;
484484
485485 spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
486- if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data )) {
486+ if (unlikely (!kcb || !get_task_current () || !get_task_current () -> data )) {
487487 spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
488488 return ;
489489 }
490490
491- tcb_t * self = kcb -> task_current -> data ;
491+ tcb_t * self = get_task_current () -> data ;
492492 self -> delay = ticks ;
493493 self -> state = TASK_BLOCKED ;
494494 spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
@@ -516,7 +516,7 @@ int32_t mo_task_suspend(uint16_t id)
516516 }
517517
518518 task -> state = TASK_SUSPENDED ;
519- bool is_current = (kcb -> task_current == node );
519+ bool is_current = (get_task_current () == node );
520520
521521 /* Clear ready hint if suspending that task */
522522 if (kcb -> last_ready_hint == node )
@@ -603,9 +603,9 @@ int32_t mo_task_rt_priority(uint16_t id, void *priority)
603603
604604uint16_t mo_task_id (void )
605605{
606- if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data ))
606+ if (unlikely (!kcb || !get_task_current () || !get_task_current () -> data ))
607607 return 0 ;
608- return ((tcb_t * ) kcb -> task_current -> data )-> id ;
608+ return ((tcb_t * ) get_task_current () -> data )-> id ;
609609}
610610
611611int32_t mo_task_idref (void * task_entry )
@@ -647,11 +647,11 @@ uint64_t mo_uptime(void)
647647
648648void _sched_block (queue_t * wait_q )
649649{
650- if (unlikely (!wait_q || !kcb || !kcb -> task_current ||
651- !kcb -> task_current -> data ))
650+ if (unlikely (!wait_q || !kcb || !get_task_current () ||
651+ !get_task_current () -> data ))
652652 panic (ERR_SEM_OPERATION );
653653
654- tcb_t * self = kcb -> task_current -> data ;
654+ tcb_t * self = get_task_current () -> data ;
655655
656656 if (queue_enqueue (wait_q , self ) != 0 ) {
657657 panic (ERR_SEM_OPERATION );
0 commit comments