2 * Copyright (c) 2017 Advanced Micro Devices, Inc.
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.
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.
29 #include <debug/Thread.hh>
30 #include <sim/futex_map.hh>
32 FutexKey::FutexKey(uint64_t addr_in
, uint64_t tgid_in
)
33 : addr(addr_in
), tgid(tgid_in
) {}
36 FutexKey::operator==(const FutexKey
&in
) const
38 return addr
== in
.addr
&& tgid
== in
.tgid
;
42 size_t hash
<FutexKey
>::operator()(const FutexKey
& in
) const
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
);
53 WaiterState::WaiterState(ThreadContext
* _tc
, int _bitmask
)
54 : tc(_tc
), bitmask(_bitmask
) { }
56 WaiterState::WaiterState(ThreadContext
* _tc
)
57 : tc(_tc
), bitmask(0xffffffff) { }
60 WaiterState::checkMask(int wakeup_bitmask
) const
62 return bitmask
& wakeup_bitmask
;
66 FutexMap::suspend(Addr addr
, uint64_t tgid
, ThreadContext
*tc
)
68 FutexKey
key(addr
, tgid
);
72 WaiterList waiterList
{WaiterState(tc
)};
73 insert({key
, waiterList
});
75 it
->second
.push_back(WaiterState(tc
));
78 /** Suspend the thread context */
83 FutexMap::wakeup(Addr addr
, uint64_t tgid
, int count
)
85 FutexKey
key(addr
, tgid
);
92 auto &waiterList
= it
->second
;
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
) {
104 waiterList
.pop_front();
107 if (waiterList
.empty())
114 FutexMap::suspend_bitset(Addr addr
, uint64_t tgid
, ThreadContext
*tc
,
117 FutexKey
key(addr
, tgid
);
121 WaiterList waiterList
{WaiterState(tc
, bitmask
)};
122 insert({key
, waiterList
});
124 it
->second
.push_back(WaiterState(tc
, bitmask
));
127 /** Suspend the thread context */
132 FutexMap::wakeup_bitset(Addr addr
, uint64_t tgid
, int bitmask
)
134 FutexKey
key(addr
, tgid
);
142 auto &waiterList
= it
->second
;
143 auto iter
= waiterList
.begin();
145 while (iter
!= waiterList
.end()) {
146 WaiterState
& waiter
= *iter
;
148 if (waiter
.checkMask(bitmask
)) {
149 waiter
.tc
->activate();
150 iter
= waiterList
.erase(iter
);
157 if (waiterList
.empty())
164 FutexMap::requeue(Addr addr1
, uint64_t tgid
, int count
, int count2
, Addr addr2
)
166 FutexKey
key1(addr1
, tgid
);
167 auto it1
= find(key1
);
173 auto &waiterList1
= it1
->second
;
175 while (!waiterList1
.empty() && woken_up
< count
) {
176 waiterList1
.front().tc
->activate();
177 waiterList1
.pop_front();
184 while (!waiterList1
.empty() && requeued
< count2
) {
185 auto w
= waiterList1
.front();
186 waiterList1
.pop_front();
187 tmpList
.push_back(w
);
191 FutexKey
key2(addr2
, tgid
);
192 auto it2
= find(key2
);
194 if (it2
== end() && requeued
> 0) {
195 insert({key2
, tmpList
});
197 it2
->second
.insert(it2
->second
.end(),
198 tmpList
.begin(), tmpList
.end());
201 if (waiterList1
.empty())
204 return woken_up
+ requeued
;