1 /*
   2  * Copyright (c) 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_G1FIXEDSIZESTACK_INLINE_HPP
  26 #define SHARE_VM_GC_G1_G1FIXEDSIZESTACK_INLINE_HPP
  27 
  28 #include "gc/g1/g1FixedSizeStack.hpp"
  29 #include "runtime/atomic.inline.hpp"
  30 #include "runtime/orderAccess.inline.hpp"
  31 #include "utilities/debug.hpp"
  32 
  33 size_t G1FixedSizeStackBase::length() const {
  34   return OrderAccess::load_acquire((volatile intptr_t*)&_length) & LengthMask;
  35 }
  36 // This is required for the use of Atomic::cmpxchg_ptr in par_push().
  37 STATIC_ASSERT(sizeof(intptr_t) == sizeof(size_t));
  38 
  39 template <class T>
  40 void G1FixedSizeStack<T>::par_push(const T elem) {
  41   // Try to set exclusive access to the length field by setting its upper bit.
  42   size_t cur_length = length();
  43   while (true) {
  44     size_t old_length = (size_t)Atomic::cmpxchg_ptr((intptr_t)cur_length | nth_bit(BitsPerWord - 1), (volatile intptr_t*)&_length, (intptr_t)cur_length);
  45     if (old_length == cur_length) {
  46       assert((cur_length & nth_bit(BitsPerWord - 1)) == 0, "Topmost bit must not be set in old value.");
  47       break;
  48     }
  49     cur_length = old_length & LengthMask;
  50   }
  51   assert(cur_length < _max_length, "Tried to push more objects than there is space.");
  52   set_by_index_raw(cur_length, elem);
  53   OrderAccess::release_store_ptr((volatile intptr_t*)&_length, cur_length + 1);
  54 }
  55 
  56 #endif /* SHARE_VM_GC_G1_G1FIXEDSIZESTACK_INLINE_HPP */