Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/jdi/ThreadState.java
41161 views
/*1* Copyright (c) 2014, 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.jdi;2425import java.util.concurrent.locks.Lock;26import java.util.concurrent.locks.ReentrantLock;27import java.util.concurrent.locks.Condition;28import java.util.concurrent.TimeUnit;2930import nsk.share.Failure;3132/**33* Functions to set and wait for states in threads.34* Used to sync main thread and debuggee thread.35*/36public class ThreadState {37private final Lock lock = new ReentrantLock();38private final Condition cond = lock.newCondition();39private volatile String currentState;40private long timeoutMs;4142public ThreadState(String startState, long timeoutMs) {43currentState = startState;44this.timeoutMs = timeoutMs;45}4647/**48* Set new state.49*/50public void setState(String newState) {51lock.lock();52try {53log(MSG_SET_STATE, newState);54currentState = newState;55cond.signalAll();56} finally {57lock.unlock();58}59}6061/**62* Wait for the specified state.63* Throws Failure if timeout.64*/65public void waitForState(String waitState) {66lock.lock();67try {68log(MSG_WAIT_STATE, waitState);69while (!currentState.equals(waitState)) {70if (!cond.await(timeoutMs, TimeUnit.MILLISECONDS)) {71throw new Failure(format(MSG_TIMEOUT, waitState));72}73}74log(MSG_GOT_STATE, waitState);75} catch (InterruptedException e) {76e.printStackTrace();77throw new Failure(e);78} finally {79lock.unlock();80}81}8283/**84* Simple helper that sets a new state and then wait for another state.85*/86public void setAndWait(String newState, String waitState) {87setState(newState);88waitForState(waitState);89}9091private static final String MSG_TIMEOUT = "ThreadState(thread='%s', state='%s') timeout waiting for %s";92private static final String MSG_SET_STATE = "ThreadState(thread='%s', state='%s') set state to %s";93private static final String MSG_WAIT_STATE = "ThreadState(thread='%s', state='%s') waiting for state %s";94private static final String MSG_GOT_STATE = "ThreadState(thread='%s', state='%s') got state %s";9596private String format(String pattern, String state) {97final String threadName = Thread.currentThread().getName();98return String.format(pattern, threadName, currentState, state);99}100101private void log(String pattern, String state) {102System.out.println(format(pattern, state));103}104}105106107