Skip to content

Commit d27e277

Browse files
committed
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.
1 parent 1976b13 commit d27e277

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

src/kernel/bitcoinkernel.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,11 @@ btck_Context* btck_context_copy(const btck_Context* context)
676676
return btck_Context::copy(context);
677677
}
678678

679+
int btck_context_interrupt(btck_Context* context)
680+
{
681+
return (*btck_Context::get(context)->m_interrupt)() ? 0 : -1;
682+
}
683+
679684
void btck_context_destroy(btck_Context* context)
680685
{
681686
delete context;

src/kernel/bitcoinkernel.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,16 @@ BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_cr
704704
BITCOINKERNEL_API btck_Context* BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_copy(
705705
const btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1);
706706

707+
/**
708+
* @brief Interrupt can be used to halt long-running validation functions like
709+
* when reindexing, importing or processing blocks.
710+
*
711+
* @param[in] context Non-null.
712+
* @return 0 if the interrupt was successful, non-zero otherwise.
713+
*/
714+
BITCOINKERNEL_API int BITCOINKERNEL_WARN_UNUSED_RESULT btck_context_interrupt(
715+
btck_Context* context) BITCOINKERNEL_ARG_NONNULL(1);
716+
707717
/**
708718
* Destroy the context.
709719
*/

src/kernel/bitcoinkernel_wrapper.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,11 @@ class Context : public Handle<btck_Context, btck_context_copy, btck_context_dest
651651

652652
Context()
653653
: Handle{btck_context_create(ContextOptions{}.get())} {}
654+
655+
bool interrupt()
656+
{
657+
return btck_context_interrupt(get()) == 0;
658+
}
654659
};
655660

656661
class ChainstateManagerOptions : public UniqueHandle<btck_ChainstateManagerOptions, btck_chainstate_manager_options_destroy>

src/test/kernel/test_kernel.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ BOOST_AUTO_TEST_CASE(btck_chainman_in_memory_tests)
634634
BOOST_CHECK(std::filesystem::exists(in_memory_test_directory.m_directory / "blocks"));
635635
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "blocks" / "index"));
636636
BOOST_CHECK(!std::filesystem::exists(in_memory_test_directory.m_directory / "chainstate"));
637+
638+
BOOST_CHECK(context.interrupt());
637639
}
638640

639641
BOOST_AUTO_TEST_CASE(btck_chainman_regtest_tests)

0 commit comments

Comments
 (0)