MIT 6s081 lab7:Multithreading

Thread switching

作业地址:Lab: Multithreading (mit.edu)

lab7:Multithreading

Uthread: switching between threads (moderate)

实现一个用户态的线程库

补全 uthread.c,完成用户态线程功能的实现。

这个实验其实相当于在用户态重新实现一遍 xv6 kernel 中的 scheduler() 和 swtch() 的功能,所以大多数代码都是可以借鉴的。

// uthread_switch.S
	.text

	/*
		 * save the old thread's registers,
		 * restore the new thread's registers.
		 */

	.globl thread_switch
thread_switch:
	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 */

在调用本函数 uthread_switch() 的过程中,caller-saved registers 已经被调用者保存到栈帧中了,所以这里无需保存这一部分寄存器。

从 proc.h 中借鉴一下 context 结构体,用于保存 ra、sp 以及 callee-saved registers:

struct u_context
{
  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;
};

struct thread {
  char       stack[STACK_SIZE]; /* the thread's stack */
  int        state;             /* FREE, RUNNING, RUNNABLE */

  int id; // for bebug
  struct u_context context;   // 需要保存cpu的一些寄存器,用来保护现场和恢复现场使用,需要保存哪些寄存器?

};

在 thread_schedule 中调用 thread_switch 进行上下文切换:

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((uint64)(&t->context), (uint64)(&next_thread->context)); //从这里返回,保护当前线程context,加载下一个线程的context

  } else
    next_thread = 0;
}

再补齐 thread_create:

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
  memset(&t->context, 0, sizeof(t->context));
  t->context.ra = (uint64)func; // 设置ra为线程函数的地址,使得第一次调度时的返回地址是在线程处理函数
  
  t->context.sp = (uint64)(t->stack) + STACK_SIZE - 1; // 修改栈的位置=>这个是个大坑,栈是向下增长的,所以要设置在高地址位置

  static int i = 1;
  t->id = i++;
}

Using threads (moderate)

解决race-condition导致的哈希表插入键值丢失问题

为每个bucket分配一个锁,在对每个bucket进行插入操作(put)时加入互斥锁保护

pthread_mutex_t lock[NBUCKET]; // 声明,每个桶一个锁
// 在main函数中对锁进行初始化
// 初始化锁
  for(int i = 0; i < NBUCKET; i++) {
    pthread_mutex_init(&lock[i], NULL);
  }
// 在put操作中使用互斥锁保护
static 
void put(int key, int value)
{
  int i = key % NBUCKET; // 

  // 加锁
  pthread_mutex_lock(&lock[i]);

  // is the key already present?
  struct entry *e = 0;
  for (e = table[i]; e != 0; e = e->next) { //On遍历当前链上的所有节点,判断是否key重复
    if (e->key == key)
      break;
  }
  if(e){ // 有重复
    // update the existing key.
    e->value = value;
  } else {
    // the new is new.
    insert(key, value, &table[i], table[i]); // 头插
  }

  // 释放锁
  pthread_mutex_unlock(&lock[i]);
}

Barrier(moderate)

利用 pthread 提供的条件变量方法,实现同步屏障机制。

修改barrier函数。

当线程进入barrier时,将已进入屏障的线程数+1,然后判断是否全部到达,如果未到达,则休眠等待,如果已经到达,就唤醒所有在等待的线程,所有线程继续运行。

如果不加锁保护,有可能会出现的情况是:线程1即将睡眠前,线程2调用了唤醒,然后线程1才进入睡眠,这会导致线程1本应该被唤醒然而却没有唤醒。

static void 
barrier()
{
  // YOUR CODE HERE
  //
  // Block until all threads have called barrier() and
  // then increment bstate.round.
  //
  // 必须在这里等待剩下的所有线程都调用了barrier,才能返回,并且递增bstate.round
  // 最简单的:维护调用barrier的线程数,当线程数等于总的线程数,则返回
  
  pthread_mutex_lock(&bstate.barrier_mutex); //先上锁
  bstate.nthread++; //到达此处的线程数+1
  if(bstate.nthread < nthread) { //还有一些线程没到达
    pthread_cond_wait(&bstate.barrier_cond, &bstate.barrier_mutex); // 等待条件成立,在这里阻塞掉,阻塞的同时释放互斥锁,在阻塞结束后会重新获取锁
  }
  else{ //这是最后一个到达次数的线程
    bstate.nthread = 0; // 清空到达的线程数
    bstate.round++; // 轮次+1
    //发送信号
    pthread_cond_signal(&bstate.barrier_cond); //给之前因条件变量阻塞掉的线程发信号。
  }
  pthread_mutex_unlock(&bstate.barrier_mutex); // 释放互斥锁
}
  • 10
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值