此次lab要实现用户级线程
前置知识是要了解xv6的进程调度
Uthread: switching between threads
需要在线程的调度 和创建中填上缺少的代码
另外 还要在线程切换context时添加汇编代码
了解了xv6怎么切换线程的 就非常容易
struct con {
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;
};
先在结构体中添加保存 callee-save registers
struct thread {
char stack[STACK_SIZE]; /* the thread's stack */
int state; /* FREE, RUNNING, RUNNABLE */
struct con context;
};
别忘了嵌入汇编
extern void thread_switch(struct con*, struct con*);
在调用线程切换函数时转换上下文
void
thread_schedule(void)
{
struct thread *t, *next_thread;
/* Find another runnable thread. */
next_thread = 0;
t = current_thread + 1;
for(int i = 0; i < MAX_THREAD; i++){
if(t >= all_thread + MAX_THREAD)
t = all_thread;
if(t->state == RUNNABLE) {
next_thread = t;
break;
}
t = t + 1;
}
if (next_thread == 0) {
printf("thread_schedule: no runnable threads\n");
exit(-1);
}
if (current_thread != next_thread) { /* switch threads? */
next_thread->state = RUNNING;
t = current_thread;
current_thread = next_thread;
/* YOUR CODE HERE
* Invoke thread_switch to switch from t to next_thread:
* thread_switch(??, ??);
*/
thread_switch(&t->context,¤t_thread->context);
} else
next_thread = 0;
}
最难的一点就是线程创建函数
了解One goal is ensure that when thread_schedule() runs a given thread for the first time, the thread executes the function passed to thread_create(), on its own stack. 这句话
void
thread_create(void (*func)())
{
struct thread *t;
for (t = all_thread; t < all_thread + MAX_THREAD; t++) {
if (t->state == FREE) break;
}
t->state = RUNNABLE;
// YOUR CODE HERE
t->context.ra=(uint64)func;
t->context.sp=(uint64)&t->stack[STACK_SIZE-1];
}
最后就是修改汇编文件
修改内容xv6的swith完全一致
Using threads
这部分实验是使用真实的线程库来编写线程代码
首先要了解 UNIX pthread threading library
然后阅读ph.c的代码
本实验的任务就是使ph.c成功的运行多线程代码 并行提高速度
直接为每个BUCKET分配一个锁就能满足要求
pthread_mutex_t locks[NBUCKET];
static
void put(int key, int value)
{
int i = key % NBUCKET;
// is the key already present?
struct entry *e = 0;
for (e = table[i]; e != 0; e = e->next) {
if (e->key == key)
break;
}
pthread_mutex_lock(&locks[i]);
if(e){
// update the existing key.
e->value = value;
} else {
// the new is new.
insert(key, value, &table[i], table[i]);
}
pthread_mutex_unlock(&locks[i]);
}
int
main(int argc, char *argv[])
{
pthread_t *tha;
void *value;
double t1, t0;
for(int i=0;i<NBUCKET;i++)
{
pthread_mutex_init(&locks[i], NULL);
}
if (argc < 2) {
fprintf(stderr, "Usage: %s nthreads\n", argv[0]);
exit(-1);
}
nthread = atoi(argv[1]);
tha = malloc(sizeof(pthread_t) * nthread);
srandom(0);
assert(NKEYS % nthread == 0);
for (int i = 0; i < NKEYS; i++) {
keys[i] = random();
}
Barrier
是一个简单的条件变量的运用
直接上代码
但一定要明白每个语句的顺序
static void
barrier()
{
// YOUR CODE HERE
//
// Block until all threads have called barrier() and
// then increment bstate.round.
//
pthread_mutex_lock(&bstate.barrier_mutex);
bstate.nthread++;
if(bstate.nthread<nthread)
pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex);
else
{
bstate.round++;
bstate.nthread=0;
pthread_cond_broadcast(&bstate.barrier_cond);
}
pthread_mutex_unlock(&bstate.barrier_mutex);
}