Skip to content

Commit

Permalink
Add general mutex for ShmemInputStreamImpl
Browse files Browse the repository at this point in the history
To avoid failures like robotology/icub-tech-support#1937
  • Loading branch information
traversaro committed Oct 27, 2024
1 parent 2ee7d1a commit 08331f9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/carriers/shmem_carrier/ShmemInputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ bool ShmemInputStreamImpl::isOk() const

bool ShmemInputStreamImpl::open(int port, ACE_SOCK_Stream* pSock, int size)
{
std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);

m_pSock = pSock;

m_pAccessMutex = m_pWaitDataMutex = nullptr;
Expand Down Expand Up @@ -102,6 +104,8 @@ bool ShmemInputStreamImpl::open(int port, ACE_SOCK_Stream* pSock, int size)

bool ShmemInputStreamImpl::Resize()
{
std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);

++m_ResizeNum;

ACE_Shared_Memory* pNewMap;
Expand Down Expand Up @@ -155,6 +159,8 @@ bool ShmemInputStreamImpl::Resize()

int ShmemInputStreamImpl::read(char* data, int len)
{
std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);

m_pAccessMutex->acquire();

if (m_pHeader->close) {
Expand Down Expand Up @@ -193,6 +199,8 @@ int ShmemInputStreamImpl::read(char* data, int len)

yarp::conf::ssize_t ShmemInputStreamImpl::read(yarp::os::Bytes& b)
{
std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);

m_ReadSerializerMutex.lock();

if (!m_bOpen) {
Expand Down Expand Up @@ -230,6 +238,8 @@ yarp::conf::ssize_t ShmemInputStreamImpl::read(yarp::os::Bytes& b)

void ShmemInputStreamImpl::close()
{
std::lock_guard<std::recursive_mutex> l_guard(m_generalMutex);

if (!m_bOpen) {
return;
}
Expand Down
3 changes: 3 additions & 0 deletions src/carriers/shmem_carrier/ShmemInputStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class ShmemInputStreamImpl

std::mutex m_ReadSerializerMutex;

// Mutex for the whole class, to avoid concurrent close and read
std::recursive_mutex m_generalMutex;

ACE_Shared_Memory* m_pMap;
char* m_pData;
ShmemHeader_t* m_pHeader;
Expand Down

0 comments on commit 08331f9

Please sign in to comment.