Repository navigation
Expand file tree
/
Copy pathtest_spsc_ring.cpp
More file actions
178 lines (166 loc) · 4.7 KB
/
Copy pathtest_spsc_ring.cpp
File metadata and controls
178 lines (166 loc) · 4.7 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* clang++ test_spsc_ring.cpp -o test_spsc_ring -std=c++11 -l pthread
./test_spsc_ring
*/
#include <memory>
#include <thread>
#include <cstring>
#include <cassert>
#include <chrono>
#include <atomic>
template< typename T, typename IndexT = uint32_t, typename AllocT = std::allocator<T> >
class SimpleRing {
public:
static constexpr uint32_t SLOTSIZE = sizeof(T);
SimpleRing( uint32_t sz ) : size(sz) {
data = allocator.allocate( size );
read_idx = 0;
write_idx = 0;
}
~SimpleRing() {
allocator.deallocate( data, size );
}
bool push( const T& obj ) {
uint32_t next_idx = write_idx+1<size ? write_idx+1 : 0;
if ( next_idx != read_idx ) {
data[write_idx] = obj;
write_idx = next_idx;
return true;
}
return false;
}
bool pop( T& obj ) {
uint32_t next_idx = read_idx+1<size ? read_idx+1 : 0;
if ( read_idx != write_idx ) {
obj = data[read_idx];
read_idx = next_idx;
return true;
}
return false;
}
private:
SimpleRing();
SimpleRing( const SimpleRing& );
std::atomic<IndexT> read_idx;
std::atomic<IndexT> write_idx;
const uint32_t size;
T* data;
AllocT allocator;
};
template< typename T, typename IndexT = uint32_t, typename AllocT = std::allocator<T> >
class SnellmanRing {
public:
static constexpr uint32_t SLOTSIZE = sizeof(T);
SnellmanRing( uint32_t sz ) : size(sz) {
data = allocator.allocate( sz );
read_idx = 0;
write_idx = 0;
}
~SnellmanRing() {
allocator.deallocate( data, size );
}
bool push( const T& obj ) {
uint32_t numel = write_idx - read_idx;
if ( numel < size ) {
data[write_idx%size] = obj;
write_idx++;
return true;
}
return false;
}
bool pop( T& obj ) {
uint32_t numel = write_idx - read_idx;
if ( numel>0 ) {
obj = data[read_idx%size];
read_idx++;
return true;
}
return false;
}
private:
SnellmanRing();
SnellmanRing( const SnellmanRing& );
std::atomic<IndexT> read_idx;
std::atomic<IndexT> write_idx;
const uint32_t size;
T* data;
AllocT allocator;
};
template< typename T, typename IndexT = uint32_t, typename AllocT = std::allocator<T> >
class VitorianRing {
public:
static constexpr uint32_t SLOTSIZE = sizeof(T);
VitorianRing( uint32_t sz ) : size(sz) {
data = allocator.allocate( sz );
read_idx = 0;
write_idx = 2*size;
}
~VitorianRing() {
allocator.deallocate( data, size );
}
bool push( const T& obj ) {
uint32_t numel = (write_idx-read_idx) % (2*size);
//fprintf( stderr, "Push read:%d write:%d diff:%d\n", read_idx, write_idx, numel );
if ( numel < size ) {
data[write_idx%size] = obj;
write_idx = ( write_idx+1 < 4*size ) ? write_idx + 1 : 2*size;
return true;
}
return false;
}
bool pop( T& obj ) {
uint32_t numel = (write_idx-read_idx) % (2*size);
//fprintf( stderr, "Pop read:%d write:%d diff:%d\n", read_idx, write_idx, numel );
if ( numel > 0 ) {
obj = data[read_idx%size];
read_idx = ( read_idx+1 < 2*size ) ? read_idx+1 : 0;
return true;
}
return false;
}
private:
VitorianRing();
VitorianRing( const VitorianRing& );
std::atomic<IndexT> read_idx;
std::atomic<IndexT> write_idx;
const uint32_t size;
T* data;
AllocT allocator;
};
template< class RingT >
void producer( RingT& rng, int count, int seed ) {
for ( int j=0; j<count; ++j ) {
while ( !rng.push( j + seed ) );
//std::this_thread::sleep_for( std::chrono::milliseconds(1) );
//fprintf( stderr, " >> pushed %d\n", j+val );
}
}
template< class RingT >
void consumer( RingT& rng, int count, int seed ) {
for ( int j=0; j<count; ++j ) {
int res;
while ( !rng.pop( res ) );
//std::this_thread::sleep_for( std::chrono::milliseconds(1) );
//fprintf( stderr, " >> popped %d\n", res );
assert( res==seed+j );
}
}
template< class RingT >
void test()
{
printf( "Testing...\n" );
try {
RingT rng( 8 );
std::thread th1( producer<RingT>, std::ref(rng), 10000, 99 );
std::thread th2( consumer<RingT>, std::ref(rng), 10000, 99 );
th1.join();
th2.join();
}
catch( std::exception& ex ) {
printf( "%s\n", ex.what() );
}
}
int main( int argc, char* argv[] ) {
test<SimpleRing<int>>();
test<SnellmanRing<int>>();
test<VitorianRing<int>>();
}