atomic_test.cc 11.4 KB
Newer Older
1
/*
2
 * Copyright (c) 2014-2015, Siemens AG. All rights reserved.
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 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <atomic_test.h>
#include <embb/base/atomic.h>
#include <embb/base/thread.h>
#include <iostream>

namespace embb {
namespace base {
namespace test {

size_t AtomicTest::numIterations_;

void AtomicTest::TestStressLoadStore::barrier(int thread) {
  const int my_sense(thread_sense[thread]);
  if (--count == 0) {
    const int tmp(z.Load());
    PT_EXPECT(tmp == 1 || tmp == 2);
    x.Store(0);
    y.Store(0);
    z.Store(0);
    count.Store(4);
    sense.Store(my_sense);
  } else {
    while (sense.Load() != my_sense) {}
  }
  thread_sense[thread] = !my_sense;
}

void AtomicTest::TestStressLoadStore::write_x() {
  x.Store(1);
  barrier(0);
}

void AtomicTest::TestStressLoadStore::write_y() {
  y.Store(1);
  barrier(1);
}

void AtomicTest::TestStressLoadStore::read_x_then_y() {
  while (!x.Load()) {}
  if (y.Load())
    z++;
  barrier(2);
}

void AtomicTest::TestStressLoadStore::read_y_then_x() {
  while (!y.Load()) {}
  if (x.Load())
    z++;
  barrier(3);
}

void AtomicTest::TestStressLoadStore::Init() {
}

void AtomicTest::TestStressLoadStore::CheckAndDestroy() {
}

AtomicTest::TestStressLoadStore::TestStressLoadStore(
  size_t number_threads, size_t number_iterations)
  : TestUnit("Load/Store Stress test for Atomics"), x(0), y(0), z(0),
  count(4), sense(0) {
  PT_ASSERT(number_threads == 1);
  thread_sense[0] = 1;
  thread_sense[1] = 1;
  thread_sense[2] = 1;
  thread_sense[3] = 1;
  Pre(&TestStressLoadStore::Init, this);
  Add(&TestStressLoadStore::write_x, this,
    number_threads, number_iterations);
  Add(&TestStressLoadStore::write_y, this,
    number_threads, number_iterations);
  Add(&TestStressLoadStore::read_x_then_y, this,
    number_threads, number_iterations);
  Add(&TestStressLoadStore::read_y_then_x, this,
    number_threads, number_iterations);
  Post(&TestStressLoadStore::CheckAndDestroy, this);
}

AtomicTest::TestStressProduceConsume::TestStressProduceConsume(
  size_t number_threads, size_t number_iterations)
  : TestUnit("Produce/Consume Stress test for Atomics"), flag(0),
  counter_producer(0), counter_consumer(0) {
  PT_ASSERT(number_threads == 1);
  Pre(&TestStressProduceConsume::Init, this);

  Add(&TestStressProduceConsume::produce, this,
    number_threads, number_iterations);
  Add(&TestStressProduceConsume::consume, this,
    number_threads, number_iterations);

  Post(&TestStressProduceConsume::CheckAndDestroy, this);
}

void AtomicTest::TestStressProduceConsume::produce() {
  counter_consumer++;
  while (flag) {}
  prod_cons_value =  static_cast<size_t>(counter_consumer);
  while (flag.Swap(true)) {}
}

void AtomicTest::TestStressProduceConsume::consume() {
  counter_producer++;
  while (flag == 0) {}
  PT_EXPECT(prod_cons_value == static_cast<size_t>(counter_producer));
  flag = 0;
}

void AtomicTest::TestStressProduceConsume::Init() {
}

void AtomicTest::TestStressProduceConsume::CheckAndDestroy() {
}

AtomicTest::TestStressIncrementDecrement::TestStressIncrementDecrement(
  size_t number_threads, size_t number_iterations)
  : TestUnit("Increment/Decrement Stress test for Atomics"), inc_dec_value(0) {
  PT_ASSERT(number_threads == 1);
  Pre(&TestStressIncrementDecrement::Init, this);

  Add(&TestStressIncrementDecrement::increment, this,
    number_threads, number_iterations);
  Add(&TestStressIncrementDecrement::decrement, this,
    number_threads, number_iterations);

  Post(&TestStressIncrementDecrement::CheckAndDestroy, this);
}

void AtomicTest::TestStressIncrementDecrement::decrement() {
  inc_dec_value--;
}

void AtomicTest::TestStressIncrementDecrement::increment() {
  inc_dec_value++;
}

void AtomicTest::TestStressIncrementDecrement::Init() {
}

void AtomicTest::TestStressIncrementDecrement::CheckAndDestroy() {
  // Increment and decrement operations must neutralize each other
  PT_EXPECT(inc_dec_value == 0);
}



AtomicTest::TestStressSwap::TestStressSwap(
  size_t number_threads, size_t number_iterations)
  : TestUnit("Swap Stress test for Atomics"), swap1_counter(1)
  , swap2_counter(2) {
  bitsets[0] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();
  bitsets[1] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();
  bitsets[2] = std::bitset<ATOMIC_TESTS_ITERATIONS * 2 + 1>();

  PT_ASSERT(number_threads == 1);
  Pre(&TestStressSwap::Init, this);

  Add(&TestStressSwap::swap1, this,
    number_threads, number_iterations);
  Add(&TestStressSwap::swap2, this,
    number_threads, number_iterations);

  Post(&TestStressSwap::CheckAndDestroy, this);
}

void AtomicTest::TestStressSwap::Init() {
}

void AtomicTest::TestStressSwap::CheckAndDestroy() {
  // Each element is read at most once
  bitsets[2] = bitsets[0];
  bitsets[2] &= bitsets[1];
  PT_EXPECT(bitsets[2].none());
  bitsets[2] = bitsets[0];
  bitsets[2] |= bitsets[1];
  // All elements except one are read (push, pop, push and so on... the last
  // push is not read), however this must not always be the same element.
  PT_EXPECT(bitsets[2].count() ==
      static_cast<size_t>(AtomicTest::GetNumberOfIterations() * 2));
}

void AtomicTest::TestStressSwap::swap1() {
  const int j(swap_value.Swap(swap1_counter));
  PT_EXPECT(!(bitsets[0].test(static_cast<size_t>(j))));
  bitsets[0].set(static_cast<size_t>(j));
  swap1_counter += 2;
}

void AtomicTest::TestStressSwap::swap2() {
  const int j(swap_value.Swap(swap2_counter));
  PT_EXPECT(!(bitsets[1].test(static_cast<size_t>(j))));
  bitsets[1].set(static_cast<size_t>(j));
  swap2_counter += 2;
}

AtomicTest::TestStressCompareAndSwap::TestStressCompareAndSwap(
  size_t number_threads, size_t number_iterations)
  : TestUnit("Compare and Swap Stress test for Atomics"), swap1_counter(1)
  , swap2_counter(2) {
  PT_ASSERT(number_threads == 1);
  Pre(&TestStressCompareAndSwap::Init, this);

  Add(&TestStressCompareAndSwap::compare_and_swap1, this,
    number_threads, number_iterations);
  Add(&TestStressCompareAndSwap::compare_and_swap2, this,
    number_threads, number_iterations);

  Post(&TestStressCompareAndSwap::CheckAndDestroy, this);
}

void AtomicTest::TestStressCompareAndSwap::Init() {
}

void AtomicTest::TestStressCompareAndSwap::CheckAndDestroy() {
  // Each element is read at most once
  bitsets[2] = bitsets[0];
  bitsets[2] &= bitsets[1];
  PT_EXPECT(bitsets[2].none());
  bitsets[2] = bitsets[0];
  bitsets[2] |= bitsets[1];
  // All elements except one are read (push, pop, push and so on... the last
  // push is not read), however this must not always be the same element.
  PT_EXPECT(bitsets[2].count() ==
      static_cast<unsigned int>(AtomicTest::GetNumberOfIterations() * 2));
}

void AtomicTest::TestStressCompareAndSwap::compare_and_swap1() {
  int j(0);
  while (!swap_value.CompareAndSwap(j, swap1_counter)) {}
  PT_EXPECT(!(bitsets[0].test(static_cast<size_t>(j))));
  bitsets[0].set(static_cast<size_t>(j));
  swap1_counter += 2;
}

void AtomicTest::TestStressCompareAndSwap::compare_and_swap2() {
  int j(0);
  while (!swap_value.CompareAndSwap(j, swap2_counter)) {}
  PT_EXPECT(!(bitsets[1].test(static_cast<size_t>(j))));
  bitsets[1].set(static_cast<size_t>(j));
  swap2_counter += 2;
}



AtomicTest::AtomicTest() {
  numIterations_ = partest::TestSuite::GetDefaultNumIterations();
  PT_ASSERT_LT_MSG(numIterations_,
    static_cast<size_t>(ATOMIC_TESTS_ITERATIONS), "Maximum allowed iterations");
  CreateUnit("BasicTestsSingleThreaded")
    .Add(&AtomicTest::BasicTests, this);
  CreateUnit<TestStressLoadStore>(static_cast<size_t>(1), numIterations_);
  CreateUnit<TestStressProduceConsume>(static_cast<size_t>(1), numIterations_);
  CreateUnit<TestStressIncrementDecrement>(static_cast<size_t>(1),
    numIterations_);
  CreateUnit<TestStressSwap>(static_cast<size_t>(1), numIterations_);
  CreateUnit<TestStressCompareAndSwap>(static_cast<size_t>(1), numIterations_);
}

typedef enum { RED, GREEN, BLUE } colors_t;

void AtomicTest::BasicTests() {
287
  embb::base::Atomic<bool> b;          // Boolean
288 289 290 291 292 293
  embb::base::Atomic<colors_t> c;        // Enumeration
  embb::base::Atomic<void*> v;         // Void pointer
  embb::base::Atomic<int> i;           // Integer
  embb::base::Atomic<int*> n;          // Non-void pointer

  //template specializations
294
  PT_EXPECT(!b.IsArithmetic() && !b.IsInteger() && !b.IsPointer());
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391
  PT_EXPECT(!c.IsArithmetic() && !c.IsInteger() && !c.IsPointer());
  PT_EXPECT(!v.IsArithmetic() && !v.IsInteger() && !v.IsPointer());
  PT_EXPECT(i.IsArithmetic() && i.IsInteger() && !i.IsPointer());
  PT_EXPECT(n.IsArithmetic() && !n.IsInteger() && n.IsPointer());


  // Constructors
  PT_EXPECT(c == RED);
  colors_t d(GREEN);
  PT_EXPECT(d == GREEN);
  // Assignment
  PT_EXPECT((c = GREEN) == GREEN);
  PT_EXPECT(c == GREEN);
  // Swap
  PT_EXPECT(c.Swap(BLUE) == GREEN);
  PT_EXPECT(c == BLUE);
  // Compare-and-swap

  d = RED;
  PT_EXPECT(!c.CompareAndSwap(d, GREEN));
  PT_EXPECT(d == BLUE);
  PT_EXPECT(c.CompareAndSwap(d, GREEN));
  PT_EXPECT(c == GREEN);

  //Arithmetic opertions...
  PT_EXPECT(i == 0);
  // Fetch-and-add
  PT_EXPECT(i.FetchAndAdd(10) == 0);
  PT_EXPECT(i == 10);
  // Fetch-and-sub
  PT_EXPECT(i.FetchAndSub(5) == 10);
  PT_EXPECT(i == 5);
  // Increment (postfix)
  PT_EXPECT(i++ == 5);
  PT_EXPECT(i == 6);
  // Decrement (postfix)
  PT_EXPECT(i-- == 6);
  PT_EXPECT(i == 5);
  // Increment (prefix)
  PT_EXPECT(++i == 6);
  PT_EXPECT(i == 6);
  // Decrement (prefix)
  PT_EXPECT(--i == 5);
  PT_EXPECT(i == 5);
  // Addition
  PT_EXPECT((i += 10) == 15);
  PT_EXPECT(i == 15);
  // Subtraction
  PT_EXPECT((i -= 10) == 5);
  PT_EXPECT(i == 5);

  //Boolean operations...
  // And
  i = 0;
  i &= 1;
  PT_EXPECT(i == 0);
  i = 1;
  i &= 1;
  PT_EXPECT(i == 1);
  // Or
  i = 1;
  i |= 0;
  PT_EXPECT(i == 1);
  i |= 1;
  PT_EXPECT(i == 1);
  i = 0;
  i |= 0;
  PT_EXPECT(i == 0);
  // Xor
  i = 0;
  i ^= 0;
  PT_EXPECT(i == 0);
  i ^= 1;
  PT_EXPECT(i == 1);

  //Pointers...;
  n = NULL;
  // Stride
  PT_EXPECT((uintptr_t)++n == sizeof(int));
  // Dereferencing
  n = new int(0x13579BDF);
  PT_EXPECT(*n == 0x13579BDF);
  delete n;

  // Scalar values
  embb::base::Atomic<int> *j = new embb::base::Atomic<int>();
  PT_EXPECT(*j == 0);
  int *k = new int(0);
  PT_EXPECT(j->CompareAndSwap(*k, 0x13579BDF));
  PT_EXPECT(*j == 0x13579BDF);
  delete j;
  delete k;
}

} // namespace test
} // namespace base
} // namespace embb