@@ -26,6 +26,7 @@ static kcb_t kernel_state = {
26
26
.task_count = 0 ,
27
27
.ticks = 0 ,
28
28
.preemptive = true, /* Default to preemptive mode */
29
+ .kcb_lock = SPINLOCK_INITIALIZER ,
29
30
};
30
31
kcb_t * kcb = & kernel_state ;
31
32
@@ -107,7 +108,6 @@ static inline uint8_t extract_priority_level(uint16_t prio)
107
108
return 4 ; /* Default to normal priority */
108
109
}
109
110
}
110
- static spinlock_t task_lock = SPINLOCK_INITIALIZER ;
111
111
static uint32_t task_flags = 0 ;
112
112
113
113
static inline bool is_valid_task (tcb_t * task )
@@ -594,12 +594,12 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
594
594
}
595
595
596
596
/* Minimize critical section duration */
597
- spin_lock_irqsave (& task_lock , & task_flags );
597
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
598
598
599
599
if (!kcb -> tasks ) {
600
600
kcb -> tasks = list_create ();
601
601
if (!kcb -> tasks ) {
602
- spin_unlock_irqrestore (& task_lock , task_flags );
602
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
603
603
free (tcb -> stack );
604
604
free (tcb );
605
605
panic (ERR_KCB_ALLOC );
@@ -608,7 +608,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
608
608
609
609
list_node_t * node = list_pushback (kcb -> tasks , tcb );
610
610
if (!node ) {
611
- spin_unlock_irqrestore (& task_lock , task_flags );
611
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
612
612
free (tcb -> stack );
613
613
free (tcb );
614
614
panic (ERR_TCB_ALLOC );
@@ -621,7 +621,7 @@ int32_t mo_task_spawn(void *task_entry, uint16_t stack_size_req)
621
621
if (!kcb -> task_current )
622
622
kcb -> task_current = node ;
623
623
624
- spin_unlock_irqrestore (& task_lock , task_flags );
624
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
625
625
626
626
/* Initialize execution context outside critical section. */
627
627
hal_context_init (& tcb -> context , (size_t ) tcb -> stack , new_stack_size ,
@@ -643,16 +643,16 @@ int32_t mo_task_cancel(uint16_t id)
643
643
if (id == 0 || id == mo_task_id ())
644
644
return ERR_TASK_CANT_REMOVE ;
645
645
646
- spin_lock_irqsave (& task_lock , & task_flags );
646
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
647
647
list_node_t * node = find_task_node_by_id (id );
648
648
if (!node ) {
649
- spin_unlock_irqrestore (& task_lock , task_flags );
649
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
650
650
return ERR_TASK_NOT_FOUND ;
651
651
}
652
652
653
653
tcb_t * tcb = node -> data ;
654
654
if (!tcb || tcb -> state == TASK_RUNNING ) {
655
- spin_unlock_irqrestore (& task_lock , task_flags );
655
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
656
656
return ERR_TASK_CANT_REMOVE ;
657
657
}
658
658
@@ -668,7 +668,7 @@ int32_t mo_task_cancel(uint16_t id)
668
668
}
669
669
}
670
670
671
- spin_unlock_irqrestore (& task_lock , task_flags );
671
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
672
672
673
673
/* Free memory outside critical section */
674
674
free (tcb -> stack );
@@ -690,9 +690,9 @@ void mo_task_delay(uint16_t ticks)
690
690
if (!ticks )
691
691
return ;
692
692
693
- spin_lock_irqsave (& task_lock , & task_flags );
693
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
694
694
if (unlikely (!kcb || !kcb -> task_current || !kcb -> task_current -> data )) {
695
- spin_unlock_irqrestore (& task_lock , task_flags );
695
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
696
696
return ;
697
697
}
698
698
@@ -701,7 +701,7 @@ void mo_task_delay(uint16_t ticks)
701
701
/* Set delay and blocked state - scheduler will skip blocked tasks */
702
702
self -> delay = ticks ;
703
703
self -> state = TASK_BLOCKED ;
704
- spin_unlock_irqrestore (& task_lock , task_flags );
704
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
705
705
706
706
mo_task_yield ();
707
707
}
@@ -711,24 +711,24 @@ int32_t mo_task_suspend(uint16_t id)
711
711
if (id == 0 )
712
712
return ERR_TASK_NOT_FOUND ;
713
713
714
- spin_lock_irqsave (& task_lock , & task_flags );
714
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
715
715
list_node_t * node = find_task_node_by_id (id );
716
716
if (!node ) {
717
- spin_unlock_irqrestore (& task_lock , task_flags );
717
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
718
718
return ERR_TASK_NOT_FOUND ;
719
719
}
720
720
721
721
tcb_t * task = node -> data ;
722
722
if (!task || (task -> state != TASK_READY && task -> state != TASK_RUNNING &&
723
723
task -> state != TASK_BLOCKED )) {
724
- spin_unlock_irqrestore (& task_lock , task_flags );
724
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
725
725
return ERR_TASK_CANT_SUSPEND ;
726
726
}
727
727
728
728
task -> state = TASK_SUSPENDED ;
729
729
bool is_current = (kcb -> task_current == node );
730
730
731
- spin_unlock_irqrestore (& task_lock , task_flags );
731
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
732
732
733
733
if (is_current )
734
734
mo_task_yield ();
@@ -741,22 +741,22 @@ int32_t mo_task_resume(uint16_t id)
741
741
if (id == 0 )
742
742
return ERR_TASK_NOT_FOUND ;
743
743
744
- spin_lock_irqsave (& task_lock , & task_flags );
744
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
745
745
list_node_t * node = find_task_node_by_id (id );
746
746
if (!node ) {
747
- spin_unlock_irqrestore (& task_lock , task_flags );
747
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
748
748
return ERR_TASK_NOT_FOUND ;
749
749
}
750
750
751
751
tcb_t * task = node -> data ;
752
752
if (!task || task -> state != TASK_SUSPENDED ) {
753
- spin_unlock_irqrestore (& task_lock , task_flags );
753
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
754
754
return ERR_TASK_CANT_RESUME ;
755
755
}
756
756
757
757
/* mark as ready - scheduler will find it */
758
758
task -> state = TASK_READY ;
759
- spin_unlock_irqrestore (& task_lock , task_flags );
759
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
760
760
return ERR_OK ;
761
761
}
762
762
@@ -765,16 +765,16 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
765
765
if (id == 0 || !is_valid_priority (priority ))
766
766
return ERR_TASK_INVALID_PRIO ;
767
767
768
- spin_lock_irqsave (& task_lock , & task_flags );
768
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
769
769
list_node_t * node = find_task_node_by_id (id );
770
770
if (!node ) {
771
- spin_unlock_irqrestore (& task_lock , task_flags );
771
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
772
772
return ERR_TASK_NOT_FOUND ;
773
773
}
774
774
775
775
tcb_t * task = node -> data ;
776
776
if (!task ) {
777
- spin_unlock_irqrestore (& task_lock , task_flags );
777
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
778
778
return ERR_TASK_NOT_FOUND ;
779
779
}
780
780
@@ -783,7 +783,7 @@ int32_t mo_task_priority(uint16_t id, uint16_t priority)
783
783
task -> prio_level = extract_priority_level (priority );
784
784
task -> time_slice = get_priority_timeslice (task -> prio_level );
785
785
786
- spin_unlock_irqrestore (& task_lock , task_flags );
786
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
787
787
788
788
return ERR_OK ;
789
789
}
@@ -793,21 +793,21 @@ int32_t mo_task_rt_priority(uint16_t id, void *priority)
793
793
if (id == 0 )
794
794
return ERR_TASK_NOT_FOUND ;
795
795
796
- spin_lock_irqsave (& task_lock , & task_flags );
796
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
797
797
list_node_t * node = find_task_node_by_id (id );
798
798
if (!node ) {
799
- spin_unlock_irqrestore (& task_lock , task_flags );
799
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
800
800
return ERR_TASK_NOT_FOUND ;
801
801
}
802
802
803
803
tcb_t * task = node -> data ;
804
804
if (!task ) {
805
- spin_unlock_irqrestore (& task_lock , task_flags );
805
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
806
806
return ERR_TASK_NOT_FOUND ;
807
807
}
808
808
809
809
task -> rt_prio = priority ;
810
- spin_unlock_irqrestore (& task_lock , task_flags );
810
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
811
811
return ERR_OK ;
812
812
}
813
813
@@ -823,9 +823,9 @@ int32_t mo_task_idref(void *task_entry)
823
823
if (!task_entry || !kcb -> tasks )
824
824
return ERR_TASK_NOT_FOUND ;
825
825
826
- spin_lock_irqsave (& task_lock , & task_flags );
826
+ spin_lock_irqsave (& kcb -> kcb_lock , & task_flags );
827
827
list_node_t * node = list_foreach (kcb -> tasks , refcmp , task_entry );
828
- spin_unlock_irqrestore (& task_lock , task_flags );
828
+ spin_unlock_irqrestore (& kcb -> kcb_lock , task_flags );
829
829
830
830
return node ? ((tcb_t * ) node -> data )-> id : ERR_TASK_NOT_FOUND ;
831
831
}
0 commit comments