From 083bad9baf2ec6000a504156c8c9a6f7e5e1e573 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 16 Jun 2020 16:54:35 +0300
Subject: [PATCH] Add lock on createChange

---
 components/structures/src/new_frame.cpp | 42 ++++++++++++++++---------
 1 file changed, 28 insertions(+), 14 deletions(-)

diff --git a/components/structures/src/new_frame.cpp b/components/structures/src/new_frame.cpp
index f969023fc..7c8f10814 100644
--- a/components/structures/src/new_frame.cpp
+++ b/components/structures/src/new_frame.cpp
@@ -100,14 +100,21 @@ const Frame::ChannelData &Frame::_getData(ftl::codecs::Channel c) const {
 std::any &Frame::createAnyChange(ftl::codecs::Channel c, ftl::data::ChangeType t) {
 	if (status_ != FrameStatus::CREATED) throw FTL_Error("Cannot apply change after store " << static_cast<int>(status_));
 
-	//UNIQUE_LOCK(mutex(), lk);
-
-	auto &d = data_[c];
-	if (d.status != ftl::data::ChannelStatus::FLUSHED) {
-		d.status = ftl::data::ChannelStatus::DISPATCHED;
-		d.encoded.clear();
+	ftl::data::Frame::ChannelData *d;
+	
+	if (parent_) {
+		UNIQUE_LOCK(mutex(), lk);
+		d = &(data_[c]);
 		touch(c, t);
-		return d.data;
+	} else {
+		d = &(data_[c]);
+		touch(c, t);
+	}
+
+	if (d->status != ftl::data::ChannelStatus::FLUSHED) {
+		d->status = ftl::data::ChannelStatus::DISPATCHED;
+		d->encoded.clear();
+		return d->data;
 	} else {
 		throw FTL_Error("Channel is flushed and read-only: " << static_cast<unsigned int>(c));
 	}
@@ -116,14 +123,21 @@ std::any &Frame::createAnyChange(ftl::codecs::Channel c, ftl::data::ChangeType t
 std::any &Frame::createAnyChange(ftl::codecs::Channel c, ftl::data::ChangeType t, ftl::codecs::Packet &data) {
 	if (status_ != FrameStatus::CREATED) throw FTL_Error("Cannot apply change after store " << static_cast<int>(status_));
 
-	//UNIQUE_LOCK(mutex(), lk);
-
-	auto &d = data_[c];
-	if (d.status != ftl::data::ChannelStatus::FLUSHED) {
-		d.status = ftl::data::ChannelStatus::DISPATCHED;
-		d.encoded.push_back(data);
+	ftl::data::Frame::ChannelData *d;
+	
+	if (parent_) {
+		UNIQUE_LOCK(mutex(), lk);
+		d = &(data_[c]);
 		touch(c, t);
-		return d.data;
+	} else {
+		d = &(data_[c]);
+		touch(c, t);
+	}
+
+	if (d->status != ftl::data::ChannelStatus::FLUSHED) {
+		d->status = ftl::data::ChannelStatus::DISPATCHED;
+		d->encoded.push_back(data);
+		return d->data;
 	} else {
 		throw FTL_Error("Channel is flushed and read-only: " << static_cast<unsigned int>(c));
 	}
-- 
GitLab