sim-se: split futex_map.cc into header and source files
[gem5.git] / src / sim / futex_map.hh
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 #ifndef __FUTEX_MAP_HH__
30 #define __FUTEX_MAP_HH__
31
32 #include <unordered_map>
33
34 #include <cpu/thread_context.hh>
35
36 /**
37 * FutexKey class defines an unique identifier for a particular futex in the
38 * system. The tgid and an address are the unique values needed as the key.
39 */
40 class FutexKey {
41 public:
42 uint64_t addr;
43 uint64_t tgid;
44
45 FutexKey(uint64_t addr_in, uint64_t tgid_in);
46
47 bool operator==(const FutexKey &in) const;
48 };
49
50 namespace std {
51 /**
52 * The unordered_map structure needs the parenthesis operator defined for
53 * std::hash if a user defined key is used. Our key is is user defined
54 * so we need to provide the hash functor.
55 */
56 template <>
57 struct hash<FutexKey>
58 {
59 size_t operator()(const FutexKey& in) const;
60 };
61 }
62
63 /**
64 * WaiterState defines internal state of a waiter thread. The state
65 * includes a pointer to the thread's context and its associated bitmask.
66 */
67 class WaiterState {
68 public:
69 ThreadContext* tc;
70 int bitmask;
71
72 /**
73 * this constructor is used if futex ops with bitset are used
74 */
75 WaiterState(ThreadContext* _tc, int _bitmask);
76
77 /**
78 * if bitset is not defined, just set bitmask to 0xffffffff
79 */
80 WaiterState(ThreadContext* _tc);
81
82 /**
83 * return true if the bit-wise AND of the wakeup_bitmask given by
84 * a waking thread and this thread's internal bitmask is non-zero
85 */
86 bool checkMask(int wakeup_bitmask) const;
87 };
88
89 typedef std::list<WaiterState> WaiterList;
90
91 /**
92 * FutexMap class holds a map of all futexes used in the system
93 */
94 class FutexMap : public std::unordered_map<FutexKey, WaiterList>
95 {
96 public:
97 /** Inserts a futex into the map with one waiting TC */
98 void suspend(Addr addr, uint64_t tgid, ThreadContext *tc);
99
100 /** Wakes up at most count waiting threads on a futex */
101 int wakeup(Addr addr, uint64_t tgid, int count);
102
103 void suspend_bitset(Addr addr, uint64_t tgid, ThreadContext *tc,
104 int bitmask);
105
106 int wakeup_bitset(Addr addr, uint64_t tgid, int bitmask);
107
108 /**
109 * This operation wakes a given number (val) of waiters. If there are
110 * more threads waiting than woken, they are removed from the wait
111 * queue of the futex pointed to by addr1 and added to the wait queue
112 * of the futex pointed to by addr2. The number of waiter moved is
113 * capped by count2 (misused timeout parameter).
114 *
115 * The return value is the number of waiters that are woken or
116 * requeued.
117 */
118 int requeue(Addr addr1, uint64_t tgid, int count, int count2, Addr addr2);
119 };
120
121 #endif // __FUTEX_MAP_HH__