@@ -11,7 +11,11 @@ pcb_t current_task = NULL;
1111pcb_t kernel_head_task = NULL ;
1212bool is_scheduler = false;
1313
14+ extern uint64_t cpu_count ; //smp.c
15+ extern uint32_t bsp_processor_id ; //smp.c
16+
1417ticketlock scheduler_lock ;
18+ ticketlock scheduler_lock_rw ;
1519
1620pcb_t get_current_task (){
1721 return current_task ;
@@ -29,20 +33,31 @@ int add_task(pcb_t new_task) {
2933 if (new_task == NULL ) return -1 ;
3034 ticket_lock (& scheduler_lock );
3135
32- smp_cpu_t * cpu = get_cpu_smp (get_current_cpuid ());
33- if (cpu == NULL ){
36+ smp_cpu_t * cpu0 = get_cpu_smp (bsp_processor_id );
37+ uint32_t cpuid = bsp_processor_id ;
38+
39+ // for (int i = 0; i < cpu_count; i++) {
40+ // smp_cpu_t *cpu = get_cpu_smp(i);
41+ // if(cpu == NULL) continue;
42+ // if(cpu->flags == 1 && cpu->scheduler_queue->size < cpu0->scheduler_queue->size){
43+ // cpu0 = cpu;
44+ // cpuid = i;
45+ // }
46+ // }
47+
48+ if (cpu0 == NULL ){
3449 ticket_unlock (& scheduler_lock );
3550 return -1 ;
3651 }
37- new_task -> queue_index = queue_enqueue (cpu -> scheduler_queue ,new_task );
52+ new_task -> cpu_id = cpuid ;
53+ new_task -> queue_index = queue_enqueue (cpu0 -> scheduler_queue ,new_task );
3854 if (new_task -> queue_index == -1 ){
3955 logkf ("Error: scheduler null %d\n" ,get_current_cpuid ());
4056 return -1 ;
4157 }
58+ if (new_task -> pid == cpu0 -> idle_pcb -> pid ) goto ret ;
4259
43- if (new_task -> pid == kernel_head_task -> pid ) goto ret ;
44-
45- pivfs_update (kernel_head_task );
60+ //pivfs_update(kernel_head_task);
4661 ret :
4762 ticket_unlock (& scheduler_lock );
4863 return new_task -> queue_index ;
@@ -58,7 +73,7 @@ void remove_task(pcb_t task){
5873 return ;
5974 }
6075 queue_remove_at (cpu -> scheduler_queue ,task -> queue_index );
61- pivfs_update (kernel_head_task );
76+ // pivfs_update(kernel_head_task);
6277 ticket_unlock (& scheduler_lock );
6378}
6479
@@ -109,17 +124,30 @@ void change_proccess(registers_t *reg,pcb_t taget){
109124 reg -> rip = taget -> context0 .rip ;
110125 reg -> rsp = taget -> context0 .rsp ;
111126
127+
112128 current_task = taget ;
113129}
114130
115131/**
116- * CP_Kernel 默认单核调度器 - 循环公平调度
132+ * CP_Kernel 默认多核调度器 - 循环公平调度
117133 * @param reg 当前进程上下文
118134 */
119135void scheduler (registers_t * reg ){
120136 if (is_scheduler ){
121137 if (current_task != NULL ){
138+ ticket_lock (& scheduler_lock );
122139 smp_cpu_t * cpu = get_cpu_smp (get_current_cpuid ());
140+ if (cpu == NULL ) {
141+ logkf ("Error: scheduler null %d\n" ,get_current_cpuid ());
142+ ticket_unlock (& scheduler_lock );
143+ return ;
144+ }
145+
146+ if (cpu -> scheduler_queue -> size == 1 ){
147+ ticket_unlock (& scheduler_lock );
148+ return ;
149+ }
150+
123151 if (cpu -> iter_node == NULL ) {
124152 iter_head :
125153 cpu -> iter_node = cpu -> scheduler_queue -> head ;
@@ -133,7 +161,6 @@ void scheduler(registers_t *reg){
133161 }
134162
135163 pcb_t next = (pcb_t )data ;
136- // logkf("p: %p\n",next);
137164
138165 current_task -> cpu_clock ++ ;
139166 if (current_task -> time_buf != NULL ){
@@ -147,6 +174,8 @@ void scheduler(registers_t *reg){
147174 change_proccess (reg ,next );
148175 enable_scheduler ();
149176 }
177+
178+ ticket_unlock (& scheduler_lock );
150179 }
151180 }
152181 send_eoi ();
0 commit comments