Skip to content

Commit

Permalink
Made locking in drift tracing done inside the function and only if ne…
Browse files Browse the repository at this point in the history
…eded. To prevent deadlock when sending in buffer mode. (#114)
  • Loading branch information
ethouris authored and rndi committed Oct 5, 2017
1 parent 9c42b34 commit eae5256
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
6 changes: 5 additions & 1 deletion srtcore/buffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1449,7 +1449,7 @@ void CRcvBuffer::printDriftOffset(int tsbPdOffset, int tsbPdDriftAvg)
}
#endif /* SRT_DEBUG_TSBPD_DRIFT */

void CRcvBuffer::addRcvTsbPdDriftSample(uint32_t timestamp)
void CRcvBuffer::addRcvTsbPdDriftSample(uint32_t timestamp, pthread_mutex_t& mutex_to_lock)
{
if (!m_bTsbPdMode) // Not checked unless in TSBPD mode
return;
Expand All @@ -1472,6 +1472,9 @@ void CRcvBuffer::addRcvTsbPdDriftSample(uint32_t timestamp)
// either schedule time or a time supplied by the application).

int64_t iDrift = CTimer::getTime() - (getTsbPdTimeBase(timestamp) + timestamp);

CGuard::enterCS(mutex_to_lock);

bool updated = m_DriftTracer.update(iDrift);

#ifdef SRT_DEBUG_TSBPD_DRIFT
Expand All @@ -1487,6 +1490,7 @@ void CRcvBuffer::addRcvTsbPdDriftSample(uint32_t timestamp)
m_ullTsbPdTimeBase += m_DriftTracer.overdrift();
}

CGuard::leaveCS(mutex_to_lock);
}

int CRcvBuffer::readMsg(char* data, int len)
Expand Down
2 changes: 1 addition & 1 deletion srtcore/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ class CRcvBuffer
/// Add packet timestamp for drift caclculation and compensation
/// @param [in] timestamp packet time stamp

void addRcvTsbPdDriftSample(uint32_t timestamp);
void addRcvTsbPdDriftSample(uint32_t timestamp, pthread_mutex_t& mutex_to_lock);

#ifdef SRT_DEBUG_TSBPD_DRIFT
void printDriftHistogram(int64_t iDrift);
Expand Down
10 changes: 7 additions & 3 deletions srtcore/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6109,9 +6109,13 @@ void CUDT::processCtrl(CPacket& ctrlpkt)

updateCC(TEV_ACKACK, ack);

CGuard::enterCS(m_RecvLock);
m_pRcvBuffer->addRcvTsbPdDriftSample(ctrlpkt.getMsgTimeStamp());
CGuard::leaveCS(m_RecvLock);
// This function will put a lock on m_RecvLock by itself, as needed.
// It must be done inside because this function reads the current time
// and if waiting for the lock has caused a delay, the time will be
// inaccurate. Additionally it won't lock if TSBPD mode is off, and
// won't update anything. Note that if you set TSBPD mode and use
// srt_recvfile (which doesn't make any sense), you'll have e deadlock.
m_pRcvBuffer->addRcvTsbPdDriftSample(ctrlpkt.getMsgTimeStamp(), m_RecvLock);

// update last ACK that has been received by the sender
if (CSeqNo::seqcmp(ack, m_iRcvLastAckAck) > 0)
Expand Down

0 comments on commit eae5256

Please sign in to comment.