Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protect WaitingTask*Holder::taskHasFailed from a data race #38765

Merged
merged 2 commits into from
Jul 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions FWCore/Concurrency/interface/WaitingTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,41 @@ namespace edm {
///Returns exception thrown by dependent task
/** If the value evalutes to true then the dependent task failed.
*/
std::exception_ptr const& exceptionPtr() const { return m_ptr; }
std::exception_ptr exceptionPtr() const {
if (m_ptrSet == static_cast<unsigned char>(State::kSet)) {
return m_ptr;
}
return std::exception_ptr{};
}

protected:
std::exception_ptr const& uncheckedExceptionPtr() const { return m_ptr; }

private:
enum class State : unsigned char { kUnset = 0, kSetting = 1, kSet = 2 };
///Called if waited for task failed
/**Allows transfer of the exception caused by the dependent task to be
* moved to another thread.
* This method should only be called by WaitingTaskList
*/
void dependentTaskFailed(std::exception_ptr iPtr) {
bool isSet = false;
if (iPtr and m_ptrSet.compare_exchange_strong(isSet, true)) {
unsigned char isSet = static_cast<unsigned char>(State::kUnset);
if (iPtr and m_ptrSet.compare_exchange_strong(isSet, static_cast<unsigned char>(State::kSetting))) {
m_ptr = iPtr;
//NOTE: the atomic counter in TaskBase will synchronize m_ptr
m_ptrSet = static_cast<unsigned char>(State::kSet);
}
}

std::exception_ptr m_ptr;
std::atomic<bool> m_ptrSet = false;
std::atomic<unsigned char> m_ptrSet = static_cast<unsigned char>(State::kUnset);
};

template <typename F>
class FunctorWaitingTask : public WaitingTask {
public:
explicit FunctorWaitingTask(F f) : func_(std::move(f)) {}

void execute() final { func_(exceptionPtr() ? &exceptionPtr() : nullptr); };
void execute() final { func_(uncheckedExceptionPtr() ? &uncheckedExceptionPtr() : nullptr); };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly for future reference, the uncheckedExceptionPtr() can be used here because this function will be called only after the reference count has reached 0, and therefore nothing else can be writing into the m_ptr at the same time, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes


private:
F func_;
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Concurrency/interface/WaitingTaskHolder.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace edm {
}

// ---------- const member functions ---------------------
bool taskHasFailed() const noexcept { return m_task->exceptionPtr() != nullptr; }
bool taskHasFailed() const noexcept { return static_cast<bool>(m_task->exceptionPtr()); }

bool hasTask() const noexcept { return m_task != nullptr; }
/** since oneapi::tbb::task_group is thread safe, we can return it non-const from here since
Expand Down
2 changes: 1 addition & 1 deletion FWCore/Concurrency/src/WaitingTaskWithArenaHolder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ namespace edm {
return holder;
}

bool WaitingTaskWithArenaHolder::taskHasFailed() const noexcept { return m_task->exceptionPtr() != nullptr; }
bool WaitingTaskWithArenaHolder::taskHasFailed() const noexcept { return static_cast<bool>(m_task->exceptionPtr()); }

bool WaitingTaskWithArenaHolder::hasTask() const noexcept { return m_task != nullptr; }

Expand Down