Esempio n. 1
0
 public void run() {
   long wakeTime = randomDelay();
   System.out.println(
       "** "
           + name
           + ": Sleeping for "
           + wakeTime
           + " (i.e., until time="
           + (wakeTime + Machine.timer().getTime())
           + ")");
   ThreadedKernel.alarm.waitUntil(wakeTime);
   System.out.println(name + " is Done Sleeping (time=" + Machine.timer().getTime() + ")");
 }
Esempio n. 2
0
 /**
  * Allocate a new Alarm. Set the machine's timer interrupt handler to this alarm's callback.
  *
  * <p><b>Note</b>: Nachos will not function correctly with more than one alarm.
  */
 public Alarm() {
   Machine.timer()
       .setInterruptHandler(
           new Runnable() {
             public void run() {
               timerInterrupt();
             }
           });
 }
Esempio n. 3
0
 public void run() {
   for (int i = 0; i < 5; i++) {
     System.out.println(
         "*** thread " + which + " looped " + i + " times, Tick:" + Machine.timer().getTime());
     if ((which == 1) && (i == 0)) ThreadedKernel.alarm.waitUntil(1000);
     if ((which == 1) && (i == 1)) dos.join();
     if ((which == 0) && (i == 2)) dos.join();
     if ((which == 2) && (i == 3)) tres.join();
     if ((which == 1) && (i == 3)) dos.join();
     currentThread.yield();
   }
 }
Esempio n. 4
0
  /**
   * Put the current thread to sleep for at least <i>x</i> ticks, waking it up in the timer
   * interrupt handler. The thread must be woken up (placed in the scheduler ready set) during the
   * first timer interrupt where
   *
   * <p>
   *
   * <blockquote>
   *
   * (current time) >= (WaitUntil called time)+(x)
   *
   * </blockquote>
   *
   * @param x the minimum number of clock ticks to wait.
   * @see nachos.machine.Timer#getTime()
   */
  public void waitUntil(long x) {
    // disable interrupts
    Machine.interrupt().disable(); // let's see if this works? DAC DEBUG
    // calculate wakeTime
    long wakeTime = Machine.timer().getTime() + x;
    // create AlarmBucket
    AlarmBucket Abuck = new AlarmBucket(wakeTime, KThread.currentThread());
    // add current thread (alarm bucket) to waitQueue
    waitQueue.add(Abuck);
    // put current thread to sleep
    KThread.currentThread().sleep();

    return;
  }
    /**
     * run() method for the RVThread. This method loops howMany times. During this loop the thread
     * waits some random amount of time, then speaks or listen, depending on whether this.isSpeaker
     * is sets to true or false.
     */
    public void run() {

      System.out.println("** " + name + " begins.");

      /* Main loop */
      for (int i = 0; i < howMany; i++) {
        /* Sleep for some random delay */
        int randomDelay = randomDelay();
        System.out.println(
            "** "
                + name
                + ": Sleeping for "
                + randomDelay
                + " (i.e., until time="
                + (randomDelay + Machine.timer().getTime())
                + ")");
        ThreadedKernel.alarm.waitUntil(randomDelay);
        System.out.println(
            "** " + name + ": Done sleeping! (time=" + Machine.timer().getTime() + ")");

        if (isSpeaker) {
          /* I am a speaker and I speak my word */
          int randomWord = randomWord();
          System.out.println(
              "** "
                  + name
                  + ": Speaking "
                  + randomWord
                  + " (time="
                  + Machine.timer().getTime()
                  + ")");
          comm.speak(randomWord);
          System.out.println(
              "** " + name + ": Spoke and Returned (time=" + Machine.timer().getTime() + ")");
        } else {
          /* I am a listener and I listen */
          System.out.println("** " + name + ": Listening (time=" + Machine.timer().getTime() + ")");
          int word = comm.listen();
          System.out.println(
              "** "
                  + name
                  + ": Listened and got "
                  + word
                  + " (time="
                  + Machine.timer().getTime()
                  + ")");
        }
      }

      /* Exits and signals it to the main thread */
      commFinished.speak(-1);
      System.out.println("** " + name + " exits.");
    }
Esempio n. 6
0
 /**
  * The timer interrupt handler. This is called by the machine's timer periodically (approximately
  * every 500 clock ticks). Causes the current thread to yield, forcing a context switch if there
  * is another thread that should be run.
  */
 public void timerInterrupt() {
   // disable interrupts (maybe not necessary)
   Machine.interrupt().disable(); // let's see if this works?
   // get current time
   long currentTime = Machine.timer().getTime();
   // go through waitQueue and put each thread that is "less than" current wait time on ready queue
   AlarmBucket placeholder = null;
   int count = waitQueue.size();
   while (count > 0) {
     placeholder = waitQueue.peek();
     if (placeholder.getTime() <= currentTime) {
       placeholder = waitQueue.poll();
       placeholder.getKThread().ready();
       count = waitQueue.size();
     } else {
       count = 0;
     }
   }
   // yield current thread
   KThread.currentThread().yield();
   // Machine.interrupt().enable(); //let's see if this works? DAC DEBUG
 }
 private void delay() {
   long time = Machine.timer().getTime();
   int amount = 1000;
   ThreadedKernel.alarm.waitUntil(amount);
   Lib.my_assert(Machine.timer().getTime() >= time + amount);
 }