Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/lock/CriticalSectionLocker.java
41162 views
/*1* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 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 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share.gc.lock;2425import nsk.share.TestBug;26import nsk.share.gc.lock.Locker;2728/**29* CriticalSectionLocker represents a way to lock a resource30* by entering some critical section.31*/32public abstract class CriticalSectionLocker<T> implements Locker<T> {33private transient boolean enabled = false;34private transient boolean locked = false;35private Object sync = new Object();36private Thread thread;37private Throwable exception;3839private final Runnable runnable = new Runnable() {40public void run() {41//System.out.println("Running");42try {43do {44synchronized (sync) {45while (enabled && !locked) {46try {47sync.wait();48} catch (InterruptedException e) {49}50}51if (!enabled)52break;53}54do {55criticalSection();56} while (locked);57} while (enabled);58// System.out.println("Exiting");59} catch (RuntimeException t) {60t.printStackTrace();61exception = t;62throw t;63}64}65};6667public CriticalSectionLocker() {68}6970/**71* Enter critical section.72*73*/74protected abstract void criticalSection();7576public void enable() {77synchronized (sync) {78if (enabled)79throw new TestBug("Locker already enabled.");80// System.out.println("Enabling " + this);81enabled = true;82thread = new Thread(runnable, "Locker: " + runnable);83thread.setDaemon(true);84thread.start();85}86}8788public void lock() {89synchronized (sync) {90if (locked)91throw new TestBug("Locker already locked.");92// System.out.println("Locking " + this);93locked = true;94sync.notifyAll();95}96}9798public void unlock() {99synchronized (sync) {100if (!locked)101throw new TestBug("Locker not locked.");102// System.out.println("Unocking " + this);103locked = false;104sync.notifyAll();105}106}107108public void disable() {109synchronized (sync) {110if (!enabled)111throw new TestBug("Locker not enabled.");112// System.out.println("Disabling " + this);113enabled = false;114sync.notifyAll();115}116try {117thread.join();118} catch (InterruptedException e) {119throw new TestBug(e);120}121}122123public Throwable getException() {124return exception;125}126}127128129