Skip to content

Commit

Permalink
[Core] Race Conditions: Make CRegistrationReceiver m_callback_custom_…
Browse files Browse the repository at this point in the history
…apply_sample threadsafe.

Co-authored-by: Rex Schilasky <[email protected]>
  • Loading branch information
KerstinKeller and rex-schilasky committed Jan 18, 2024
1 parent 710d8d3 commit eaf1d51
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
16 changes: 15 additions & 1 deletion ecal/core/src/registration/ecal_registration_receiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ namespace eCAL
// CMemfileRegistrationReceiver
//////////////////////////////////////////////////////////////////

CMemfileRegistrationReceiver::CMemfileRegistrationReceiver()
= default;

CMemfileRegistrationReceiver::~CMemfileRegistrationReceiver()
{
Destroy();
}

void CMemfileRegistrationReceiver::Create(eCAL::CMemoryFileBroadcastReader* memfile_broadcast_reader_)
{
if (m_created) return;
Expand Down Expand Up @@ -190,7 +198,11 @@ namespace eCAL
eCAL::pb::Sample modified_ttype_sample;
ModifyIncomingSampleForBackwardsCompatibility(ecal_sample_, modified_ttype_sample);

m_callback_custom_apply_sample(modified_ttype_sample);
// forward all registration samples to outside "customer" (e.g. Monitoring)
{
const std::lock_guard<std::mutex> lock(m_callback_custom_apply_sample_mtx);
m_callback_custom_apply_sample(modified_ttype_sample);
}

std::string reg_sample;
if ( m_callback_pub
Expand Down Expand Up @@ -405,11 +417,13 @@ namespace eCAL

void CRegistrationReceiver::SetCustomApplySampleCallback(const ApplySampleCallbackT& callback_)
{
const std::lock_guard<std::mutex> lock(m_callback_custom_apply_sample_mtx);
m_callback_custom_apply_sample = callback_;
}

void CRegistrationReceiver::RemCustomApplySampleCallback()
{
const std::lock_guard<std::mutex> lock(m_callback_custom_apply_sample_mtx);
m_callback_custom_apply_sample = [](const auto&){};
}
}
16 changes: 15 additions & 1 deletion ecal/core/src/registration/ecal_registration_receiver.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

#include <atomic>
#include <memory>
#include <mutex>
#include <string>
#include <thread>

Expand All @@ -54,13 +55,25 @@ namespace eCAL
class CMemfileRegistrationReceiver
{
public:
CMemfileRegistrationReceiver();
~CMemfileRegistrationReceiver();

// default copy constructor
CMemfileRegistrationReceiver(const CMemfileRegistrationReceiver& other) = delete;
// default copy assignment operator
CMemfileRegistrationReceiver& operator=(const CMemfileRegistrationReceiver& other) = delete;
// default move constructor
CMemfileRegistrationReceiver(CMemfileRegistrationReceiver&& other) noexcept = delete;
// default move assignment operator
CMemfileRegistrationReceiver& operator=(CMemfileRegistrationReceiver&& other) noexcept = delete;

void Create(CMemoryFileBroadcastReader* memfile_broadcast_reader_);
void Destroy();

private:
void Receive();

CMemoryFileBroadcastReader* m_memfile_broadcast_reader = nullptr;
CMemoryFileBroadcastReader* m_memfile_broadcast_reader = nullptr;
std::shared_ptr<CCallbackThread> m_memfile_broadcast_reader_thread;

bool m_created = false;
Expand Down Expand Up @@ -115,6 +128,7 @@ namespace eCAL
bool m_use_network_monitoring;
bool m_use_shm_monitoring;

std::mutex m_callback_custom_apply_sample_mtx;
ApplySampleCallbackT m_callback_custom_apply_sample;

std::string m_host_group_name;
Expand Down

1 comment on commit eaf1d51

@FlorianReimold
Copy link
Member

Choose a reason for hiding this comment

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

Part of PR #1312

Please sign in to comment.