condition_var_test.cc 5.1 KB
Newer Older
Michael Schmid committed
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
/*
 * Copyright (c) 2014-2016, Siemens AG. All rights reserved.
 *
 * 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>

namespace embb {
namespace base {
namespace test {

ConditionVarTest::ConditionVarTest()
:num_threads_(partest::TestSuite::GetDefaultNumThreads()) {
  embb_counter_init(&counter_);
  embb_condition_init(&cond_notify_);
  embb_mutex_init(&mutex_cond_notify_, EMBB_MUTEX_PLAIN);
  embb_condition_init(&cond_wait_);
  embb_mutex_init(&mutex_cond_wait_, EMBB_MUTEX_PLAIN);

  CreateUnit("Timed wait timeouts")
      .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::TestNotify() {
  size_t threadID =  partest::TestSuite::GetCurrentThreadID();

  if (threadID != 0) {
    embb_mutex_lock(&mutex_cond_notify_);
    embb_counter_increment(&counter_);
    embb_condition_wait(&cond_notify_, &mutex_cond_notify_);
    embb_counter_increment(&counter_);
    embb_mutex_unlock(&mutex_cond_notify_);
  } else {
    embb_duration_t duration = EMBB_DURATION_INIT;
    embb_duration_set_milliseconds(&duration, 1);

    while (embb_counter_get(&counter_)
        < static_cast<unsigned int>(num_threads_-1))
    {}  // All threads entered critical section
    embb_mutex_lock(&mutex_cond_notify_);
    embb_mutex_unlock(&mutex_cond_notify_);
    // All threads called wait on the condition (even last thread)

    embb_counter_init(&counter_);

    embb_condition_notify_one(&cond_notify_);
    embb_mutex_lock(&mutex_cond_wait_);
    embb_condition_wait_for(&cond_wait_, &mutex_cond_wait_, &duration);
    while (embb_counter_get(&counter_) == 0)
    {} // If test hangs here, signalling has not succeeded
    PT_ASSERT_EQ_MSG(embb_counter_get(&counter_), static_cast<unsigned int>(1),
        "Only one thread notified");

    embb_condition_notify_all(&cond_notify_);

    embb_condition_wait_for(&cond_wait_, &mutex_cond_wait_, &duration);

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

    embb_mutex_unlock(&mutex_cond_wait_);
    embb_mutex_destroy(&mutex_cond_wait_);
    embb_mutex_destroy(&mutex_cond_notify_);
    embb_condition_destroy(&cond_wait_);
    embb_condition_destroy(&cond_notify_);
  }
}


void ConditionVarTest::TestTimedWaitTimeouts() {
  // Set up data structures
  embb_condition_t cond;
  embb_mutex_t mutex;
  embb_condition_init(&cond);
  embb_mutex_init(&mutex, EMBB_MUTEX_PLAIN);
  embb_time_t time;
  embb_duration_t duration = EMBB_DURATION_INIT;

  // Wait for "now" tests already passed time point
  embb_time_now(&time);
  embb_mutex_lock(&mutex);
  int status = embb_condition_wait_until(&cond, &mutex, &time);
  PT_EXPECT_EQ(status, EMBB_TIMEDOUT);

  // Wait for a future time point
  status = embb_duration_set_milliseconds(&duration, 1);
  PT_EXPECT_EQ(status, EMBB_SUCCESS);
  status = embb_time_in(&time, &duration); // Time now
  PT_EXPECT_EQ(status, EMBB_SUCCESS);
  status = embb_condition_wait_until(&cond, &mutex, &time);
  PT_EXPECT_EQ(status, EMBB_TIMEDOUT);

  // Wait for a zero duration
  status = embb_duration_set_milliseconds(&duration, 0);
  PT_EXPECT_EQ(status, EMBB_SUCCESS);
  status = embb_condition_wait_for(&cond, &mutex, &duration);
  PT_EXPECT_EQ(status, EMBB_TIMEDOUT);

  // Wait for some duration
  status = embb_duration_set_milliseconds(&duration, 1);
  PT_EXPECT_EQ(status, EMBB_SUCCESS);
  status = embb_condition_wait_for(&cond, &mutex, &duration);
  PT_EXPECT_EQ(status, EMBB_TIMEDOUT);

  // Tear down data structures
  embb_mutex_unlock(&mutex);
  embb_mutex_destroy(&mutex);
  embb_condition_destroy(&cond);
}

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