1 /*
2  * Copyright (c) 2017, 2020, 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_INLINE_HPP
26 #define SHARE_GC_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_INLINE_HPP
27 
28 #include "gc/shenandoah/shenandoahStrDedupQueue.hpp"
29 #include "oops/access.hpp"
30 #include "runtime/atomic.hpp"
31 
32 // With concurrent string dedup cleaning up, GC worker threads
33 // may see oops just enqueued, so release_store and load_acquire
34 // relationship needs to be established between enqueuing threads
35 // and GC workers.
36 // For example, when GC sees a slot (index), there must be a valid
37 // (dead or live) oop.
38 // Note: There is no concern if GC misses newly enqueued oops,
39 // since LRB ensures they are in to-space.
40 template <uint buffer_size>
ShenandoahOopBuffer()41 ShenandoahOopBuffer<buffer_size>::ShenandoahOopBuffer() :
42   _index(0), _next(NULL) {
43 }
44 
45 template <uint buffer_size>
is_full() const46 bool ShenandoahOopBuffer<buffer_size>::is_full() const {
47   return index_acquire() >= buffer_size;
48 }
49 
50 template <uint buffer_size>
is_empty() const51 bool ShenandoahOopBuffer<buffer_size>::is_empty() const {
52   return index_acquire() == 0;
53 }
54 
55 template <uint buffer_size>
size() const56 uint ShenandoahOopBuffer<buffer_size>::size() const {
57   return index_acquire();
58 }
59 
60 template <uint buffer_size>
push(oop obj)61 void ShenandoahOopBuffer<buffer_size>::push(oop obj) {
62   assert(!is_full(),  "Buffer is full");
63   uint idx = index_acquire();
64   RawAccess<IS_NOT_NULL>::oop_store(&_buf[idx], obj);
65   set_index_release(idx + 1);
66 }
67 
68 template <uint buffer_size>
pop()69 oop ShenandoahOopBuffer<buffer_size>::pop() {
70   assert(!is_empty(), "Buffer is empty");
71   uint idx = index_acquire() - 1;
72   oop value = NativeAccess<ON_PHANTOM_OOP_REF | AS_NO_KEEPALIVE | MO_ACQUIRE>::oop_load(&_buf[idx]);
73   set_index_release(idx);
74   return value;
75 }
76 
77 template <uint buffer_size>
set_next(ShenandoahOopBuffer<buffer_size> * next)78 void ShenandoahOopBuffer<buffer_size>::set_next(ShenandoahOopBuffer<buffer_size>* next) {
79   _next = next;
80 }
81 
82 template <uint buffer_size>
next() const83 ShenandoahOopBuffer<buffer_size>* ShenandoahOopBuffer<buffer_size>::next() const {
84   return _next;
85 }
86 
87 template <uint buffer_size>
reset()88 void ShenandoahOopBuffer<buffer_size>::reset() {
89   _index = 0;
90   _next = NULL;
91 }
92 
93 template <uint buffer_size>
index_acquire() const94 uint ShenandoahOopBuffer<buffer_size>::index_acquire() const {
95   return Atomic::load_acquire(&_index);
96 }
97 
98 template <uint buffer_size>
set_index_release(uint index)99 void ShenandoahOopBuffer<buffer_size>::set_index_release(uint index) {
100   return Atomic::release_store(&_index, index);
101 }
102 
103 template <uint buffer_size>
unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure * cl)104 void ShenandoahOopBuffer<buffer_size>::unlink_or_oops_do(StringDedupUnlinkOrOopsDoClosure* cl) {
105   uint len = size();
106   for (uint index = 0; index < len; index ++) {
107     oop* obj_addr = &_buf[index];
108     if (*obj_addr != NULL) {
109       if (cl->is_alive(*obj_addr)) {
110         cl->keep_alive(obj_addr);
111       } else {
112         RawAccess<MO_RELEASE>::oop_store(&_buf[index], oop());
113       }
114     }
115   }
116 }
117 
118 template <uint buffer_size>
oops_do(OopClosure * cl)119 void ShenandoahOopBuffer<buffer_size>::oops_do(OopClosure* cl) {
120   uint len = size();
121   for (uint index = 0; index < len; index ++) {
122     cl->do_oop(&_buf[index]);
123   }
124 }
125 
126 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHSTRDEDUPQUEUE_INLINE_HPP
127