From e6fa1c70791367b5cb900d520a5bbbbac5faa8f2 Mon Sep 17 00:00:00 2001 From: Yaossg Date: Sun, 19 Jan 2025 20:52:48 +0800 Subject: [PATCH] remove worker mutex --- hisat_3n_table.cpp | 4 ---- position_3n_table.h | 42 +++++++++++++++--------------------------- 2 files changed, 15 insertions(+), 31 deletions(-) diff --git a/hisat_3n_table.cpp b/hisat_3n_table.cpp index a537228..40cc549 100644 --- a/hisat_3n_table.cpp +++ b/hisat_3n_table.cpp @@ -289,7 +289,6 @@ int hisat_3n_table() // then load a new reference chromosome. auto old = positions->chromosome; if (samChromosome != (old ? *old : "")) { - positions->appendingFinished(); positions->moveAllToOutput(); positions->loadNewChromosome(std::move(samChromosome)); reloadPos = loadingBlockSize; @@ -297,7 +296,6 @@ int hisat_3n_table() } // if the samPos is larger than reloadPos, load 1 loadingBlockSize bp in from reference. while (samPos > reloadPos) { - positions->appendingFinished(); positions->moveBlockToOutput(); positions->loadMore(); reloadPos += loadingBlockSize; @@ -319,8 +317,6 @@ int hisat_3n_table() workers[i].join(); } - // make sure all workers finished their appending work. - positions->appendingFinished(); // move all position to outputPool positions->moveAllToOutput(); // wait until outputPool is empty diff --git a/position_3n_table.h b/position_3n_table.h index 2057691..6e55d4c 100644 --- a/position_3n_table.h +++ b/position_3n_table.h @@ -199,7 +199,6 @@ public: mutex mutex_; long long int refCoveredPosition; // this is the last position in reference chromosome we loaded in refPositions. ifstream refFile; - vector workerLock; // one lock for one worker thread. int nThreads = 1; ChromosomeFilePositions chromosomePos; // store the chromosome name and it's streamPos. To quickly find new chromosome in file. bool addedChrName = false; @@ -209,23 +208,11 @@ public: nThreads = inputNThreads; addedChrName = inputAddedChrName; removedChrName = inputRemovedChrName; - for (int i = 0; i < nThreads; i++) { - workerLock.push_back(new mutex); - } refFile.open(inputRefFileName, ios_base::in); LoadChromosomeNamesPos(); } - ~Positions() { - for (int i = 0; i < workerLock.size(); i++) { - delete workerLock[i]; - } - Position* pos; - // freePositionPool.close(); - // while(freePositionPool.recv(pos)) { - // delete pos; - // } - } + ~Positions() = default; /** * given the target Position output the corresponding position index in refPositions. @@ -309,16 +296,6 @@ public: location += line.size(); } - /** - * if we can go through all the workerLock, that means no worker is appending new position. - */ - void appendingFinished() { - for (int i = 0; i < nThreads; i++) { - workerLock[i]->lock(); - workerLock[i]->unlock(); - } - } - /** * the output function for output thread. */ @@ -346,10 +323,22 @@ public: tableFile.close(); } + + /** + * if we can go through all the workerLock, that means no worker is appending new position. + */ + void appendingFinished() { + while (!linePool.empty()) { + this_thread::sleep_for (std::chrono::microseconds(1)); + } + } + + /** * move the position which position smaller than refCoveredPosition - loadingBlockSize, output it. */ void moveBlockToOutput() { + appendingFinished(); if (refPositions.empty()) { return; } @@ -372,6 +361,7 @@ public: * move all the refPosition into output pool. */ void moveAllToOutput() { + appendingFinished(); if (refPositions.empty()) { return; } @@ -496,9 +486,7 @@ public: Alignment newAlignment; - while (true) { - std::unique_lock lk{*workerLock[threadID]}; - if (!linePool.recv(line)) break; + while (linePool.recv(line)) { while (refPositions.empty()) { this_thread::sleep_for (std::chrono::microseconds(1)); }