feat(util/interrupt): restore interrupt module
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
e1047c4ebb
commit
b31233e8c2
3 changed files with 123 additions and 2 deletions
|
@ -1,3 +1,3 @@
|
|||
add_library(util trace.cpp debug.cpp name.cpp exception.cpp
|
||||
hash.cpp escaped.cpp bit_tricks.cpp safe_arith.cpp ascii.cpp
|
||||
memory.cpp shared_mutex.cpp)
|
||||
interrupt.cpp hash.cpp escaped.cpp bit_tricks.cpp safe_arith.cpp
|
||||
ascii.cpp memory.cpp shared_mutex.cpp)
|
||||
|
|
56
src/util/interrupt.cpp
Normal file
56
src/util/interrupt.cpp
Normal file
|
@ -0,0 +1,56 @@
|
|||
/*
|
||||
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
Author: Leonardo de Moura
|
||||
*/
|
||||
#include "util/interrupt.h"
|
||||
#include "util/exception.h"
|
||||
|
||||
namespace lean {
|
||||
static thread_local std::atomic_bool g_interrupt;
|
||||
|
||||
void request_interrupt() {
|
||||
g_interrupt.store(true);
|
||||
}
|
||||
|
||||
void reset_interrupt() {
|
||||
g_interrupt.store(false);
|
||||
}
|
||||
|
||||
bool interrupted() {
|
||||
return g_interrupt.load();
|
||||
}
|
||||
|
||||
void check_interrupt() {
|
||||
if (interrupted())
|
||||
throw interrupted();
|
||||
}
|
||||
|
||||
std::atomic_bool * interruptible_thread::get_flag_addr() {
|
||||
return &g_interrupt;
|
||||
}
|
||||
|
||||
bool interruptible_thread::interrupted() const {
|
||||
std::atomic_bool * f = m_flag_addr.load();
|
||||
if (f == nullptr)
|
||||
return false;
|
||||
return f->load();
|
||||
}
|
||||
|
||||
bool interruptible_thread::request_interrupt() {
|
||||
std::atomic_bool * f = m_flag_addr.load();
|
||||
if (f == nullptr)
|
||||
return false;
|
||||
f->store(true);
|
||||
return true;
|
||||
}
|
||||
|
||||
void interruptible_thread::join() {
|
||||
m_thread.join();
|
||||
}
|
||||
|
||||
bool interruptible_thread::joinable() {
|
||||
return m_thread.joinable();
|
||||
}
|
||||
}
|
65
src/util/interrupt.h
Normal file
65
src/util/interrupt.h
Normal file
|
@ -0,0 +1,65 @@
|
|||
/*
|
||||
Copyright (c) 2013 Microsoft Corporation. All rights reserved.
|
||||
Released under Apache 2.0 license as described in the file LICENSE.
|
||||
|
||||
Author: Leonardo de Moura
|
||||
*/
|
||||
#pragma once
|
||||
#include <atomic>
|
||||
#include <thread>
|
||||
#include <utility>
|
||||
|
||||
namespace lean {
|
||||
/**
|
||||
\brief Mark flag for interrupting current thread.
|
||||
*/
|
||||
void request_interrupt();
|
||||
/**
|
||||
\brief Reset (interrupt) flag for current thread.
|
||||
*/
|
||||
void reset_interrupt();
|
||||
|
||||
/**
|
||||
\brief Return true iff the current thread was marked for interruption.
|
||||
*/
|
||||
bool interrupted();
|
||||
|
||||
/**
|
||||
\brief Throw an interrupt exception if the (interrupt) flag is set.
|
||||
*/
|
||||
void check_interrupt();
|
||||
|
||||
/**
|
||||
\brief Thread that provides a method for setting its interrupt flag.
|
||||
*/
|
||||
class interruptible_thread {
|
||||
std::atomic<std::atomic_bool*> m_flag_addr;
|
||||
std::thread m_thread;
|
||||
static std::atomic_bool * get_flag_addr();
|
||||
public:
|
||||
template<typename Function, typename... Args>
|
||||
interruptible_thread(Function && fun, Args &&... args):
|
||||
m_flag_addr(nullptr),
|
||||
m_thread(
|
||||
[&](Function&& fun, Args&&... args) {
|
||||
m_flag_addr.store(get_flag_addr());
|
||||
fun(std::forward<Args>(args)...);
|
||||
},
|
||||
std::forward<Function>(fun),
|
||||
std::forward<Args>(args)...)
|
||||
{}
|
||||
|
||||
/**
|
||||
\brief Return true iff an interrupt request has been made to the current thread.
|
||||
*/
|
||||
bool interrupted() const;
|
||||
/**
|
||||
\brief Send a interrupt request to the current thread. Return
|
||||
true iff the request has been successfully performed.
|
||||
*/
|
||||
bool request_interrupt();
|
||||
|
||||
void join();
|
||||
bool joinable();
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue