[CP-SAT] fix potential race when stopping a search asynchronously

This commit is contained in:
Laurent Perron
2025-03-25 11:22:07 -07:00
parent 2d03cb0b18
commit 420c2fd26b
5 changed files with 11 additions and 4 deletions

View File

@@ -3901,7 +3901,6 @@ cc_library(
":util",
"//ortools/util:logging",
"//ortools/util:sorted_interval_list",
"//ortools/util:time_limit",
"@abseil-cpp//absl/log:check",
],
)

View File

@@ -93,6 +93,7 @@ JAGGED_MATRIX_AS_CSHARP_ARRAY(int64_t, int64_t, long, Int64VectorVector);
// Wrap the SolveWrapper class.
%unignore operations_research::sat::SolveWrapper;
%unignore operations_research::sat::SolveWrapper::SolveWrapper;
%unignore operations_research::sat::SolveWrapper::AddBestBoundCallbackFromClass;
%unignore operations_research::sat::SolveWrapper::AddLogCallbackFromClass;
%unignore operations_research::sat::SolveWrapper::AddSolutionCallback;

View File

@@ -169,6 +169,7 @@ PROTO2_RETURN(operations_research::sat::CpSolverResponse,
// Wrap the SolveWrapper class.
%unignore operations_research::sat::SolveWrapper;
%unignore operations_research::sat::SolveWrapper::SolveWrapper;
%rename (addBestBoundCallback) operations_research::sat::SolveWrapper::AddBestBoundCallback;
%rename (addLogCallback) operations_research::sat::SolveWrapper::AddLogCallback;
%rename (addSolutionCallback) operations_research::sat::SolveWrapper::AddSolutionCallback;

View File

@@ -26,6 +26,7 @@
#include "ortools/sat/cp_model_utils.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
#include "ortools/sat/util.h"
#include "ortools/util/logging.h"
#include "ortools/util/sorted_interval_list.h"
@@ -102,6 +103,10 @@ void SolutionCallback::SetWrapperClass(SolveWrapper* wrapper) const {
bool SolutionCallback::HasResponse() const { return has_response_; }
SolveWrapper::SolveWrapper() {
shared_time_limit_ = model_.GetOrCreate<ModelSharedTimeLimit>();
}
void SolveWrapper::SetParameters(
const operations_research::sat::SatParameters& parameters) {
model_.Add(NewSatParameters(parameters));
@@ -154,9 +159,7 @@ operations_research::sat::CpSolverResponse SolveWrapper::Solve(
return operations_research::sat::SolveCpModel(model_proto, &model_);
}
void SolveWrapper::StopSearch() {
::operations_research::sat::StopSearch(&model_);
}
void SolveWrapper::StopSearch() { shared_time_limit_->Stop(); }
std::string CpSatHelper::ModelStats(
const operations_research::sat::CpModelProto& model_proto) {

View File

@@ -21,6 +21,7 @@
#include "ortools/sat/cp_model.pb.h"
#include "ortools/sat/model.h"
#include "ortools/sat/sat_parameters.pb.h"
#include "ortools/sat/util.h"
#include "ortools/util/sorted_interval_list.h"
namespace operations_research {
@@ -95,6 +96,7 @@ class BestBoundCallback {
// This class is not meant to be reused after one solve.
class SolveWrapper {
public:
SolveWrapper();
// The arguments of the functions defined below must follow these rules
// to be wrapped by swig correctly:
// 1) Their types must include the full operations_research::sat::
@@ -124,6 +126,7 @@ class SolveWrapper {
private:
Model model_;
ModelSharedTimeLimit* shared_time_limit_;
};
// Static methods are stored in a module which name can vary.