30
30
31
31
import com .google .common .base .Preconditions ;
32
32
import com .google .common .base .Splitter ;
33
+ import com .google .common .base .Throwables ;
33
34
import com .google .common .collect .ImmutableMap ;
34
35
import com .google .common .collect .Lists ;
35
36
import com .google .common .primitives .Longs ;
42
43
import java .util .Objects ;
43
44
import java .util .Optional ;
44
45
import java .util .Set ;
46
+ import java .util .concurrent .Callable ;
45
47
import org .sosy_lab .common .ShutdownNotifier ;
46
48
import org .sosy_lab .java_smt .api .BooleanFormula ;
47
49
import org .sosy_lab .java_smt .api .Evaluator ;
@@ -58,7 +60,6 @@ abstract class Mathsat5AbstractProver<T2> extends AbstractProver<T2> {
58
60
protected final Mathsat5SolverContext context ;
59
61
protected final long curEnv ;
60
62
private final long curConfig ;
61
- private final long terminationTest ;
62
63
protected final Mathsat5FormulaCreator creator ;
63
64
private final ShutdownNotifier shutdownNotifier ;
64
65
@@ -72,7 +73,6 @@ protected Mathsat5AbstractProver(
72
73
creator = pCreator ;
73
74
curConfig = buildConfig (pOptions );
74
75
curEnv = context .createEnvironment (curConfig );
75
- terminationTest = context .addTerminationTest (curEnv );
76
76
shutdownNotifier = pShutdownNotifier ;
77
77
}
78
78
@@ -98,29 +98,53 @@ private long buildConfig(Set<ProverOptions> opts) {
98
98
/** add needed options into the given map. */
99
99
protected abstract void createConfig (Map <String , String > pConfig );
100
100
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
+
101
114
@ Override
102
- public boolean isUnsat () throws InterruptedException , SolverException {
115
+ public synchronized boolean isUnsat () throws InterruptedException , SolverException {
103
116
Preconditions .checkState (!closed );
104
117
boolean result ;
105
118
try {
106
- result = !msat_check_sat (curEnv );
107
- } catch (IllegalStateException pE ) {
119
+ result = exec (() -> !msat_check_sat (curEnv ) );
120
+ } catch (IllegalStateException e ) {
108
121
if (Objects .equals (
109
- pE .getMessage (), "msat_solve returned \" unknown\" : user-requested termination" )) {
122
+ e .getMessage (), "msat_solve returned \" unknown\" : user-requested termination" )) {
110
123
assert shutdownNotifier .shouldShutdown ();
111
124
throw new InterruptedException ();
112
125
}
113
- throw pE ;
126
+ throw e ;
114
127
}
115
128
return result ;
116
129
}
117
130
118
131
@ Override
119
- public boolean isUnsatWithAssumptions (Collection <BooleanFormula > pAssumptions )
132
+ public synchronized boolean isUnsatWithAssumptions (Collection <BooleanFormula > pAssumptions )
120
133
throws SolverException , InterruptedException {
121
134
Preconditions .checkState (!closed );
122
135
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 ;
124
148
}
125
149
126
150
private void checkForLiterals (Collection <BooleanFormula > formulas ) {
@@ -226,7 +250,6 @@ public ImmutableMap<String, String> getStatistics() {
226
250
public void close () {
227
251
if (!closed ) {
228
252
msat_destroy_env (curEnv );
229
- msat_free_termination_callback (terminationTest );
230
253
msat_destroy_config (curConfig );
231
254
}
232
255
super .close ();
@@ -237,7 +260,7 @@ protected boolean isClosed() {
237
260
}
238
261
239
262
@ Override
240
- public <T > T allSat (AllSatCallback <T > callback , List <BooleanFormula > important )
263
+ public synchronized <T > T allSat (AllSatCallback <T > callback , List <BooleanFormula > important )
241
264
throws InterruptedException , SolverException {
242
265
Preconditions .checkState (!closed );
243
266
checkGenerateAllSat ();
0 commit comments