Skip to content

Commit dca5399

Browse files
cl-mentethourisClement Gerouville
authored
Automatic call of startup() and cleanup() #3098 (#3109)
This pull request fixes issue #3098 by ensuring that startup() and cleanup() are called automatically. --------- Co-authored-by: Sektor van Skijlen <[email protected]> Co-authored-by: Clement Gerouville <[email protected]> Co-authored-by: Sektor van Skijlen <[email protected]>
1 parent 00e14f7 commit dca5399

8 files changed

Lines changed: 225 additions & 190 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ compile_commands.json
4747
# ignore files generated by clion and cmake
4848
.idea/
4949
cmake-build-debug/
50+
tags

‎srtcore/api.cpp‎

Lines changed: 111 additions & 113 deletions
Original file line numberDiff line numberDiff line change
@@ -189,23 +189,34 @@ srt::CUDTUnited::CUDTUnited()
189189
// XXX An unlikely exception thrown from the below calls
190190
// might destroy the application before `main`. This shouldn't
191191
// be a problem in general.
192+
setupMutex(m_GCStartLock, "GCStart");
192193
setupMutex(m_GCStopLock, "GCStop");
193194
setupCond(m_GCStopCond, "GCStop");
194195
setupMutex(m_GlobControlLock, "GlobControl");
195196
setupMutex(m_IDLock, "ID");
196197
setupMutex(m_InitLock, "Init");
198+
// Global initialization code
199+
#ifdef _WIN32
200+
WORD wVersionRequested;
201+
WSADATA wsaData;
202+
wVersionRequested = MAKEWORD(2, 2);
203+
204+
if (0 != WSAStartup(wVersionRequested, &wsaData))
205+
throw CUDTException(MJ_SETUP, MN_NONE, WSAGetLastError());
206+
#endif
207+
CCryptoControl::globalInit();
208+
HLOGC(inlog.Debug, log << "SRT Clock Type: " << SRT_SYNC_CLOCK_STR);
197209
}
198210

199211
srt::CUDTUnited::~CUDTUnited()
200212
{
201213
// Call it if it wasn't called already.
202214
// This will happen at the end of main() of the application,
203215
// when the user didn't call srt_cleanup().
204-
if (m_bGCStatus)
205-
{
206-
cleanup();
207-
}
208-
216+
enterCS(m_InitLock);
217+
stopGarbageCollector();
218+
leaveCS(m_InitLock);
219+
closeAllSockets();
209220
releaseMutex(m_GlobControlLock);
210221
releaseMutex(m_IDLock);
211222
releaseMutex(m_InitLock);
@@ -219,8 +230,11 @@ srt::CUDTUnited::~CUDTUnited()
219230
releaseCond(m_GCStopCond);
220231
#endif
221232
releaseMutex(m_GCStopLock);
222-
233+
releaseMutex(m_GCStartLock);
223234
delete m_pCache;
235+
#ifdef _WIN32
236+
WSACleanup();
237+
#endif
224238
}
225239

226240
string srt::CUDTUnited::CONID(SRTSOCKET sock)
@@ -233,39 +247,107 @@ string srt::CUDTUnited::CONID(SRTSOCKET sock)
233247
return os.str();
234248
}
235249

236-
int srt::CUDTUnited::startup()
250+
bool srt::CUDTUnited::startGarbageCollector()
237251
{
238-
ScopedLock gcinit(m_InitLock);
252+
253+
ScopedLock guard(m_GCStartLock);
254+
if (!m_bGCStatus)
255+
{
256+
m_bClosing = false;
257+
m_bGCStatus = StartThread(m_GCThread, garbageCollect, this, "SRT:GC");
258+
}
259+
return m_bGCStatus;
260+
}
261+
262+
void srt::CUDTUnited::stopGarbageCollector()
263+
{
264+
265+
ScopedLock guard(m_GCStartLock);
239266
if (m_bGCStatus)
240-
return 1;
267+
{
268+
m_bGCStatus = false;
269+
{
270+
CUniqueSync gclock (m_GCStopLock, m_GCStopCond);
271+
m_bClosing = true;
272+
gclock.notify_all();
273+
}
274+
m_GCThread.join();
275+
}
276+
}
241277

