@@ -161,13 +161,13 @@ func (p *Pigosat) Delete() {
161161 runtime .SetFinalizer (p , nil )
162162}
163163
164- // ready, for use at the beginning of all Pigosat methods, locks p and returns
165- // the corresponding unlock function to be called when the method is done. If
166- // p is uninitialized or already deleted, ready panics.
167- //
168- // If readonly is true, the read-only lock will be used. Otherwise the
169- // read-write lock will be used. A method must be careful not to write to p if
170- // it calls ready with readonly set to true .
164+ // ready readies a Pigosat object for use in a public method. It obtains the
165+ // appropriate lock and returns the appropriate unlocking method, so it can be
166+ // used like
167+ // defer p.ready(readonly)()
168+ // where readonly should be true if the method does not write to p and must be
169+ // false if the method does write to p. If p is uninitialized or deleted,
170+ // ready panics .
171171func (p * Pigosat ) ready (readonly bool ) (unlock func ()) {
172172 if readonly {
173173 p .lock .RLock ()
@@ -186,25 +186,22 @@ func (p *Pigosat) ready(readonly bool) (unlock func()) {
186186// Variables returns the number of variables in the formula: The m in the DIMACS
187187// header "p cnf <m> n".
188188func (p * Pigosat ) Variables () int {
189- unlock := p .ready (true )
190- defer unlock ()
189+ defer p .ready (true )()
191190 // int picosat_variables (PicoSAT *);
192191 return int (C .picosat_variables (p .p ))
193192}
194193
195194// AddedOriginalClauses returns the number of clauses in the formula: The n in
196195// the DIMACS header "p cnf m <n>".
197196func (p * Pigosat ) AddedOriginalClauses () int {
198- unlock := p .ready (true )
199- defer unlock ()
197+ defer p .ready (true )()
200198 // int picosat_added_original_clauses (PicoSAT *);
201199 return int (C .picosat_added_original_clauses (p .p ))
202200}
203201
204202// Seconds returns the time spent in the PicoSAT library.
205203func (p * Pigosat ) Seconds () time.Duration {
206- unlock := p .ready (true )
207- defer unlock ()
204+ defer p .ready (true )()
208205 // double picosat_seconds (PicoSAT *);
209206 return time .Duration (float64 (C .picosat_seconds (p .p )) * float64 (time .Second ))
210207}
@@ -218,8 +215,7 @@ func (p *Pigosat) Seconds() time.Duration {
218215// the clause, and causes AddClauses to skip reading the rest of the slice. Nil
219216// slices are ignored and skipped.
220217func (p * Pigosat ) AddClauses (clauses [][]int32 ) {
221- unlock := p .ready (false )
222- defer unlock ()
218+ defer p .ready (false )()
223219 var had0 bool
224220 for _ , clause := range clauses {
225221 if len (clause ) == 0 {
@@ -263,8 +259,7 @@ func (p *Pigosat) blocksol(sol []bool) {
263259// // Do stuff with status, solution
264260// }
265261func (p * Pigosat ) Solve () (status int , solution []bool ) {
266- unlock := p .ready (false )
267- defer unlock ()
262+ defer p .ready (false )()
268263 // int picosat_sat (PicoSAT *, int decision_limit);
269264 status = int (C .picosat_sat (p .p , - 1 ))
270265 if status == Unsatisfiable || status == Unknown {
@@ -293,8 +288,7 @@ func (p *Pigosat) Solve() (status int, solution []bool) {
293288// length. There is no need to call BlockSolution after calling Pigosat.Solve,
294289// which calls it automatically for every Satisfiable solution.
295290func (p * Pigosat ) BlockSolution (solution []bool ) error {
296- unlock := p .ready (false )
297- defer unlock ()
291+ defer p .ready (false )()
298292 if n := int (C .picosat_variables (p .p )); len (solution ) != n + 1 {
299293 return fmt .Errorf ("solution length %d, but have %d variables" ,
300294 len (solution ), n )
0 commit comments