Skip to content

Commit

Permalink
Cyber: cyber_recorder file writer threading bug fix
Browse files Browse the repository at this point in the history
Cyber: cyber_recorder file writer threading bug fix
  • Loading branch information
lfcarol authored and storypku committed Oct 27, 2020
1 parent 87127ff commit 3bf0934
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion cyber/record/file/record_file_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class RecordFileBase {
std::string path_;
proto::Header header_;
proto::Index index_;
int fd_;
int fd_ = -1;
};

} // namespace record
Expand Down
18 changes: 15 additions & 3 deletions cyber/record/file/record_file_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ using apollo::cyber::proto::Header;
using apollo::cyber::proto::SectionType;
using apollo::cyber::proto::SingleIndex;

RecordFileWriter::RecordFileWriter() {}
RecordFileWriter::RecordFileWriter() : is_writing_(false) {}

RecordFileWriter::~RecordFileWriter() { Close(); }

Expand Down Expand Up @@ -66,7 +66,13 @@ bool RecordFileWriter::Open(const std::string& path) {
void RecordFileWriter::Close() {
if (is_writing_) {
// wait for the flush operation that may exist now
while (!chunk_flush_->empty()) {
while (1) {
{
std::unique_lock<std::mutex> flush_lock(flush_mutex_);
if (chunk_flush_->empty()) {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

Expand All @@ -78,7 +84,13 @@ void RecordFileWriter::Close() {
}

// wait for the last flush operation
while (!chunk_flush_->empty()) {
while (1) {
{
std::unique_lock<std::mutex> flush_lock(flush_mutex_);
if (chunk_flush_->empty()) {
break;
}
}
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}

Expand Down
2 changes: 1 addition & 1 deletion cyber/record/file/record_file_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ class RecordFileWriter : public RecordFileBase {
bool WriteSection(const T& message);
bool WriteIndex();
void Flush();
bool is_writing_ = false;
std::atomic_bool is_writing_;
std::unique_ptr<Chunk> chunk_active_ = nullptr;
std::unique_ptr<Chunk> chunk_flush_ = nullptr;
std::shared_ptr<std::thread> flush_thread_ = nullptr;
Expand Down

0 comments on commit 3bf0934

Please sign in to comment.