gem5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
futex_map.hh
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  * Authors: Brandon Potter
29  * Steve Reinhardt
30  * Alexandru Dutu
31  */
32 
33 #ifndef __FUTEX_MAP_HH__
34 #define __FUTEX_MAP_HH__
35 
36 #include <unordered_map>
37 
38 #include <cpu/thread_context.hh>
39 
44 class FutexKey {
45  public:
46  uint64_t addr;
47  uint64_t tgid;
48 
49  FutexKey(uint64_t addr_in, uint64_t tgid_in)
50  : addr(addr_in), tgid(tgid_in)
51  {
52  }
53 
54  bool
55  operator==(const FutexKey &in) const
56  {
57  return addr == in.addr && tgid == in.tgid;
58  }
59 };
60 
61 namespace std {
67  template <>
68  struct hash<FutexKey>
69  {
70  size_t operator()(const FutexKey& in) const
71  {
72  size_t hash = 65521;
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);
76  }
77  return hash;
78  }
79  };
80 }
81 
83 
87 class FutexMap : public std::unordered_map<FutexKey, ThreadContextList>
88 {
89  public:
91  void
92  suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
93  {
94  FutexKey key(addr, tgid);
95  auto it = find(key);
96 
97  if (it == end()) {
98  ThreadContextList tcList {tc};
99  insert({key, tcList});
100  } else {
101  it->second.push_back(tc);
102  }
103 
105  tc->suspend();
106  }
107 
109  int
110  wakeup(Addr addr, uint64_t tgid, int count)
111  {
112  FutexKey key(addr, tgid);
113  auto it = find(key);
114 
115  if (it == end())
116  return 0;
117 
118  int woken_up = 0;
119  auto &tcList = it->second;
120 
121  while (!tcList.empty() && woken_up < count) {
122  tcList.front()->activate();
123  tcList.pop_front();
124  woken_up++;
125  }
126 
127  if (tcList.empty())
128  erase(it);
129 
130  return woken_up;
131  }
132 
133 };
134 
135 #endif // __FUTEX_MAP_HH__
count
Definition: misc.hh:704
Bitfield< 7 > i
Definition: miscregs.hh:1378
ip6_addr_t addr
Definition: inet.hh:335
ThreadContext is the external interface to all thread state for anything outside of the CPU...
int wakeup(Addr addr, uint64_t tgid, int count)
Wakes up at most count waiting threads on a futex.
Definition: futex_map.hh:110
virtual void suspend()=0
Set the status to Suspended.
bool operator==(const FutexKey &in) const
Definition: futex_map.hh:55
STL list class.
Definition: stl.hh:54
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition: types.hh:142
uint64_t addr
Definition: futex_map.hh:46
FutexKey(uint64_t addr_in, uint64_t tgid_in)
Definition: futex_map.hh:49
void suspend(Addr addr, uint64_t tgid, ThreadContext *tc)
Inserts a futex into the map with one waiting TC.
Definition: futex_map.hh:92
std::list< ThreadContext * > ThreadContextList
Definition: futex_map.hh:82
FutexMap class holds a map of all futexes used in the system.
Definition: futex_map.hh:87
FutexKey class defines an unique identifier for a particular futex in the system. ...
Definition: futex_map.hh:44
uint64_t tgid
Definition: futex_map.hh:47
size_t operator()(const FutexKey &in) const
Definition: futex_map.hh:70

Generated on Fri Jun 9 2017 13:03:51 for gem5 by doxygen 1.8.6