mirror of
https://github.com/Z3Prover/z3
synced 2025-09-03 08:38:06 +00:00
* fix #7603: race condition in Ctrl-C handling * fix race in cancel_eh * fix build
This commit is contained in:
parent
7a8ba4b474
commit
b1ab695eb6
9 changed files with 49 additions and 44 deletions
|
@ -18,6 +18,8 @@ Revision History:
|
|||
--*/
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include "util/event_handler.h"
|
||||
|
||||
/**
|
||||
|
@ -25,22 +27,29 @@ Revision History:
|
|||
*/
|
||||
template<typename T>
|
||||
class cancel_eh : public event_handler {
|
||||
bool m_canceled = false;
|
||||
std::mutex m_mutex;
|
||||
std::atomic<bool> m_canceled = false;
|
||||
bool m_auto_cancel = false;
|
||||
T & m_obj;
|
||||
public:
|
||||
cancel_eh(T & o): m_obj(o) {}
|
||||
~cancel_eh() override { if (m_canceled) m_obj.dec_cancel(); if (m_auto_cancel) m_obj.auto_cancel(); }
|
||||
|
||||
// Note that this method doesn't race with the destructor since
|
||||
// potential callers like scoped_ctrl_c/scoped_timer are destroyed
|
||||
// before the cancel_eh destructor is invoked.
|
||||
// Thus, the only races are with itself and with the getters.
|
||||
void operator()(event_handler_caller_t caller_id) override {
|
||||
std::lock_guard lock(m_mutex);
|
||||
if (!m_canceled) {
|
||||
m_caller_id = caller_id;
|
||||
m_obj.inc_cancel();
|
||||
m_canceled = true;
|
||||
m_obj.inc_cancel();
|
||||
}
|
||||
}
|
||||
|
||||
bool canceled() const { return m_canceled; }
|
||||
void reset() { m_canceled = false; }
|
||||
T& t() { return m_obj; }
|
||||
void set_auto_cancel() { m_auto_cancel = true; }
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue