shared_mutex_test.cc 4.94 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 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * 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.
 */

#include <shared_mutex_test.h>
#include <embb/base/c/errors.h>
#include <embb/base/mutex.h>

namespace embb {
namespace base {
namespace test {

SharedMutexTest::SharedMutexTest()
    : shared_mutex_(),
      counter_(0),
      num_threads_(partest::TestSuite::GetDefaultNumThreads()),
      num_iterations_(partest::TestSuite::GetDefaultNumIterations()) {
  CreateUnit("Shared read")
41
      .Add(&SharedMutexTest::TestSharedReadThreadMethod, this,
42 43
           num_threads_, num_iterations_);
  CreateUnit("Exclusive write")
44 45
      .Pre(&SharedMutexTest::TestExclusiveWritePre, this)
      .Add(&SharedMutexTest::TestExclusiveWriteReaderMethod, this,
46
           num_threads_ / 2, num_iterations_)
47
      .Add(&SharedMutexTest::TestExclusiveWriteWriterMethod, this,
48
           num_threads_ / 2, num_iterations_)
49
      .Post(&SharedMutexTest::TestExclusiveWritePost, this);
50
  CreateUnit("SharedLock")
51
      .Add(&SharedMutexTest::TestSharedLockThreadMethod, this,
52 53 54
           num_threads_, num_iterations_);
}

55
void SharedMutexTest::TestSharedReadThreadMethod() {
56 57 58 59 60 61 62
  SharedLock<embb::base::SharedMutex> lock(shared_mutex_, embb::base::try_lock);
  PT_ASSERT_EQ_MSG(lock.OwnsLock(), true, "Failed to lock for reading.");

  int spin = 10000;
  while (--spin != 0);
}

63
void SharedMutexTest::TestExclusiveWritePre() {
64 65 66
  counter_ = 0;
}

67
void SharedMutexTest::TestExclusiveWriteReaderMethod() {
68 69 70 71 72 73 74 75 76
  // Just add some contention
  SharedLock<embb::base::SharedMutex> lock(shared_mutex_, embb::base::try_lock);
  
  if (lock.OwnsLock()) {
    int spin = 10000;
    while (--spin != 0);
  }
}

77
void SharedMutexTest::TestExclusiveWriteWriterMethod() {
78 79 80 81 82
  UniqueLock<embb::base::SharedMutex> lock(shared_mutex_);

  ++counter_;
}

83
void SharedMutexTest::TestExclusiveWritePost() {
84 85 86 87
  PT_ASSERT_EQ_MSG(counter_, num_iterations_ * (num_threads_ / 2),
                   "Counter value is inconsistent.");
}

88
void SharedMutexTest::TestSharedLockThreadMethod() {
89 90 91 92 93
  // Test basic usage
  {
    SharedLock<> lock(shared_mutex_);
    PT_EXPECT_EQ(lock.OwnsLock(), true);

94
    lock.Unlock();
95 96
    PT_EXPECT_EQ(lock.OwnsLock(), false);

97
    lock.Lock();
98 99
    PT_EXPECT_EQ(lock.OwnsLock(), true);

100
    lock.Unlock();
101 102
    PT_EXPECT_EQ(lock.OwnsLock(), false);

103
    bool locked_after_try = lock.TryLock();
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
    PT_EXPECT_EQ(locked_after_try, true);
    PT_EXPECT_EQ(lock.OwnsLock(), true);

    lock.Release()->UnlockShared();
    PT_EXPECT_EQ(lock.OwnsLock(), false);
  }

  // Test deferred lock construction
  {
    SharedLock<> lock(shared_mutex_, embb::base::defer_lock);
    PT_EXPECT_EQ(lock.OwnsLock(), false);
  }
  
  // Test try-lock construction
  {
    SharedLock<> lock(shared_mutex_, embb::base::try_lock);
    PT_EXPECT_EQ(lock.OwnsLock(), true);
  }
  
  // Test adopt lock construction
  {
    shared_mutex_.LockShared();
    SharedLock<> lock(shared_mutex_, embb::base::adopt_lock);
    PT_EXPECT_EQ(lock.OwnsLock(), true);
  }
  
  // Test lock swapping
  {
    SharedMutex another_mutex;
    SharedLock<> lock1(another_mutex);
    PT_EXPECT_EQ(lock1.OwnsLock(), true);

    {
      SharedLock<> lock2(shared_mutex_);
      PT_EXPECT_EQ(lock2.OwnsLock(), true);

      lock1.Swap(lock2);
      PT_EXPECT_EQ(lock1.OwnsLock(), true);
      PT_EXPECT_EQ(lock2.OwnsLock(), true);
    }

    // At this point, lock2 was destroyed and "another_mutex" must be unlocked
    SharedLock<> lock3(another_mutex, embb::base::try_lock);
    PT_EXPECT_EQ(lock3.OwnsLock(), true);

    // But lock1 must still be locking "shared_mutex_"
    PT_EXPECT_EQ(lock1.OwnsLock(), true);
    lock1.Release()->UnlockShared();
    PT_EXPECT_EQ(lock1.OwnsLock(), false);
  }
}

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