Fix RegionWorker thread exiting prematurely due to referencing
uninitialized memory.
Bug: 76021749
Test: manually
Merged-In: I5c3a9378dfd33f798f8a735698992a2cab7c543b
Change-Id: I5c3a9378dfd33f798f8a735698992a2cab7c543b
diff --git a/common/vsoc/lib/region_view.cpp b/common/vsoc/lib/region_view.cpp
index b86defd..4fcadc6 100644
--- a/common/vsoc/lib/region_view.cpp
+++ b/common/vsoc/lib/region_view.cpp
@@ -15,7 +15,12 @@
std::shared_ptr<RegionControl> control)
: control_(control),
region_(region),
- thread_(&vsoc::RegionWorker::Work, this) {}
+ stopping_(false) {}
+
+void vsoc::RegionWorker::start() {
+ CHECK(thread_ == nullptr);
+ thread_.reset(new std::thread(&vsoc::RegionWorker::Work, this));
+}
void vsoc::RegionWorker::Work() {
while (!stopping_) {
@@ -25,14 +30,17 @@
}
region_->ProcessSignalsFromPeer([this](uint32_t offset) {
control_->SignalSelf(offset);
- });
+ });
}
}
vsoc::RegionWorker::~RegionWorker() {
stopping_ = true;
- region_->InterruptSelf();
- thread_.join();
+
+ if (thread_ != nullptr) {
+ region_->InterruptSelf();
+ thread_->join();
+ }
}
vsoc::RegionView::~RegionView() {
@@ -179,8 +187,11 @@
}
std::unique_ptr<vsoc::RegionWorker> vsoc::RegionView::StartWorker() {
- return std::unique_ptr<vsoc::RegionWorker>(new vsoc::RegionWorker(
- this, control_));
+ std::unique_ptr<vsoc::RegionWorker> worker(
+ new vsoc::RegionWorker(this /* region */, control_));
+
+ worker->start();
+ return worker;
}
int vsoc::RegionView::WaitForSignal(std::atomic<uint32_t>* uaddr,
diff --git a/common/vsoc/lib/region_view.h b/common/vsoc/lib/region_view.h
index bfe29db..af089f3 100644
--- a/common/vsoc/lib/region_view.h
+++ b/common/vsoc/lib/region_view.h
@@ -50,13 +50,16 @@
public:
RegionWorker(RegionView* region, std::shared_ptr<RegionControl> control);
~RegionWorker();
+
+ void start();
+
void Work();
protected:
std::shared_ptr<RegionControl> control_;
RegionView* region_;
- std::thread thread_;
- volatile bool stopping_{};
+ std::unique_ptr<std::thread> thread_;
+ volatile bool stopping_;
};
/**