1 /*
   2  * Copyright (c) 2017, 2019, Red Hat, Inc. All rights reserved.
   3  *
   4  * This code is free software; you can redistribute it and/or modify it
   5  * under the terms of the GNU General Public License version 2 only, as
   6  * published by the Free Software Foundation.
   7  *
   8  * This code is distributed in the hope that it will be useful, but WITHOUT
   9  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  11  * version 2 for more details (a copy is included in the LICENSE file that
  12  * accompanied this code).
  13  *
  14  * You should have received a copy of the GNU General Public License version
  15  * 2 along with this work; if not, write to the Free Software Foundation,
  16  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  17  *
  18  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  19  * or visit www.oracle.com if you need additional information or have any
  20  * questions.
  21  *
  22  */
  23 
  24 #ifndef SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP
  25 #define SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP
  26 
  27 #include "memory/allocation.hpp"
  28 #include "runtime/thread.hpp"
  29 
  30 class ShenandoahLock  {
  31 private:
  32   enum LockState { unlocked = 0, locked = 1 };
  33 
  34   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int));
  35   volatile int _state;
  36   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile Thread*));
  37   volatile Thread* _owner;
  38   DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, 0);
  39 
  40 public:
  41   ShenandoahLock() : _state(unlocked), _owner(NULL) {};
  42 
  43   void lock() {
  44 #ifdef ASSERT
  45     assert(_owner != Thread::current(), "reentrant locking attempt, would deadlock");
  46 #endif
  47     Thread::SpinAcquire(&_state, "Shenandoah Heap Lock");
  48 #ifdef ASSERT
  49     assert(_state == locked, "must be locked");
  50     assert(_owner == NULL, "must not be owned");
  51     _owner = Thread::current();
  52 #endif
  53   }
  54 
  55   void unlock() {
  56 #ifdef ASSERT
  57     assert (_owner == Thread::current(), "sanity");
  58     _owner = NULL;
  59 #endif
  60     Thread::SpinRelease(&_state);
  61   }
  62 
  63 #ifdef ASSERT
  64   void assert_owned_by_current_thread() {
  65     assert(_state == locked, "must be locked");
  66     assert(_owner == Thread::current(), "must be owned by current thread");
  67   }
  68 
  69   void assert_not_owned_by_current_thread() {
  70     assert(_owner != Thread::current(), "must be not owned by current thread");
  71   }
  72 
  73   void assert_owned_by_current_thread_or_safepoint() {
  74     Thread* thr = Thread::current();
  75     assert((_state == locked && _owner == thr) ||
  76            (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()),
  77            "must own heap lock or by VM thread at safepoint");
  78   }
  79 #endif
  80 };
  81 
  82 class ShenandoahLocker : public StackObj {
  83 private:
  84   ShenandoahLock* const _lock;
  85 public:
  86   ShenandoahLocker(ShenandoahLock* lock) : _lock(lock) {
  87     if (_lock != NULL) {
  88       _lock->lock();
  89     }
  90   }
  91 
  92   ~ShenandoahLocker() {
  93     if (_lock != NULL) {
  94       _lock->unlock();
  95     }
  96   }
  97 };
  98 
  99 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP