1 /*
   2  * Copyright (c) 2001, 2016, 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_VM_GC_G1_G1BARRIERSET_HPP
  26 #define SHARE_VM_GC_G1_G1BARRIERSET_HPP
  27 
  28 #include "gc/shared/cardTableModRefBS.hpp"
  29 
  30 class CardTable;
  31 class DirtyCardQueueSet;
  32 class G1CardTable;
  33 class SATBMarkQueueSet;
  34 
  35 // This barrier is specialized to use a logging barrier to support
  36 // snapshot-at-the-beginning marking.
  37 
  38 class G1BarrierSet: public CardTableModRefBS {
  39   friend class VMStructs;
  40 private:
  41   void write_ref_nmethod_pre(nmethod* nm, ptrdiff_t offset);
  42   void write_ref_nmethod_post(nmethod* nm, oop new_value);
  43   static bool is_referent_field(oop base, ptrdiff_t offset);
  44 
  45 public:
  46   G1BarrierSet(CardTable* table);
  47   ~G1BarrierSet() { }
  48 
  49   static SATBMarkQueueSet& satb_mark_queue_set();
  50   static DirtyCardQueueSet& dirty_card_queue_set();
  51 
  52   // Add "pre_val" to a set of objects that may have been disconnected from the
  53   // pre-marking object graph.
  54   static void satb_enqueue(oop pre_val);
  55   static void card_enqueue(volatile jbyte* card);
  56 
  57   template <DecoratorSet decorators>
  58   void write_ref_field_pre(void* field);
  59 
  60   template <DecoratorSet decorators>
  61   void write_ref_field_post(void* field, oop new_val);
  62 
  63   void klass_update_barrier_set_pre(Klass* klass, oop* p);
  64 
  65   template <class T> void write_ref_array_pre_work(T* dst, int count);
  66   virtual void write_ref_array_pre(oop* dst, int count, bool dest_uninitialized);
  67   virtual void write_ref_array_pre(narrowOop* dst, int count, bool dest_uninitialized);
  68 
  69   // NB: if you do a whole-heap invalidation, the "usual invariant" defined
  70   // above no longer applies.
  71   void invalidate(MemRegion mr);
  72 
  73   virtual void write_region(MemRegion mr) { invalidate(mr); }
  74 
  75   virtual void write_ref_array_region(MemRegion mr) { invalidate(mr); }
  76 
  77   // This method initializes the SATB and dirty card queues before a
  78   // JavaThread is added to the Java thread list. Right now, we don't
  79   // have to do anything to the dirty card queue (it should have been
  80   // activated when the thread was created), but we have to activate
  81   // the SATB queue if the thread is created while a marking cycle is
  82   // in progress. The activation / de-activation of the SATB queues at
  83   // the beginning / end of a marking cycle is done during safepoints
  84   // so we have to make sure this method is called outside one to be
  85   // able to safely read the active field of the SATB queue set. Right
  86   // now, it is called just before the thread is added to the Java
  87   // thread list in the Threads::add() method. That method is holding
  88   // the Threads_lock which ensures we are outside a safepoint. We
  89   // cannot do the obvious and set the active field of the SATB queue
  90   // when the thread is created given that, in some cases, safepoints
  91   // might happen between the JavaThread constructor being called and the
  92   // thread being added to the Java thread list (an example of this is
  93   // when the structure for the DestroyJavaVM thread is created).
  94   virtual void on_add_thread(JavaThread* thread);
  95 
  96   virtual void on_destroy_thread(JavaThread* thread);
  97 
  98   virtual BarrierSetCodeGen *make_code_gen();
  99   virtual C1BarrierSetCodeGen* make_c1_code_gen();
 100   virtual C2BarrierSetCodeGen* make_c2_code_gen();
 101 
 102   // Runtime calls for generated code
 103   static void g1_wb_pre(oopDesc* orig, JavaThread *thread);
 104   static void g1_wb_post(void* card_addr, JavaThread* thread);
 105 
 106   // Callbacks for runtime accesses.
 107   template <DecoratorSet decorators>
 108   class AccessBarrier: public ModRefBarrierSet::AccessBarrier<decorators, G1BarrierSet> {
 109     typedef ModRefBarrierSet::AccessBarrier<decorators, G1BarrierSet> ModRef;
 110     typedef BarrierSet::AccessBarrier<decorators> Basic;
 111 
 112   public:
 113     typedef ModRef SuperAccessBarrier;
 114 
 115     // Needed for loads on weak references
 116     static oop oop_load(void* addr);
 117 
 118     // Needed for anonymous weak references
 119     static oop oop_load_at(oop base, ptrdiff_t offset);
 120 
 121     // Needed for stores in nmethods
 122     static void oop_store_at(nmethod* base, ptrdiff_t offset, oop new_value);
 123   };
 124 };
 125 
 126 template<>
 127 struct BSTypeToName<G1BarrierSet> {
 128   static const BarrierSet::Name value = BarrierSet::G1BarrierSet;
 129 };
 130 
 131 template<>
 132 struct BSNameToType<BarrierSet::G1BarrierSet> {
 133   typedef G1BarrierSet type;
 134 };
 135 
 136 #endif // SHARE_VM_GC_G1_G1BARRIERSET_HPP