Skip to content

Commit

Permalink
Added check for non-Ruby threads
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Oct 6, 2024
1 parent dfc2cac commit b19773c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
11 changes: 6 additions & 5 deletions ext/or-tools/constraint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -421,12 +421,13 @@ void init_constraint(Rice::Module& m) {

m.Add(NewFeasibleSolutionObserver(
[&callback](const CpSolverResponse& r) {
// ensure Ruby thread
if (ruby_native_thread_p()) {
// TODO find a better way to do this
callback.call("response=", r);
callback.call("on_solution_callback");
if (!ruby_native_thread_p()) {
throw std::runtime_error("Non-Ruby thread");
}

// TODO find a better way to do this
callback.call("response=", r);
callback.call("on_solution_callback");
})
);
}
Expand Down
10 changes: 10 additions & 0 deletions ext/or-tools/routing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,13 @@ void init_routing(Rice::Module& m) {
.define_method(
"register_unary_transit_callback",
[](RoutingModel& self, Object callback) {
// TODO guard callback?
return self.RegisterUnaryTransitCallback(
[callback](int64_t from_index) -> int64_t {
if (!ruby_native_thread_p()) {
throw std::runtime_error("Non-Ruby thread");
}

return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index));
}
);
Expand All @@ -314,8 +319,13 @@ void init_routing(Rice::Module& m) {
.define_method(
"register_transit_callback",
[](RoutingModel& self, Object callback) {
// TODO guard callback?
return self.RegisterTransitCallback(
[callback](int64_t from_index, int64_t to_index) -> int64_t {
if (!ruby_native_thread_p()) {
throw std::runtime_error("Non-Ruby thread");
}

return Rice::detail::From_Ruby<int64_t>().convert(callback.call("call", from_index, to_index));
}
);
Expand Down

0 comments on commit b19773c

Please sign in to comment.