1 /*
   2  * Copyright (c) 2017, 2019, Red Hat, Inc. 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_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_HPP
  27 
  28 #include "gc/shared/stringdedup/stringDedup.hpp"
  29 #include "gc/shenandoah/shenandoahHeap.hpp"
  30 #include "oops/oop.hpp"
  31 
  32 template <uint buffer_size>
  33 class ShenandoahOopBuffer : public CHeapObj<mtGC> {
  34 private:
  35   oop           _buf[buffer_size];
  36   volatile uint _index;
  37   ShenandoahOopBuffer<buffer_size>* _next;
  38 
  39 public:
  40   ShenandoahOopBuffer();
  41 
  42   bool is_full()  const;
  43   bool is_empty() const;
  44   uint size()     const;
  45 
  46   void push(oop obj);
  47   oop pop();
  48 
  49   void reset();
  50 
  51   void set_next(ShenandoahOopBuffer<buffer_size>* next);
  52   ShenandoahOopBuffer<buffer_size>* next() const;
  53 
  54   void unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl);
  55   void oops_do(OopClosure* cl);
  56 
  57 private:
  58   uint index_acquire() const;
  59   void set_index_release(uint index);
  60 };
  61 
  62 typedef ShenandoahOopBuffer<64> ShenandoahQueueBuffer;
  63 
  64 // Muti-producer and single consumer queue set
  65 class ShenandoahStrDedupQueue : public StringDedupQueue {
  66 private:
  67   ShenandoahQueueBuffer** _producer_queues;
  68   ShenandoahQueueBuffer*  _consumer_queue;
  69   size_t                  _num_producer_queue;
  70 
  71   // The queue is used for producers to publish completed buffers
  72   ShenandoahQueueBuffer* _published_queues;
  73 
  74   // Cached free buffers
  75   ShenandoahQueueBuffer* _free_list;
  76   size_t                 _num_free_buffer;
  77   const size_t           _max_free_buffer;
  78 
  79   bool                   _cancel;
  80 
  81   // statistics
  82   size_t                 _total_buffers;
  83 
  84 private:
  85   ~ShenandoahStrDedupQueue();
  86 
  87 public:
  88   ShenandoahStrDedupQueue();
  89 
  90   void wait_impl();
  91   void cancel_wait_impl();
  92 
  93   void push_impl(uint worker_id, oop string_oop);
  94   oop  pop_impl();
  95 
  96   void unlink_or_oops_do_impl(StringDedupUnlinkOrOopsDoClosure* cl, size_t queue);
  97 
  98   void print_statistics_impl();
  99   void verify_impl();
 100 
 101 protected:
 102   size_t num_queues() const { return (_num_producer_queue + 2); }
 103 
 104 private:
 105   ShenandoahQueueBuffer* new_buffer();
 106 
 107   void release_buffers(ShenandoahQueueBuffer* list);
 108 
 109   ShenandoahQueueBuffer* queue_at(size_t queue_id) const;
 110 
 111   bool pop_candidate(oop& obj);
 112 
 113   void set_producer_buffer(ShenandoahQueueBuffer* buf, size_t queue_id);
 114 
 115   void verify(ShenandoahQueueBuffer* head);
 116 };
 117 
 118 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_HPP