org.snapscript.agent.debug.SuspendController Maven / Gradle / Ivy
package org.snapscript.agent.debug;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public class SuspendController {
private final Map listeners;
private final Map browsers;
private final Map types;
private final Map locks;
public SuspendController() {
this.listeners = new ConcurrentHashMap();
this.browsers = new ConcurrentHashMap();
this.types = new ConcurrentHashMap();
this.locks = new ConcurrentHashMap();
}
public ResumeType suspend(ResumeListener listener, ScopeBrowser browser) {
String name = Thread.currentThread().getName();
Object lock = locks.get(name);
if(lock == null) {
lock = new Object();
locks.put(name, lock);
}
synchronized(lock) {
try {
browsers.put(name, browser);
listeners.put(name, listener);
lock.wait();
}catch(Exception e) {
throw new IllegalStateException("Could not suspend thread '" + name + "'", e);
}
}
return types.remove(name); // resume in a specific way
}
public void resume(ResumeType type, String thread) {
Object lock = locks.get(thread);
ResumeListener listener = listeners.remove(thread);
synchronized(lock) {
try {
browsers.remove(thread);
if(listener != null) {
types.put(thread, type);
listener.resume(thread);
}
lock.notify();
}catch(Exception e) {
throw new IllegalStateException("Could not resume thread '" + thread + "'", e);
}
}
}
public void browse(Set expand, String thread) {
Object lock = locks.get(thread);
ScopeBrowser browser = browsers.get(thread);
synchronized(lock) {
try {
if(browser != null) {
browser.browse(expand);
}
}catch(Exception e) {
throw new IllegalStateException("Could not browse thread '" + thread + "'", e);
}
}
}
public void evaluate(Set expand, String thread, String expression, boolean refresh) {
Object lock = locks.get(thread);
ScopeBrowser browser = browsers.get(thread);
synchronized(lock) {
try {
if(browser != null) {
browser.evaluate(expand, expression, refresh);
}
}catch(Exception e) {
throw new IllegalStateException("Could not evaluate '" + expression + "' for thread '" + thread + "'", e);
}
}
}
}