lockfree: improve building of testsuite
[boost_lockfree.git] / libs / lockfree / test / test_helpers.hpp
blob1dfccbaa7f6cde9d6f55547ff4a97196b7697b2e
1 #include <set>
2 #include <boost/array.hpp>
3 #include <boost/lockfree/detail/atomic.hpp>
4 #include <boost/thread.hpp>
6 template <typename int_type>
7 int_type generate_id(void)
9 static boost::lockfree::detail::atomic<int_type> generator(0);
10 return ++generator;
13 template <typename int_type, unsigned int buckets>
14 class static_hashed_set
16 public:
17 bool insert(int_type const & id)
19 std::size_t index = id % buckets;
21 boost::mutex::scoped_lock lock (ref_mutex[index]);
23 std::pair<typename std::set<int_type>::iterator, bool> p;
24 p = data[index].insert(id);
26 return p.second;
29 bool find (int_type const & id)
31 std::size_t index = id % buckets;
33 boost::mutex::scoped_lock lock (ref_mutex[index]);
35 return data[index].find(id) != data[index].end();
38 bool erase(int_type const & id)
40 std::size_t index = id % buckets;
42 boost::mutex::scoped_lock lock (ref_mutex[index]);
44 if (data[index].find(id) != data[index].end())
46 data[index].erase(id);
47 assert(data[index].find(id) == data[index].end());
48 return true;
50 else
51 return false;
54 int count_nodes(void) const
56 int ret = 0;
57 for (int i = 0; i != buckets; ++i)
59 boost::mutex::scoped_lock lock (ref_mutex[i]);
60 ret += data[i].size();
62 return ret;
65 private:
66 boost::array<std::set<int_type>, buckets> data;
67 mutable boost::array<boost::mutex, buckets> ref_mutex;