001/*
002 *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 *  This file is licensed to You under the Eclipse Public License (EPL);
005 *  You may not use this file except in compliance with the License. You
006 *  may obtain a copy of the License at
007 *
008 *      http://www.opensource.org/licenses/eclipse-1.0.php
009 *
010 *  See the COPYRIGHT.txt file distributed with this work for information
011 *  regarding copyright ownership.
012 */
013package org.jikesrvm.mm.mminterface;
014
015import org.jikesrvm.mm.mmtk.ScanThread;
016import org.jikesrvm.scheduler.SystemThread;
017import org.mmtk.plan.CollectorContext;
018import org.vmmagic.pragma.BaselineNoRegisters;
019import org.vmmagic.pragma.BaselineSaveLSRegisters;
020import org.vmmagic.pragma.NoOptCompile;
021import org.vmmagic.pragma.NonMoving;
022import org.vmmagic.pragma.Uninterruptible;
023import org.vmmagic.pragma.Unpreemptible;
024
025/**
026 * System thread used to perform garbage collection work.
027 */
028@NonMoving
029public final class CollectorThread extends SystemThread {
030
031  /***********************************************************************
032   *
033   * Class variables
034   */
035
036  /***********************************************************************
037   *
038   * Instance variables
039   */
040
041  /** used by collector threads to hold state during stack scanning */
042  private final ScanThread threadScanner = new ScanThread();
043
044  /** @return the thread scanner instance associated with this instance */
045  @Uninterruptible
046  public ScanThread getThreadScanner() {
047    return threadScanner;
048  }
049
050  /***********************************************************************
051   *
052   * Initialization
053   */
054
055  /**
056   * @param stack The stack this thread will run on
057   * @param context the context that will provide the thread's
058   *  functionality
059   */
060  public CollectorThread(byte[] stack, CollectorContext context) {
061    super(stack, context.getClass().getName() + " [" + nextId + "]");
062    rvmThread.collectorContext = context;
063    rvmThread.collectorContext.initCollector(nextId);
064    nextId++;
065  }
066
067  /** Next collector thread id. Collector threads are not created concurrently. */
068  private static int nextId = 0;
069
070  /**
071   * Collection entry point. Delegates the real work to MMTk.
072   */
073  @Override
074  @NoOptCompile
075  // refs stored in registers by opt compiler will not be relocated by GC
076  @BaselineNoRegisters
077  // refs stored in registers by baseline compiler will not be relocated by GC, so use stack only
078  @BaselineSaveLSRegisters
079  // and store all registers from previous method in prologue, so that we can stack access them while scanning this thread.
080  @Unpreemptible
081  public void run() {
082    rvmThread.collectorContext.run();
083  }
084}
085