Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/main/java/uk/co/edstow/cain/ReverseSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ public RunConfig<G,T,R> setCostFunction(Function<Plan<G,T,R>, Integer> costFunct
private final int goalReductionsPerStep;
private final int goalReductionsTolerance;

private final Object workMonitor = new Object();

public ReverseSearch(List<G> initialGoals, List<G> finalGoals, Generator<G,T,R> generator, RunConfig<G,T,R> runConfig, RegisterAllocator<G,T,R> registerAllocator) {
this.liveCounter = runConfig.liveCounter;
Expand Down Expand Up @@ -409,7 +410,9 @@ public void run() {
}
endTime();
}
if(workersFinished.tryAcquire()){

boolean allSleeping = workersThreads.stream().noneMatch(w -> w.active);
if(workersFinished.tryAcquire() || allSleeping){
workersFinished.release();
if(!quiet) {
System.out.println("\nWorkers Are finished");
Expand Down Expand Up @@ -488,12 +491,25 @@ public void run() {
}

private WorkState<G,T,R> stealWork() throws InterruptedException {
Worker c = next;
WorkState<G,T,R> s = null;
while (s==null){
s = localTraversalSystem.steal(c.localTraversalSystem);
c = c.next;
}
while (true) {

for (Worker c = next; c.id != id && s == null; c = c.next) {
s = localTraversalSystem.steal(c.localTraversalSystem);
}

if (s != null)
break;

synchronized (workMonitor) {
try {
active = false;
workMonitor.wait();
active = true;
} catch (InterruptedException ignored) {}
}
}

steals++;
return s;
}
Expand Down Expand Up @@ -604,7 +620,9 @@ private void iSearch(WorkState<G,T,R> s){
}
WorkState<G,T,R> next = new WorkState<>(depth, goals, currentPlan, goalPairs);
localTraversalSystem.add(child, next);

synchronized (workMonitor) {
workMonitor.notifyAll();
}
}

private boolean tryDirectSolve(int depth, GoalBag<G> goals, Plan<G,T,R> currentPlan) {
Expand Down