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 public: 46 ShenandoahLock() : _state(unlocked), _owner(nullptr) {}; 47 48 void lock(bool allow_block_for_safepoint) { 49 assert(Atomic::load(&_owner) != Thread::current(), "reentrant locking attempt, would deadlock"); 50 51 if ((allow_block_for_safepoint && SafepointSynchronize::is_synchronizing()) || 52 (Atomic::cmpxchg(&_state, unlocked, locked) != unlocked)) { 53 // 1. Java thread, and there is a pending safepoint. Dive into contended locking 54 // immediately without trying anything else, and block. 55 // 2. Fast lock fails, dive into contended lock handling. 56 contended_lock(allow_block_for_safepoint); 57 } 58 59 assert(Atomic::load(&_state) == locked, "must be locked"); 60 assert(Atomic::load(&_owner) == nullptr, "must not be owned"); 61 DEBUG_ONLY(Atomic::store(&_owner, Thread::current());) 62 } 63 64 void unlock() { 65 assert(Atomic::load(&_owner) == Thread::current(), "sanity"); 66 DEBUG_ONLY(Atomic::store(&_owner, (Thread*)nullptr);) 67 OrderAccess::fence(); 68 Atomic::store(&_state, unlocked); 69 } 70 71 void contended_lock(bool allow_block_for_safepoint); 72 73 bool owned_by_self() { 74 #ifdef ASSERT 75 return _state == locked && _owner == Thread::current(); 76 #else 77 ShouldNotReachHere(); 78 return false; 79 #endif 80 } 81 }; 82 83 class ShenandoahLocker : public StackObj { 84 private: 85 ShenandoahLock* const _lock; 86 public: 87 ShenandoahLocker(ShenandoahLock* lock, bool allow_block_for_safepoint = false) : _lock(lock) { 88 if (_lock != nullptr) { 89 _lock->lock(allow_block_for_safepoint); 90 } 91 } 92 93 ~ShenandoahLocker() { 94 if (_lock != nullptr) { 95 _lock->unlock(); 96 } 97 } 98 }; 99 100 class ShenandoahSimpleLock { 101 private: 102 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 != nullptr) { 134 _lock->lock(); 135 } 136 } 137 138 ~ShenandoahReentrantLocker() { 139 if (_lock != nullptr) { 140 assert(_lock->owned_by_self(), "Must be owner"); 141 _lock->unlock(); 142 } 143 } 144 }; 145 146 #endif // SHARE_GC_SHENANDOAH_SHENANDOAHLOCK_HPP