From 483eb2f56657e8e7f419ab1a4fab8dce9ade8609 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 27 Apr 2024 20:24:20 +0200 Subject: Adding upstream version 14.2.21. Signed-off-by: Daniel Baumann --- src/boost/libs/lockfree/test/test_helpers.hpp | 110 ++++++++++++++++++++++++++ 1 file changed, 110 insertions(+) create mode 100644 src/boost/libs/lockfree/test/test_helpers.hpp (limited to 'src/boost/libs/lockfree/test/test_helpers.hpp') diff --git a/src/boost/libs/lockfree/test/test_helpers.hpp b/src/boost/libs/lockfree/test/test_helpers.hpp new file mode 100644 index 00000000..882fa1c4 --- /dev/null +++ b/src/boost/libs/lockfree/test/test_helpers.hpp @@ -0,0 +1,110 @@ +// Copyright (C) 2011 Tim Blechmann +// +// Distributed under the Boost Software License, Version 1.0. (See +// accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +#ifndef BOOST_LOCKFREE_TEST_HELPERS +#define BOOST_LOCKFREE_TEST_HELPERS + +#include +#include +#include +#include + +#include + +template +int_type generate_id(void) +{ + static boost::lockfree::detail::atomic generator(0); + return ++generator; +} + +template +class static_hashed_set +{ + +public: + int calc_index(int_type id) + { + // knuth hash ... does not need to be good, but has to be portable + size_t factor = size_t((float)buckets * 1.616f); + + return ((size_t)id * factor) % buckets; + } + + bool insert(int_type const & id) + { + std::size_t index = calc_index(id); + + boost::lock_guard lock (ref_mutex[index]); + + std::pair::iterator, bool> p; + p = data[index].insert(id); + + return p.second; + } + + bool find (int_type const & id) + { + std::size_t index = calc_index(id); + + boost::lock_guard lock (ref_mutex[index]); + + return data[index].find(id) != data[index].end(); + } + + bool erase(int_type const & id) + { + std::size_t index = calc_index(id); + + boost::lock_guard lock (ref_mutex[index]); + + if (data[index].find(id) != data[index].end()) { + data[index].erase(id); + assert(data[index].find(id) == data[index].end()); + return true; + } + else + return false; + } + + std::size_t count_nodes(void) const + { + std::size_t ret = 0; + for (int i = 0; i != buckets; ++i) { + boost::lock_guard lock (ref_mutex[i]); + ret += data[i].size(); + } + return ret; + } + +private: + boost::array, buckets> data; + mutable boost::array ref_mutex; +}; + +struct test_equal +{ + test_equal(int i): + i(i) + {} + + void operator()(int arg) const + { + BOOST_REQUIRE_EQUAL(arg, i); + } + + int i; +}; + +struct dummy_functor +{ + void operator()(int /* arg */) const + { + } +}; + + +#endif -- cgit v1.2.3