blob: 882fa1c455aee98450e1f8efde732fdb2f02b5d7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
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
|