sim-se: split futex_map.cc into header and source files
[gem5.git] / src / sim / futex_map.cc
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 <debug/Thread.hh>
30 #include <sim/futex_map.hh>
31
32 FutexKey::FutexKey(uint64_t addr_in, uint64_t tgid_in)
33 : addr(addr_in), tgid(tgid_in) {}
34
35 bool
36 FutexKey::operator==(const FutexKey &in) const
37 {
38 return addr == in.addr && tgid == in.tgid;
39 }
40
41 namespace std {
42 size_t hash<FutexKey>::operator()(const FutexKey& in) const
43 {
44 size_t hash = 65521;
45 for (int i = 0; i < sizeof(uint64_t) / sizeof(size_t); i++) {
46 hash ^= (size_t)(in.addr >> sizeof(size_t) * i) ^
47 (size_t)(in.tgid >> sizeof(size_t) * i);
48 }
49 return hash;
50 }
51 }
52
53 WaiterState::WaiterState(ThreadContext* _tc, int _bitmask)
54 : tc(_tc), bitmask(_bitmask) { }
55
56 WaiterState::WaiterState(ThreadContext* _tc)
57 : tc(_tc), bitmask(0xffffffff) { }
58
59 bool
60 WaiterState::checkMask(int wakeup_bitmask) const
61 {
62 return bitmask & wakeup_bitmask;
63 }
64
65 void
66 FutexMap::suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
67 {
68 FutexKey key(addr, tgid);
69 auto it = find(key);
70
71 if (it == end()) {
72 WaiterList waiterList {WaiterState(tc)};
73 insert({key, waiterList});
74 } else {
75 it->second.push_back(WaiterState(tc));
76 }
77
78 /** Suspend the thread context */
79 tc->suspend();
80 }
81
82 int
83 FutexMap::wakeup(Addr addr, uint64_t tgid, int count)
84 {
85 FutexKey key(addr, tgid);
86 auto it = find(key);
87
88 if (it == end())
89 return 0;
90
91 int woken_up = 0;
92 auto &waiterList = it->second;
93
94 while (!waiterList.empty() && woken_up < count) {
95 // Threads may be woken up by access to locked
96 // memory addresses outside of syscalls, so we
97 // must only count threads that were actually
98 // woken up by this syscall.
99 auto& tc = waiterList.front().tc;
100 if (tc->status() == ThreadContext::Suspended) {
101 tc->activate();
102 woken_up++;
103 }
104 waiterList.pop_front();
105 }
106
107 if (waiterList.empty())
108 erase(it);
109
110 return woken_up;
111 }
112
113 void
114 FutexMap::suspend_bitset(Addr addr, uint64_t tgid, ThreadContext *tc,
115 int bitmask)
116 {
117 FutexKey key(addr, tgid);
118 auto it = find(key);
119
120 if (it == end()) {
121 WaiterList waiterList {WaiterState(tc, bitmask)};
122 insert({key, waiterList});
123 } else {
124 it->second.push_back(WaiterState(tc, bitmask));
125 }
126
127 /** Suspend the thread context */
128 tc->suspend();
129 }
130
131 int
132 FutexMap::wakeup_bitset(Addr addr, uint64_t tgid, int bitmask)
133 {
134 FutexKey key(addr, tgid);
135 auto it = find(key);
136
137 if (it == end())
138 return 0;
139
140 int woken_up = 0;
141
142 auto &waiterList = it->second;
143 auto iter = waiterList.begin();
144
145 while (iter != waiterList.end()) {
146 WaiterState& waiter = *iter;
147
148 if (waiter.checkMask(bitmask)) {
149 waiter.tc->activate();
150 iter = waiterList.erase(iter);
151 woken_up++;
152 } else {
153 ++iter;
154 }
155 }
156
157 if (waiterList.empty())
158 erase(it);
159
160 return woken_up;
161 }
162
163 int
164 FutexMap::requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2)
165 {
166 FutexKey key1(addr1, tgid);
167 auto it1 = find(key1);
168
169 if (it1 == end())
170 return 0;
171
172 int woken_up = 0;
173 auto &waiterList1 = it1->second;
174
175 while (!waiterList1.empty() && woken_up < count) {
176 waiterList1.front().tc->activate();
177 waiterList1.pop_front();
178 woken_up++;
179 }
180
181 WaiterList tmpList;
182 int requeued = 0;
183
184 while (!waiterList1.empty() && requeued < count2) {
185 auto w = waiterList1.front();
186 waiterList1.pop_front();
187 tmpList.push_back(w);
188 requeued++;
189 }
190
191 FutexKey key2(addr2, tgid);
192 auto it2 = find(key2);
193
194 if (it2 == end() && requeued > 0) {
195 insert({key2, tmpList});
196 } else {
197 it2->second.insert(it2->second.end(),
198 tmpList.begin(), tmpList.end());
199 }
200
201 if (waiterList1.empty())
202 erase(it1);
203
204 return woken_up + requeued;
205 }