Skip to content

Commit

Permalink
[core] Changed CUnit::m_iFlag type to bool.
Browse files Browse the repository at this point in the history
  • Loading branch information
maxsharabayko committed Sep 29, 2022
1 parent 2fed400 commit 345517b
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 28 deletions.
2 changes: 1 addition & 1 deletion srtcore/buffer_rcv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ int CRcvBuffer::insert(CUnit* unit)
}
SRT_ASSERT(m_entries[pos].pUnit == NULL);

m_pUnitQueue->makeUnitGood(unit);
m_pUnitQueue->makeUnitTaken(unit);
m_entries[pos].pUnit = unit;
m_entries[pos].status = EntryState_Avail;
countBytes(1, (int)unit->m_Packet.getLength());
Expand Down
2 changes: 1 addition & 1 deletion srtcore/buffer_rcv.h
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ class CRcvBuffer
int getTimespan_ms() const;

private:
// TODO: Call makeUnitGood upon assignment, and makeUnitFree upon clearing.
// TODO: Call makeUnitTaken upon assignment, and makeUnitFree upon clearing.
// TODO: CUnitPtr is not in use at the moment, but may be a smart pointer.
// class CUnitPtr
// {
Expand Down
10 changes: 5 additions & 5 deletions srtcore/packetfilter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void srt::PacketFilter::receive(CUnit* unit, std::vector<CUnit*>& w_incoming, lo
{
// For the sake of rebuilding MARK THIS UNIT GOOD, otherwise the
// unit factory will supply it from getNextAvailUnit() as if it were not in use.
unit->m_iFlag = CUnit::GOOD;
unit->m_bTaken = true;
HLOGC(pflog.Debug, log << "FILTER: PASSTHRU current packet %" << unit->m_Packet.getSeqNo());
w_incoming.push_back(unit);
}
Expand Down Expand Up @@ -178,11 +178,11 @@ void srt::PacketFilter::receive(CUnit* unit, std::vector<CUnit*>& w_incoming, lo
// Wanted units will be set GOOD flag, unwanted will remain
// with FREE and therefore will be returned at the next
// call to getNextAvailUnit().
unit->m_iFlag = CUnit::FREE;
unit->m_bTaken = false;
for (vector<CUnit*>::iterator i = w_incoming.begin(); i != w_incoming.end(); ++i)
{
CUnit* u = *i;
u->m_iFlag = CUnit::FREE;
u->m_bTaken = false;
}

// Packets must be sorted by sequence number, ascending, in order
Expand Down Expand Up @@ -251,9 +251,9 @@ void srt::PacketFilter::InsertRebuilt(vector<CUnit*>& incoming, CUnitQueue* uq)
break;
}

// LOCK the unit as GOOD because otherwise the next
// LOCK the unit as taken because otherwise the next
// call to getNextAvailUnit will return THE SAME UNIT.
u->m_iFlag = CUnit::GOOD;
u->m_bTaken = true;
// After returning from this function, all units will be
// set back to FREE so that the buffer can decide whether
// it wants them or not.
Expand Down
14 changes: 7 additions & 7 deletions srtcore/queue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ srt::CUnitQueue::CQEntry* srt::CUnitQueue::allocateEntry(const int iNumUnits, co

for (int i = 0; i < iNumUnits; ++i)
{
tempu[i].m_iFlag = CUnit::FREE;
tempu[i].m_bTaken = false;
tempu[i].m_Packet.m_pcData = tempb + i * mss;
}

Expand Down Expand Up @@ -172,7 +172,7 @@ srt::CUnit* srt::CUnitQueue::getNextAvailUnit()
const CUnit* end = m_pCurrQueue->m_pUnit + m_pCurrQueue->m_iSize;
for (; m_pAvailUnit != end; ++m_pAvailUnit, ++units_checked)
{
if (m_pAvailUnit->m_iFlag == CUnit::FREE)
if (!m_pAvailUnit->m_bTaken)
{
return m_pAvailUnit;
}
Expand All @@ -188,19 +188,19 @@ srt::CUnit* srt::CUnitQueue::getNextAvailUnit()
void srt::CUnitQueue::makeUnitFree(CUnit* unit)
{
SRT_ASSERT(unit != NULL);
SRT_ASSERT(unit->m_iFlag != CUnit::FREE);
unit->m_iFlag.store(CUnit::FREE);
SRT_ASSERT(unit->m_bTaken);
unit->m_bTaken.store(false);

--m_iNumTaken;
}

void srt::CUnitQueue::makeUnitGood(CUnit* unit)
void srt::CUnitQueue::makeUnitTaken(CUnit* unit)
{
++m_iNumTaken;

SRT_ASSERT(unit != NULL);
SRT_ASSERT(unit->m_iFlag == CUnit::FREE);
unit->m_iFlag.store(CUnit::GOOD);
SRT_ASSERT(!unit->m_bTaken);
unit->m_bTaken.store(true);
}

srt::CSndUList::CSndUList(sync::CTimer* pTimer)
Expand Down
13 changes: 2 additions & 11 deletions srtcore/queue.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ class CUDT;
struct CUnit
{
CPacket m_Packet; // packet
enum Flag
{
FREE = 0,
GOOD = 1,
PASSACK = 2,
DROPPED = 3
};

// TODO: The new RcvBuffer allows to use atomic_bool here.
sync::atomic<Flag> m_iFlag; // 0: free, 1: occupied, 2: msg read but not freed (out-of-order), 3: msg dropped
sync::atomic<bool> m_bTaken; // true if the unit is is use (can be stored in the RCV buffer).
};

class CUnitQueue
Expand All @@ -106,7 +97,7 @@ class CUnitQueue

void makeUnitFree(CUnit* unit);

void makeUnitGood(CUnit* unit);
void makeUnitTaken(CUnit* unit);

private:
struct CQEntry
Expand Down
6 changes: 3 additions & 3 deletions test/test_unitqueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ TEST(CUnitQueue, Increase)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);
unit_queue.makeUnitTaken(unit);
taken_units.push_back(unit);
}
}
Expand All @@ -43,7 +43,7 @@ TEST(CUnitQueue, IncreaseAndFree)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);
unit_queue.makeUnitTaken(unit);

if (taken_unit)
unit_queue.makeUnitFree(taken_unit);
Expand All @@ -67,7 +67,7 @@ TEST(CUnitQueue, IncreaseAndFreeGrouped)
{
CUnit* unit = unit_queue.getNextAvailUnit();
ASSERT_NE(unit, nullptr);
unit_queue.makeUnitGood(unit);
unit_queue.makeUnitTaken(unit);

if (taken_units.size() >= buffer_size_pkts)
{
Expand Down

0 comments on commit 345517b

Please sign in to comment.