shared_mutex_test.cc 5.72 KB
Newer Older
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
/*
 * Copyright (c) 2014-2015, 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.
 */

27
#include <shared_mutex_test.h>
28 29 30 31 32 33
#include <embb/base/c/errors.h>

namespace embb {
namespace base {
namespace test {

34
SharedMutexTest::SharedMutexTest()
35 36
    : shared_mutex_(),
      counter_(0),
37 38 39
      num_threads_(partest::TestSuite::GetDefaultNumThreads()),
      num_iterations_(partest::TestSuite::GetDefaultNumIterations()) {
  CreateUnit("Shared read")
40 41
      .Pre(&SharedMutexTest::TestSharedReadPre, this)
      .Add(&SharedMutexTest::TestSharedReadThreadMethod, this,
42
           num_threads_, num_iterations_)
43 44
      .Post(&SharedMutexTest::TestSharedReadPost, this);
  CreateUnit("Multiple writer")
45 46
      .Pre(&SharedMutexTest::TestExclusiveWriterPre, this)
      .Add(&SharedMutexTest::TestExclusiveWriterReaderMethod, this,
47
           num_threads_ / 2, num_iterations_)
48
      .Add(&SharedMutexTest::TestExclusiveWriterWriterMethod, this,
49
           num_threads_ / 2, num_iterations_)
50 51 52 53 54 55 56 57
      .Post(&SharedMutexTest::TestExclusiveWriterPost, this);
//  CreateUnit("Single writer")
//      .Pre(&SharedMutexTest::TestSingleWriterPre, this)
//      .Add(&SharedMutexTest::TestSingleWriterReaderMethod, this,
//           num_threads_, num_iterations_)
//      .Add(&SharedMutexTest::TestSingleWriterWriterMethod, this,
//           1, num_iterations_)
//      .Post(&SharedMutexTest::TestSingleWriterPost, this);
58 59
}

60
void SharedMutexTest::TestSharedReadPre() {
61 62
  int success = embb_shared_mutex_init(&shared_mutex_);
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to initialize shared mutex.");
63 64
}

65
void SharedMutexTest::TestSharedReadThreadMethod() {
66
  int success = embb_shared_mutex_try_lock_shared(&shared_mutex_);
67 68 69 70
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to try-lock for reading.");

  success = embb_shared_mutex_unlock_shared(&shared_mutex_);
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (reading).");
71

72 73
  success = embb_shared_mutex_lock_shared(&shared_mutex_);
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to lock for reading.");
74

75
  success = embb_shared_mutex_unlock_shared(&shared_mutex_);
76 77 78
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (reading).");
}

79
void SharedMutexTest::TestSharedReadPost() {
80
  embb_shared_mutex_destroy(&shared_mutex_);
81 82
}

83
void SharedMutexTest::TestExclusiveWriterPre() {
84 85
  int success = embb_shared_mutex_init(&shared_mutex_);
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to initialize shared mutex.");
86 87 88 89

  counter_ = 0;
}

90
void SharedMutexTest::TestExclusiveWriterReaderMethod() {
91 92
  // Just add some contention

93
  int success = embb_shared_mutex_lock_shared(&shared_mutex_);
94 95
  if (success != EMBB_SUCCESS) return;

96
  success = embb_shared_mutex_unlock_shared(&shared_mutex_);
97 98 99
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (reading).");
}

100
void SharedMutexTest::TestExclusiveWriterWriterMethod() {
101
  int success = embb_shared_mutex_lock(&shared_mutex_);
102 103 104 105
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to lock for writing.");

  ++counter_;

106
  success = embb_shared_mutex_unlock(&shared_mutex_);
107 108 109
  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (writing).");
}

110
void SharedMutexTest::TestExclusiveWriterPost() {
111
  PT_ASSERT_EQ_MSG(counter_, num_iterations_ * (num_threads_ / 2),
112
                   "Counter value is inconsistent.");
113
  embb_shared_mutex_destroy(&shared_mutex_);
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
//void SharedMutexTest::TestSingleWriterPre() {
//  int success = embb_shared_mutex_init(&shared_mutex_);
//  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to initialize shared mutex.");
//
//  counter_ = 0;
//}

//void SharedMutexTest::TestSingleWriterReaderMethod() {
//  // Just add some contention
//
//  int success = embb_shared_mutex_lock_shared(&shared_mutex_);
//  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to lock for reading.");
//
//  success = embb_shared_mutex_unlock_shared(&shared_mutex_);
//  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (reading).");
//}
//
//void SharedMutexTest::TestSingleWriterWriterMethod() {
//  int success = embb_shared_mutex_lock(&shared_mutex_);
//  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to lock for writing.");
//
//  ++counter_;
//
//  success = embb_shared_mutex_unlock(&shared_mutex_);
//  PT_ASSERT_EQ_MSG(success, EMBB_SUCCESS, "Failed to unlock (writing).");
//}
//
//void SharedMutexTest::TestSingleWriterPost() {
//  PT_ASSERT_EQ_MSG(counter_, num_iterations_, "Counter value is inconsistent.");
//  embb_shared_mutex_destroy(&shared_mutex_);
//}
147

148 149 150
} // namespace test
} // namespace base
} // namespace embb