55#include "pivfs.h"
66#include "smp.h"
77#include "lock.h"
8+ #include "lock_queue.h"
89
910pcb_t current_task = NULL ;
1011pcb_t kernel_head_task = NULL ;
@@ -25,15 +26,39 @@ void disable_scheduler(){
2526 is_scheduler = false;
2627}
2728
28- void add_task (pcb_t new_task ) {
29- if (new_task == NULL ) return ;
29+ int add_task (pcb_t new_task ) {
30+ if (new_task == NULL ) return -1 ;
3031 ticket_lock (& scheduler_lock );
31- pcb_t tailt = kernel_head_task ;
32- while (tailt -> next != kernel_head_task ) {
33- if (tailt -> next == NULL ) break ;
34- tailt = tailt -> next ;
32+
33+ smp_cpu_t * cpu = get_cpu_smp (get_current_cpuid ());
34+ if (cpu == NULL ){
35+ ticket_unlock (& scheduler_lock );
36+ return -1 ;
37+ }
38+ new_task -> queue_index = queue_enqueue (cpu -> scheduler_queue ,new_task );
39+ if (new_task -> queue_index == -1 ){
40+ logkf ("Error: scheduler null %d\n" ,get_current_cpuid ());
41+ return -1 ;
42+ }
43+
44+ if (new_task -> pid == kernel_head_task -> pid ) goto ret ;
45+
46+ pivfs_update (kernel_head_task );
47+ ret :
48+ ticket_unlock (& scheduler_lock );
49+ return new_task -> queue_index ;
50+ }
51+
52+ void remove_task (pcb_t task ){
53+ if (task == NULL ) return ;
54+ ticket_lock (& scheduler_lock );
55+
56+ smp_cpu_t * cpu = get_cpu_smp (get_current_cpuid ());
57+ if (cpu == NULL ){
58+ ticket_unlock (& scheduler_lock );
59+ return ;
3560 }
36- tailt -> next = new_task ;
61+ queue_remove_at ( cpu -> scheduler_queue , task -> queue_index ) ;
3762 pivfs_update (kernel_head_task );
3863 ticket_unlock (& scheduler_lock );
3964}
@@ -95,17 +120,28 @@ void change_proccess(registers_t *reg,pcb_t taget){
95120}
96121
97122/**
98- * CP_Kernel 默认调度器 - 循环调度
99- * TODO 只适用于单核调度所以做了进程的CPU归属判断
123+ * CP_Kernel 默认单核调度器 - 循环调度
100124 * @param reg 当前进程上下文
101125 */
102126void scheduler (registers_t * reg ){
103127 if (is_scheduler ){
104128 if (current_task != NULL ){
105- pcb_t next = current_task -> next ;
106- while (next -> cpu_id != get_current_cpuid ()){
107- next = next -> next ;
129+ smp_cpu_t * cpu = get_cpu_smp (get_current_cpuid ());
130+ if (cpu -> iter_node == NULL ) {
131+ iter_head :
132+ cpu -> iter_node = cpu -> scheduler_queue -> head ;
133+ } else {
134+ cpu -> iter_node = cpu -> iter_node -> next ;
135+ if (cpu -> iter_node == NULL ) goto iter_head ;
108136 }
137+ void * data = NULL ;
138+ if (cpu -> iter_node != NULL ) {
139+ data = cpu -> iter_node -> data ;
140+ }
141+
142+ pcb_t next = (pcb_t )data ;
143+ // logkf("p: %p\n",next);
144+
109145 current_task -> cpu_clock ++ ;
110146 if (current_task -> time_buf != NULL ){
111147 current_task -> cpu_timer += get_time (current_task -> time_buf );
0 commit comments