remove worker mutex

This commit is contained in:
Yaossg 2025-01-19 20:52:48 +08:00
parent ebac0d06a6
commit e6fa1c7079
2 changed files with 15 additions and 31 deletions

View File

@ -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

View File

@ -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<mutex*> 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<std::mutex> lk{*workerLock[threadID]};
if (!linePool.recv(line)) break;
while (linePool.recv(line)) {
while (refPositions.empty()) {
this_thread::sleep_for (std::chrono::microseconds(1));
}