1 /* 2 * Copyright (c) 2017, 2018, 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_VM_GC_SHENANDOAH_SHENANDOAHHEAPLOCK_HPP 25 #define SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPLOCK_HPP 26 27 #include "gc_implementation/shenandoah/shenandoahPadding.hpp" 28 #include "memory/allocation.hpp" 29 #include "runtime/safepoint.hpp" 30 #include "runtime/thread.hpp" 31 32 class ShenandoahLock { 33 private: 34 enum LockState { unlocked = 0, locked = 1 }; 35 36 shenandoah_padding(0); 37 volatile int _state; 38 shenandoah_padding(1); 39 volatile Thread* _owner; 40 shenandoah_padding(2); 41 42 public: 43 ShenandoahLock() : _state(unlocked), _owner(NULL) {}; 44 45 void lock() { 46 #ifdef ASSERT 47 assert(_owner != Thread::current(), "reentrant locking attempt, would deadlock"); 48 #endif 49 Thread::SpinAcquire(&_state, "Shenandoah Heap Lock"); 50 #ifdef ASSERT 51 assert(_state == locked, "must be locked"); 52 assert(_owner == NULL, "must not be owned"); 53 _owner = Thread::current(); 54 #endif 55 } 56 57 void unlock() { 58 #ifdef ASSERT 59 assert (_owner == Thread::current(), "sanity"); 60 _owner = NULL; 61 #endif 62 Thread::SpinRelease(&_state); 63 } 64 65 bool owned_by_self() { 66 #ifdef ASSERT 67 return _state == locked && _owner == Thread::current(); 68 #else 69 ShouldNotReachHere(); 70 return false; 71 #endif 72 } 73 }; 74 75 class ShenandoahLocker : public StackObj { 76 private: 77 ShenandoahLock* _lock; 78 public: 79 ShenandoahLocker(ShenandoahLock* lock) { 80 _lock = lock; 81 _lock->lock(); 82 } 83 84 ~ShenandoahLocker() { 85 _lock->unlock(); 86 } 87 }; 88 89 #endif // SHARE_VM_GC_SHENANDOAH_SHENANDOAHHEAPLOCK_HPP