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   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 
  58 typedef ShenandoahOopBuffer<64> ShenandoahQueueBuffer;
  59 
  60 // Muti-producer and single consumer queue set
  61 class ShenandoahStrDedupQueue : public StringDedupQueue {
  62 private:
  63   ShenandoahQueueBuffer** _producer_queues;
  64   ShenandoahQueueBuffer*  _consumer_queue;
  65   size_t                  _num_producer_queue;
  66 
  67   // The queue is used for producers to publish completed buffers
  68   ShenandoahQueueBuffer* _published_queues;
  69 
  70   // Cached free buffers
  71   ShenandoahQueueBuffer* _free_list;
  72   size_t                 _num_free_buffer;
  73   const size_t           _max_free_buffer;
  74 
  75   bool                   _cancel;
  76 
  77   // statistics
  78   size_t                 _total_buffers;
  79 
  80 private:
  81   ~ShenandoahStrDedupQueue();
  82 
  83 public:
  84   ShenandoahStrDedupQueue();
  85 
  86   void wait_impl();
  87   void cancel_wait_impl();
  88 
  89   void push_impl(uint worker_id, oop string_oop);
  90   oop  pop_impl();
  91 
  92   void unlink_or_oops_do_impl(StringDedupUnlinkOrOopsDoClosure* cl, size_t queue);
  93 
  94   void print_statistics_impl();
  95   void verify_impl();
  96 
  97 protected:
  98   size_t num_queues() const { return (_num_producer_queue + 2); }
  99 
 100 private:
 101   ShenandoahQueueBuffer* new_buffer();
 102 
 103   void release_buffers(ShenandoahQueueBuffer* list);
 104 
 105   ShenandoahQueueBuffer* queue_at(size_t queue_id) const;
 106 
 107   bool pop_candidate(oop& obj);
 108 
 109   void set_producer_buffer(ShenandoahQueueBuffer* buf, size_t queue_id);
 110 
 111   void verify(ShenandoahQueueBuffer* head);
 112 };
 113 
 114 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_HPP