shared_mutex_test.cc 4.81 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
/*
 * 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")
40
      .Add(&SharedMutexTest::TestSharedReadThreadMethod, this,
41 42
           num_threads_, num_iterations_);
  CreateUnit("Exclusive write")
43 44
      .Pre(&SharedMutexTest::TestExclusiveWritePre, this)
      .Add(&SharedMutexTest::TestExclusiveWriteReaderMethod, this,
45
           num_threads_ / 2, num_iterations_)
46
      .Add(&SharedMutexTest::TestExclusiveWriteWriterMethod, this,
47
           num_threads_ / 2, num_iterations_)
48
      .Post(&SharedMutexTest::TestExclusiveWritePost, this);
49
  CreateUnit("SharedLock")
50
      .Add(&SharedMutexTest::TestSharedLockThreadMethod, this,
51 52 53
           num_threads_, num_iterations_);
}

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

59
void SharedMutexTest::TestExclusiveWritePre() {
60 61 62
  counter_ = 0;
}

63
void SharedMutexTest::TestExclusiveWriteReaderMethod() {
64 65 66 67
  // Just add some contention
  SharedLock<embb::base::SharedMutex> lock(shared_mutex_, embb::base::try_lock);
}

68
void SharedMutexTest::TestExclusiveWriteWriterMethod() {
69 70 71 72 73
  UniqueLock<embb::base::SharedMutex> lock(shared_mutex_);

  ++counter_;
}

74
void SharedMutexTest::TestExclusiveWritePost() {
75 76 77 78
  PT_ASSERT_EQ_MSG(counter_, num_iterations_ * (num_threads_ / 2),
                   "Counter value is inconsistent.");
}

79
void SharedMutexTest::TestSharedLockThreadMethod() {
80 81 82 83 84
  // Test basic usage
  {
    SharedLock<> lock(shared_mutex_);
    PT_EXPECT_EQ(lock.OwnsLock(), true);

85
    lock.Unlock();
86 87
    PT_EXPECT_EQ(lock.OwnsLock(), false);

88
    lock.Lock();
89 90
    PT_EXPECT_EQ(lock.OwnsLock(), true);

91
    lock.Unlock();
92 93
    PT_EXPECT_EQ(lock.OwnsLock(), false);

94
    bool locked_after_try = lock.TryLock();
95 96 97 98 99 100 101 102 103 104 105 106
    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);
  }
107

108 109 110 111 112
  // Test try-lock construction
  {
    SharedLock<> lock(shared_mutex_, embb::base::try_lock);
    PT_EXPECT_EQ(lock.OwnsLock(), true);
  }
113

114 115 116 117 118 119
  // Test adopt lock construction
  {
    shared_mutex_.LockShared();
    SharedLock<> lock(shared_mutex_, embb::base::adopt_lock);
    PT_EXPECT_EQ(lock.OwnsLock(), true);
  }
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
  // 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