condition_var_test.cc 3.95 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
 *
 * 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 <condition_var_test.h>
#include <embb/base/condition_variable.h>
#include <iostream>

#include <partest/test_method.h>
#include <partest/test_instance_info.h>


namespace embb {
namespace base {
namespace test {

ConditionVarTest::ConditionVarTest()
    : num_threads_(partest::TestSuite::GetDefaultNumThreads()) {
  embb_counter_init(&counter_);
  CreateUnit("Timed wait timouts")
      .Add(&ConditionVarTest::TestTimedWaitTimeouts, this);

  if (num_threads_ >= 2) {
    CreateUnit("Condition Notify Test")
        .Add(&ConditionVarTest::TestNotify, this, num_threads_);
  } else {
     std::cout <<
         "Warning: Condition Notify Test needs a minimum of two threads"
               << std::endl;
  }
}

void ConditionVarTest::TestTimedWaitTimeouts() {
  // Set up data structures
  ConditionVariable cond;
  Mutex mutex;
  UniqueLock<Mutex> lock(mutex);

  // Wait for now tests already passed time point
  bool success = cond.WaitUntil(lock, Time());
  PT_EXPECT_EQ(success, false);

  // Wait for a future timepoint
66
  success = cond.WaitUntil(lock, Time(DurationMilliseconds(1)));
67 68 69
  PT_EXPECT_EQ(success, false);

  // Wait for a zero duration
70
  success = cond.WaitFor(lock, DurationMilliseconds());
71 72 73
  PT_EXPECT_EQ(success, false);

  // Wait for some duration
74
  success = cond.WaitFor(lock, DurationMilliseconds(1));
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  PT_EXPECT_EQ(success, false);
}

void ConditionVarTest::TestNotify() {
  size_t threadID = partest::TestSuite::GetCurrentThreadID();

  if (threadID != 0) {
    UniqueLock<Mutex> lock(mutex_cond_notify_);
    embb_counter_increment(&counter_);
    cond_notify_.Wait(lock);
    embb_counter_increment(&counter_);
  } else {
    while (embb_counter_get(&counter_) <
        static_cast<unsigned int>(num_threads_-1))
    {}  // all threads entered critical section
    UniqueLock<Mutex> lock_notify(mutex_cond_notify_);
    lock_notify.Unlock();
    // All threads called wait on the condition (Even last thread)

    embb_counter_init(&counter_);
    UniqueLock<Mutex> lock_wait(mutex_cond_wait_);

    cond_notify_.NotifyOne();

99
    cond_wait_.WaitUntil(lock_wait, Time(DurationMilliseconds(1)));
100 101 102 103 104 105
    while (embb_counter_get(&counter_) == 0)
    {}  // If hangs here signal has not succeeded
    PT_ASSERT_EQ_MSG(embb_counter_get(&counter_),
        static_cast<unsigned int>(1), "Only 1 thread notified");
    cond_notify_.NotifyAll();

106
    cond_wait_.WaitUntil(lock_wait, Time(DurationMilliseconds(2)));
107 108 109 110 111 112 113 114 115 116 117 118 119

    while (embb_counter_get(&counter_) !=
        static_cast<unsigned int>(num_threads_-1))
    {}  // If this hangs then not all threads were notified.
  }
}

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