Skip to content

Commit

Permalink
GH-1716 pop before execute as the executed lambda can switch to read-…
Browse files Browse the repository at this point in the history
…only mode and spawn threads that access the queue.
  • Loading branch information
heifner committed Oct 4, 2023
1 parent 6af28bb commit 3854555
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
24 changes: 13 additions & 11 deletions libraries/custom_appbase/include/eosio/chain/exec_pri_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,6 @@ class exec_pri_queue : public boost::asio::execution_context
handlers_ = prio_queue();
}

// only call when no lock required
bool execute_highest()
{
if( !handlers_.empty() ) {
handlers_.top()->execute();
handlers_.pop();
}

return !handlers_.empty();
}

private:
// has to be defined before use, auto return type
auto pop() {
Expand All @@ -74,6 +63,19 @@ class exec_pri_queue : public boost::asio::execution_context

public:

// only call when no lock required
bool execute_highest()
{
if( !handlers_.empty() ) {
auto t = pop();
bool empty = handlers_.empty();
t->execute();
return !empty;
}

return false;
}

bool execute_highest_locked(bool should_block) {
std::unique_lock g(mtx_);
if (should_block) {
Expand Down
4 changes: 2 additions & 2 deletions libraries/custom_appbase/tests/custom_appbase_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ BOOST_AUTO_TEST_CASE( default_exec_window ) {
app->executor().post( priority::lowest, exec_queue::read_only, [&]() {
// read_only_queue should only contain the current lambda function,
// and read_write_queue should have executed all its functions
BOOST_REQUIRE_EQUAL( app->executor().read_only_queue().size(), 1); // pop()s after execute
BOOST_REQUIRE_EQUAL( app->executor().read_only_queue().size(), 0); // pop()s before execute
BOOST_REQUIRE_EQUAL( app->executor().read_write_queue().size(), 0 );
app->quit();
} );
Expand Down Expand Up @@ -175,7 +175,7 @@ BOOST_AUTO_TEST_CASE( execute_from_both_queues ) {
// stop application. Use lowest at the end to make sure this executes the last
app->executor().post( priority::lowest, exec_queue::read_only, [&]() {
// read_queue should have current function and write_queue's functions are all executed
BOOST_REQUIRE_EQUAL( app->executor().read_only_queue().size(), 1); // pop()s after execute
BOOST_REQUIRE_EQUAL( app->executor().read_only_queue().size(), 0); // pop()s before execute
BOOST_REQUIRE_EQUAL( app->executor().read_write_queue().size(), 0 );
app->quit();
} );
Expand Down

0 comments on commit 3854555

Please sign in to comment.