-
Notifications
You must be signed in to change notification settings - Fork 863
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
Changes from 4 commits
3f83e2a
82a052e
a4686d3
c91ebee
44d298a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -67,6 +67,36 @@ namespace srt | |||||
{ | ||||||
class CChannel; | ||||||
class CUDT; | ||||||
class CUDTWrapper; | ||||||
|
||||||
class CUDTWrapper { | ||||||
public: | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
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(); | ||||||
} | ||||||
}; | ||||||
Comment on lines
+72
to
+99
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 template <class T>
class CSharedObject
{
private:
T* m_pObj;
sync::SharedMutex m_mtx;
public:
// ...
}; |
||||||
|
||||||
struct CUnit | ||||||
{ | ||||||
|
@@ -555,7 +585,7 @@ class CRcvQueue | |||||
|
||||||
private: | ||||||
sync::Mutex m_LSLock; | ||||||
CUDT* m_pListener; // pointer to the (unique, if any) listening UDT entity | ||||||
CUDTWrapper m_pListener; // pointer to the (unique, if any) listening UDT entity | ||||||
CRendezvousQueue* m_pRendezvousQueue; // The list of sockets in rendezvous mode | ||||||
|
||||||
std::vector<CUDT*> m_vNewEntry; // newly added entries, to be inserted | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -943,9 +943,74 @@ CUDTException& GetThreadLocalError(); | |
/// @param[in] maxVal maximum allowed value of the resulting random number. | ||
int genRandomInt(int minVal, int maxVal); | ||
|
||
class SharedMutex | ||
{ | ||
private: | ||
Condition m_pLockWriteCond; | ||
Condition m_pLockReadCond; | ||
|
||
Mutex m_pMutex; | ||
|
||
int m_pCountRead; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "p" mean pointer, while the variable is not a pointer. |
||
bool m_pWriterLocked; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
||
|
||
public: | ||
SharedMutex() | ||
:m_pLockWriteCond() | ||
,m_pLockReadCond() | ||
,m_pMutex() | ||
,m_pCountRead(0) | ||
,m_pWriterLocked(false) | ||
{ | ||
m_pCountRead = 0; | ||
m_pWriterLocked = false; | ||
|
||
} | ||
|
||
void lockWrite() | ||
{ | ||
UniqueLock l1(m_pMutex); | ||
if(m_pWriterLocked) | ||
m_pLockWriteCond.wait(l1); | ||
m_pWriterLocked = true; | ||
if(m_pCountRead) | ||
m_pLockReadCond.wait(l1); | ||
} | ||
|
||
void unlockWrite() | ||
{ | ||
UniqueLock l2(m_pMutex); | ||
m_pWriterLocked = false; | ||
m_pLockWriteCond.notify_all(); | ||
} | ||
|
||
void lockRead() | ||
{ | ||
UniqueLock l3(m_pMutex); | ||
if(m_pWriterLocked) | ||
m_pLockWriteCond.wait(l3); | ||
|
||
m_pCountRead++; | ||
} | ||
|
||
void unlockRead() | ||
{ | ||
UniqueLock l4(m_pMutex); | ||
m_pCountRead--; | ||
if(m_pWriterLocked) | ||
m_pLockReadCond.notify_one(); | ||
else if (m_pCountRead > 0) | ||
m_pLockWriteCond.notify_one(); | ||
|
||
} | ||
|
||
}; | ||
|
||
} // namespace sync | ||
} // namespace srt | ||
|
||
|
||
#include "atomic_clock.h" | ||
|
||
#endif // INC_SRT_SYNC_H |
There was a problem hiding this comment.
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.