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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章