Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 :
11 : #ifndef QUEUE_MANAGER_H
12 : #define QUEUE_MANAGER_H
13 :
14 : #include <cstdint>
15 : #include <atomic>
16 : #include <condition_variable>
17 : #include "bqs_status.h"
18 : #include "bqs_log.h"
19 : #include "bind_relation.h"
20 : #include "bqs_util.h"
21 :
22 : namespace bqs {
23 : class QueueManager {
24 : public:
25 : static QueueManager &GetInstance();
26 :
27 : ~QueueManager();
28 :
29 : QueueManager(const QueueManager &) = delete;
30 :
31 : QueueManager(QueueManager &&) = delete;
32 :
33 : QueueManager &operator=(const QueueManager &) = delete;
34 :
35 : QueueManager &operator=(QueueManager &&) = delete;
36 :
37 : /**
38 : * init/create/subscribe buff queue
39 : * @return BQS_STATUS_OK:success other:failed
40 : */
41 : BqsStatus InitQueueManager(const uint32_t deviceId, const uint32_t groupId, const bool hasAICPU,
42 : const std::string& groupName);
43 : void InitExtra(const uint32_t deviceIdExtra, const uint32_t groupIdExtra);
44 :
45 : BqsStatus InitQueueExtra();
46 :
47 : /**
48 : * init/create/subscribe buff queue
49 : * @return BQS_STATUS_OK:success other:failed
50 : */
51 : BqsStatus InitQueue();
52 :
53 : /**
54 : * destroy buff queue
55 : * @return NA
56 : */
57 : void Destroy();
58 :
59 : /**
60 : * Enqueue a data to implies that the client sent a message
61 : * @return BQS_STATUS_OK:success other:failed
62 : */
63 : BqsStatus EnqueueRelationEvent();
64 :
65 : BqsStatus EnqueueRelationEventExtra();
66 :
67 : BqsStatus EnqueueRelationEventToQ(const uint32_t deviceId, const uint32_t relationEventQ) const;
68 :
69 : /**
70 : * handle the bind or unbind msg that the client sent
71 : * @return true:has handle relation msg, false:not handle
72 : */
73 : bool HandleRelationEvent(const uint32_t index = 0U) const;
74 :
75 : /**
76 : * enqueue the queue id of full to not full queue
77 : * @return BQS_STATUS_OK:success other:failed
78 : */
79 : BqsStatus EnqueueFullToNotFullEvent(const uint32_t index);
80 :
81 : /**
82 : * handle the event of full to not full
83 : * @return true:has handle f2nf msg, false:not handle
84 : */
85 : bool HandleFullToNotFullEvent(const uint32_t index);
86 :
87 : /**
88 : * enqueue the queue id of asyn mem buff queue
89 : * @return BQS_STATUS_OK:success other:failed
90 : */
91 : BqsStatus EnqueueAsynMemBuffEvent();
92 :
93 : /**
94 : * handle the event of asyn mem buff event
95 : * @return true:has handle asyn mem buff msg, false:not handle
96 : */
97 : bool HandleAsynMemBuffEvent(const uint32_t index);
98 :
99 : /**
100 : * enable async mem dequeu flag
101 : */
102 1 : void enableAsyncMemDequeueFlag()
103 : {
104 1 : isTriggeredByAsyncMemDequeue_ = true;
105 1 : }
106 :
107 : /**
108 : * enable async mem enqueue flag
109 : */
110 2 : void enableAsyncMemEnqueueFlag()
111 : {
112 2 : isTriggeredByAsyncMemEnqueue_ = true;
113 2 : }
114 :
115 : /**
116 : * work thread init success will notify queue manager
117 : * @return NA
118 : */
119 : void NotifyInitSuccess(const uint32_t index);
120 :
121 : /**
122 : * log queue status for error occur
123 : * @return NA
124 : */
125 : void LogErrorQueueStatus(const uint32_t queueId) const;
126 :
127 : /**
128 : * log relation queue status for error occur
129 : * @return NA
130 : */
131 : void LogErrorRelationQueueStatus() const;
132 :
133 : /**
134 : * create and subscribe buff queue
135 : * @param name queue name
136 : * @param depth queue depth
137 : * @param queueId queue id
138 : * @return BQS_STATUS_OK: success, other: failed
139 : */
140 : BqsStatus CreateAndSubscribeQueue(const char_t * const name, const uint32_t depth, uint32_t &queueId) const;
141 :
142 : BqsStatus CreateAndSubscribeQueueExtra(const char_t * const name, const uint32_t depth, uint32_t &queueId) const;
143 :
144 : /**
145 : * destroy queue
146 : * @param queueId queue id
147 : * @return BQS_STATUS_OK: success, other: failed
148 : */
149 : BqsStatus DestroyQueue(const uint32_t queueId) const;
150 :
151 : BqsStatus DestroyQueue(const uint32_t queueId, uint32_t deviceId) const;
152 :
153 : /**
154 : * @brief Create a Queue object
155 : * @param name queue name
156 : * @param depth queue depth
157 : * @param queueId queue id
158 : * @return BQS_STATUS_OK: success, other: failed
159 : */
160 : BqsStatus CreateQueue(const char_t * const name, const uint32_t depth, uint32_t &queueId) const;
161 :
162 : BqsStatus CreateQueue(const char_t * const name, const uint32_t depth, uint32_t &queueId, uint32_t deviceId) const;
163 :
164 : /**
165 : * @brief get device id
166 : * @return device id
167 : */
168 7 : inline uint32_t GetDeviceId() const
169 : {
170 7 : return deviceId_;
171 : }
172 :
173 2 : inline uint32_t GetExtraDeviceId() const
174 : {
175 2 : return deviceIdExtra_;
176 : }
177 : private:
178 : /**
179 : * @brief Construct a new Queue Manager object
180 : */
181 : QueueManager();
182 : BqsStatus UnsubscribeQueue(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
183 : void Clear();
184 : void ClearQueue(const uint32_t queueId, const QUEUE_EVENT_TYPE eventType) const;
185 : void MakeUpF2NFMbuf(const uint32_t index);
186 : void MakeUpMbuf(Mbuf **mbufPtr) const;
187 :
188 : private:
189 : uint32_t deviceId_;
190 : uint32_t groupId_;
191 : std::string grpName_;
192 : uint32_t relationEventQId_;
193 : uint32_t fullToNotFullEventQId_;
194 : uint32_t asyncMemDequeueBuffQId_;
195 : uint32_t asyncMemEnqueueBuffQId_;
196 :
197 : std::condition_variable cv_; // condition var to wait queue_schedule init success
198 : std::mutex mutex_;
199 : bool initialized_; // true means queue_schedule has init success
200 : bool stopped_; // true means queue manager has been stopped
201 : std::atomic<bool> f2nfQueueEmptyFlag_;
202 : Mbuf *mbufForF2nf_;
203 : SpinLock f2nfLock_;
204 : bool relationEventQInitialized_;
205 : bool fullToNotFullEventQInitialized_;
206 : uint32_t deviceIdExtra_;
207 : uint32_t groupIdExtra_;
208 : uint32_t relationEventQIdExtra_;
209 : bool relationEventQInitializedExtra_;
210 : uint32_t fullToNotFullEventQIdExtra_;
211 : bool fullToNotFullEventQInitializedExtra_;
212 : Mbuf *mbufForF2nfExtra_;
213 : SpinLock f2nfLockExtra_;
214 : std::atomic<bool> f2nfQueueEmptyFlagExtra_;
215 : bool isTriggeredByAsyncMemDequeue_;
216 : bool isTriggeredByAsyncMemEnqueue_;
217 : bool ayncMemBuffEventQInitialized_;
218 : bool initiallizedExtra_;
219 : };
220 : } // namespace bqs
221 : #endif // QUEUE_MANAGER_H
|