gem5  v21.1.0.2
uncontended_mutex.test.cc
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Google, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met: redistributions of source code must retain the above copyright
7  * notice, this list of conditions and the following disclaimer;
8  * redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution;
11  * neither the name of the copyright holders nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <gtest/gtest.h>
29 #include <mutex>
30 #include <thread>
31 #include <vector>
32 
34 
35 using namespace gem5;
36 
38 {
39  int data = 0;
41 
42  std::thread t1([&] () {
43  std::lock_guard<UncontendedMutex> g(m);
44  // Simulate += operation with a racing change between read and write.
45  int tmp = data;
46  std::this_thread::sleep_for(std::chrono::milliseconds(200));
47  data = tmp + 1;
48  });
49 
50  std::thread t2([&] () {
51  std::this_thread::sleep_for(std::chrono::milliseconds(100));
52  std::lock_guard<UncontendedMutex> g(m);
53  data = data + 1;
54  });
55 
56  std::thread t3([&] () {
57  std::this_thread::sleep_for(std::chrono::milliseconds(100));
58  std::lock_guard<UncontendedMutex> g(m);
59  data = data + 1;
60  });
61  t1.join();
62  t2.join();
63  t3.join();
64 
65  EXPECT_EQ(data, 3);
66 }
67 
68 TEST(UncontendedMutex, HeavyContention)
69 {
70  int num_of_iter = 1000;
71  int num_of_thread = 1000;
73 
74  int data = 0;
76 
77  for (int t = 0 ; t < num_of_thread; ++t) {
78  threads.emplace_back([&] () {
79  for (int k = 0; k < num_of_iter; ++k) {
80  std::lock_guard<UncontendedMutex> g(m);
81  data++;
82  }
83  });
84  }
85 
86  for (auto& t : threads) {
87  t.join();
88  }
89  EXPECT_EQ(data, num_of_iter * num_of_thread);
90 }
gem5::UncontendedMutex
Definition: uncontended_mutex.hh:47
data
const char data[]
Definition: circlebuf.test.cc:48
std::vector
STL vector class.
Definition: stl.hh:37
gem5::ArmISA::t1
Bitfield< 1 > t1
Definition: misc_types.hh:232
gem5::ArmISA::t
Bitfield< 5 > t
Definition: misc_types.hh:70
TEST
TEST(UncontendedMutex, Lock)
Definition: uncontended_mutex.test.cc:37
gem5::MipsISA::g
Bitfield< 4 > g
Definition: dt_constants.hh:86
gem5::ArmISA::t2
Bitfield< 2 > t2
Definition: misc_types.hh:231
gem5::ArmISA::t3
Bitfield< 3 > t3
Definition: misc_types.hh:230
uncontended_mutex.hh
gem5::ArmISA::m
Bitfield< 0 > m
Definition: misc_types.hh:394
gem5::MipsISA::k
Bitfield< 23 > k
Definition: dt_constants.hh:81
gem5::X86ISA::Lock
@ Lock
Definition: types.hh:70
gem5
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
Definition: decoder.cc:40

Generated on Tue Sep 21 2021 12:25:00 for gem5 by doxygen 1.8.17