1 /*------------------------------------------------------------------------
2 Junction: Concurrent data structures in C++
3 Copyright (c) 2016 Jeff Preshing
5 Distributed under the Simplified BSD License.
6 Original location: https://github.com/preshing/junction
8 This software is distributed WITHOUT ANY WARRANTY; without even the
9 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10 See the LICENSE file for more information.
11 ------------------------------------------------------------------------*/
13 #include <junction/Core.h>
14 #include <turf/extra/JobDispatcher.h>
15 #include <junction/extra/MapAdapter.h>
16 #include <turf/extra/Random.h>
19 using namespace turf::intTypes;
20 typedef junction::extra::MapAdapter MapAdapter;
22 turf::extra::Random g_random[4];
24 class StoreBufferTest {
26 MapAdapter::Map m_map;
34 StoreBufferTest() : m_map(1024) {
37 X = g_random[0].next32();
40 Y = g_random[0].next32();
41 } while (Y == 0 || Y == X);
44 void run(ureg threadIndex) {
47 while ((g_random[threadIndex].next32() & 0x7f) != 0) { // Random delay
49 if (threadIndex == 0) {
50 // We store 2 because Junction maps reserve 1 for the default Redirect value.
51 // The default can be overridden, but this is easier.
52 m_map.insert(x, (void*) 2);
53 m_r1 = (uptr) m_map.get(y);
55 m_map.insert(y, (void*) 2);
56 m_r2 = (uptr) m_map.get(x);
63 MapAdapter::Map m_map;
73 IRIWTest() : m_map(1024) {
76 X = g_random[0].next32();
79 Y = g_random[0].next32();
80 } while (Y == 0 || Y == X);
83 void run(ureg threadIndex) {
86 while ((g_random[threadIndex].next32() & 0x7f) != 0) { // Random delay
88 switch (threadIndex) {
90 // We store 2 because Junction maps reserve 1 for the default Redirect value.
91 // The default can be overridden, but this is easier.
92 m_map.insert(x, (void*) 2);
96 m_map.insert(y, (void*) 2);
100 m_r1 = (uptr) m_map.get(x);
101 m_r2 = (uptr) m_map.get(y);
105 m_r3 = (uptr) m_map.get(y);
106 m_r4 = (uptr) m_map.get(x);
114 // Run StoreBufferTest
115 turf::extra::JobDispatcher dispatcher(2);
116 MapAdapter adapter(2);
118 u64 nonLinearizable = 0;
119 for (u64 iterations = 0;; iterations++) {
120 StoreBufferTest test;
121 dispatcher.kick(&StoreBufferTest::run, test);
122 if (test.m_r1 == 0 && test.m_r2 == 0) {
125 if (iterations % 10000 == 0) {
126 printf("%" TURF_U64D " non-linearizable histories after %" TURF_U64D " iterations\n", nonLinearizable, iterations);
131 turf::extra::JobDispatcher dispatcher(4);
132 MapAdapter adapter(4);
134 u64 nonLinearizable = 0;
135 for (u64 iterations = 0;; iterations++) {
137 dispatcher.kick(&IRIWTest::run, test);
138 if (test.m_r1 == 2 && test.m_r2 == 0 && test.m_r3 == 2 && test.m_r4 == 0) {
141 if (iterations % 10000 == 0) {
142 printf("%" TURF_U64D " non-linearizable histories after %" TURF_U64D " iterations\n", nonLinearizable, iterations);