Google OR-Tools v9.11
a fast and portable software suite for combinatorial optimization
|
#include <solve_interrupter.h>
Public Types | |
using | Callback = std::function<void()> |
Public Member Functions | |
DEFINE_STRONG_INT_TYPE (CallbackId, int64_t) | |
Id used to identify a callback. | |
SolveInterrupter ()=default | |
SolveInterrupter (const SolveInterrupter &)=delete | |
SolveInterrupter & | operator= (const SolveInterrupter &)=delete |
void | Interrupt () |
bool | IsInterrupted () const |
CallbackId | AddInterruptionCallback (Callback callback) const |
void | RemoveInterruptionCallback (CallbackId id) const |
Interrupter used by solvers to know if/when they should interrupt the solve.
Once triggered with Interrupt(), an interrupter can't be reset. It can be triggered from any thread.
Thread-safety: APIs on this class are safe to call concurrently from multiple threads.
Definition at line 36 of file solve_interrupter.h.
using operations_research::SolveInterrupter::Callback = std::function<void()> |
Definition at line 41 of file solve_interrupter.h.
|
default |
|
delete |
SolveInterrupter::CallbackId operations_research::SolveInterrupter::AddInterruptionCallback | ( | Callback | callback | ) | const |
Registers a callback to be called when the interruption is requested.
The callback is immediately called if the interrupter has already been triggered or if it is triggered during the registration. This is typically useful for a solver implementation so that it does not have to test IsInterrupted() to do the same thing it does in the callback. Simply registering the callback is enough.
The callback function can't make calls to AddInterruptionCallback(), RemoveInterruptionCallback() and Interrupt(). This would result is a deadlock. Calling IsInterrupted() is fine though.
This method is const
since it does not modify the state of the interrupter (the result of IsInterrupted()). This enables passing a const-ref to solvers, making sure they can't call Interrupt() by mistake.
We must make this call while holding the lock since we want to be sure that the calls to the callbacks_ won't occur before we registered the new one. If we were not holding the lock, this could return false and before we could add the new callback to callbacks_, the Interrupt() function may still have called them.
We make the call before putting the callback in the map to since we need to move it in place.
Definition at line 54 of file solve_interrupter.cc.
operations_research::SolveInterrupter::DEFINE_STRONG_INT_TYPE | ( | CallbackId | , |
int64_t | ) |
Id used to identify a callback.
void operations_research::SolveInterrupter::Interrupt | ( | ) |
Interrupts the solve as soon as possible.
Once requested the interruption can't be reset. The user should use a new SolveInterrupter for later solves.
It is safe to call this function multiple times. Only the first call will have visible effects; other calls will be ignored.
Here we don't use compare_exchange_strong since we need to hold the lock before changing the value of interrupted_ anyway. So there is no need to use this complex function.
We must not call the callbacks more than once.
We need to change this value while holding the lock since in AddInterruptionCallback() we must know if we need to call the new callback of if this function has called it.
We are holding the lock while calling callbacks. This make it impossible to call Interrupt(), AddInterruptionCallback(), or RemoveInterruptionCallback() from a callback but it ensures that external code that can modify callbacks_ will wait the end of Interrupt.
Definition at line 29 of file solve_interrupter.cc.
|
inline |
Returns true if the solve interruption has been requested.
This API is fast; it costs the read of an atomic.
Definition at line 60 of file solve_interrupter.h.
|
delete |
void operations_research::SolveInterrupter::RemoveInterruptionCallback | ( | CallbackId | id | ) | const |
Unregisters a callback previously registered. It fails (with a CHECK) if the callback was already unregistered or unkonwn. After this calls returns, the caller can assume the callback won't be called.
This function can't be called from a callback since this would result in a deadlock.
Definition at line 76 of file solve_interrupter.cc.