Skip to content
Permalink
Browse files
8241123: Refactor vmTestbase stress framework to use j.u.c and make c…
…reation of threads more flexible

Reviewed-by: iignatyev, sspitsyn
  • Loading branch information
lmesnik committed Mar 20, 2020
1 parent 333a832 commit e7204cbc52ac75a428318ad0ac2ee8d67b686dbd
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -27,18 +27,27 @@
import nsk.share.*;
import nsk.share.jdi.HeapwalkingDebuggee;

import java.util.concurrent.atomic.AtomicBoolean;

/*
* Test class handle request for start/stop test threads(threads are included in special thread group)
*/
public class referringObjects003a extends HeapwalkingDebuggee {

static AtomicBoolean shouldStop = new AtomicBoolean(false);

class TestThread implements Runnable {
public void run() {
wicket.waitFor();
while(!shouldStop.get()) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// just wait for shouldStop
}
}
}
}

private Wicket wicket = new Wicket();

private List<Thread> threads = new ArrayList<Thread>();

private List<ReferringObject> referrers;
@@ -71,7 +80,7 @@ public void startThreads(int threadCount) {
}

public void stopThreads() {
wicket.unlockAll();
shouldStop.set(true);

for (Thread testThread : threads) {
try {
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2003, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -24,6 +24,10 @@
package nsk.share;

import java.io.PrintStream;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
* Wicket provides a means for one or more threads to suspend execution
@@ -60,6 +64,9 @@
/** Wicket's string identifier */
private String name = "";

private final Lock lock = new ReentrantLock();
private final Condition condition = lock.newCondition();

/**
* Construct a Wicket with only one closed lock.
*/
@@ -106,18 +113,26 @@ public Wicket(int count) {
*
* <p>Please note, that the method would ignore Thread.interrupt() requests.
*/
public synchronized void waitFor() {
++waiters;
if (debugOutput != null) {
debugOutput.printf("Wicket %s: waitFor()\n", name);
}
public void waitFor() {
long id = System.currentTimeMillis();

try {
lock.lock();
++waiters;
if (debugOutput != null) {
debugOutput.printf("Wicket %d %s: waitFor(). There are %d waiters totally now.\n", id, name, waiters);
}

while (count > 0) {
try {
wait();
} catch (InterruptedException e) {}
while (count > 0) {
try {
condition.await();
} catch (InterruptedException e) {
}
}
--waiters;
} finally {
lock.unlock();
}
--waiters;
}

/**
@@ -150,25 +165,35 @@ public synchronized void waitFor() {
* @return the number of closed locks
* @throws IllegalArgumentException if timeout is less than 0
*/
public synchronized int waitFor(long timeout) {
if (debugOutput != null) {
debugOutput.printf("Wicket %s: waitFor(%d)\n", name, timeout);
}

public int waitFor(long timeout) {
if (timeout < 0)
throw new IllegalArgumentException(
"timeout value is negative: " + timeout);
++waiters;
long waitTime = timeout;
long startTime = System.currentTimeMillis();
while (count > 0 && waitTime > 0) {
try {
wait(waitTime);
} catch (InterruptedException e) {}
waitTime = timeout - (System.currentTimeMillis() - startTime);
"timeout value is negative: " + timeout);

long id = System.currentTimeMillis();

try {
lock.lock();
++waiters;
if (debugOutput != null) {
debugOutput.printf("Wicket %d %s: waitFor(). There are %d waiters totally now.\n", id, name, waiters);
}

long waitTime = timeout;
long startTime = System.currentTimeMillis();

while (count > 0 && waitTime > 0) {
try {
condition.await(waitTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException e) {
}
waitTime = timeout - (System.currentTimeMillis() - startTime);
}
--waiters;
return count;
} finally {
lock.unlock();
}
--waiters;
return (count);
}

/**
@@ -182,17 +207,23 @@ public synchronized int waitFor(long timeout) {
*
* @throws IllegalStateException if there is no one closed lock
*/
public synchronized void unlock() {
if (debugOutput != null) {
debugOutput.printf("Wicket %s: unlock()\n", name);
}
public void unlock() {

try {
lock.lock();
if (count == 0)
throw new IllegalStateException("locks are already open");

if (count == 0)
throw new IllegalStateException("locks are already open");
--count;
if (debugOutput != null) {
debugOutput.printf("Wicket %s: unlock() the count is now %d\n", name, count);
}

--count;
if (count == 0) {
notifyAll();
if (count == 0) {
condition.signalAll();
}
} finally {
lock.unlock();
}
}

@@ -205,13 +236,18 @@ public synchronized void unlock() {
* this Wicket then they will be released and re-enabled for thread
* scheduling purposes.
*/
public synchronized void unlockAll() {
public void unlockAll() {
if (debugOutput != null) {
debugOutput.printf("Wicket %s: unlockAll()\n", name);
}

count = 0;
notifyAll();
try {
lock.lock();
count = 0;
condition.signalAll();
} finally {
lock.unlock();
}
}

/**
@@ -220,10 +256,15 @@ public synchronized void unlockAll() {
*
* @return number of waiters
*/
public synchronized int getWaiters() {
if (debugOutput != null) {
debugOutput.printf("Wicket %s: getWaiters()\n", name);
public int getWaiters() {
try {
lock.lock();
if (debugOutput != null) {
debugOutput.printf("Wicket %s: getWaiters()\n", name);
}
return waiters;
} finally {
lock.unlock();
}
return waiters;
}
}

0 comments on commit e7204cb

Please sign in to comment.