Skip to content

Commit 4464c90

Browse files
Bugfix for #339
1 parent bd81f54 commit 4464c90

File tree

3 files changed

+35
-13
lines changed

3 files changed

+35
-13
lines changed

src/org/sosy_lab/java_smt/solvers/mathsat5/Mathsat5AbstractProver.java

+34-11
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030

3131
import com.google.common.base.Preconditions;
3232
import com.google.common.base.Splitter;
33+
import com.google.common.base.Throwables;
3334
import com.google.common.collect.ImmutableMap;
3435
import com.google.common.collect.Lists;
3536
import com.google.common.primitives.Longs;
@@ -42,6 +43,7 @@
4243
import java.util.Objects;
4344
import java.util.Optional;
4445
import java.util.Set;
46+
import java.util.concurrent.Callable;
4547
import org.sosy_lab.common.ShutdownNotifier;
4648
import org.sosy_lab.java_smt.api.BooleanFormula;
4749
import org.sosy_lab.java_smt.api.Evaluator;
@@ -58,7 +60,6 @@ abstract class Mathsat5AbstractProver<T2> extends AbstractProver<T2> {
5860
protected final Mathsat5SolverContext context;
5961
protected final long curEnv;
6062
private final long curConfig;
61-
private final long terminationTest;
6263
protected final Mathsat5FormulaCreator creator;
6364
private final ShutdownNotifier shutdownNotifier;
6465

@@ -72,7 +73,6 @@ protected Mathsat5AbstractProver(
7273
creator = pCreator;
7374
curConfig = buildConfig(pOptions);
7475
curEnv = context.createEnvironment(curConfig);
75-
terminationTest = context.addTerminationTest(curEnv);
7676
shutdownNotifier = pShutdownNotifier;
7777
}
7878

@@ -98,29 +98,53 @@ private long buildConfig(Set<ProverOptions> opts) {
9898
/** add needed options into the given map. */
9999
protected abstract void createConfig(Map<String, String> pConfig);
100100

101+
private <T> T exec(Callable<T> closure) throws SolverException {
102+
long hook = context.addTerminationTest(curEnv);
103+
T value = null;
104+
try {
105+
value = closure.call();
106+
} catch (Throwable t) {
107+
Throwables.propagateIfPossible(t, IllegalStateException.class, SolverException.class);
108+
} finally {
109+
msat_free_termination_callback(hook);
110+
}
111+
return value;
112+
}
113+
101114
@Override
102-
public boolean isUnsat() throws InterruptedException, SolverException {
115+
public synchronized boolean isUnsat() throws InterruptedException, SolverException {
103116
Preconditions.checkState(!closed);
104117
boolean result;
105118
try {
106-
result = !msat_check_sat(curEnv);
107-
} catch (IllegalStateException pE) {
119+
result = exec(() -> !msat_check_sat(curEnv));
120+
} catch (IllegalStateException e) {
108121
if (Objects.equals(
109-
pE.getMessage(), "msat_solve returned \"unknown\": user-requested termination")) {
122+
e.getMessage(), "msat_solve returned \"unknown\": user-requested termination")) {
110123
assert shutdownNotifier.shouldShutdown();
111124
throw new InterruptedException();
112125
}
113-
throw pE;
126+
throw e;
114127
}
115128
return result;
116129
}
117130

118131
@Override
119-
public boolean isUnsatWithAssumptions(Collection<BooleanFormula> pAssumptions)
132+
public synchronized boolean isUnsatWithAssumptions(Collection<BooleanFormula> pAssumptions)
120133
throws SolverException, InterruptedException {
121134
Preconditions.checkState(!closed);
122135
checkForLiterals(pAssumptions);
123-
return !msat_check_sat_with_assumptions(curEnv, getMsatTerm(pAssumptions));
136+
boolean result;
137+
try {
138+
result = exec(() -> !msat_check_sat_with_assumptions(curEnv, getMsatTerm(pAssumptions)));
139+
} catch (IllegalStateException e) {
140+
if (Objects.equals(
141+
e.getMessage(), "msat_solve returned \"unknown\": user-requested termination")) {
142+
assert shutdownNotifier.shouldShutdown();
143+
throw new InterruptedException();
144+
}
145+
throw e;
146+
}
147+
return result;
124148
}
125149

126150
private void checkForLiterals(Collection<BooleanFormula> formulas) {
@@ -226,7 +250,6 @@ public ImmutableMap<String, String> getStatistics() {
226250
public void close() {
227251
if (!closed) {
228252
msat_destroy_env(curEnv);
229-
msat_free_termination_callback(terminationTest);
230253
msat_destroy_config(curConfig);
231254
}
232255
super.close();
@@ -237,7 +260,7 @@ protected boolean isClosed() {
237260
}
238261

239262
@Override
240-
public <T> T allSat(AllSatCallback<T> callback, List<BooleanFormula> important)
263+
public synchronized <T> T allSat(AllSatCallback<T> callback, List<BooleanFormula> important)
241264
throws InterruptedException, SolverException {
242265
Preconditions.checkState(!closed);
243266
checkGenerateAllSat();

src/org/sosy_lab/java_smt/solvers/mathsat5/Mathsat5InterpolatingProver.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public List<BooleanFormula> getTreeInterpolants(
152152
}
153153

154154
@Override
155-
public <T> T allSat(AllSatCallback<T> callback, List<BooleanFormula> important) {
155+
public synchronized <T> T allSat(AllSatCallback<T> callback, List<BooleanFormula> important) {
156156
// TODO how can we support allsat in MathSat5-interpolation-prover?
157157
// error: "allsat is not compatible wwith proof generation"
158158
throw new UnsupportedOperationException(

src/org/sosy_lab/java_smt/solvers/mathsat5/Mathsat5NativeApiTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -537,7 +537,6 @@ public void enumTypeTest() throws SolverException, InterruptedException {
537537

538538
private final ExecutorService executor = Executors.newSingleThreadExecutor();
539539

540-
541540
private long createSharedEnv(long sibling) {
542541
long cfg = msat_create_config();
543542
msat_set_option_checked(cfg, "dpll.ghost_filtering", "true");

0 commit comments

Comments
 (0)