gem5  v22.1.0.0
futex_map.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Advanced Micro Devices, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met: redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer;
9  * redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution;
12  * neither the name of the copyright holders nor the names of its
13  * contributors may be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sim/futex_map.hh>
30 
31 namespace gem5
32 {
33 
34 FutexKey::FutexKey(uint64_t addr_in, uint64_t tgid_in)
35  : addr(addr_in), tgid(tgid_in) {}
36 
37 bool
39 {
40  return addr == in.addr && tgid == in.tgid;
41 }
42 
44  : tc(_tc), bitmask(_bitmask) { }
45 
46 bool
47 WaiterState::checkMask(int wakeup_bitmask) const
48 {
49  return bitmask & wakeup_bitmask;
50 }
51 
52 void
54 {
55  suspend_bitset(addr, tgid, tc, 0xffffffff);
56 }
57 
58 int
59 FutexMap::wakeup(Addr addr, uint64_t tgid, int count)
60 {
61  FutexKey key(addr, tgid);
62  auto it = find(key);
63 
64  if (it == end())
65  return 0;
66 
67  int woken_up = 0;
68  auto &waiterList = it->second;
69 
70  while (!waiterList.empty() && woken_up < count) {
71  // Threads may be woken up by access to locked
72  // memory addresses outside of syscalls, so we
73  // must only count threads that were actually
74  // woken up by this syscall.
75  auto& tc = waiterList.front().tc;
76  tc->activate();
77  woken_up++;
78  waiterList.pop_front();
79  waitingTcs.erase(tc);
80  }
81 
82  if (waiterList.empty())
83  erase(it);
84 
85  return woken_up;
86 }
87 
88 void
90  int bitmask)
91 {
92  FutexKey key(addr, tgid);
93  auto it = find(key);
94 
95  if (it == end()) {
96  WaiterList waiterList {WaiterState(tc, bitmask)};
97  insert({key, waiterList});
98  } else {
99  it->second.push_back(WaiterState(tc, bitmask));
100  }
101  waitingTcs.emplace(tc);
102 
104  tc->suspend();
105 }
106 
107 int
108 FutexMap::wakeup_bitset(Addr addr, uint64_t tgid, int bitmask)
109 {
110  FutexKey key(addr, tgid);
111  auto it = find(key);
112 
113  if (it == end())
114  return 0;
115 
116  int woken_up = 0;
117 
118  auto &waiterList = it->second;
119  auto iter = waiterList.begin();
120 
121  while (iter != waiterList.end()) {
122  WaiterState& waiter = *iter;
123 
124  if (waiter.checkMask(bitmask)) {
125  waiter.tc->activate();
126  waitingTcs.erase(waiter.tc);
127  iter = waiterList.erase(iter);
128  woken_up++;
129  } else {
130  ++iter;
131  }
132  }
133 
134  if (waiterList.empty())
135  erase(it);
136 
137  return woken_up;
138 }
139 
140 int
141 FutexMap::requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2)
142 {
143  FutexKey key1(addr1, tgid);
144  auto it1 = find(key1);
145 
146  if (it1 == end())
147  return 0;
148 
149  int woken_up = 0;
150  auto &waiterList1 = it1->second;
151 
152  while (!waiterList1.empty() && woken_up < count) {
153  waiterList1.front().tc->activate();
154  waiterList1.pop_front();
155  woken_up++;
156  }
157 
158  WaiterList tmpList;
159  int requeued = 0;
160 
161  while (!waiterList1.empty() && requeued < count2) {
162  auto w = waiterList1.front();
163  waiterList1.pop_front();
164  tmpList.push_back(w);
165  requeued++;
166  }
167 
168  FutexKey key2(addr2, tgid);
169  auto it2 = find(key2);
170 
171  if (it2 == end() && requeued > 0) {
172  insert({key2, tmpList});
173  } else {
174  it2->second.insert(it2->second.end(),
175  tmpList.begin(), tmpList.end());
176  }
177 
178  if (waiterList1.empty())
179  erase(it1);
180 
181  return woken_up + requeued;
182 }
183 
184 bool
186 {
187  return waitingTcs.find(tc) != waitingTcs.end();
188 }
189 
190 } // namespace gem5
FutexKey class defines an unique identifier for a particular futex in the system.
Definition: futex_map.hh:45
FutexKey(uint64_t addr_in, uint64_t tgid_in)
Definition: futex_map.cc:34
uint64_t tgid
Definition: futex_map.hh:48
uint64_t addr
Definition: futex_map.hh:47
bool operator==(const FutexKey &in) const
Definition: futex_map.cc:38
void suspend_bitset(Addr addr, uint64_t tgid, ThreadContext *tc, int bitmask)
Definition: futex_map.cc:89
bool is_waiting(ThreadContext *tc)
Determine if the given thread context is currently waiting on a futex wait operation on any of the fu...
Definition: futex_map.cc:185
int requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2)
This operation wakes a given number (val) of waiters.
Definition: futex_map.cc:141
void suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
Inserts a futex into the map with one waiting TC.
Definition: futex_map.cc:53
std::unordered_set< ThreadContext * > waitingTcs
Definition: futex_map.hh:143
int wakeup(Addr addr, uint64_t tgid, int count)
Wakes up at most count waiting threads on a futex.
Definition: futex_map.cc:59
int wakeup_bitset(Addr addr, uint64_t tgid, int bitmask)
Definition: futex_map.cc:108
ThreadContext is the external interface to all thread state for anything outside of the CPU.
virtual void activate()=0
Set the status to Active.
virtual void suspend()=0
Set the status to Suspended.
WaiterState defines internal state of a waiter thread.
Definition: futex_map.hh:87
bool checkMask(int wakeup_bitmask) const
return true if the bit-wise AND of the wakeup_bitmask given by a waking thread and this thread's inte...
Definition: futex_map.cc:47
ThreadContext * tc
Definition: futex_map.hh:89
WaiterState(ThreadContext *_tc, int _bitmask)
this constructor is used if futex ops with bitset are used
Definition: futex_map.cc:43
STL list class.
Definition: stl.hh:51
Bitfield< 15, 10 > it2
Definition: misc_types.hh:63
Bitfield< 26, 25 > it1
Definition: misc_types.hh:56
Bitfield< 6 > w
Definition: pagetable.hh:59
Bitfield< 3 > addr
Definition: types.hh:84
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:147

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