001 /*
002 * This file is part of the Jikes RVM project (http://jikesrvm.org).
003 *
004 * This file is licensed to You under the Common Public License (CPL);
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/cpl1.0.php
009 *
010 * See the COPYRIGHT.txt file distributed with this work for information
011 * regarding copyright ownership.
012 */
013 package org.jikesrvm.mm.mmtk;
014
015 import org.mmtk.plan.Plan;
016 import org.mmtk.plan.CollectorContext;
017 import org.mmtk.plan.MutatorContext;
018 import org.mmtk.plan.PlanConstraints;
019 import org.mmtk.utility.Log;
020
021 import org.jikesrvm.mm.mminterface.Selected;
022
023 import org.vmmagic.pragma.*;
024
025 /**
026 * This class contains interfaces to access the current plan, plan local and
027 * plan constraints instances.
028 */
029 @Uninterruptible public final class ActivePlan extends org.mmtk.vm.ActivePlan {
030
031 /* Collector and Mutator Context Management */
032 private static final int MAX_CONTEXTS = 100;
033 private static Selected.Collector[] collectors = new Selected.Collector[MAX_CONTEXTS];
034 private static int collectorCount = 0; // Number of collector instances
035 private static Selected.Mutator[] mutators = new Selected.Mutator[MAX_CONTEXTS];
036 private static int mutatorCount = 0; // Number of mutator instances
037 private static SynchronizedCounter mutatorCounter = new SynchronizedCounter();
038
039 /** @return The active Plan instance. */
040 @Inline
041 public Plan global() {
042 return Selected.Plan.get();
043 }
044
045 /** @return The active PlanConstraints instance. */
046 @Inline
047 public PlanConstraints constraints() {
048 return Selected.Constraints.get();
049 }
050
051 /** @return The active CollectorContext instance. */
052 @Inline
053 public CollectorContext collector() {
054 return Selected.Collector.get();
055 }
056
057 /** @return The active MutatorContext instance. */
058 @Inline
059 public MutatorContext mutator() {
060 return Selected.Mutator.get();
061 }
062
063 /** @return The log for the active thread */
064 public Log log() {
065 return Selected.Mutator.get().getLog();
066 }
067
068 /** Flush the mutator remembered sets (if any) for this active plan */
069 public static void flushRememberedSets() {
070 Selected.Mutator.get().flushRememberedSets();
071 }
072
073 /**
074 * Return the CollectorContext instance given its unique identifier.
075 *
076 * @param id The identifier of the CollectorContext to return
077 * @return The specified CollectorContext
078 */
079 @Inline
080 public CollectorContext collector(int id) {
081 return collectors[id];
082 }
083
084 /**
085 * Return the MutatorContext instance given its unique identifier.
086 *
087 * @param id The identifier of the MutatorContext to return
088 * @return The specified MutatorContext
089 */
090 @Inline
091 public MutatorContext mutator(int id) {
092 return mutators[id];
093 }
094
095 /**
096 * Return the Selected.Collector instance given its unique identifier.
097 *
098 * @param id The identifier of the Selected.Collector to return
099 * @return The specified Selected.Collector
100 */
101 @Inline
102 public Selected.Collector selectedCollector(int id) {
103 return collectors[id];
104 }
105 /**
106 * Return the Selected.Mutator instance given its unique identifier.
107 *
108 * @param id The identifier of the Selected.Mutator to return
109 * @return The specified Selected.Mutator
110 */
111 @Inline
112 public Selected.Mutator selectedMutator(int id) {
113 return mutators[id];
114 }
115
116
117 /** @return The number of registered CollectorContext instances. */
118 @Inline
119 public int collectorCount() {
120 return collectorCount;
121 }
122
123 /** @return The number of registered MutatorContext instances. */
124 public int mutatorCount() {
125 return mutatorCount;
126 }
127
128 /** Reset the mutator iterator */
129 public void resetMutatorIterator() {
130 mutatorCounter.reset();
131 }
132
133 /**
134 * Return the next <code>MutatorContext</code> in a
135 * synchronized iteration of all mutators.
136 *
137 * @return The next <code>MutatorContext</code> in a
138 * synchronized iteration of all mutators, or
139 * <code>null</code> when all mutators have been done.
140 */
141 public MutatorContext getNextMutator() {
142 int id = mutatorCounter.increment();
143 return id >= mutatorCount ? null : mutators[id];
144 }
145
146 /**
147 * Register a new CollectorContext instance.
148 *
149 * @param collector The CollectorContext to register
150 * @return The CollectorContext's unique identifier
151 */
152 @Interruptible
153 public synchronized int registerCollector(CollectorContext collector) {
154 collectors[collectorCount] = (Selected.Collector) collector;
155 return collectorCount++;
156 }
157
158 /**
159 * Register a new MutatorContext instance.
160 *
161 * @param mutator The MutatorContext to register
162 * @return The MutatorContext's unique identifier
163 */
164 @Interruptible
165 public synchronized int registerMutator(MutatorContext mutator) {
166 mutators[mutatorCount] = (Selected.Mutator) mutator;
167 return mutatorCount++;
168 }
169 }