1 /* 2 * Copyright (c) 2018, 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_OOPSTORAGEPARSTATE_HPP 26 #define SHARE_GC_SHARED_OOPSTORAGEPARSTATE_HPP 27 28 #include "gc/shared/oopStorage.hpp" 29 #include "utilities/macros.hpp" 30 31 #if INCLUDE_ALL_GCS 32 33 ////////////////////////////////////////////////////////////////////////////// 34 // Support for parallel and optionally concurrent state iteration. 35 // 36 // Parallel iteration is for the exclusive use of the GC. Other iteration 37 // clients must use serial iteration. 38 // 39 // Concurrent Iteration 40 // 41 // Iteration involves the _active_list, which contains all of the blocks owned 42 // by a storage object. This is a doubly-linked list, linked through 43 // dedicated fields in the blocks. 44 // 45 // At most one concurrent ParState can exist at a time for a given storage 46 // object. 47 // 48 // A concurrent ParState sets the associated storage's 49 // _concurrent_iteration_active flag true when the state is constructed, and 50 // sets it false when the state is destroyed. These assignments are made with 51 // _active_mutex locked. Meanwhile, empty block deletion is not done while 52 // _concurrent_iteration_active is true. The flag check and the dependent 53 // removal of a block from the _active_list is performed with _active_mutex 54 // locked. This prevents concurrent iteration and empty block deletion from 55 // interfering with with each other. 56 // 57 // Both allocate() and delete_empty_blocks_concurrent() lock the 58 // _allocate_mutex while performing their respective list manipulations, 59 // preventing them from interfering with each other. 60 // 61 // When allocate() creates a new block, it is added to the front of the 62 // _active_list. Then _active_head is set to the new block. When concurrent 63 // iteration is started (by a parallel worker thread calling the state's 64 // iterate() function), the current _active_head is used as the initial block 65 // for the iteration, with iteration proceeding down the list headed by that 66 // block. 67 // 68 // As a result, the list over which concurrent iteration operates is stable. 69 // However, once the iteration is started, later allocations may add blocks to 70 // the front of the list that won't be examined by the iteration. And while 71 // the list is stable, concurrent allocate() and release() operations may 72 // change the set of allocated entries in a block at any time during the 73 // iteration. 74 // 75 // As a result, a concurrent iteration handler must accept that some 76 // allocations and releases that occur after the iteration started will not be 77 // seen by the iteration. Further, some may overlap examination by the 78 // iteration. To help with this, allocate() and release() have an invariant 79 // that an entry's value must be NULL when it is not in use. 80 // 81 // An in-progress delete_empty_blocks_concurrent() operation can contend with 82 // the start of a concurrent iteration over the _active_mutex. Since both are 83 // under GC control, that potential contention can be eliminated by never 84 // scheduling both operations to run at the same time. 85 // 86 // ParState<concurrent, is_const> 87 // concurrent must be true if iteration is concurrent with the 88 // mutator, false if iteration is at a safepoint. 89 // 90 // is_const must be true if the iteration is over a constant storage 91 // object, false if the iteration may modify the storage object. 92 // 93 // ParState([const] OopStorage* storage) 94 // Construct an object for managing an iteration over storage. For a 95 // concurrent ParState, empty block deletion for the associated storage 96 // is inhibited for the life of the ParState. There can be no more 97 // than one live concurrent ParState at a time for a given storage object. 98 // 99 // template<typename F> void iterate(F f) 100 // Repeatedly claims a block from the associated storage that has 101 // not been processed by this iteration (possibly by other threads), 102 // and applies f to each entry in the claimed block. Assume p is of 103 // type const oop* or oop*, according to is_const. Then f(p) must be 104 // a valid expression whose value is ignored. Concurrent uses must 105 // be prepared for an entry's value to change at any time, due to 106 // mutator activity. 107 // 108 // template<typename Closure> void oops_do(Closure* cl) 109 // Wrapper around iterate, providing an adaptation layer allowing 110 // the use of OopClosures and similar objects for iteration. Assume 111 // p is of type const oop* or oop*, according to is_const. Then 112 // cl->do_oop(p) must be a valid expression whose value is ignored. 113 // Concurrent uses must be prepared for the entry's value to change 114 // at any time, due to mutator activity. 115 // 116 // Optional operations, provided only if !concurrent && !is_const. 117 // These are not provided when is_const, because the storage object 118 // may be modified by the iteration infrastructure, even if the 119 // provided closure doesn't modify the storage object. These are not 120 // provided when concurrent because any pre-filtering behavior by the 121 // iteration infrastructure is inappropriate for concurrent iteration; 122 // modifications of the storage by the mutator could result in the 123 // pre-filtering being applied (successfully or not) to objects that 124 // are unrelated to what the closure finds in the entry. 125 // 126 // template<typename Closure> void weak_oops_do(Closure* cl) 127 // template<typename IsAliveClosure, typename Closure> 128 // void weak_oops_do(IsAliveClosure* is_alive, Closure* cl) 129 // Wrappers around iterate, providing an adaptation layer allowing 130 // the use of is-alive closures and OopClosures for iteration. 131 // Assume p is of type oop*. Then 132 // 133 // - cl->do_oop(p) must be a valid expression whose value is ignored. 134 // 135 // - is_alive->do_object_b(*p) must be a valid expression whose value 136 // is convertible to bool. 137 // 138 // If *p == NULL then neither is_alive nor cl will be invoked for p. 139 // If is_alive->do_object_b(*p) is false, then cl will not be 140 // invoked on p. 141 142 class OopStorage::BasicParState { 143 OopStorage* _storage; 144 void* volatile _next_block; 145 bool _concurrent; 146 147 // Noncopyable. 148 BasicParState(const BasicParState&); 149 BasicParState& operator=(const BasicParState&); 150 151 void update_iteration_state(bool value); 152 void ensure_iteration_started(); 153 Block* claim_next_block(); 154 155 // Wrapper for iteration handler; ignore handler result and return true. 156 template<typename F> class AlwaysTrueFn; 157 158 public: 159 BasicParState(OopStorage* storage, bool concurrent); 160 ~BasicParState(); 161 162 template<bool is_const, typename F> void iterate(F f); 163 }; 164 165 template<bool concurrent, bool is_const> 166 class OopStorage::ParState { 167 BasicParState _basic_state; 168 169 public: 170 ParState(const OopStorage* storage) : 171 // For simplicity, always recorded as non-const. 172 _basic_state(const_cast<OopStorage*>(storage), concurrent) 173 {} 174 175 template<typename F> void iterate(F f); 176 template<typename Closure> void oops_do(Closure* cl); 177 }; 178 179 template<> 180 class OopStorage::ParState<false, false> { 181 BasicParState _basic_state; 182 183 public: 184 ParState(OopStorage* storage) : 185 _basic_state(storage, false) 186 {} 187 188 template<typename F> void iterate(F f); 189 template<typename Closure> void oops_do(Closure* cl); 190 template<typename Closure> void weak_oops_do(Closure* cl); 191 template<typename IsAliveClosure, typename Closure> 192 void weak_oops_do(IsAliveClosure* is_alive, Closure* cl); 193 }; 194 195 #endif // INCLUDE_ALL_GCS 196 197 #endif // SHARE_GC_SHARED_OOPSTORAGEPARSTATE_HPP