242-
if (m_iInstanceCount++ > 0)
243-
return 1;
278+
void srt::CUDTUnited::closeAllSockets()
279+
{
280+
// remove all sockets and multiplexers
281+
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all pending sockets. Acquring control lock...");
244282

245-
// Global initialization code
246-
#ifdef _WIN32
247-
WORD wVersionRequested;
248-
WSADATA wsaData;
249-
wVersionRequested = MAKEWORD(2, 2);
283+
{
284+
ScopedLock glock(m_GlobControlLock);
250285

251-
if (0 != WSAStartup(wVersionRequested, &wsaData))
252-
throw CUDTException(MJ_SETUP, MN_NONE, WSAGetLastError());
286+
for (sockets_t::iterator i = m_Sockets.begin(); i != m_Sockets.end(); ++i)
287+
{
288+
CUDTSocket* s = i->second;
289+
s->breakSocket_LOCKED();
290+
291+
#if ENABLE_BONDING
292+
if (s->m_GroupOf)
293+
{
294+
HLOGC(smlog.Debug,
295+
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id()
296+
<< " (IPE?) - REMOVING FROM GROUP");
297+
s->removeFromGroup(false);
298+
}
253299
#endif
300+
m_ClosedSockets[i->first] = s;
254301

255-
CCryptoControl::globalInit();
302+
// remove from listener's queue
303+
sockets_t::iterator ls = m_Sockets.find(s->m_ListenSocket);
304+
if (ls == m_Sockets.end())
305+
{
306+
ls = m_ClosedSockets.find(s->m_ListenSocket);
307+
if (ls == m_ClosedSockets.end())
308+
continue;
309+
}
256310

257-
PacketFilter::globalInit();
311+
enterCS(ls->second->m_AcceptLock);
312+
ls->second->m_QueuedSockets.erase(s->m_SocketID);
313+
leaveCS(ls->second->m_AcceptLock);
314+
}
315+
m_Sockets.clear();
258316

259-
m_bClosing = false;
317+
for (sockets_t::iterator j = m_ClosedSockets.begin(); j != m_ClosedSockets.end(); ++j)
318+
{
319+
j->second->m_tsClosureTimeStamp = steady_clock::time_point();
320+
}
321+
}
260322

261-
if (!StartThread(m_GCThread, garbageCollect, this, "SRT:GC"))
262-
return -1;
323+
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all CLOSED sockets.");
324+
while (true)
325+
{
326+
checkBrokenSockets();
263327

264-
m_bGCStatus = true;
328+
enterCS(m_GlobControlLock);
329+
bool empty = m_ClosedSockets.empty();
330+
leaveCS(m_GlobControlLock);
331+
332+
if (empty)
333+
break;
334+
335+
HLOGC(inlog.Debug, log << "GC: checkBrokenSockets didn't wipe all sockets, repeating after 1s sleep");
336+
srt::sync::this_thread::sleep_for(milliseconds_from(1));
337+
}
265338

266-
HLOGC(inlog.Debug, log << "SRT Clock Type: " << SRT_SYNC_CLOCK_STR);
267339

268-
return 0;
340+
}
341+
342+
343+
int srt::CUDTUnited::startup()
344+
{
345+
ScopedLock gcinit(m_InitLock);
346+
m_iInstanceCount++;
347+
if (m_bGCStatus)
348+
return (m_iInstanceCount == 1) ? 1 : 0;
349+
else
350+
return startGarbageCollector() ? 0 : -1;
269351
}
270352

271353
int srt::CUDTUnited::cleanup()
@@ -283,28 +365,8 @@ int srt::CUDTUnited::cleanup()
283365
if (--m_iInstanceCount > 0)
284366
return 0;
285367

286-
if (!m_bGCStatus)
287-
return 0;
288-
289-
{
290-
UniqueLock gclock(m_GCStopLock);
291-
m_bClosing = true;
292-
}
293-
// NOTE: we can do relaxed signaling here because
294-
// waiting on m_GCStopCond has a 1-second timeout,
295-
// after which the m_bClosing flag is cheched, which
296-
// is set here above. Worst case secenario, this
297-
// pthread_join() call will block for 1 second.
298-
CSync::notify_one_relaxed(m_GCStopCond);
299-
m_GCThread.join();
300-
301-
m_bGCStatus = false;
302-
303-
// Global destruction code
304-
#ifdef _WIN32
305-
WSACleanup();
306-
#endif
307-
368+
stopGarbageCollector();
369+
closeAllSockets();
308370
return 0;
309371
}
310372

@@ -458,6 +520,7 @@ SRTSOCKET srt::CUDTUnited::newSocket(CUDTSocket** pps)
458520
throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0);
459521
}
460522

523+
startGarbageCollector();
461524
if (pps)
462525
*pps = ns;
463526

