Skip to content

Commit

Permalink
Merge pull request #41669 from smorovic/13_1_X-daqsource-race
Browse files Browse the repository at this point in the history
[DAQ] fix unsafe vector handling in input sources (13_1_X)
  • Loading branch information
cmsbuild committed May 15, 2023
2 parents 774ffd8 + a2d9782 commit e6aede3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 31 deletions.
2 changes: 1 addition & 1 deletion EventFilter/Utilities/interface/DAQSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class DAQSource : public edm::RawInputSource {
tbb::concurrent_queue<std::unique_ptr<RawInputFile>> fileQueue_;

std::mutex mReader_;
std::vector<std::condition_variable*> cvReader_;
std::vector<std::unique_ptr<std::condition_variable>> cvReader_;
std::vector<unsigned int> tid_active_;

std::atomic<bool> quit_threads_;
Expand Down
2 changes: 1 addition & 1 deletion EventFilter/Utilities/interface/FedRawDataInputSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ class FedRawDataInputSource : public edm::RawInputSource {
tbb::concurrent_queue<std::unique_ptr<InputFile>> fileQueue_;

std::mutex mReader_;
std::vector<std::condition_variable*> cvReader_;
std::vector<std::unique_ptr<std::condition_variable>> cvReader_;
std::vector<unsigned int> tid_active_;

std::atomic<bool> quit_threads_;
Expand Down
21 changes: 8 additions & 13 deletions EventFilter/Utilities/src/DAQSource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -160,14 +160,18 @@ DAQSource::DAQSource(edm::ParameterSet const& pset, edm::InputSourceDescription

quit_threads_ = false;

//prepare data shared by threads
for (unsigned int i = 0; i < numConcurrentReads_; i++) {
std::unique_lock<std::mutex> lk(startupLock_);
//issue a memory fence here and in threads (constructor was segfaulting without this)
thread_quit_signal.push_back(false);
workerJob_.push_back(ReaderInfo(nullptr, nullptr));
cvReader_.push_back(new std::condition_variable);
cvReader_.push_back(std::make_unique<std::condition_variable>());
tid_active_.push_back(0);
threadInit_.store(false, std::memory_order_release);
}

//start threads
for (unsigned int i = 0; i < numConcurrentReads_; i++) {
//wait for each thread to complete initialization
std::unique_lock<std::mutex> lk(startupLock_);
workerThreads_.push_back(new std::thread(&DAQSource::readWorker, this, i));
startupCv_.wait(lk);
}
Expand Down Expand Up @@ -211,15 +215,6 @@ DAQSource::~DAQSource() {
delete workerThreads_[i];
}
}
for (unsigned int i = 0; i < numConcurrentReads_; i++)
delete cvReader_[i];
/*
for (unsigned int i=0;i<numConcurrentReads_+1;i++) {
InputChunk *ch;
while (!freeChunks_.try_pop(ch)) {}
delete ch;
}
*/
}

void DAQSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
Expand Down
24 changes: 8 additions & 16 deletions EventFilter/Utilities/src/FedRawDataInputSource.cc
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,18 @@ FedRawDataInputSource::FedRawDataInputSource(edm::ParameterSet const& pset, edm:

quit_threads_ = false;

//prepare data shared by threads
for (unsigned int i = 0; i < numConcurrentReads_; i++) {
std::unique_lock<std::mutex> lk(startupLock_);
//issue a memory fence here and in threads (constructor was segfaulting without this)
thread_quit_signal.push_back(false);
workerJob_.push_back(ReaderInfo(nullptr, nullptr));
cvReader_.push_back(new std::condition_variable);
cvReader_.push_back(std::make_unique<std::condition_variable>());
tid_active_.push_back(0);
threadInit_.store(false, std::memory_order_release);
}

//start threads
for (unsigned int i = 0; i < numConcurrentReads_; i++) {
//wait for each thread to complete initialization
std::unique_lock<std::mutex> lk(startupLock_);
workerThreads_.push_back(new std::thread(&FedRawDataInputSource::readWorker, this, i));
startupCv_.wait(lk);
}
Expand Down Expand Up @@ -205,15 +209,6 @@ FedRawDataInputSource::~FedRawDataInputSource() {
delete workerThreads_[i];
}
}
for (unsigned int i = 0; i < numConcurrentReads_; i++)
delete cvReader_[i];
/*
for (unsigned int i=0;i<numConcurrentReads_+1;i++) {
InputChunk *ch;
while (!freeChunks_.try_pop(ch)) {}
delete ch;
}
*/
}

void FedRawDataInputSource::fillDescriptions(edm::ConfigurationDescriptions& descriptions) {
Expand Down Expand Up @@ -244,7 +239,6 @@ void FedRawDataInputSource::fillDescriptions(edm::ConfigurationDescriptions& des
edm::RawInputSource::Next FedRawDataInputSource::checkNext() {
if (!startedSupervisorThread_) {
//this thread opens new files and dispatches reading to worker readers
//threadInit_.store(false,std::memory_order_release);
std::unique_lock<std::mutex> lk(startupLock_);
readSupervisorThread_ = std::make_unique<std::thread>(&FedRawDataInputSource::readSupervisor, this);
startedSupervisorThread_ = true;
Expand Down Expand Up @@ -765,7 +759,6 @@ void FedRawDataInputSource::rewind_() {}
void FedRawDataInputSource::readSupervisor() {
bool stop = false;
unsigned int currentLumiSection = 0;
//threadInit_.exchange(true,std::memory_order_acquire);

{
std::unique_lock<std::mutex> lk(startupLock_);
Expand Down Expand Up @@ -1268,7 +1261,6 @@ void FedRawDataInputSource::readSupervisor() {

void FedRawDataInputSource::readWorker(unsigned int tid) {
bool init = true;
threadInit_.exchange(true, std::memory_order_acquire);

while (true) {
tid_active_[tid] = false;
Expand Down

0 comments on commit e6aede3

Please sign in to comment.