1 /*
  2  * Copyright (c) 2017, 2019, 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_SHENANDOAHLOCK_HPP
 26 #define SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP
 27 
 28 #include "gc/shenandoah/shenandoahPadding.hpp"
 29 #include "memory/allocation.hpp"
 30 #include "runtime/javaThread.hpp"
 31 #include "runtime/safepoint.hpp"
 32 
 33 class ShenandoahLock  {
 34 private:
 35   enum LockState { unlocked = 0, locked = 1 };
 36 
 37   shenandoah_padding(0);
 38   volatile LockState _state;
 39   shenandoah_padding(1);
 40   Thread* volatile _owner;
 41   shenandoah_padding(2);
 42 
 43   template<bool ALLOW_BLOCK>
 44   void contended_lock_internal(JavaThread* java_thread);
 45   static void yield_or_sleep(int &yields);
 46 
 47 public:
 48   ShenandoahLock() : _state(unlocked), _owner(nullptr) {};
 49 
 50   void lock(bool allow_block_for_safepoint) {
 51     assert(Atomic::load(&_owner) != Thread::current(), "reentrant locking attempt, would deadlock");
 52 
 53     if ((allow_block_for_safepoint && SafepointSynchronize::is_synchronizing()) ||
 54         (Atomic::cmpxchg(&_state, unlocked, locked) != unlocked)) {
 55       // 1. Java thread, and there is a pending safepoint. Dive into contended locking
 56       //    immediately without trying anything else, and block.
 57       // 2. Fast lock fails, dive into contended lock handling.
 58       contended_lock(allow_block_for_safepoint);
 59     }
 60 
 61     assert(Atomic::load(&_state) == locked, "must be locked");
 62     assert(Atomic::load(&_owner) == nullptr, "must not be owned");
 63     DEBUG_ONLY(Atomic::store(&_owner, Thread::current());)
 64   }
 65 
 66   void unlock() {
 67     assert(Atomic::load(&_owner) == Thread::current(), "sanity");
 68     DEBUG_ONLY(Atomic::store(&_owner, (Thread*)nullptr);)
 69     OrderAccess::fence();
 70     Atomic::store(&_state, unlocked);
 71   }
 72 
 73   void contended_lock(bool allow_block_for_safepoint);
 74 
 75   bool owned_by_self() {
 76 #ifdef ASSERT
 77     return _state == locked && _owner == Thread::current();
 78 #else
 79     ShouldNotReachHere();
 80     return false;
 81 #endif
 82   }
 83 };
 84 
 85 class ShenandoahLocker : public StackObj {
 86 private:
 87   ShenandoahLock* const _lock;
 88 public:
 89   ShenandoahLocker(ShenandoahLock* lock, bool allow_block_for_safepoint = false) : _lock(lock) {
 90     if (_lock != nullptr) {
 91       _lock->lock(allow_block_for_safepoint);
 92     }
 93   }
 94 
 95   ~ShenandoahLocker() {
 96     if (_lock != nullptr) {
 97       _lock->unlock();
 98     }
 99   }
100 };
101 
102 class ShenandoahSimpleLock {
103 private:
104   PlatformMonitor   _lock; // native lock
105 public:
106   ShenandoahSimpleLock();
107 
108   virtual void lock();
109   virtual void unlock();
110 };
111 
112 class ShenandoahReentrantLock : public ShenandoahSimpleLock {
113 private:
114   Thread* volatile      _owner;
115   uint64_t              _count;
116 
117 public:
118   ShenandoahReentrantLock();
119   ~ShenandoahReentrantLock();
120 
121   virtual void lock();
122   virtual void unlock();
123 
124   // If the lock already owned by this thread
125   bool owned_by_self() const ;
126 };
127 
128 class ShenandoahReentrantLocker : public StackObj {
129 private:
130   ShenandoahReentrantLock* const _lock;
131 
132 public:
133   ShenandoahReentrantLocker(ShenandoahReentrantLock* lock) :
134     _lock(lock) {
135     if (_lock != nullptr) {
136       _lock->lock();
137     }
138   }
139 
140   ~ShenandoahReentrantLocker() {
141     if (_lock != nullptr) {
142       assert(_lock->owned_by_self(), "Must be owner");
143       _lock->unlock();
144     }
145   }
146 };
147 
148 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP