MIT6.828_HW8_User-level threads
uthread creates two threads and switches back and forth between them. Each thread prints “my thread …” and then yields to give the other thread a chance to run.
对thread
结构体以及线程指针位置的理解,是完成这个 HW 的关键。
struct thread {
int sp; /* saved stack pointer */
char stack[STACK_SIZE]; /* the thread's stack */
int state; /* FREE, RUNNING, RUNNABLE */
};
经过LAB4的洗礼,在根据给出的提示,写出实现代码并不难。不过在实现过程发现,在 .S 文件中竟然可以直接使用 .c 文件中的变量current_thread
等,而且在讲义中还特意强调uthread_switch.S ends with .S,这引起了我的好奇,毕竟一直都没有探究汇编的细节。
- .s: 汇编语言源程序;汇编
- 小写的s文件,在后期阶段不在进行预处理操作,所以我们不能在这里面写预处理的语句在里面
- .S:汇编语言源程序;预处理,汇编
- 大写的S文件,还会进行预处理、汇编等操作,所以我们可以在这里面加入预处理的命令
.globl thread_switch
thread_switch:
/* YOUR CODE HERE */
// C语言函数调用会压入下一条语句的 eip.
pushal
// eax 指向 sp
movl current_thread, %eax
// save sp
movl %esp, (%eax)
movl next_thread, %eax
movl %eax, current_thread
// restore sp
movl (%eax), %esp
popal
movl $0x0, next_thread
// popal 后esp指向return address
ret /* pop return address from stack */
/* pop return address from stack */