kernel: Add interrupt function to C header

Calling interrupt can halt long-running functions associated with
objects that were created through the passed-in context.
This commit is contained in:
TheCharlatan 2024-06-05 10:20:40 +02:00
parent 0459e9ac29
commit 8d9088c5db
No known key found for this signature in database
GPG Key ID: 9B79B45691DB4173
4 changed files with 22 additions and 0 deletions

View File

@ -672,6 +672,11 @@ btck_Context* btck_context_copy(const btck_Context* context)
return btck_Context::copy(context);
}
int btck_context_interrupt(btck_Context* context)
{
return (*btck_Context::get(context)->m_interrupt)() ? 0 : -1;
}
void btck_context_destroy(btck_Context* context)
{
delete context;

View File

@ -689,6 +689,16 @@ BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_cr
BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_copy(
const btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1);
/**
* @brief Interrupt can be used to halt long-running validation functions like
* when reindexing, importing or processing blocks.
*
* @param[in] context Non-null.
* @return 0 if the interrupt was successfully, non-zero otherwise.
*/
BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_interrupt(
btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1);
/**
* Destroy the context.
*/

View File

@ -657,6 +657,11 @@ public:
Context()
: Handle{btck_context_create(ContextOptions{}.get())} {}
bool interrupt()
{
return btck_context_interrupt(get()) == 0;
}
friend class ChainstateManagerOptions;
};

View File

@ -670,6 +670,8 @@ BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "blocks" / "index"));
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "chainstate"));
BOOST_CHECK(context.interrupt());
}
BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)