@@ -3391,66 +3454,6 @@ void* srt::CUDTUnited::garbageCollect(void* p)
33913454
HLOGC(inlog.Debug, log << "GC: sleep 1 s");
33923455
self->m_GCStopCond.wait_for(gclock, seconds_from(1));
33933456
}
3394-
3395-
// remove all sockets and multiplexers
3396-
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all pending sockets. Acquring control lock...");
3397-
3398-
{
3399-
ScopedLock glock(self->m_GlobControlLock);
3400-
3401-
for (sockets_t::iterator i = self->m_Sockets.begin(); i != self->m_Sockets.end(); ++i)
3402-
{
3403-
CUDTSocket* s = i->second;
3404-
s->breakSocket_LOCKED();
3405-
3406-
#if ENABLE_BONDING
3407-
if (s->m_GroupOf)
3408-
{
3409-
HLOGC(smlog.Debug,
3410-
log << "@" << s->m_SocketID << " IS MEMBER OF $" << s->m_GroupOf->id()
3411-
<< " (IPE?) - REMOVING FROM GROUP");
3412-
s->removeFromGroup(false);
3413-
}
3414-
#endif
3415-
self->m_ClosedSockets[i->first] = s;
3416-
3417-
// remove from listener's queue
3418-
sockets_t::iterator ls = self->m_Sockets.find(s->m_ListenSocket);
3419-
if (ls == self->m_Sockets.end())
3420-
{
3421-
ls = self->m_ClosedSockets.find(s->m_ListenSocket);
3422-
if (ls == self->m_ClosedSockets.end())
3423-
continue;
3424-
}
3425-
3426-
enterCS(ls->second->m_AcceptLock);
3427-
ls->second->m_QueuedSockets.erase(s->m_SocketID);
3428-
leaveCS(ls->second->m_AcceptLock);
3429-
}
3430-
self->m_Sockets.clear();
3431-
3432-
for (sockets_t::iterator j = self->m_ClosedSockets.begin(); j != self->m_ClosedSockets.end(); ++j)
3433-
{
3434-
j->second->m_tsClosureTimeStamp = steady_clock::time_point();
3435-
}
3436-
}
3437-
3438-
HLOGC(inlog.Debug, log << "GC: GLOBAL EXIT - releasing all CLOSED sockets.");
3439-
while (true)
3440-
{
3441-
self->checkBrokenSockets();
3442-
3443-
enterCS(self->m_GlobControlLock);
3444-
bool empty = self->m_ClosedSockets.empty();
3445-
leaveCS(self->m_GlobControlLock);
3446-
3447-
if (empty)
3448-
break;
3449-
3450-
HLOGC(inlog.Debug, log << "GC: checkBrokenSockets didn't wipe all sockets, repeating after 1s sleep");
3451-
srt::sync::this_thread::sleep_for(milliseconds_from(1));
3452-
}
3453-
34543457
THREAD_EXIT();
34553458
return NULL;
34563459
}
@@ -3469,8 +3472,6 @@ int srt::CUDT::cleanup()
34693472

34703473
SRTSOCKET srt::CUDT::socket()
34713474
{
3472-
uglobal().startup();
3473-
34743475
try
34753476
{
34763477
return uglobal().newSocket();
@@ -3518,9 +3519,6 @@ srt::CUDTGroup& srt::CUDT::newGroup(const int type)
35183519

35193520
SRTSOCKET srt::CUDT::createGroup(SRT_GROUP_TYPE gt)
35203521
{
3521-
// Doing the same lazy-startup as with srt_create_socket()
3522-
uglobal().startup();
3523-
35243522
try
35253523
{
35263524
srt::sync::ScopedLock globlock(uglobal().m_GlobControlLock);

‎srtcore/api.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,9 @@ class CUDTUnited
462462

463463
CUDTSocket* locateAcquireSocket(SRTSOCKET u, ErrorHandling erh = ERH_RETURN);
464464
bool acquireSocket(CUDTSocket* s);
465+
bool startGarbageCollector();
466+
void stopGarbageCollector();
467+
void closeAllSockets();
465468

466469
public:
467470
struct SocketKeeper
@@ -534,6 +537,7 @@ class CUDTUnited
534537

535538
private:
536539
srt::sync::atomic<bool> m_bClosing;
540+
sync::Mutex m_GCStartLock;
537541
sync::Mutex m_GCStopLock;
538542
sync::Condition m_GCStopCond;
539543

0 commit comments

Comments
 (0)