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/safepoint.hpp"
  29 #include "runtime/thread.hpp"
  30 
  31 class ShenandoahLock  {
  32 private:
  33   enum LockState { unlocked = 0, locked = 1 };
  34 
  35   DEFINE_PAD_MINUS_SIZE(0, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile int));
  36   volatile int _state;
  37   DEFINE_PAD_MINUS_SIZE(1, DEFAULT_CACHE_LINE_SIZE, sizeof(volatile Thread*));
  38   volatile Thread* _owner;
  39   DEFINE_PAD_MINUS_SIZE(2, DEFAULT_CACHE_LINE_SIZE, 0);
  40 
  41 public:
  42   ShenandoahLock() : _state(unlocked), _owner(NULL) {};
  43 
  44   void lock() {
  45     Thread::SpinAcquire(&_state, "Shenandoah Heap Lock");
  46 #ifdef ASSERT
  47     assert(_state == locked, "must be locked");
  48     assert(_owner == NULL, "must not be owned");
  49     _owner = Thread::current();
  50 #endif
  51   }
  52 
  53   void unlock() {
  54 #ifdef ASSERT
  55     assert (_owner == Thread::current(), "sanity");
  56     _owner = NULL;
  57 #endif
  58     Thread::SpinRelease(&_state);
  59   }
  60 
  61 #ifdef ASSERT
  62   void assert_owned_by_current_thread() {
  63     assert(_state == locked, "must be locked");
  64     assert(_owner == Thread::current(), "must be owned by current thread");
  65   }
  66 
  67   void assert_not_owned_by_current_thread() {
  68     assert(_owner != Thread::current(), "must be not owned by current thread");
  69   }
  70 
  71   void assert_owned_by_current_thread_or_safepoint() {
  72     Thread* thr = Thread::current();
  73     assert((_state == locked && _owner == thr) ||
  74            (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()),
  75            "must own heap lock or by VM thread at safepoint");
  76   }
  77 #endif
  78 };
  79 
  80 class ShenandoahLocker : public StackObj {
  81 private:
  82   ShenandoahLock* const _lock;
  83 public:
  84   ShenandoahLocker(ShenandoahLock* lock) : _lock(lock) {
  85     if (_lock != NULL) {
  86       _lock->lock();
  87     }
  88   }
  89 
  90   ~ShenandoahLocker() {
  91     if (_lock != NULL) {
  92       _lock->unlock();
  93     }
  94   }
  95 };
  96 
  97 class ShenandoahSimpleLock {
  98 private:
  99   os::PlatformMonitor   _lock; // native lock
 100 public:
 101   ShenandoahSimpleLock();
 102 
 103   virtual void lock();
 104   virtual void unlock();
 105 };
 106 
 107 class ShenandoahReentrantLock : public ShenandoahSimpleLock {
 108 private:
 109   Thread* volatile      _owner;
 110   uint64_t              _count;
 111 
 112 public:
 113   ShenandoahReentrantLock();
 114   ~ShenandoahReentrantLock();
 115 
 116   virtual void lock();
 117   virtual void unlock();
 118 
 119   // If the lock already owned by this thread
 120   bool owned_by_self() const ;
 121 };
 122 
 123 class ShenandoahReentrantLocker : public StackObj {
 124 private:
 125   ShenandoahReentrantLock* const _lock;
 126 
 127 public:
 128   ShenandoahReentrantLocker(ShenandoahReentrantLock* lock) :
 129     _lock(lock) {
 130     if (_lock != NULL) {
 131       _lock->lock();
 132     }
 133   }
 134 
 135   ~ShenandoahReentrantLocker() {
 136     if (_lock != NULL) {
 137       assert(_lock->owned_by_self(), "Must be owner");
 138       _lock->unlock();
 139     }
 140   }
 141 };
 142 
 143 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP