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 volatile Thread* _owner; 41 shenandoah_padding(2); 42 43 template<typename BlockOp> 44 void contended_lock_internal(JavaThread* java_thread); 45 46 public: 47 ShenandoahLock() : _state(unlocked), _owner(nullptr) {}; 48 49 void lock(bool allow_block_for_safepoint) { 50 assert(Atomic::load(&_owner) != Thread::current(), "reentrant locking attempt, would deadlock"); 51 52 // Try to lock fast, or dive into contended lock handling. 53 if (Atomic::cmpxchg(&_state, unlocked, locked) != unlocked) { 54 contended_lock(allow_block_for_safepoint); 55 } 56 57 assert(Atomic::load(&_state) == locked, "must be locked"); 58 assert(Atomic::load(&_owner) == nullptr, "must not be owned"); 59 DEBUG_ONLY(Atomic::store(&_owner, Thread::current());) 60 } 61 62 void unlock() { 63 assert(Atomic::load(&_owner) == Thread::current(), "sanity"); 64 DEBUG_ONLY(Atomic::store(&_owner, (Thread*)nullptr);) 65 OrderAccess::fence(); 66 Atomic::store(&_state, unlocked); 67 } 68 69 void contended_lock(bool allow_block_for_safepoint); 70 71 bool owned_by_self() { 72 #ifdef ASSERT 73 return _state == locked && _owner == Thread::current(); 74 #else 75 ShouldNotReachHere(); 76 return false; 77 #endif 78 } 79 }; 80 81 class ShenandoahLocker : public StackObj { 82 private: 83 ShenandoahLock* const _lock; 84 public: 85 ShenandoahLocker(ShenandoahLock* lock, bool allow_block_for_safepoint = false) : _lock(lock) { 86 if (_lock != nullptr) { 87 _lock->lock(allow_block_for_safepoint); 88 } 89 } 90 91 ~ShenandoahLocker() { 92 if (_lock != nullptr) { 93 _lock->unlock(); 94 } 95 } 96 }; 97 98 class ShenandoahSimpleLock { 99 private: 100 PlatformMonitor _lock; // native lock 101 public: 102 ShenandoahSimpleLock(); 103 104 virtual void lock(); 105 virtual void unlock(); 106 }; 107 108 class ShenandoahReentrantLock : public ShenandoahSimpleLock { 109 private: 110 Thread* volatile _owner; 111 uint64_t _count; 112 113 public: 114 ShenandoahReentrantLock(); 115 ~ShenandoahReentrantLock(); 116 117 virtual void lock(); 118 virtual void unlock(); 119 120 // If the lock already owned by this thread 121 bool owned_by_self() const ; 122 }; 123 124 class ShenandoahReentrantLocker : public StackObj { 125 private: 126 ShenandoahReentrantLock* const _lock; 127 128 public: 129 ShenandoahReentrantLocker(ShenandoahReentrantLock* lock) : 130 _lock(lock) { 131 if (_lock != nullptr) { 132 _lock->lock(); 133 } 134 } 135 136 ~ShenandoahReentrantLocker() { 137 if (_lock != nullptr) { 138 assert(_lock->owned_by_self(), "Must be owner"); 139 _lock->unlock(); 140 } 141 } 142 }; 143 144 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP