@@ -515,6 +515,8 @@ void dispatch(void)
515
515
/* Cooperative context switch */
516
516
void yield (void )
517
517
{
518
+ uint32_t flag = 0 ;
519
+
518
520
if (unlikely (!kcb || !get_task_current () || !get_task_current ()-> data ))
519
521
return ;
520
522
@@ -530,6 +532,8 @@ void yield(void)
530
532
#endif
531
533
532
534
/* In cooperative mode, delays are only processed on an explicit yield. */
535
+ spin_lock_irqsave (& kcb -> kcb_lock , & flag );
536
+
533
537
if (!kcb -> preemptive )
534
538
list_foreach (kcb -> tasks , delay_update , NULL );
535
539
@@ -824,10 +828,13 @@ uint16_t mo_task_id(void)
824
828
825
829
int32_t mo_task_idref (void * task_entry )
826
830
{
827
- if (!task_entry || !kcb -> tasks )
831
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
832
+
833
+ if (!task_entry || !kcb -> tasks ) {
834
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
828
835
return ERR_TASK_NOT_FOUND ;
836
+ }
829
837
830
- spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
831
838
list_node_t * node = list_foreach (kcb -> tasks , refcmp , task_entry );
832
839
spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
833
840
@@ -838,23 +845,46 @@ void mo_task_wfi(void)
838
845
{
839
846
/* Process deferred timer work before waiting */
840
847
process_deferred_timer_work ();
848
+ uint32_t flag = 0 ;
841
849
842
850
if (!kcb -> preemptive )
843
851
return ;
844
852
853
+ spin_lock_irqsave (& kcb -> kcb_lock , & flag );
845
854
volatile uint32_t current_ticks = kcb -> ticks ;
846
- while (current_ticks == kcb -> ticks )
855
+ spin_unlock_irqrestore (& kcb -> kcb_lock , flag );
856
+
857
+ while (1 ) {
858
+ spin_lock_irqsave (& kcb -> kcb_lock , & flag );
859
+ if (current_ticks != kcb -> ticks ) {
860
+ spin_unlock_irqrestore (& kcb -> kcb_lock , flag );
861
+ break ;
862
+ }
863
+ spin_unlock_irqrestore (& kcb -> kcb_lock , flag );
847
864
hal_cpu_idle ();
865
+ }
848
866
}
849
867
850
868
uint16_t mo_task_count (void )
851
869
{
852
- return kcb -> task_count ;
870
+ uint32_t task_count ;
871
+ uint32_t flag ;
872
+
873
+ spin_lock_irqsave (& kcb -> kcb_lock , & flag );
874
+ task_count = kcb -> task_count ;
875
+ spin_unlock_irqrestore (& kcb -> kcb_lock , flag );
876
+ return task_count ;
853
877
}
854
878
855
879
uint32_t mo_ticks (void )
856
880
{
857
- return kcb -> ticks ;
881
+ uint32_t ticks ;
882
+ uint32_t flag ;
883
+
884
+ spin_lock_irqsave (& kcb -> kcb_lock , & flag );
885
+ ticks = kcb -> ticks ;
886
+ spin_unlock_irqrestore (& kcb -> kcb_lock , flag );
887
+ return ticks ;
858
888
}
859
889
860
890
uint64_t mo_uptime (void )
0 commit comments