当前位置: 首页 > 工具软件 > Alarm Clock > 使用案例 >

pintos (1) -- Alarm Clock

方航
2023-12-01

实现非阻塞的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

 类似资料: