condition_var_test.cc 5.09 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
 *
 * 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);

41
  CreateUnit("Timed wait timouts")
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
      .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))
67
    {}  // all threads entered critical section
68 69
    embb_mutex_lock(&mutex_cond_notify_);
    embb_mutex_unlock(&mutex_cond_notify_);
70
    // All threads called wait on the condition (Even last thread)
71 72 73 74 75 76 77

    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)
78
    {} //if hangs here signal has not succeded
79 80 81 82 83 84 85 86 87
    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))
88
    {} // If this hangs then not all threads were notified.
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

    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;

108
  // Wait for now tests already passed time point
109 110 111 112 113
  embb_time_now(&time);
  embb_mutex_lock(&mutex);
  int status = embb_condition_wait_until(&cond, &mutex, &time);
  PT_EXPECT_EQ(status, EMBB_TIMEDOUT);

114
  // Wait for a future timepoint
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
  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