2013-11-13 05:16:10 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
2013-12-10 00:55:13 +00:00
|
|
|
#include "util/thread.h"
|
2013-11-13 05:16:10 +00:00
|
|
|
#include "util/interrupt.h"
|
|
|
|
#include "util/exception.h"
|
2015-01-15 21:50:02 +00:00
|
|
|
#include "util/memory.h"
|
2013-11-13 05:16:10 +00:00
|
|
|
|
|
|
|
namespace lean {
|
2014-06-07 17:18:32 +00:00
|
|
|
MK_THREAD_LOCAL_GET(atomic_bool, get_g_interrupt, false);
|
2013-11-13 05:16:10 +00:00
|
|
|
|
|
|
|
void request_interrupt() {
|
2014-06-07 17:18:32 +00:00
|
|
|
get_g_interrupt().store(true);
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void reset_interrupt() {
|
2014-06-07 17:18:32 +00:00
|
|
|
get_g_interrupt().store(false);
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 05:42:22 +00:00
|
|
|
bool interrupt_requested() {
|
2014-06-07 17:18:32 +00:00
|
|
|
return get_g_interrupt().load();
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
2013-11-13 05:42:22 +00:00
|
|
|
void check_interrupted() {
|
2013-11-21 23:31:09 +00:00
|
|
|
if (interrupt_requested()) {
|
|
|
|
reset_interrupt();
|
2013-11-13 05:16:10 +00:00
|
|
|
throw interrupted();
|
2013-11-21 23:31:09 +00:00
|
|
|
}
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
2015-01-15 21:50:02 +00:00
|
|
|
void check_system(char const * component_name) {
|
|
|
|
check_stack(component_name);
|
|
|
|
check_memory(component_name);
|
|
|
|
check_interrupted();
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:32:09 +00:00
|
|
|
void sleep_for(unsigned ms, unsigned step_ms) {
|
|
|
|
if (step_ms == 0)
|
|
|
|
step_ms = 1;
|
|
|
|
unsigned rounds = ms / step_ms;
|
2013-12-10 00:55:13 +00:00
|
|
|
chrono::milliseconds c(step_ms);
|
|
|
|
chrono::milliseconds r(ms % step_ms);
|
2013-11-22 19:32:09 +00:00
|
|
|
for (unsigned i = 0; i < rounds; i++) {
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(c);
|
2013-11-22 19:32:09 +00:00
|
|
|
check_interrupted();
|
|
|
|
}
|
2013-12-09 22:56:48 +00:00
|
|
|
this_thread::sleep_for(r);
|
2013-11-28 16:03:05 +00:00
|
|
|
check_interrupted();
|
2013-11-22 19:32:09 +00:00
|
|
|
}
|
|
|
|
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic_bool * interruptible_thread::get_flag_addr() {
|
2014-06-07 17:18:32 +00:00
|
|
|
return &get_g_interrupt();
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool interruptible_thread::interrupted() const {
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic_bool * f = m_flag_addr.load();
|
2013-11-13 05:16:10 +00:00
|
|
|
if (f == nullptr)
|
|
|
|
return false;
|
|
|
|
return f->load();
|
|
|
|
}
|
|
|
|
|
2013-11-22 19:32:09 +00:00
|
|
|
void interruptible_thread::request_interrupt(unsigned try_ms) {
|
|
|
|
while (true) {
|
2013-12-09 22:56:48 +00:00
|
|
|
atomic_bool * f = m_flag_addr.load();
|
2013-11-22 19:32:09 +00:00
|
|
|
if (f != nullptr) {
|
|
|
|
f->store(true);
|
|
|
|
return;
|
|
|
|
}
|
2013-12-10 00:55:13 +00:00
|
|
|
this_thread::sleep_for(chrono::milliseconds(try_ms));
|
2013-11-22 19:32:09 +00:00
|
|
|
check_interrupted();
|
|
|
|
}
|
2013-11-13 05:16:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void interruptible_thread::join() {
|
|
|
|
m_thread.join();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool interruptible_thread::joinable() {
|
|
|
|
return m_thread.joinable();
|
|
|
|
}
|
|
|
|
}
|