XV6学习(11)Lab thread: Multithreading

代码放在github上。

这一次实验感觉挺简单的,特别是后面两个小实验。主要就是对多线程和锁进行一个学习。

Uthread: switching between threads

这一个实验是要实现一个简单的用户级线程,写完之后发现原来用户级线程的简单实现也没有想象的那么复杂。

首先定义一个context结构体保存线程上下文,并加入到thread结构体中。在上下文中只需要保存被调用者保存的寄存器,即sps0-s11ra用来保存线程的返回地址,类似于进程中的pc

struct thread_context{
  uint64     ra;
  uint64     sp;
  uint64     fp; // 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 */
  struct thread_context context; /* context of thread */
};

之后在thread_create中加入初始化代码,使ra指向线程的入口函数,spfp指向栈底。注意栈底应该是t->stack[STACK_SIZE - 1],因为栈是从高地址向低地址增长的。

void 
thread_create(void (*func)())
{
  ...
  // YOUR CODE HERE
  t->context.ra = (uint64)func;
  t->context.sp = (uint64)&t->stack[STACK_SIZE - 1];
  t->context.fp = (uint64)&t->stack[STACK_SIZE - 1];
}

最后实现thread_switch函数并在thread_schedule中通过thread_switch((uint64)&t->context, (uint64)&next_thread->context);调用即可。thread_switch需要对上下文进行保护和恢复,并通过设置ra寄存器和ret指令来恢复下一个线程的执行。

thread_switch:
	/* YOUR CODE HERE */
	sd ra, 0(a0)

	sd sp, 8(a0)
	sd fp, 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 sp, 8(a1)
	ld fp, 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)

	ld ra, 0(a1) /* set return address to next thread */
	ret    /* return to ra */

Using threads

这一个实验是通过对哈希表的并行操作来练习锁的使用。代码就只放桶级锁的。

因为测试程序是将put和get操作进行了分离的,因此只需要考虑put操作之间的互斥。在put函数读写bucket之前加锁,在函数结束时释放锁。

pthread_mutex_t lock[NBUCKET]; // 定义锁

static 
void put(int key, int value)
{
  int i = key % NBUCKET;

  // is the key already present?
  struct entry *e = 0;
  pthread_mutex_lock(&lock[i]); // 获取锁
  for (e = table[i]; e != 0; e = e->next) {
    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]); // 释放锁
}

int
main(int argc, char *argv[])
{
  ...
  // 初始化锁
  for (int i = 0; i < NBUCKET; i++) {
    pthread_mutex_init(&lock[i], NULL);
  }
  ...
}

表级锁的结果如下:

$ ./ph 1
100000 puts, 7.336 seconds, 13631 puts/second
0: 0 keys missing
100000 gets, 7.599 seconds, 13160 gets/second

$ ./ph 2
100000 puts, 8.965 seconds, 11155 puts/second
1: 0 keys missing
0: 0 keys missing
200000 gets, 7.397 seconds, 27036 gets/second

可以看出表级锁多线程的性能甚至比单线程要低,这是因为表级锁将所有的操作都串行化了,无法利用多线程的性能,而多线程的初始化和切换以及锁的获取和释放本身也会带来一定的性能开销。

桶级锁的结果如下:

$ ./ph 1
100000 puts, 7.429 seconds, 13461 puts/second
0: 0 keys missing
100000 gets, 7.242 seconds, 13809 gets/second

$ ./ph 2
100000 puts, 4.472 seconds, 22359 puts/second
0: 0 keys missing
1: 0 keys missing
200000 gets, 7.347 seconds, 27221 gets/second

可以看出在使用桶级锁的情况下,多线程能够带来一定的加速,因为桶级锁是允许不同桶之间的操作并行执行的,从而能够利用多线程的优势。

Barrier

这一个实验是要实现一个屏障点,使所有线程都到达这个点之后才能继续执行。主要就是练习POSIX的条件变量的使用。

只需要实现一个barrier函数即可。函数实现也没有什么多说的,就是加锁然后判断到达屏障点的线程数,如果所有线程都到达了就调用pthread_cond_broadcast唤醒其他线程,否则就调用pthread_cond_wait进行等待。

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(&bstate.barrier_cond, &bstate.barrier_mutex);
  }

  pthread_mutex_unlock(&bstate.barrier_mutex);
}
posted @ 2021-02-05 21:24  星見遥  阅读(2823)  评论(1编辑  收藏  举报