diff --git a/p4-fusion/commands/change_list.cc b/p4-fusion/commands/change_list.cc index 06646507..3bdf8c16 100644 --- a/p4-fusion/commands/change_list.cc +++ b/p4-fusion/commands/change_list.cc @@ -20,6 +20,10 @@ ChangeList::ChangeList(const std::string& clNumber, const std::string& clDescrip , description(clDescription) , timestamp(clTimestamp) , changedFileGroups(ChangedFileGroups::Empty()) + , stateCV(new std::condition_variable()) + , stateMutex(new std::mutex()) + , filesDownloaded(-1) + , state(Initialized) { } @@ -48,11 +52,9 @@ void ChangeList::PrepareDownload(const BranchSet& branchSet) cl.changedFileGroups = branchSet.ParseAffectedFiles(describe.GetFileData()); } - { - std::unique_lock lock((*(cl.canDownloadMutex))); - *cl.canDownload = true; - } - cl.canDownloadCV->notify_all(); + std::unique_lock lock(*cl.stateMutex); + cl.state = Described; + cl.stateCV->notify_all(); }); } @@ -64,12 +66,12 @@ void ChangeList::StartDownload(const int& printBatch) { // Wait for describe to finish, if it is still running { - std::unique_lock lock(*(cl.canDownloadMutex)); - cl.canDownloadCV->wait(lock, [&cl]() - { return *(cl.canDownload) == true; }); + std::unique_lock lock(*cl.stateMutex); + cl.stateCV->wait(lock, [&cl]() + { return cl.state == Described; }); } - *cl.filesDownloaded = 0; + cl.filesDownloaded = 0; std::shared_ptr> printBatchFiles = std::make_shared>(); std::shared_ptr> printBatchFileData = std::make_shared>(); @@ -122,20 +124,23 @@ void ChangeList::Flush(std::shared_ptr> printBatchFiles { printBatchFileData->at(i)->MoveContentsOnceFrom(printData.GetPrintData().at(i).contents); } - - (*filesDownloaded) += printBatchFiles->size(); } - // Ensure the notify_all is called. - commitCV->notify_all(); + std::lock_guard lock(*stateMutex); + filesDownloaded += printBatchFiles->size(); + if (filesDownloaded == changedFileGroups->totalFileCount) + { + state = Downloaded; + stateCV->notify_all(); + } }); } void ChangeList::WaitForDownload() { - std::unique_lock lock(*commitMutex); - commitCV->wait(lock, [this]() - { return *(filesDownloaded) == (int)changedFileGroups->totalFileCount; }); + std::unique_lock lock(*stateMutex); + stateCV->wait(lock, [this]() + { return state == Downloaded; }); } void ChangeList::Clear() @@ -145,10 +150,8 @@ void ChangeList::Clear() description.clear(); changedFileGroups->Clear(); - filesDownloaded.reset(); - canDownload.reset(); - canDownloadMutex.reset(); - canDownloadCV.reset(); - commitMutex.reset(); - commitCV.reset(); + stateCV.release(); + stateMutex.release(); + filesDownloaded = -1; + state = Freed; } diff --git a/p4-fusion/commands/change_list.h b/p4-fusion/commands/change_list.h index d1bda7a7..d143036c 100644 --- a/p4-fusion/commands/change_list.h +++ b/p4-fusion/commands/change_list.h @@ -16,22 +16,28 @@ struct ChangeList { + enum State + { + Initialized, + Described, + Downloaded, + Freed + }; + std::string number; std::string user; std::string description; int64_t timestamp = 0; std::unique_ptr changedFileGroups = ChangedFileGroups::Empty(); - std::shared_ptr> filesDownloaded = std::make_shared>(-1); - std::shared_ptr> canDownload = std::make_shared>(false); - std::shared_ptr canDownloadMutex = std::make_shared(); - std::shared_ptr canDownloadCV = std::make_shared(); - std::shared_ptr commitMutex = std::make_shared(); - std::shared_ptr commitCV = std::make_shared(); + std::unique_ptr stateCV; + std::unique_ptr stateMutex; + int filesDownloaded; + State state; ChangeList(const std::string& number, const std::string& description, const std::string& user, const int64_t& timestamp); - ChangeList(const ChangeList& other) = default; + ChangeList(const ChangeList& other) = delete; ChangeList& operator=(const ChangeList&) = delete; ChangeList(ChangeList&&) = default; ChangeList& operator=(ChangeList&&) = default;