1 /*
   2  * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP
  26 
  27 #include "code/codeCache.hpp"
  28 #include "gc/shenandoah/shenandoahSharedVariables.hpp"
  29 #include "gc/shenandoah/shenandoahLock.hpp"
  30 #include "gc/shenandoah/shenandoahNMethod.hpp"
  31 #include "memory/allocation.hpp"
  32 #include "memory/iterator.hpp"
  33 #include "utilities/globalDefinitions.hpp"
  34 
  35 class ShenandoahHeap;
  36 class ShenandoahHeapRegion;
  37 
  38 class ShenandoahParallelCodeHeapIterator {
  39   friend class CodeCache;
  40 private:
  41   CodeHeap*     _heap;
  42   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int));
  43   volatile int  _claimed_idx;
  44   volatile bool _finished;
  45   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, 0);
  46 public:
  47   ShenandoahParallelCodeHeapIterator(CodeHeap* heap);
  48   void parallel_blobs_do(CodeBlobClosure* f);
  49 };
  50 
  51 class ShenandoahParallelCodeCacheIterator {
  52   friend class CodeCache;
  53 private:
  54   ShenandoahParallelCodeHeapIterator* _iters;
  55   int                       _length;
  56 
  57   NONCOPYABLE(ShenandoahParallelCodeCacheIterator);
  58 
  59 public:
  60   ShenandoahParallelCodeCacheIterator(const GrowableArray<CodeHeap*>* heaps);
  61   ~ShenandoahParallelCodeCacheIterator();
  62   void parallel_blobs_do(CodeBlobClosure* f);
  63 };
  64 
  65 class ShenandoahCodeRootsIterator {
  66   friend class ShenandoahCodeRoots;
  67 protected:
  68   ShenandoahParallelCodeCacheIterator _par_iterator;
  69   ShenandoahSharedFlag _seq_claimed;
  70   ShenandoahNMethodTableSnapshot* _table_snapshot;
  71 
  72 protected:
  73   ShenandoahCodeRootsIterator();
  74   ~ShenandoahCodeRootsIterator();
  75 
  76   template<bool CSET_FILTER>
  77   void dispatch_parallel_blobs_do(CodeBlobClosure *f);
  78 
  79   template<bool CSET_FILTER>
  80   void fast_parallel_blobs_do(CodeBlobClosure *f);
  81 };
  82 
  83 class ShenandoahAllCodeRootsIterator : public ShenandoahCodeRootsIterator {
  84 public:
  85   ShenandoahAllCodeRootsIterator() : ShenandoahCodeRootsIterator() {};
  86   void possibly_parallel_blobs_do(CodeBlobClosure *f);
  87 };
  88 
  89 class ShenandoahCsetCodeRootsIterator : public ShenandoahCodeRootsIterator {
  90 public:
  91   ShenandoahCsetCodeRootsIterator() : ShenandoahCodeRootsIterator() {};
  92   void possibly_parallel_blobs_do(CodeBlobClosure* f);
  93 };
  94 
  95 class ShenandoahCodeRoots : public AllStatic {
  96   friend class ShenandoahHeap;
  97   friend class ShenandoahCodeRootsIterator;
  98 
  99 public:
 100   static void initialize();
 101   static void register_nmethod(nmethod* nm);
 102   static void unregister_nmethod(nmethod* nm);
 103   static void flush_nmethod(nmethod* nm);
 104 
 105   static ShenandoahNMethodTable* table() {
 106     return _nmethod_table;
 107   }
 108 
 109   // Concurrent nmethod unloading support
 110   static void unlink(WorkGang* workers, bool unloading_occurred);
 111   static void purge(WorkGang* workers);
 112   static void prepare_concurrent_unloading();
 113   static int  disarmed_value()         { return _disarmed_value; }
 114   static int* disarmed_value_address() { return &_disarmed_value; }
 115 
 116 private:
 117   static ShenandoahNMethodTable* _nmethod_table;
 118   static int                     _disarmed_value;
 119 };
 120 
 121 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHCODEROOTS_HPP