mirror of
https://github.com/Evolution-X/hardware_interfaces
synced 2026-02-01 11:36:00 +00:00
Merge "Gracefully stop the GeneratorHub worker thread in destructor" into rvc-qpr-dev
This commit is contained in:
@@ -31,6 +31,14 @@ namespace impl {
|
|||||||
GeneratorHub::GeneratorHub(const OnHalEvent& onHalEvent)
|
GeneratorHub::GeneratorHub(const OnHalEvent& onHalEvent)
|
||||||
: mOnHalEvent(onHalEvent), mThread(&GeneratorHub::run, this) {}
|
: mOnHalEvent(onHalEvent), mThread(&GeneratorHub::run, this) {}
|
||||||
|
|
||||||
|
GeneratorHub::~GeneratorHub() {
|
||||||
|
mShuttingDownFlag.store(true);
|
||||||
|
mCond.notify_all();
|
||||||
|
if (mThread.joinable()) {
|
||||||
|
mThread.join();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void GeneratorHub::registerGenerator(int32_t cookie, FakeValueGeneratorPtr generator) {
|
void GeneratorHub::registerGenerator(int32_t cookie, FakeValueGeneratorPtr generator) {
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> g(mLock);
|
std::lock_guard<std::mutex> g(mLock);
|
||||||
@@ -58,15 +66,18 @@ void GeneratorHub::unregisterGenerator(int32_t cookie) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void GeneratorHub::run() {
|
void GeneratorHub::run() {
|
||||||
while (true) {
|
while (!mShuttingDownFlag.load()) {
|
||||||
std::unique_lock<std::mutex> g(mLock);
|
std::unique_lock<std::mutex> g(mLock);
|
||||||
// Pop events whose generator does not exist (may be already unregistered)
|
// Pop events whose generator does not exist (may be already unregistered)
|
||||||
while (!mEventQueue.empty()
|
while (!mEventQueue.empty()
|
||||||
&& mGenerators.find(mEventQueue.top().cookie) == mGenerators.end()) {
|
&& mGenerators.find(mEventQueue.top().cookie) == mGenerators.end()) {
|
||||||
mEventQueue.pop();
|
mEventQueue.pop();
|
||||||
}
|
}
|
||||||
// Wait until event queue is not empty
|
// Wait until event queue is not empty or shutting down flag is set
|
||||||
mCond.wait(g, [this] { return !mEventQueue.empty(); });
|
mCond.wait(g, [this] { return !mEventQueue.empty() || mShuttingDownFlag.load(); });
|
||||||
|
if (mShuttingDownFlag.load()) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const VhalEvent& curEvent = mEventQueue.top();
|
const VhalEvent& curEvent = mEventQueue.top();
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ private:
|
|||||||
|
|
||||||
public:
|
public:
|
||||||
GeneratorHub(const OnHalEvent& onHalEvent);
|
GeneratorHub(const OnHalEvent& onHalEvent);
|
||||||
~GeneratorHub() = default;
|
~GeneratorHub();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a new generator. The generator will be discarded if it could not produce next event.
|
* Register a new generator. The generator will be discarded if it could not produce next event.
|
||||||
@@ -84,6 +84,7 @@ private:
|
|||||||
mutable std::mutex mLock;
|
mutable std::mutex mLock;
|
||||||
std::condition_variable mCond;
|
std::condition_variable mCond;
|
||||||
std::thread mThread;
|
std::thread mThread;
|
||||||
|
std::atomic<bool> mShuttingDownFlag{false};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace impl
|
} // namespace impl
|
||||||
|
|||||||
Reference in New Issue
Block a user