@@ -71,12 +71,12 @@ func SolveCpModelWithParameters(input *cmpb.CpModelProto, params *sppb.SatParame
7171}
7272
7373// SolveCpModelInterruptibleWithParameters solves a CP Model with the given input proto
74- // and parameters and returns a CPSolverResponse. The solve can be interrupted by triggering
75- // the `interrupt `.
74+ // and parameters and returns a CPSolverResponse. The solve can be interrupted by calling
75+ // the `stopSolve `.
7676func SolveCpModelInterruptibleWithParameters (input * cmpb.CpModelProto , params * sppb.SatParameters , interrupt <- chan struct {}) (* cmpb.CpSolverResponse , error ) {
77- // Create the atomic bool for interrupting solves.
78- limitReached := newAtomicBoolWrapper ()
79- defer limitReached .delete ()
77+ // Create the environment for interrupting solves.
78+ env := newEnvWrapper ()
79+ defer env .delete ()
8080
8181 // Transform `input` into bytes.
8282 bReq , err := proto .Marshal (input )
@@ -100,25 +100,24 @@ func SolveCpModelInterruptibleWithParameters(input *cmpb.CpModelProto, params *s
100100 go func () {
101101 select {
102102 case <- interrupt :
103- limitReached . trigger ()
103+ env . stopSolve ()
104104 case <- solveDone :
105105 }
106106 }()
107107
108- // We want to make sure we trigger the atomic Bool before we call the solver
109- // if the input `interrupt` is already closed. We can't trust the
108+ // We want to make sure we stop the search before we call the solver. We can't trust the
110109 // scheduler to execute the previous goroutine immediately, even calling
111110 // `runtime.Gosched()` (the unit test failed 3 out of 1000 times when doing
112111 // so).
113112 select {
114113 case <- interrupt :
115- limitReached . trigger ()
114+ env . stopSolve ()
116115 default :
117116 }
118117
119118 var cRes unsafe.Pointer
120119 var cResLen C.int
121- C .SolveCpInterruptible (limitReached .ptr , cReq , C .int (len (bReq )), cParams , C .int (len (bParams )), & cRes , & cResLen )
120+ C .SolveCpInterruptible (env .ptr , cReq , C .int (len (bReq )), cParams , C .int (len (bParams )), & cRes , & cResLen )
122121 defer C .free (cRes )
123122
124123 // Transform `cRes` into the Go response proto.
@@ -131,29 +130,29 @@ func SolveCpModelInterruptibleWithParameters(input *cmpb.CpModelProto, params *s
131130 return result , nil
132131}
133132
134- // atomicBoolWrapper keeps a pointer on a C++ AtomicBool instance.
135- type atomicBoolWrapper struct {
133+ // envWrapper keeps a pointer on a C++ Model instance.
134+ type envWrapper struct {
136135 mutex sync.Mutex
137136 ptr unsafe.Pointer // Guarded by mutex.
138137}
139138
140- // newAtomicBoolWrapper returns a new instance of a C++ AtomicBool .
139+ // newEnvWrapper returns a new instance of a C++ Model .
141140//
142141// The returned object must be destroyed with delete() for the C++ object not to
143142// leak.
144143//
145- // This object is thread-safe: delete() and trigger () can be called
144+ // This object is thread-safe: delete() and stopSolve () can be called
146145// concurrently.
147- func newAtomicBoolWrapper () * atomicBoolWrapper {
148- return & atomicBoolWrapper {
149- ptr : C .SolveCpNewAtomicBool (),
146+ func newEnvWrapper () * envWrapper {
147+ return & envWrapper {
148+ ptr : C .SolveCpNewEnv (),
150149 }
151150}
152151
153- // trigger triggers the C++ SolveCpStopSolve method with the atomic bool .
152+ // stopSolve triggers the C++ SolveCpStopSolve method with the environment .
154153//
155- // If the atomic bool has been deleted this has no effect.
156- func (intr * atomicBoolWrapper ) trigger () {
154+ // If the environment has been deleted this has no effect.
155+ func (intr * envWrapper ) stopSolve () {
157156 intr .mutex .Lock ()
158157 defer intr .mutex .Unlock ()
159158 if uintptr (intr .ptr ) != 0 {
@@ -164,12 +163,12 @@ func (intr *atomicBoolWrapper) trigger() {
164163// delete deletes the underlying C++ object.
165164//
166165// Calling it multiple times has not effect.
167- func (intr * atomicBoolWrapper ) delete () {
166+ func (intr * envWrapper ) delete () {
168167 intr .mutex .Lock ()
169168 defer intr .mutex .Unlock ()
170169 // We don't test that intr.ptr is not nullptr here since C++ `delete` can be
171170 // called with nullptr.
172- C .SolveCpDestroyAtomicBool (intr .ptr )
171+ C .SolveCpDestroyEnv (intr .ptr )
173172 intr .ptr = unsafe .Pointer (uintptr (0 ))
174173}
175174
0 commit comments