summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/lockfree/test/test_helpers.hpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/lockfree/test/test_helpers.hpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/lockfree/test/test_helpers.hpp')
-rw-r--r--src/boost/libs/lockfree/test/test_helpers.hpp110
1 files changed, 110 insertions, 0 deletions
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 <set>
+#include <boost/array.hpp>
+#include <boost/lockfree/detail/atomic.hpp>
+#include <boost/thread.hpp>
+
+#include <boost/cstdint.hpp>
+
+template <typename int_type>
+int_type generate_id(void)
+{
+ static boost::lockfree::detail::atomic<int_type> generator(0);
+ return ++generator;
+}
+
+template <typename int_type, size_t buckets>
+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<boost::mutex> lock (ref_mutex[index]);
+
+ std::pair<typename std::set<int_type>::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<boost::mutex> 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<boost::mutex> 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<boost::mutex> lock (ref_mutex[i]);
+ ret += data[i].size();
+ }
+ return ret;
+ }
+
+private:
+ boost::array<std::set<int_type>, buckets> data;
+ mutable boost::array<boost::mutex, buckets> 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