Przeglądaj źródła

线程进入临界区产生冲突

simon 1 rok temu
rodzic
commit
ae1d8bcc55
3 zmienionych plików z 10 dodań i 4 usunięć
  1. 6 0
      kernel/main.c
  2. 2 2
      thread/thread.c
  3. 2 2
      thread/thread.h

+ 6 - 0
kernel/main.c

@@ -27,7 +27,9 @@ int main(void)
     // 主线程会一直执行, 直到被中断或被其他线程强制结束
     while (1)
     {
+        intr_disable();
         put_str("Main ");
+        intr_enable();
     }
     return 0;
 }
@@ -38,7 +40,9 @@ void thread_a_func(void *arg)
     char *para = arg;
     while (1)
     {
+        intr_disable();
         put_str(para);
+        intr_enable();
     }
 }
 void thread_b_func(void *arg)
@@ -47,6 +51,8 @@ void thread_b_func(void *arg)
     char *para = arg;
     while (1)
     {
+        intr_disable();
         put_str(para);
+        intr_enable();
     }
 }

+ 2 - 2
thread/thread.c

@@ -16,7 +16,7 @@ static struct list_elem *thread_tag; // 用于保存队列中的线程节点
 extern void switch_to(struct task_struct *cur, struct task_struct *next);
 
 // 获取当前线程的 PCB 指针
-struct task_struct *running_thread()
+struct task_struct *running_thread(void)
 {
     uint32_t esp;
     asm("mov %%esp, %0" : "=g"(esp));
@@ -105,7 +105,7 @@ static void make_main_thread(void)
 
 /// @brief 将当前线程换下处理器,并在就绪队列中找出下个可运行的程序,换上处理器
 ///! 此过程由 时钟中断 来调用
-void schedule()
+void schedule(void)
 {
     ASSERT(intr_get_status() == INTR_OFF);
 

+ 2 - 2
thread/thread.h

@@ -96,7 +96,7 @@ struct task_struct
 };
 
 // 获取当前线程的 PCB 指针
-struct task_struct *running_thread();
+struct task_struct *running_thread(void);
 // 初始化线程栈 thread_stack
 void thread_stack_create(struct task_struct *pthread, thread_func function, void *func_arg);
 // 初始化线程基本信息
@@ -104,7 +104,7 @@ void init_thread(struct task_struct *pthread, char *name, int prio);
 // 创建优先级为 prio 的线程, 线程名为 name, 线程所执行的函数是 function(func_arg)
 struct task_struct *thread_start(char *name, int prio, thread_func function, void *func_arg);
 // 任务调度
-void schedule();
+void schedule(void);
 // 初始化线程环境
 void thread_init(void);