gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
35using 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
68TEST(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[]
STL vector class.
Definition stl.hh:37
Bitfield< 2 > t2
Bitfield< 5 > t
Definition misc_types.hh:71
Bitfield< 3 > t3
Bitfield< 1 > t1
Bitfield< 0 > m
Bitfield< 4 > g
Bitfield< 23 > k
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
TEST(UncontendedMutex, Lock)

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0