pintos (1) -- Alarm Clock

实现非阻塞的timer_sleep()函数。

线程调用timer_sleep()后主动block,并设置blockticks,每次时钟中断blockticks减1,待其减为0时unblock


struct thread 添加属性:

long long blockticks;    /* Ticks to be block.*/

同时在init_thread()中对其进行初始化:

t->blockticks = 0;

timer.c修改timer_sleep()函数:

/* Sleeps for approximately TICKS timer ticks.
 * Interrupts must be turned on.
 */
void
timer_sleep (int64_t ticks) 
{
    if(ticks <= 0){
        return;
    }
    enum intr_level old_level = intr_disable ();/* Disable interrupt.*/
    thread_current()->blockticks = ticks;       /* Set block ticks.*/
    thread_block();                             /* Block current thread.*/
    intr_set_level (old_level);                 /* Enable interrupt.*/

}

thread.c添加thread_dec_blockticks()函数:

/*
 * DEC a thread's block ticks
 * and awake thread if block ticks over.
 * This function must be called with interrupts off.
 */
void thread_dec_blockticks(struct thread *t, void *aux){
    if(t->status == THREAD_BLOCKED){
        if(t->blockticks == 1){
            /* It's time to unblock.
             * 0 means the thread no set block ticks.
             * So here use 1 to judge.
             */
            thread_unblock(t);
        }
        /*
         * DEC the block ticks;
         */
        if(t->blockticks != 0)
            t->blockticks--;
    }
}

同时在thread_tick()函数中使用thread_foreach()调用thread_dec_blockticks()

  /* Find if a thread need to be awake from block
   * by block ticks over.
   */
  enum intr_level old_level = intr_disable ();
  thread_foreach(thread_dec_blockticks, NULL);
  intr_set_level (old_level);

通过测试:alarm-single alarm-multiple alarm-simultaneous alarm-zero alarm-negative

  • 1
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值