ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

MIT 6.S081 操作系统 LAB7:Multithreading

2022-01-08 15:31:12  阅读:193  来源: 互联网

标签:uint64 ld thread a1 a0 LAB7 Multithreading S081 sd


Lab: Multithreading

Uthread: switching between threads

实现一个用户级线程,跟内核线程的切换没什么区别。只要上课听懂了,复制内核代码就行了
只贴关键代码

线程上下文

struct 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 */
  struct context context;
};

线程切换

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)&current_thread->context);
}

创建线程

设置好开始执行的位置和栈指针

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;
}

上下文切换

汇编代码,与内核一致
保存ra,sp,callee saved registers

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 */

Using threads

确定哈希表的线程安全
其实跟kalloc.c/freeList的意思是一样,上课听懂了就能理解
大致情况如下

在插入的时候加锁就行
性能上可以优化一下,变成每个桶一个锁,测试里能快个两秒左右吧

if(e){
    // update the existing key.
    e->value = value;
} else {
    // the new is new.
    pthread_mutex_lock(&locks[i]);
    insert(key, value, &table[i], table[i]);
    pthread_mutex_unlock(&locks[i]);
}

Barrier

线程间的屏障,可以参考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)
  {
    bstate.nthread=0;
    bstate.round++;
    pthread_cond_broadcast(&bstate.barrier_cond);
  }
  else
  {
    int cur_round=bstate.round;
    while(bstate.round==cur_round)
      pthread_cond_wait(&bstate.barrier_cond , &bstate.barrier_mutex);
  }
  pthread_mutex_unlock(&bstate.barrier_mutex);
}

为了不被偷袭,防了一手虚假唤醒,其实不防也没事

test

标签:uint64,ld,thread,a1,a0,LAB7,Multithreading,S081,sd
来源: https://www.cnblogs.com/traver/p/15778379.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有