mirror of
https://github.com/Z3Prover/z3
synced 2026-06-29 11:58:51 +00:00
add the safe_run pattern from the tactic (which handles external cancel like Ctrl+C safely) to smt_parallel
This commit is contained in:
parent
293cb384c2
commit
e45683de4b
2 changed files with 44 additions and 4 deletions
|
|
@ -1112,6 +1112,11 @@ namespace smt {
|
||||||
cancel_background_threads();
|
cancel_background_threads();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void parallel::batch_manager::set_canceled() {
|
||||||
|
std::scoped_lock lock(mux);
|
||||||
|
set_canceled_unlocked();
|
||||||
|
}
|
||||||
|
|
||||||
void parallel::batch_manager::release_worker_lease_unlocked(unsigned worker_id, node_lease& lease) {
|
void parallel::batch_manager::release_worker_lease_unlocked(unsigned worker_id, node_lease& lease) {
|
||||||
if (worker_id >= m_worker_leases.size()) {
|
if (worker_id >= m_worker_leases.size()) {
|
||||||
lease = {};
|
lease = {};
|
||||||
|
|
@ -1902,17 +1907,51 @@ namespace smt {
|
||||||
|
|
||||||
m_batch_manager.initialize(num_global_bb_threads);
|
m_batch_manager.initialize(num_global_bb_threads);
|
||||||
|
|
||||||
|
auto safe_run = [&](auto&& run_fn, reslimit& lim) {
|
||||||
|
try {
|
||||||
|
run_fn();
|
||||||
|
if (lim.is_canceled())
|
||||||
|
m_batch_manager.set_canceled();
|
||||||
|
} catch (z3_error &err) {
|
||||||
|
IF_VERBOSE(0, verbose_stream() << "Exception in parallel solver: " << err.what() << "\n");
|
||||||
|
if (!lim.is_canceled())
|
||||||
|
m_batch_manager.set_exception(err.error_code());
|
||||||
|
else
|
||||||
|
m_batch_manager.set_canceled();
|
||||||
|
} catch (z3_exception &ex) {
|
||||||
|
IF_VERBOSE(0, verbose_stream() << "Exception in parallel solver: " << ex.what() << "\n");
|
||||||
|
if (!lim.is_canceled() && !is_cancellation_exception(ex.what()))
|
||||||
|
m_batch_manager.set_exception(ex.what());
|
||||||
|
else
|
||||||
|
m_batch_manager.set_canceled();
|
||||||
|
} catch (...) {
|
||||||
|
IF_VERBOSE(0, verbose_stream() << "Unknown exception in parallel solver\n");
|
||||||
|
if (!lim.is_canceled())
|
||||||
|
m_batch_manager.set_exception("unknown exception");
|
||||||
|
else
|
||||||
|
m_batch_manager.set_canceled();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// Launch threads
|
// Launch threads
|
||||||
vector<std::thread> threads(total_threads);
|
vector<std::thread> threads(total_threads);
|
||||||
unsigned thread_idx = 0;
|
unsigned thread_idx = 0;
|
||||||
for (auto* w : m_workers)
|
for (auto* w : m_workers)
|
||||||
threads[thread_idx++] = std::thread([&, w]() { w->run(); });
|
threads[thread_idx++] = std::thread([w, &safe_run]() {
|
||||||
|
safe_run([w]() { w->run(); }, w->limit());
|
||||||
|
});
|
||||||
if (m_sls_worker)
|
if (m_sls_worker)
|
||||||
threads[thread_idx++] = std::thread([&]() { m_sls_worker->run(); });
|
threads[thread_idx++] = std::thread([this, &safe_run]() {
|
||||||
|
safe_run([this]() { m_sls_worker->run(); }, m_sls_worker->limit());
|
||||||
|
});
|
||||||
if (m_core_minimizer_worker)
|
if (m_core_minimizer_worker)
|
||||||
threads[thread_idx++] = std::thread([&]() { m_core_minimizer_worker->run(); });
|
threads[thread_idx++] = std::thread([this, &safe_run]() {
|
||||||
|
safe_run([this]() { m_core_minimizer_worker->run(); }, m_core_minimizer_worker->limit());
|
||||||
|
});
|
||||||
for (auto* w : m_global_backbones_workers)
|
for (auto* w : m_global_backbones_workers)
|
||||||
threads[thread_idx++] = std::thread([&, w]() { w->run(); });
|
threads[thread_idx++] = std::thread([w, &safe_run]() {
|
||||||
|
safe_run([w]() { w->run(); }, w->limit());
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
// Wait for all threads to finish
|
// Wait for all threads to finish
|
||||||
|
|
|
||||||
|
|
@ -196,6 +196,7 @@ namespace smt {
|
||||||
|
|
||||||
void set_unsat(ast_translation& l2g, expr_ref_vector const& unsat_core);
|
void set_unsat(ast_translation& l2g, expr_ref_vector const& unsat_core);
|
||||||
void set_sat(ast_translation& l2g, model& m);
|
void set_sat(ast_translation& l2g, model& m);
|
||||||
|
void set_canceled();
|
||||||
void set_exception(std::string const& msg);
|
void set_exception(std::string const& msg);
|
||||||
void set_exception(unsigned error_code);
|
void set_exception(unsigned error_code);
|
||||||
void collect_statistics(::statistics& st) const;
|
void collect_statistics(::statistics& st) const;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue