1 /*
   2  * Copyright (c) 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_SHENANDOAHEVACLOCKINGBITMAP_INLINE_HPP
  26 #define SHARE_GC_SHENANDOAH_SHENANDOAHEVACLOCKINGBITMAP_INLINE_HPP
  27 
  28 #include "gc/shenandoah/shenandoahEvacLockingBitmap.hpp"
  29 #include "memory/memRegion.hpp"
  30 #include "oops/oopsHierarchy.hpp"
  31 #include "utilities/bitMap.hpp"
  32 
  33 inline size_t ShenandoahEvacLockingBitmap::addr_to_offset(const HeapWord* addr) const {
  34   return pointer_delta(addr, _covered.start()) >> _shifter;
  35 }
  36 
  37 inline void ShenandoahEvacLockingBitmap::acquire(oop obj) {
  38   size_t offset = addr_to_offset(cast_from_oop<HeapWord*>(obj));
  39   int ctr = 0;
  40   int yields = 0;
  41   while (!_bm.par_set_bit(offset)) {
  42     // Spin/yield/sleep-strategy inspired by Thread::SpinAcquire()
  43     ++ctr;
  44     if ((ctr & 0xFFF) == 0 || !os::is_MP()) {
  45       if (yields > 5) {
  46         os::naked_short_sleep(1);
  47       } else {
  48         os::naked_yield();
  49         ++yields;
  50       }
  51     } else {
  52       SpinPause();
  53     }
  54 
  55   }
  56 }
  57 
  58 inline void ShenandoahEvacLockingBitmap::release(oop obj) {
  59   size_t offset = addr_to_offset(cast_from_oop<HeapWord*>(obj));
  60   assert(_bm.at(offset), "");
  61   bool success = _bm.par_clear_bit(offset);
  62   assert(success, "");
  63 }
  64 
  65 inline ShenandoahEvacLocker::ShenandoahEvacLocker(ShenandoahEvacLockingBitmap* bitmap, oop obj):
  66 _bitmap(bitmap), _obj(obj) {
  67   _bitmap->acquire(_obj);
  68 }
  69 
  70 inline ShenandoahEvacLocker::~ShenandoahEvacLocker() {
  71   _bitmap->release(_obj);
  72 }
  73 
  74 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHEVACLOCKINGBITMAP_INLINE_HPP