forked from rigtorp/MPMCQueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MPMCQueueTest.cpp
138 lines (124 loc) · 3.54 KB
/
MPMCQueueTest.cpp
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
/*
Copyright (c) 2016 Erik Rigtorp <[email protected]>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "MPMCQueue.h"
#include <cassert>
#include <chrono>
#include <iostream>
#include <thread>
#include <vector>
// TestType has a global reference count and detects incorrect use of placement
// new
struct TestType {
static int refCount;
static constexpr uint64_t kMagic = 0x38c8fb6f8207a508;
uint64_t magic;
TestType() noexcept {
refCount++;
assert(magic != kMagic);
magic = kMagic;
};
TestType(const TestType &) noexcept {
refCount++;
assert(magic != kMagic);
magic = kMagic;
};
TestType(TestType &&) noexcept {
refCount++;
assert(magic != kMagic);
magic = kMagic;
};
TestType &operator=(const TestType &other) noexcept {
assert(magic == kMagic);
assert(other.magic == kMagic);
return *this;
};
TestType &operator=(TestType &&other) noexcept {
assert(magic == kMagic);
assert(other.magic == kMagic);
return *this;
}
~TestType() noexcept {
refCount--;
assert(magic == kMagic);
magic = 0;
};
};
int TestType::refCount = 0;
int main(int argc, char *argv[]) {
using namespace rigtorp;
{
MPMCQueue<TestType> q(11);
for (int i = 0; i < 10; i++) {
q.emplace();
}
assert(TestType::refCount == 10);
TestType t;
q.pop(t);
assert(TestType::refCount == 10);
q.pop(t);
q.emplace();
assert(TestType::refCount == 10);
}
assert(TestType::refCount == 0);
{
MPMCQueue<int> q(1);
int t = 0;
assert(q.try_push(1) == true);
assert(q.try_push(2) == false);
assert(q.try_pop(t) == true && t == 1);
assert(q.try_pop(t) == false && t == 1);
}
// Fuzz test
{
const uint64_t numOps = 1000;
const int numThreads = 10;
MPMCQueue<int> q(numThreads);
std::atomic<bool> flag(false);
std::vector<std::thread> threads;
std::atomic<uint64_t> sum(0);
for (uint64_t i = 0; i < numThreads; ++i) {
threads.push_back(std::thread([&, i] {
while (!flag)
;
for (auto j = i; j < numOps; j += numThreads) {
q.push(j);
}
}));
}
for (uint64_t i = 0; i < numThreads; ++i) {
threads.push_back(std::thread([&, i] {
while (!flag)
;
uint64_t threadSum = 0;
for (auto j = i; j < numOps; j += numThreads) {
int v;
q.pop(v);
threadSum += v;
}
sum += threadSum;
}));
}
flag = true;
for (auto &thread : threads) {
thread.join();
}
assert(sum == numOps * (numOps - 1) / 2);
}
return 0;
}