@@ -33,6 +33,9 @@ static kcb_t kernel_state = {
33
33
};
34
34
kcb_t * kcb = & kernel_state ;
35
35
36
+ /* Deferred timer work flag to reduce interrupt latency */
37
+ static volatile bool timer_work_pending = false;
38
+
36
39
/* Magic number written to both ends of a task's stack for corruption detection.
37
40
*/
38
41
#define STACK_CANARY 0x33333333U
@@ -288,7 +291,7 @@ static int32_t noop_rtsched(void)
288
291
void dispatcher (void )
289
292
{
290
293
kcb -> ticks ++ ;
291
- _timer_tick_handler () ;
294
+ timer_work_pending = true ;
292
295
_dispatch ();
293
296
}
294
297
@@ -327,6 +330,12 @@ void yield(void)
327
330
if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data ))
328
331
return ;
329
332
333
+ /* Process deferred timer work during yield */
334
+ if (timer_work_pending ) {
335
+ timer_work_pending = false;
336
+ _timer_tick_handler ();
337
+ }
338
+
330
339
/* HAL context switching is used for preemptive scheduling. */
331
340
if (hal_context_save (((tcb_t * ) kcb -> task_current -> data )-> context ) != 0 )
332
341
return ;
@@ -491,6 +500,12 @@ void mo_task_yield(void)
491
500
492
501
void mo_task_delay (uint16_t ticks )
493
502
{
503
+ /* Process deferred timer work before sleeping */
504
+ if (timer_work_pending ) {
505
+ timer_work_pending = false;
506
+ _timer_tick_handler ();
507
+ }
508
+
494
509
if (!ticks )
495
510
return ;
496
511
@@ -634,6 +649,12 @@ int32_t mo_task_idref(void *task_entry)
634
649
635
650
void mo_task_wfi (void )
636
651
{
652
+ /* Process deferred timer work before waiting */
653
+ if (timer_work_pending ) {
654
+ timer_work_pending = false;
655
+ _timer_tick_handler ();
656
+ }
657
+
637
658
if (!kcb -> preemptive )
638
659
return ;
639
660
@@ -663,6 +684,12 @@ void _sched_block(queue_t *wait_q)
663
684
!kcb -> task_current -> data ))
664
685
panic (ERR_SEM_OPERATION );
665
686
687
+ /* Process deferred timer work before blocking */
688
+ if (timer_work_pending ) {
689
+ timer_work_pending = false;
690
+ _timer_tick_handler ();
691
+ }
692
+
666
693
tcb_t * self = kcb -> task_current -> data ;
667
694
668
695
if (queue_enqueue (wait_q , self ) != 0 ) {
0 commit comments