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.
28 * Authors: Brandon Potter
33 #ifndef __FUTEX_MAP_HH__
34 #define __FUTEX_MAP_HH__
36 #include <unordered_map>
38 #include <cpu/thread_context.hh>
41 * FutexKey class defines an unique identifier for a particular futex in the
42 * system. The tgid and an address are the unique values needed as the key.
49 FutexKey(uint64_t addr_in, uint64_t tgid_in)
50 : addr(addr_in), tgid(tgid_in)
55 operator==(const FutexKey &in) const
57 return addr == in.addr && tgid == in.tgid;
63 * The unordered_map structure needs the parenthesis operator defined for
64 * std::hash if a user defined key is used. Our key is is user defined
65 * so we need to provide the hash functor.
70 size_t operator()(const FutexKey& in) const
73 for (int i = 0; i < sizeof(uint64_t) / sizeof(size_t); i++) {
74 hash ^= (size_t)(in.addr >> sizeof(size_t) * i) ^
75 (size_t)(in.tgid >> sizeof(size_t) * i);
82 typedef std::list<ThreadContext *> ThreadContextList;
85 * FutexMap class holds a map of all futexes used in the system
87 class FutexMap : public std::unordered_map<FutexKey, ThreadContextList>
90 /** Inserts a futex into the map with one waiting TC */
92 suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
94 FutexKey key(addr, tgid);
98 ThreadContextList tcList {tc};
99 insert({key, tcList});
101 it->second.push_back(tc);
104 /** Suspend the thread context */
108 /** Wakes up at most count waiting threads on a futex */
110 wakeup(Addr addr, uint64_t tgid, int count)
112 FutexKey key(addr, tgid);
119 auto &tcList = it->second;
121 while (!tcList.empty() && woken_up < count) {
122 tcList.front()->activate();
135 #endif // __FUTEX_MAP_HH__