3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-15 13:28:47 +00:00

further simplifications to scoped_timer

This commit is contained in:
Nuno Lopes 2019-02-21 10:42:42 +00:00
parent 598fc810b5
commit f3cd7d646d

View file

@ -26,39 +26,33 @@ Revision History:
struct scoped_timer::imp { struct scoped_timer::imp {
event_handler * m_eh; private:
std::thread m_thread; std::thread m_thread;
std::timed_mutex m_mutex; std::timed_mutex m_mutex;
unsigned m_ms;
static void* thread_func(imp * st) { static void thread_func(unsigned ms, event_handler * eh, std::timed_mutex * mutex) {
auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(st->m_ms); auto end = std::chrono::steady_clock::now() + std::chrono::milliseconds(ms);
while (!st->m_mutex.try_lock_until(end)) { while (!mutex->try_lock_until(end)) {
if (std::chrono::steady_clock::now() > end) { if (std::chrono::steady_clock::now() >= end) {
st->m_eh->operator()(TIMEOUT_EH_CALLER); eh->operator()(TIMEOUT_EH_CALLER);
return nullptr; return;
} }
} }
st->m_mutex.unlock(); mutex->unlock();
return nullptr;
} }
imp(unsigned ms, event_handler * eh): public:
m_eh(eh), m_ms(ms) { imp(unsigned ms, event_handler * eh) {
m_mutex.lock(); m_mutex.lock();
m_thread = std::thread(thread_func, this); m_thread = std::thread(thread_func, ms, eh, &m_mutex);
} }
~imp() { ~imp() {
m_mutex.unlock(); m_mutex.unlock();
while (!m_thread.joinable()) {
std::this_thread::yield();
}
m_thread.join(); m_thread.join();
} }
}; };
scoped_timer::scoped_timer(unsigned ms, event_handler * eh) { scoped_timer::scoped_timer(unsigned ms, event_handler * eh) {
@ -69,6 +63,5 @@ scoped_timer::scoped_timer(unsigned ms, event_handler * eh) {
} }
scoped_timer::~scoped_timer() { scoped_timer::~scoped_timer() {
if (m_imp) dealloc(m_imp);
dealloc(m_imp);
} }