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 <sim/futex_map.hh>
31 FutexKey::FutexKey(uint64_t addr_in
, uint64_t tgid_in
)
32 : addr(addr_in
), tgid(tgid_in
) {}
35 FutexKey::operator==(const FutexKey
&in
) const
37 return addr
== in
.addr
&& tgid
== in
.tgid
;
41 size_t hash
<FutexKey
>::operator()(const FutexKey
& in
) const
44 for (int i
= 0; i
< sizeof(uint64_t) / sizeof(size_t); i
++) {
45 hash
^= (size_t)(in
.addr
>> sizeof(size_t) * i
) ^
46 (size_t)(in
.tgid
>> sizeof(size_t) * i
);
52 WaiterState::WaiterState(ThreadContext
* _tc
, int _bitmask
)
53 : tc(_tc
), bitmask(_bitmask
) { }
56 WaiterState::checkMask(int wakeup_bitmask
) const
58 return bitmask
& wakeup_bitmask
;
62 FutexMap::suspend(Addr addr
, uint64_t tgid
, ThreadContext
*tc
)
64 suspend_bitset(addr
, tgid
, tc
, 0xffffffff);
68 FutexMap::wakeup(Addr addr
, uint64_t tgid
, int count
)
70 FutexKey
key(addr
, tgid
);
77 auto &waiterList
= it
->second
;
79 while (!waiterList
.empty() && woken_up
< count
) {
80 // Threads may be woken up by access to locked
81 // memory addresses outside of syscalls, so we
82 // must only count threads that were actually
83 // woken up by this syscall.
84 auto& tc
= waiterList
.front().tc
;
87 waiterList
.pop_front();
91 if (waiterList
.empty())
98 FutexMap::suspend_bitset(Addr addr
, uint64_t tgid
, ThreadContext
*tc
,
101 FutexKey
key(addr
, tgid
);
105 WaiterList waiterList
{WaiterState(tc
, bitmask
)};
106 insert({key
, waiterList
});
108 it
->second
.push_back(WaiterState(tc
, bitmask
));
110 waitingTcs
.emplace(tc
);
112 /** Suspend the thread context */
117 FutexMap::wakeup_bitset(Addr addr
, uint64_t tgid
, int bitmask
)
119 FutexKey
key(addr
, tgid
);
127 auto &waiterList
= it
->second
;
128 auto iter
= waiterList
.begin();
130 while (iter
!= waiterList
.end()) {
131 WaiterState
& waiter
= *iter
;
133 if (waiter
.checkMask(bitmask
)) {
134 waiter
.tc
->activate();
135 iter
= waiterList
.erase(iter
);
136 waitingTcs
.erase(waiter
.tc
);
143 if (waiterList
.empty())
150 FutexMap::requeue(Addr addr1
, uint64_t tgid
, int count
, int count2
, Addr addr2
)
152 FutexKey
key1(addr1
, tgid
);
153 auto it1
= find(key1
);
159 auto &waiterList1
= it1
->second
;
161 while (!waiterList1
.empty() && woken_up
< count
) {
162 waiterList1
.front().tc
->activate();
163 waiterList1
.pop_front();
170 while (!waiterList1
.empty() && requeued
< count2
) {
171 auto w
= waiterList1
.front();
172 waiterList1
.pop_front();
173 tmpList
.push_back(w
);
177 FutexKey
key2(addr2
, tgid
);
178 auto it2
= find(key2
);
180 if (it2
== end() && requeued
> 0) {
181 insert({key2
, tmpList
});
183 it2
->second
.insert(it2
->second
.end(),
184 tmpList
.begin(), tmpList
.end());
187 if (waiterList1
.empty())
190 return woken_up
+ requeued
;
194 FutexMap::is_waiting(ThreadContext
*tc
)
196 return waitingTcs
.find(tc
) != waitingTcs
.end();