forked from HSAFoundation/phsa-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Queue.hh
132 lines (104 loc) · 4.31 KB
/
Queue.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (c) 2016-2017 General Processor Tech.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
/**
* @author [email protected] and [email protected]
* for General Processor Tech.
*/
#ifndef HSA_RUNTIME_QUEUE_HH
#define HSA_RUNTIME_QUEUE_HH
#include <atomic>
#include <cinttypes>
#include <unordered_map>
#include <mutex>
#include <boost/thread/shared_mutex.hpp>
#include "common/Atomic.hh"
#include "hsa.h"
#include "phsa-queue.h"
#include <iostream>
namespace phsa {
class Agent;
// Queue class is an interface for implementing user mode queues and soft
// queues.
//
// For an implementation example, see `src/Devices/CPU/UserModeQueue.[cc|hh]`.
class Queue {
public:
using QueueCallback =
std::function<void(hsa_status_t, hsa_queue_t *, void *)>;
Queue(hsa_queue_t *Queue, Agent *Owner = nullptr)
: Owner(Owner), Destroyed(false), Inactivated(false),
LastHandledDoorBell(std::numeric_limits<hsa_signal_value_t>::max()) {
registerQueue(this, Queue);
}
virtual ~Queue() {}
Agent *ownerAgent() { return Owner; }
virtual uint64_t loadWriteIndex(MemoryOrder MO) = 0;
virtual void storeWriteIndex(uint64_t Value, MemoryOrder MO) = 0;
virtual uint64_t addWriteIndex(uint64_t Increment, MemoryOrder MO) = 0;
virtual uint64_t compareExchangeWriteIndex(uint64_t ExpectedValue,
uint64_t Value,
MemoryOrder MO) = 0;
virtual uint64_t loadReadIndex(MemoryOrder MO) = 0;
virtual void storeReadIndex(uint64_t Value, MemoryOrder MO) = 0;
static Queue *FindQueue(const hsa_queue_t *HSAQueue);
virtual void ExecuteCallback(hsa_status_t Status) = 0;
void setDestroyed() { Destroyed = true; }
bool isDestroyed() const { return Destroyed; }
void setInactivated();
bool isInactivated() const { return Inactivated; }
static void garbageCollect();
virtual hsa_queue_t *getHSAQueue() {
return reinterpret_cast<hsa_queue_t *>(&HSAQueue);
}
// Used to internally keep book of packets that have been processed.
void SetPacketProcessed(size_t QI, bool Flag)
{ if (PacketIsProcessed.size() == 0)
PacketIsProcessed.resize(HSAQueue.hsa_queue.size, false);
PacketIsProcessed[QI] = Flag; }
bool IsPacketProcessed(size_t QI) const {
if (PacketIsProcessed.size() == 0) return false;
return PacketIsProcessed[QI]; }
hsa_signal_value_t getLastHandledDoorBell() const
{ return LastHandledDoorBell; }
void setLastHandledDoorBell(hsa_signal_value_t DBValue)
{ LastHandledDoorBell = DBValue; }
protected:
uint64_t getNextId() const { return ++QueueCount; }
static std::unordered_map<const hsa_queue_t *, Queue *> Registry;
static boost::shared_mutex RegistryLock;
private:
static void registerQueue(Queue *Q, const hsa_queue_t *HSAQueue) {
boost::lock_guard<boost::shared_mutex> L(RegistryLock);
Registry[HSAQueue] = Q;
}
static void deregisterQueue(Queue *Q) {
boost::lock_guard<boost::shared_mutex> L(RegistryLock);
Registry.erase(Q->getHSAQueue());
}
static std::atomic<uint64_t> QueueCount;
Agent *Owner;
bool Destroyed;
bool Inactivated;
std::vector<bool> PacketIsProcessed;
hsa_signal_value_t LastHandledDoorBell;
protected:
phsa_queue HSAQueue;
};
} // namespace phsa
#endif // HSA_RUNTIME_QUEUE_HH