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

[core] Fix a data race by adding a shared mutex #2961

Closed
wants to merge 5 commits into from

Conversation

yomnes0
Copy link
Collaborator

@yomnes0 yomnes0 commented Jun 17, 2024

No description provided.

@yomnes0 yomnes0 added the [core] Area: Changes in SRT library core label Jun 17, 2024
srtcore/queue.cpp Fixed Show fixed Hide fixed
@maxsharabayko maxsharabayko added this to the v1.5.4 milestone Jun 17, 2024
@maxsharabayko maxsharabayko added the Type: Bug Indicates an unexpected problem or unintended behavior label Jun 17, 2024
@@ -67,6 +67,36 @@ namespace srt
{
class CChannel;
class CUDT;
class CUDTWrapper;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The class definition follows below. This declaration is excessive.

Comment on lines +72 to +99
class CUDTWrapper {
public:
CUDT *udt;
sync::SharedMutex mut;

public:
CUDTWrapper()
:udt(NULL)
,mut()
{
}
void lockRead()
{
return mut.lockRead();
}
void lockWrite()
{
return mut.lockWrite();
}
void unlockRead()
{
return mut.unlockRead();

}
void unlockWrite(){
return mut.unlockWrite();
}
};
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't quite like the name. It does not express what the class does, except for wrapping CUDT.
In fact, it can be a template class holding a pointer to a resource and a shared mutex, e.g.

template <class T>
class CSharedObject
{
private:
    T* m_pObj;
    sync::SharedMutex m_mtx;
public:
    // ...
};

class CUDTWrapper;

class CUDTWrapper {
public:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
public:
private:

Mutex m_pMutex;

int m_pCountRead;
bool m_pWriterLocked;
Copy link
Collaborator

Choose a reason for hiding this comment

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

'p' means "pointer". This variable is of a type "Boolean", so should be m_bWriterLocked.


Mutex m_pMutex;

int m_pCountRead;
Copy link
Collaborator

Choose a reason for hiding this comment

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

"p" mean pointer, while the variable is not a pointer.
Please review the whole class.

Comment on lines +1023 to +1099
/* REFERENCE IMPLEMENTATION
class shared_mutex
{
Mutex mut_;
Condition gate1_;
Condition gate2_;
unsigned state_;

static const unsigned write_entered_ = 1U << (sizeof(unsigned)*CHAR_BIT - 1);
static const unsigned n_readers_ = ~write_entered_;

public:

shared_mutex() : state_(0) {}


// Exclusive ownership

void
lock()
{
UniqueLock lk(mut_);
std::cout << "LOCK WRITE " << std::endl;
while (state_ & write_entered_)
gate1_.wait(lk);
state_ |= write_entered_;
while (state_ & n_readers_)
gate2_.wait(lk);
std::cout << "LOCK WRITE DONE" << std::endl;

}

void
unlock()
{
{
ScopedLock _(mut_);
state_ = 0;
}
std::cout << "UNLOCK WRITE " << std::endl;
gate1_.notify_all();
std::cout << "UNLOCK WRITE DONE" << std::endl;

}

// Shared ownership

void
lock_shared()
{
UniqueLock lk(mut_);
while ((state_ & write_entered_) || (state_ & n_readers_) == n_readers_)
gate1_.wait(lk);
unsigned num_readers = (state_ & n_readers_) + 1;
state_ &= ~n_readers_;
state_ |= num_readers;
}

void
unlock_shared()
{
ScopedLock _(mut_);
unsigned num_readers = (state_ & n_readers_) - 1;
state_ &= ~n_readers_;
state_ |= num_readers;
if (state_ & write_entered_)
{
if (num_readers == 0)
gate2_.notify_one();
}
else
{
if (num_readers == n_readers_ - 1)
gate1_.notify_one();
}
}
};*/

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

if (u == m_pListener)
m_pListener = NULL;
//ScopedLock lslock(m_LSLock);

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.
@maxsharabayko
Copy link
Collaborator

I suppose this PR is replaced by #2981 and #2984. Closing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[core] Area: Changes in SRT library core Type: Bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants