Skip to content

Commit

Permalink
helium: use a lock to make DeferredCommitBuffer thread safe
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffamstutz committed Feb 4, 2025
1 parent 208c6f7 commit f5ed37d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/helium/utility/DeferredCommitBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ DeferredCommitBuffer::~DeferredCommitBuffer()

void DeferredCommitBuffer::addObject(BaseObject *obj)
{
std::lock_guard<std::recursive_mutex> guard(m_mutex);
obj->refInc(RefType::INTERNAL);
if (commitPriority(obj->type()) != commitPriority(ANARI_OBJECT))
m_needToSortCommits = true;
Expand All @@ -31,6 +32,8 @@ bool DeferredCommitBuffer::flush()
if (m_commitBuffer.empty())
return false;

std::lock_guard<std::recursive_mutex> guard(m_mutex);

if (m_needToSortCommits) {
std::sort(m_commitBuffer.begin(),
m_commitBuffer.end(),
Expand Down Expand Up @@ -66,6 +69,8 @@ TimeStamp DeferredCommitBuffer::lastFlush() const

void DeferredCommitBuffer::clear()
{
std::lock_guard<std::recursive_mutex> guard(m_mutex);

for (auto &obj : m_commitBuffer)
obj->refDec(RefType::INTERNAL);
m_commitBuffer.clear();
Expand Down
2 changes: 2 additions & 0 deletions src/helium/utility/DeferredCommitBuffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "TimeStamp.h"
// std
#include <mutex>
#include <vector>

namespace helium {
Expand Down Expand Up @@ -36,6 +37,7 @@ struct DeferredCommitBuffer
std::vector<BaseObject *> m_commitBuffer;
bool m_needToSortCommits{false};
TimeStamp m_lastFlush{0};
std::recursive_mutex m_mutex;
};

} // namespace helium

0 comments on commit f5ed37d

Please sign in to comment.