1 /*
  2  * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  *
 23  */
 24 
 25 #ifndef SHARE_GC_SHARED_WEAKPROCESSORPHASES_HPP
 26 #define SHARE_GC_SHARED_WEAKPROCESSORPHASES_HPP
 27 
 28 #include "gc/shared/oopStorageSet.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "utilities/globalDefinitions.hpp"
 31 #include "utilities/macros.hpp"
 32 
 33 class BoolObjectClosure;
 34 class OopClosure;
 35 class OopStorage;
 36 
 37 class WeakProcessorPhases : AllStatic {
 38 public:
 39   class Iterator;
 40 
 41   typedef void (*Processor)(BoolObjectClosure*, OopClosure*);
 42 
 43   enum Phase {
 44     // Serial phases.
 45     JVMTI_ONLY(jvmti JFR_ONLY(COMMA))
 46     JFR_ONLY(jfr TSAN_ONLY(COMMA))
 47     TSAN_ONLY(tsan)
 48 
 49     // Additional implicit phase values follow for oopstorages.
 50   };
 51 
 52   static const uint serial_phase_start = 0;
 53   static const uint serial_phase_count = 0 JVMTI_ONLY(+ 1) JFR_ONLY(+ 1) TSAN_ONLY(+ 1);
 54   static const uint oopstorage_phase_start = serial_phase_count;
 55   static const uint oopstorage_phase_count = OopStorageSet::weak_count;
 56   static const uint phase_count = serial_phase_count + oopstorage_phase_count;
 57 
 58   // Precondition: value < serial_phase_count
 59   static Phase serial_phase(uint value);
 60 
 61   // Precondition: value < oopstorage_phase_count
 62   static Phase oopstorage_phase(uint value);
 63 
 64   // Indexes relative to the corresponding phase_start constant.
 65   // Precondition: is_serial(phase) or is_oopstorage(phase) accordingly
 66   static uint serial_index(Phase phase);
 67   static uint oopstorage_index(Phase phase);
 68 
 69   static bool is_serial(Phase phase);
 70   static bool is_oopstorage(Phase phase);
 71 
 72   static Iterator serial_iterator();
 73   static Iterator oopstorage_iterator();
 74 
 75   // Precondition: is_serial(phase)
 76   static const char* description(Phase phase);
 77 
 78   // Precondition: is_serial(phase)
 79   static Processor processor(Phase phase);
 80 };
 81 
 82 typedef WeakProcessorPhases::Phase WeakProcessorPhase;
 83 
 84 class WeakProcessorPhases::Iterator {
 85   friend class WeakProcessorPhases;
 86 
 87   uint _index;
 88   uint _limit;
 89 
 90   Iterator(uint index, uint limit) : _index(index), _limit(limit) {}
 91 
 92   static const uint singular_value = UINT_MAX;
 93   void verify_nonsingular() const NOT_DEBUG_RETURN;
 94   void verify_category_match(const Iterator& other) const NOT_DEBUG_RETURN;
 95   void verify_dereferenceable() const NOT_DEBUG_RETURN;
 96 
 97 public:
 98   // Construct a singular iterator for later assignment.  The only valid
 99   // operations are destruction and assignment.
100   Iterator() : _index(singular_value), _limit(singular_value) {}
101 
102   bool is_end() const {
103     verify_nonsingular();
104     return _index == _limit;
105   }
106 
107   bool operator==(const Iterator& other) const {
108     verify_category_match(other);
109     return _index == other._index;
110   }
111 
112   bool operator!=(const Iterator& other) const {
113     return !operator==(other);
114   }
115 
116   Phase operator*() const {
117     verify_dereferenceable();
118     return static_cast<Phase>(_index);
119   }
120 
121   // Phase doesn't have members, so no operator->().
122 
123   Iterator& operator++() {
124     verify_dereferenceable();
125     ++_index;
126     return *this;
127   }
128 
129   Iterator operator++(int) {
130     verify_dereferenceable();
131     return Iterator(_index++, _limit);
132   }
133 
134   Iterator begin() const {
135     verify_nonsingular();
136     return *this;
137   }
138 
139   Iterator end() const {
140     verify_nonsingular();
141     return Iterator(_limit, _limit);
142   }
143 };
144 
145 inline WeakProcessorPhases::Iterator WeakProcessorPhases::serial_iterator() {
146   return Iterator(serial_phase_start, serial_phase_start + serial_phase_count);
147 }
148 
149 inline WeakProcessorPhases::Iterator WeakProcessorPhases::oopstorage_iterator() {
150   return Iterator(oopstorage_phase_start, oopstorage_phase_start + oopstorage_phase_count);
151 }
152 
153 #endif // SHARE_GC_SHARED_WEAKPROCESSORPHASES_HPP