gem5  v22.1.0.0
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 }
const char data[]
Bitfield< 2 > t2
Definition: misc_types.hh:233
Bitfield< 3 > t3
Definition: misc_types.hh:232
Bitfield< 1 > t1
Definition: misc_types.hh:234
Bitfield< 4 > g
Definition: dt_constants.hh:86
Bitfield< 23 > k
Definition: dt_constants.hh:81
Bitfield< 51 > t
Definition: pagetable.hh:56
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
TEST(UncontendedMutex, Lock)

Generated on Wed Dec 21 2022 10:22:29 for gem5 by doxygen 1.9.1