Skip to content

Commit

Permalink
Use std::atomic_int instead of bool
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Aug 24, 2022
1 parent 28485d5 commit a5b0803
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 10 deletions.
16 changes: 7 additions & 9 deletions src/inet/UDPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ void UDPEndPointImplLwIP::CloseImpl()
// event pending in the event queue (SystemLayer::ScheduleLambda), we
// schedule a release call to the end of the queue, to ensure that the
// queued pointer to UDPEndPointImplLwIP is not dangling.
if (mDelayRelease == true)
if (mDelayReleaseCount == 0)
{
mDelayRelease = false;

Retain();
CHIP_ERROR err = GetSystemLayer().ScheduleLambda([this] { Release(); });
if (err != CHIP_NO_ERROR)
Expand All @@ -283,8 +281,8 @@ void UDPEndPointImplLwIP::Free()

void UDPEndPointImplLwIP::HandleDataReceived(System::PacketBufferHandle && msg, IPPacketInfo * pktInfo)
{
// Clear mDelayRelease flag so that we don't need to delay release of current UDPEndPoint.
mDelayRelease = false;
// Decrease mDelayReleaseCount so that we don't need to delay release of current UDPEndPoint if it reaches 0.
mDelayReleaseCount--;

if ((mState == State::kListening) && (OnMessageReceived != nullptr))
{
Expand Down Expand Up @@ -399,9 +397,9 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
pktInfo->SrcPort = port;
pktInfo->DestPort = pcb->local_port;

// Set mDelayRelease flag to delay release of this UDP EndPoint until HandleDataReceived is
// called on this UDP EndPoint.
ep->mDelayRelease = true;
// Increase mDelayReleaseCount to delay release of this UDP EndPoint until on more HandleDataReceived is
// pending on this UDP EndPoint.
ep->mDelayReleaseCount++;

CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(buf), pktInfo] {
ep->HandleDataReceived(System::PacketBufferHandle::Adopt(p), pktInfo);
Expand All @@ -414,7 +412,7 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
}
else
{
ep->mDelayRelease = false;
ep->mDelayReleaseCount--;

// If ScheduleLambda() succeeded, `pktInfo` will be deleted in `HandleDataReceived`.
// Otherwise we delete it here.
Expand Down
2 changes: 1 addition & 1 deletion src/inet/UDPEndPointImplLwIP.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class UDPEndPointImplLwIP : public UDPEndPoint, public EndPointStateLwIP
static void LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb, struct pbuf * p, const ip_addr_t * addr, u16_t port);

udp_pcb * mUDP; // LwIP User datagram protocol (UDP) control block.
bool mDelayRelease = false;
std::atomic_int mDelayReleaseCount{ 0 };
};

using UDPEndPointImpl = UDPEndPointImplLwIP;
Expand Down

0 comments on commit a5b0803

Please sign in to comment.