Skip to content

Commit

Permalink
[UDP] Remove thread-unsafe reference counting (#20535)
Browse files Browse the repository at this point in the history
  • Loading branch information
gjc13 authored and pull[bot] committed Jul 16, 2022
1 parent d139fa6 commit e8f9362
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 13 deletions.
7 changes: 1 addition & 6 deletions src/inet/UDPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,20 +371,15 @@ void UDPEndPointImplLwIP::LwIPReceiveUDPMessage(void * arg, struct udp_pcb * pcb
pktInfo->DestPort = pcb->local_port;
}

ep->Retain();
// TODO: add thread-safe reference counting for UDP endpoints
CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, p = System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(buf)] {
ep->HandleDataReceived(System::PacketBufferHandle::Adopt(p));
ep->Release();
});
if (err == CHIP_NO_ERROR)
{
// If ScheduleLambda() succeeded, it has ownership of the buffer, so we need to release it (without freeing it).
static_cast<void>(std::move(buf).UnsafeRelease());
}
else
{
ep->Release();
}
}

CHIP_ERROR UDPEndPointImplLwIP::SetMulticastLoopback(IPVersion aIPVersion, bool aLoopback)
Expand Down
11 changes: 4 additions & 7 deletions src/inet/UDPEndPointImplOpenThread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,18 +98,15 @@ void UDPEndPointImplOT::handleUdpReceive(void * aContext, otMessage * aMessage,
}
payload->SetDataLength(static_cast<uint16_t>(msgLen));

ep->Retain();
auto * buf = std::move(payload).UnsafeRelease();
CHIP_ERROR err = ep->GetSystemLayer().ScheduleLambda([ep, buf] {
ep->HandleDataReceived(System::PacketBufferHandle::Adopt(buf));
ep->Release();
});
// TODO: add thread-safe reference counting for UDP endpoints
auto * buf = std::move(payload).UnsafeRelease();
CHIP_ERROR err =
ep->GetSystemLayer().ScheduleLambda([ep, buf] { ep->HandleDataReceived(System::PacketBufferHandle::Adopt(buf)); });
if (err != CHIP_NO_ERROR)
{
// Make sure we properly clean up buf and ep, since our lambda will not
// run.
payload = System::PacketBufferHandle::Adopt(buf);
ep->Release();
}
}

Expand Down

0 comments on commit e8f9362

Please sign in to comment.