Skip to content

Commit

Permalink
Merge pull request #74 from msuvajac/default-init-fix
Browse files Browse the repository at this point in the history
stack, spsc_queue: don't create temporaries when deleting elements
  • Loading branch information
timblechmann committed Sep 17, 2021
2 parents be391d4 + af906f5 commit fdd4d06
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 13 deletions.
18 changes: 8 additions & 10 deletions include/boost/lockfree/spsc_queue.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,9 +352,8 @@ class ringbuffer_base
if ( !boost::has_trivial_destructor<T>::value ) {
// make sure to call all destructors!

T dummy_element;
while (pop(dummy_element))
{}
detail::consume_noop consume_functor;
(void)consume_all( consume_functor );
} else {
write_index_.store(0, memory_order_relaxed);
read_index_.store(0, memory_order_release);
Expand Down Expand Up @@ -451,8 +450,8 @@ class compile_time_sized_ringbuffer:
~compile_time_sized_ringbuffer(void)
{
// destroy all remaining items
T out;
while (pop(&out, 1)) {}
detail::consume_noop consume_functor;
(void)consume_all(consume_functor);
}

public:
Expand Down Expand Up @@ -583,8 +582,8 @@ class runtime_sized_ringbuffer:
~runtime_sized_ringbuffer(void)
{
// destroy all remaining items
T out;
while (pop(&out, 1)) {}
detail::consume_noop consume_functor;
(void)consume_all(consume_functor);

#ifdef BOOST_NO_CXX11_ALLOCATOR
Alloc::deallocate(array_, max_elements_);
Expand Down Expand Up @@ -1039,9 +1038,8 @@ class spsc_queue:
if ( !boost::has_trivial_destructor<T>::value ) {
// make sure to call all destructors!

T dummy_element;
while (pop(dummy_element))
{}
detail::consume_noop consume_functor;
(void)consume_all(consume_functor);
} else {
base_type::write_index_.store(0, memory_order_relaxed);
base_type::read_index_.store(0, memory_order_release);
Expand Down
5 changes: 2 additions & 3 deletions include/boost/lockfree/stack.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,8 @@ class stack
* */
~stack(void)
{
T dummy;
while(unsynchronized_pop(dummy))
{}
detail::consume_noop consume_functor;
(void)consume_all(consume_functor);
}

private:
Expand Down
68 changes: 68 additions & 0 deletions test/destructor_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,71 @@ BOOST_AUTO_TEST_CASE( spsc_queue_fixed_sized_instance_deleter_test )
assert(g_instance_counter == 0);
BOOST_REQUIRE(g_instance_counter == 0);
}

struct no_default_init_tester
{
int value;

no_default_init_tester(int value) : value(value)
{
++g_instance_counter;
}

no_default_init_tester(no_default_init_tester const& t)
{
value = t.value;

++g_instance_counter;
}

~no_default_init_tester()
{
--g_instance_counter;
}
};

BOOST_AUTO_TEST_CASE( stack_instance_deleter_no_default_init_test )
{
{
boost::lockfree::stack<no_default_init_tester> q(128);
q.push(no_default_init_tester(1));
q.push(no_default_init_tester(2));
q.push(no_default_init_tester(3));
q.push(no_default_init_tester(4));
q.push(no_default_init_tester(5));
}

assert(g_instance_counter == 0);
BOOST_REQUIRE(g_instance_counter == 0);
}


BOOST_AUTO_TEST_CASE( spsc_queue_instance_deleter_no_default_init_test )
{
{
boost::lockfree::spsc_queue<no_default_init_tester> q(128);
q.push(no_default_init_tester(1));
q.push(no_default_init_tester(2));
q.push(no_default_init_tester(3));
q.push(no_default_init_tester(4));
q.push(no_default_init_tester(5));
}

assert(g_instance_counter == 0);
BOOST_REQUIRE(g_instance_counter == 0);
}

BOOST_AUTO_TEST_CASE( spsc_queue_fixed_sized_instance_deleter_no_default_init_test )
{
{
boost::lockfree::spsc_queue<no_default_init_tester, boost::lockfree::capacity<128> > q;
q.push(no_default_init_tester(1));
q.push(no_default_init_tester(2));
q.push(no_default_init_tester(3));
q.push(no_default_init_tester(4));
q.push(no_default_init_tester(5));
}

assert(g_instance_counter == 0);
BOOST_REQUIRE(g_instance_counter == 0);
}

0 comments on commit fdd4d06

Please sign in to comment.