-
Notifications
You must be signed in to change notification settings - Fork 865
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
WIP: Fixing synchronisation of the TSBPD thread #1620
Closed
maxsharabayko
wants to merge
3
commits into
Haivision:master
from
maxsharabayko:hotfix/tsbpd-thread-sync
Closed
WIP: Fixing synchronisation of the TSBPD thread #1620
maxsharabayko
wants to merge
3
commits into
Haivision:master
from
maxsharabayko:hotfix/tsbpd-thread-sync
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maxsharabayko
added
Type: Bug
Indicates an unexpected problem or unintended behavior
[core]
Area: Changes in SRT library core
labels
Oct 22, 2020
8 tasks
maxsharabayko
force-pushed
the
hotfix/tsbpd-thread-sync
branch
from
October 26, 2020 15:27
8bf1a64
to
982bbb0
Compare
Core threads synchronization overview. Note. tsbpd()A separate internal thread per receiving socket. CUDT::tsbpd() (click to expand/collapse)
{
UniqueLock recv_lock (self->m_RecvLock);
CSync tsbpd_cc (self->m_RcvTsbPdCond, recv_lock);
while (!self->m_bClosing)
{
enterCS(self->m_RcvBufferLock);
self->m_pRcvBuffer->getRcvFirstMsg();
self->m_pRcvBuffer->skipData(seqlen);
self->m_pRcvBuffer->isRcvDataReady(..);
leaveCS(self->m_RcvBufferLock);
if (self->m_bSynRecving)
// TODO: [SYNC] Lock before signalling?
self->m_ReadyToReadEvent.notify_one();
self->s_UDTUnited.m_EPoll.update_events(self->m_SocketID, self->m_sPollID, SRT_EPOLL_IN, true);
CGlobEvent::triggerEvent();
if (tsbpdtime)
tsbpd_cc.wait_for(timediff);
else
tsbpd_cc.wait();
}
} receiveMessage()
CUDT::receiveMessage() (click to expand/collapse)
{
UniqueLock recvguard (m_RecvLock);
CSync tscond (m_RcvTsbPdCond, recvguard);
if (m_bBroken || m_bClosing)
{
enterCS(m_RcvBufferLock);
const int res = m_pRcvBuffer->readMsg(data, len);
leaveCS(m_RcvBufferLock);
if (m_bTsbPd)
{
HLOGP(tslog.Debug, "Ping TSBPD thread to schedule wakeup");
tscond.signal_locked(recvguard);
}
}
if (!m_bSynRecving)
{
enterCS(m_RcvBufferLock);
const int res = m_pRcvBuffer->readMsg(data, len, (w_mctrl), seqdistance);
leaveCS(m_RcvBufferLock);
if (m_bTsbPd)
{
HLOGP(arlog.Debug, "receiveMessage: nothing to read, kicking TSBPD, return AGAIN");
tscond.signal_locked(recvguard);
}
if (!m_pRcvBuffer->isRcvDataReady())
{
// Kick TsbPd thread to schedule next wakeup (if running)
if (m_bTsbPd)
{
HLOGP(arlog.Debug, "receiveMessage: DATA READ, but nothing more - kicking TSBPD.");
tscond.signal_locked(recvguard);
}
}
return res;
}
do
{
if (stillConnected() && !timeout && !m_pRcvBuffer->isRcvDataReady(..))
{
/* Kick TsbPd thread to schedule next wakeup (if running) */
if (m_bTsbPd)
tscond.signal_locked(recvguard);
do
{
if (!m_ReadyToReadEvent.lock_wait_until(exptime))
{
if (m_iRcvTimeOut >= 0) // otherwise it's "no timeout set"
timeout = true;
}
} while (stillConnected() && !timeout && (!m_pRcvBuffer->isRcvDataReady()));
}
enterCS(m_RcvBufferLock);
res = m_pRcvBuffer->readMsg((data), len, (w_mctrl), seqdistance);
leaveCS(m_RcvBufferLock);
} while ((res == 0) && !timeout);
if (!m_pRcvBuffer->isRcvDataReady())
{
// Kick TsbPd thread to schedule next wakeup (if running)
if (m_bTsbPd)
tscond.signal_locked(recvguard);
s_UDTUnited.m_EPoll.update_events(m_SocketID, m_sPollID, SRT_EPOLL_IN, false);
}
return res;
} processData(..)
CUDT::processData(..) (click to expand/collapse)
{
const bool need_tsbpd = m_bTsbPd || m_bGroupTsbPd;
// We are receiving data, start tsbpd thread if TsbPd is enabled
if (need_tsbpd && !m_RcvTsbPdThread.joinable())
{
StartThread(m_RcvTsbPdThread, CUDT::tsbpd, this, thname);
}
{
UniqueLock recvbuf_acklock(m_RcvBufferLock);
m_pRcvBuffer->addData(*i, offset);
}
// Wake up TSBPD on loss to reschedule possible TL drop
if (!srt_loss_seqs.empty() && m_bTsbPd)
CSync::lock_signal(m_RcvTsbPdCond, m_RecvLock);
if (!filter_loss_seqs.empty() && m_bTsbPd)
CSync::lock_signal(m_RcvTsbPdCond, m_RecvLock);
} releaseSynch()
CUDT::releaseSynch() (click to expand/collapse)
{
// wake up user calls
CSync::lock_signal(m_SendBlockCond, m_SendBlockLock);
enterCS(m_SendLock);
leaveCS(m_SendLock);
m_ReadyToReadEvent.lock_notify_one();
CSync::lock_signal(m_RcvTsbPdCond, m_RecvLock);
// TODO: [SYNC] Protect TBBPD Thread join
//enterCS(m_NewDataReceivedLock);
if (m_RcvTsbPdThread.joinable())
{
m_RcvTsbPdThread.join();
}
//leaveCS(m_NewDataReceivedLock);
enterCS(m_RecvLock);
leaveCS(m_RecvLock);
} |
23 tasks
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Issue: The
join()
operation on the TSBPD thread may be missed ifCUDT::releaseSynch()
finishes its execution while the receiving thread is still active and is about to create this thread.Changes
CUDT::m_RecvDataCond
andCUDT::m_RecvDataLock
mutex are replaced byCUDT::m_ReadyToReadEvent
event (of typeCEvent
that combines CV and the corresponding mutex).TODO
Closes #1606
Includes unit test from #1619