Debian Codegraph @

media_softlet/agnostic/common/os/mos_hybrid_cmd_manager.cpp

raw
1 /*
2 * Copyright (c) 2025, Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 * OTHER DEALINGS IN THE SOFTWARE.
21 */
22 #include "mos_hybrid_cmd_manager.h"
23 #include "mos_util_debug.h"
24 #include "mos_utilities.h"
25 #include <functional>
26
27 HybridCmdMgr ::~HybridCmdMgr()
28 {
29 StopThread();
30 }
31
32 MOS_STATUS HybridCmdMgr::SetSubmitMode(HYBRID_MGR_SUBMIT_MODE submitMode)
33 {
34 m_submitMode = submitMode;
35 return MOS_STATUS_SUCCESS;
36 }
37
38 MOS_STATUS HybridCmdMgr::StartThread()
39 {
40 std::lock_guard<std::mutex> lock(m_threadMutex);
41 {
42 std::lock_guard<std::mutex> lock(m_queueMutex);
43 m_stopFlag = false;
44 }
45 if (!m_consumerThread.joinable())
46 {
47 MOS_OS_NORMALMESSAGE("Hybrid Consumer Thread Started");
48 m_consumerThread = std::thread(std::bind(&HybridCmdMgr::Consumer, this));
49 }
50
51 return MOS_STATUS_SUCCESS;
52 }
53
54 MOS_STATUS HybridCmdMgr::StopThread()
55 {
56 std::lock_guard<std::mutex> lock(m_threadMutex);
57 {
58 std::lock_guard<std::mutex> lock(m_queueMutex);
59 m_stopFlag = true;
60 }
61 m_wakeCondition.notify_one();
62
63 if (m_consumerThread.joinable())
64 {
65 MOS_OS_NORMALMESSAGE("Hybrid Consumer Thread is Stopping");
66 m_consumerThread.join();
67 }
68
69 return MOS_STATUS_SUCCESS;
70 }
71
72 MOS_STATUS HybridCmdMgr::SubmitPackage(CmdPackage &cmdPackage)
73 {
74 std::unique_ptr<CmdPackage> package = cmdPackage.Clone();
75 if (m_submitMode == HYBRID_MGR_SUBMIT_MODE::ASYNC)
76 {
77 MOS_OS_CHK_STATUS_RETURN(SubmitPackageInAsync(std::move(package)));
78 }
79 else if (m_submitMode == HYBRID_MGR_SUBMIT_MODE::SYNC)
80 {
81 MOS_OS_CHK_STATUS_RETURN(SubmitPackageInSync(std::move(package)));
82 }
83 else
84 {
85 MOS_OS_CHK_STATUS_RETURN(MOS_STATUS_INVALID_PARAMETER);
86 }
87
88 return MOS_STATUS_SUCCESS;
89 }
90
91 MOS_STATUS HybridCmdMgr::SubmitPackageInAsync(std::unique_ptr<CmdPackage> cmdPackage)
92 {
93 {
94 std::lock_guard<std::mutex> lock(m_queueMutex);
95 m_queue.push(std::move(cmdPackage));
96 }
97 m_wakeCondition.notify_one();
98 return MOS_STATUS_SUCCESS;
99 }
100
101 MOS_STATUS HybridCmdMgr::SubmitPackageInSync(std::unique_ptr<CmdPackage> cmdPackage)
102 {
103 MOS_OS_CHK_NULL_RETURN(cmdPackage);
104
105 if (m_lastCmdPackage != nullptr &&
106 typeid(*cmdPackage) != typeid(*m_lastCmdPackage) &&
107 (cmdPackage->IsAsyncExecute() || m_lastCmdPackage->IsAsyncExecute()))
108 {
109 MOS_OS_CHK_STATUS_RETURN(m_lastCmdPackage->Wait());
110 }
111
112 MOS_OS_CHK_STATUS_RETURN(cmdPackage->Submit())
113
114 auto it = m_deferReleaseList.begin();
115 while (it != m_deferReleaseList.end())
116 {
117 if ((*it) == nullptr || (*it)->Releaseable())
118 {
119 it = m_deferReleaseList.erase(it);
120 }
121 else
122 {
123 ++it;
124 }
125 }
126 if (m_lastCmdPackage != nullptr && !m_lastCmdPackage->Releaseable())
127 {
128 //for those asyn cmd package, they cannot be immediately relased after submit
129 m_deferReleaseList.push_back(std::move(m_lastCmdPackage));
130 }
131
132 m_lastCmdPackage = std::move(cmdPackage);
133
134 return MOS_STATUS_SUCCESS;
135 }
136
137 bool HybridCmdMgr::Enabled()
138 {
139 return m_consumerThread.joinable();
140 }
141
142 void HybridCmdMgr::Consumer()
143 {
144 while (true)
145 {
146 std::unique_lock<std::mutex> lock(m_queueMutex);
147 m_wakeCondition.wait(lock, [&] { return !m_queue.empty() || m_stopFlag; });
148
149 //When asked to stop, need to first finish the queue
150 //Or the monitor fence of context will never update and destroy will stuck
151 if (m_queue.empty() && m_stopFlag)
152 {
153 if (m_lastCmdPackage && !m_lastCmdPackage->Releaseable())
154 {
155 MOS_OS_NORMALMESSAGE("Thread Exit with Last Cmd Package in Execution");
156 }
157 break;
158 }
159
160 while (!m_queue.empty())
161 {
162 std::unique_ptr<CmdPackage> lastCmdPackage = std::move(m_lastCmdPackage);
163 m_lastCmdPackage = std::move(m_queue.front());
164 m_queue.pop();
165 lock.unlock();
166
167 if (lastCmdPackage != nullptr &&
168 m_lastCmdPackage != nullptr &&
169 typeid(*lastCmdPackage) != typeid(*m_lastCmdPackage) &&
170 (lastCmdPackage->IsAsyncExecute() || m_lastCmdPackage->IsAsyncExecute()))
171 {
172 if (MOS_STATUS_SUCCESS != lastCmdPackage->Wait())
173 {
174 MOS_OS_ASSERTMESSAGE("Wait Last Cmd Package Fail");
175 }
176 }
177
178 if (m_lastCmdPackage != nullptr)
179 {
180 if (MOS_STATUS_SUCCESS != m_lastCmdPackage->Submit())
181 {
182 MOS_OS_ASSERTMESSAGE("Execute Current Package Fail");
183 }
184 }
185 else
186 {
187 MOS_OS_ASSERTMESSAGE("Current Cmd Package is nullptr, cannot submit");
188 }
189
190 //check the cmd packages that need defered released and release those releaseable ones
191 auto it = m_deferReleaseList.begin();
192 while (it != m_deferReleaseList.end())
193 {
194 if ((*it) == nullptr || (*it)->Releaseable())
195 {
196 it = m_deferReleaseList.erase(it);
197 }
198 else
199 {
200 ++it;
201 }
202 }
203 if (lastCmdPackage != nullptr && !lastCmdPackage->Releaseable())
204 {
205 //for those asyn cmd package, they cannot be immediately relased after submit
206 m_deferReleaseList.push_back(std::move(lastCmdPackage));
207 }
208
209 lock.lock();
210 }
211 }
212 }