< prev index next >

src/hotspot/share/gc/shenandoah/shenandoahLock.hpp

Print this page




   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");


  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


   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 #ifdef ASSERT
  46     assert(_owner != Thread::current(), "reentrant locking attempt, would deadlock");
  47 #endif
  48     Thread::SpinAcquire(&_state, "Shenandoah Heap Lock");


  75     Thread* thr = Thread::current();
  76     assert((_state == locked && _owner == thr) ||
  77            (SafepointSynchronize::is_at_safepoint() && thr->is_VM_thread()),
  78            "must own heap lock or by VM thread at safepoint");
  79   }
  80 #endif
  81 };
  82 
  83 class ShenandoahLocker : public StackObj {
  84 private:
  85   ShenandoahLock* const _lock;
  86 public:
  87   ShenandoahLocker(ShenandoahLock* lock) : _lock(lock) {
  88     if (_lock != NULL) {
  89       _lock->lock();
  90     }
  91   }
  92 
  93   ~ShenandoahLocker() {
  94     if (_lock != NULL) {
  95       _lock->unlock();
  96     }
  97   }
  98 };
  99 
 100 class ShenandoahSimpleLock {
 101 private:
 102   os::PlatformMonitor   _lock; // native lock
 103 public:
 104   ShenandoahSimpleLock();
 105 
 106   virtual void lock();
 107   virtual void unlock();
 108 };
 109 
 110 class ShenandoahReentrantLock : public ShenandoahSimpleLock {
 111 private:
 112   Thread* volatile      _owner;
 113   uint64_t              _count;
 114 
 115 public:
 116   ShenandoahReentrantLock();
 117   ~ShenandoahReentrantLock();
 118 
 119   virtual void lock();
 120   virtual void unlock();
 121 
 122   // If the lock already owned by this thread
 123   bool owned_by_self() const ;
 124 };
 125 
 126 class ShenandoahReentrantLocker : public StackObj {
 127 private:
 128   ShenandoahReentrantLock* const _lock;
 129 
 130 public:
 131   ShenandoahReentrantLocker(ShenandoahReentrantLock* lock) :
 132     _lock(lock) {
 133     if (_lock != NULL) {
 134       _lock->lock();
 135     }
 136   }
 137 
 138   ~ShenandoahReentrantLocker() {
 139     if (_lock != NULL) {
 140       assert(_lock->owned_by_self(), "Must be owner");
 141       _lock->unlock();
 142     }
 143   }
 144 };
 145 
 146 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP
< prev index next >