Uthread: switching between threads
本实验是在给定的代码基础上实现用户级线程切换,相比于XV6中实现的内核级线程,这个要简单许多。因为是用户级线程,不需要设计用户栈和内核栈,用户页表和内核页表等等切换,所以本实验中只需要一个类似于context
的结构,而不需要费尽心机的维护trapframe
(1). 定义存储上下文的结构体tcontext
// 用户线程的上下文结构体
struct tcontext {
uint64 ra;
uint64 sp;
// callee-saved
uint64 s0;
uint64 s1;
uint64 s2;
uint64 s3;
uint64 s4;
uint64 s5;
uint64 s6;
uint64 s7;
uint64 s8;
uint64 s9;
uint64 s10;
uint64 s11;
};
(2). 修改thread
结构体,添加context
字段
struct thread {
char stack[STACK_SIZE]; /* the thread's stack */
int state; /* FREE, RUNNING, RUNNABLE */
struct tcontext context; /* 用户进程上下文 */
};
(3). 模仿kernel/swtch.S,在kernel/uthread_switch.S中写入如下代码
.text
/*
* save the old thread's registers,
* restore the new thread's registers.
*/
.globl thread_switch
thread_switch:
/* YOUR CODE HERE */
sd ra, 0(a0)
sd sp, 8(a0)
sd s0, 16(a0)
sd s1, 24(a0)
sd s2, 32(a0)
sd s3, 40(a0)
sd s4, 48(a0)
sd s5, 56(a0)
sd s6, 64(a0)
sd s7, 72(a0)
sd s8, 80(a0)
sd s9, 88(a0)
sd s10, 96(a0)
sd s11, 104(a0)
ld ra, 0(a1)
ld sp, 8(a1)
ld s0, 16(a1)
ld s1, 24(a1)
ld s2, 32(a1)
ld s3, 40(a1)
ld s4, 48(a1)
ld s5, 56(a1)
ld s6, 64(a1)
ld s7, 72(a1)
ld s8, 80(a1)
ld s9, 88(a1)
ld s10, 96(a1)
ld s11, 104(a1)
ret /* return to ra */
(4). 修改thread_scheduler
,添加线程切换语句
...
if (current_thread != next_thread) { /* switch threads? */
...
/* YOUR CODE HERE */
thread_switch((uint64)&t->context, (uint64)¤t_thread->context);
} else
next_thread = 0;
(5). 在thread_create
中对thread
结构体做一些初始化设定,主要是ra
返回地址和sp
栈指针,其他的都不重要
// YOUR CODE HERE
t->context.ra = (uint64)func; // 设定函数返回地址
t->context.sp = (uint64)t->stack + STACK_SIZE; // 设定栈指针
Using threads
首先是问为什么为造成数据丢失:
假设现在有两个线程T1和T2,两个线程都走到put函数,且假设两个线程中key%NBUCKET相等,即要插入同一个散列桶中。两个线程同时调用insert(key, value, &table[i], table[i]),insert是通过头插法实现的。如果先insert的线程还未返回另一个线程就开始insert,那么前面的数据会被覆盖
因此只需要对插入操作上锁即可
(1). 为每个散列桶定义一个锁,将五个锁放在一个数组中,并进行初始化
pthread_mutex_t lock[NBUCKET] = { PTHREAD_MUTEX_INITIALIZER }; // 每个散列桶一把锁
(2). 在put
函数中对insert
上锁
if(e){
// update the existing key.
e->value = value;
} else {
pthread_mutex_lock(&lock[i]);
// the new is new.
insert(key, value, &table[i], table[i]);
pthread_mutex_unlock(&lock[i]);
}
Barrier:
实验要求: 实现线程的同步,即每一个线程必须等待其他所有线程都到达barrier之后才能继续进行下面的操作,需要用到pthread_cond_wait(&cond, &mutex)
和pthread_cond_broadcast(&cond)
来进行线程的sleep和唤醒其他所有&cond
中睡眠的线程. 需要对barrier.c
中的barrier()
进行实现.
当每个线程调用了barrier()
之后,需要增加bstate.nthread
以表明到达当前round的线程数量增加了1, 但是由于bstate
这个数据结构是线程之间共享的, 因此需要用pthread_mutex_lock
对这个数据结构进行保护. 当bstate.nthread
的数量达到线程总数nthread
之后, 将bstate.round
加1. 注意, 一定要等到所有的线程都达到了这个round, 将bstate.nthread
清零之后才能将所有正在睡眠的线程唤醒, 否则如果先唤醒线程的话其他线程如果跑得很快, 在之前的线程将bstate.nthread
清零之前就调用了bstate.nthread++
,会出现问题(round之间是共用bstate.nthread
的)
static void
barrier()
{
// 申请持有锁
pthread_mutex_lock(&bstate.barrier_mutex);
bstate.nthread++;
if(bstate.nthread == nthread) {
// 所有线程已到达
bstate.round++;
bstate.nthread = 0;
pthread_cond_broadcast(&bstate.barrier_cond);
} else {
// 等待其他线程
// 调用pthread_cond_wait时,mutex必须已经持有
pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex);
}
// 释放锁
pthread_mutex_unlock(&bstate.barrier_mutex);
}