diff --git a/srtcore/api.cpp b/srtcore/api.cpp index ca26600d1..665593c39 100644 --- a/srtcore/api.cpp +++ b/srtcore/api.cpp @@ -174,8 +174,7 @@ srt::CUDTUnited::CUDTUnited() , m_GlobControlLock() , m_IDLock() , m_mMultiplexer() - , m_MultiplexerLock() - , m_pCache(NULL) + , m_pCache(new CCache) , m_bClosing(false) , m_GCStopCond() , m_InitLock() @@ -195,8 +194,6 @@ srt::CUDTUnited::CUDTUnited() setupMutex(m_GlobControlLock, "GlobControl"); setupMutex(m_IDLock, "ID"); setupMutex(m_InitLock, "Init"); - - m_pCache = new CCache; } srt::CUDTUnited::~CUDTUnited() diff --git a/srtcore/api.h b/srtcore/api.h index fddbfc294..6dbad6634 100644 --- a/srtcore/api.h +++ b/srtcore/api.h @@ -385,11 +385,13 @@ class CUDTUnited private: typedef std::map sockets_t; // stores all the socket structures - sockets_t m_Sockets; + SRT_ATTR_GUARDED_BY(m_GlobControlLock) + sockets_t m_Sockets; #if ENABLE_BONDING typedef std::map groups_t; - groups_t m_Groups; + SRT_ATTR_GUARDED_BY(m_GlobControlLock) + groups_t m_Groups; #endif sync::Mutex m_GlobControlLock; // used to synchronize UDT API @@ -399,6 +401,7 @@ class CUDTUnited SRTSOCKET m_SocketIDGenerator; // seed to generate a new unique socket ID SRTSOCKET m_SocketIDGenerator_init; // Keeps track of the very first one + SRT_ATTR_GUARDED_BY(m_GlobControlLock) std::map > m_PeerRec; // record sockets from peers to avoid repeated connection request, int64_t = (socker_id << 30) + isn @@ -460,11 +463,13 @@ class CUDTUnited const sockaddr_any& reqaddr, const CSrtMuxerConfig& cfgSocket); private: + SRT_ATTR_GUARDED_BY(m_GlobControlLock) std::map m_mMultiplexer; // UDP multiplexer - sync::Mutex m_MultiplexerLock; -private: - CCache* m_pCache; // UDT network information cache + /// UDT network information cache. + /// Existence is guarded by m_GlobControlLock, but the cache itself is thread-safe. + SRT_ATTR_GUARDED_BY(m_GlobControlLock) + CCache* const m_pCache; private: srt::sync::atomic m_bClosing; @@ -472,14 +477,19 @@ class CUDTUnited sync::Condition m_GCStopCond; sync::Mutex m_InitLock; + SRT_ATTR_GUARDED_BY(m_InitLock) int m_iInstanceCount; // number of startup() called by application + SRT_ATTR_GUARDED_BY(m_InitLock) bool m_bGCStatus; // if the GC thread is working (true) + SRT_ATTR_GUARDED_BY(m_InitLock) sync::CThread m_GCThread; static void* garbageCollect(void*); + SRT_ATTR_GUARDED_BY(m_GlobControlLock) sockets_t m_ClosedSockets; // temporarily store closed sockets #if ENABLE_BONDING + SRT_ATTR_GUARDED_BY(m_GlobControlLock) groups_t m_ClosedGroups; #endif diff --git a/srtcore/cache.h b/srtcore/cache.h index 47633706a..d5a037633 100644 --- a/srtcore/cache.h +++ b/srtcore/cache.h @@ -192,9 +192,11 @@ template class CCache return 0; } - /// Specify the cache size (i.e., max number of items). - /// @param [in] size max cache size. +private: + /// Specify the cache size (i.e., max number of items). + /// Private or else must be protected by a lock. + /// @param [in] size max cache size. void setSizeLimit(int size) { m_iMaxSize = size; @@ -202,8 +204,8 @@ template class CCache m_vHashPtr.resize(m_iHashSize); } - /// Clear all entries in the cache, restore to initialization state. - + /// Clear all entries in the cache, restore to initialization state. + /// Private or else must be protected by a lock. void clear() { for (typename std::list::iterator i = m_StorageList.begin(); i != m_StorageList.end(); ++ i) diff --git a/srtcore/core.cpp b/srtcore/core.cpp index 83ff52f16..b802bdec9 100644 --- a/srtcore/core.cpp +++ b/srtcore/core.cpp @@ -11946,7 +11946,7 @@ void srt::CUDT::processKeepalive(const CPacket& ctrlpkt, const time_point& tsArr if (m_parent->m_GroupOf) { // Lock GlobControlLock in order to make sure that - // the state if the socket having the group and the + // the state of the socket having the group and the // existence of the group will not be changed during // the operation. The attempt of group deletion will // have to wait until this operation completes. diff --git a/srtcore/group.cpp b/srtcore/group.cpp index d2d275bf6..d4598d7c1 100644 --- a/srtcore/group.cpp +++ b/srtcore/group.cpp @@ -3199,7 +3199,7 @@ void CUDTGroup::send_CloseBrokenSockets(vector& w_wipeme) InvertedLock ug(m_GroupLock); // With unlocked GroupLock, we can now lock GlobControlLock. - // This is needed prevent any of them be deleted from the container + // This is needed to prevent any of them deleted from the container // at the same time. ScopedLock globlock(CUDT::uglobal().m_GlobControlLock);