Пример #1
0
  /**
   * Relinquish the CPU, because the current thread has either finished or it is blocked. This
   * thread must be the current thread.
   *
   * <p>If the current thread is blocked (on a synchronization primitive, i.e. a <tt>Semaphore</tt>,
   * <tt>Lock</tt>, or <tt>Condition</tt>), eventually some thread will wake this thread up, putting
   * it back on the ready queue so that it can be rescheduled. Otherwise, <tt>finish()</tt> should
   * have scheduled this thread to be destroyed by the next thread to run.
   */
  public static void sleep() {
    Lib.debug(dbgThread, "Sleeping thread: " + currentThread.toString());

    Lib.assertTrue(Machine.interrupt().disabled());

    if (currentThread.status != statusFinished) currentThread.status = statusBlocked;

    runNextThread();
  }
Пример #2
0
  /**
   * Finish the current thread and schedule it to be destroyed when it is safe to do so. This method
   * is automatically called when a thread's <tt>run</tt> method returns, but it may also be called
   * directly.
   *
   * <p>The current thread cannot be immediately destroyed because its stack and other execution
   * state are still in use. Instead, this thread will be destroyed automatically by the next thread
   * to run, when it is safe to delete this thread.
   */
  public static void finish() {
    Lib.debug(dbgThread, "Finishing thread: " + currentThread.toString());

    Machine.interrupt().disable();

    Machine.autoGrader().finishingCurrentThread();

    Lib.assertTrue(toBeDestroyed == null);
    toBeDestroyed = currentThread;

    currentThread.status = statusFinished;

    // When this thread is finished, we need to reset the status of all
    // waited/blocked thread to ready state.
    while (!currentThread.waitedThreadList.isEmpty()) {
      KThread threadToRestore = currentThread.waitedThreadList.remove(0);
      threadToRestore.ready();
    }

    sleep();
  }