Skip to content

Commit

Permalink
Decouple ObjectPool from SystemLayer (#9329)
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca authored and pull[bot] committed Sep 29, 2021
1 parent 236b933 commit 2780417
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 179 deletions.
4 changes: 2 additions & 2 deletions src/inet/AsyncDNSResolverSockets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,10 @@ void AsyncDNSResolverSockets::DNSResultEventHandler(chip::System::Layer * aLayer
void AsyncDNSResolverSockets::NotifyChipThread(DNSResolver * resolver)
{
// Post work item via Timer Event for the CHIP thread
chip::System::Layer & lSystemLayer = resolver->SystemLayer();
chip::System::Layer * lSystemLayer = resolver->Layer().SystemLayer();

ChipLogDetail(Inet, "Posting DNS completion event to CHIP thread.");
lSystemLayer.ScheduleWork(AsyncDNSResolverSockets::DNSResultEventHandler, resolver);
lSystemLayer->ScheduleWork(AsyncDNSResolverSockets::DNSResultEventHandler, resolver);
}

void * AsyncDNSResolverSockets::AsyncDNSThreadRun(void * args)
Expand Down
14 changes: 14 additions & 0 deletions src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,19 @@ void EndPointBasis::InitEndPointBasis(InetLayer & aInetLayer, void * aAppState)
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void EndPointBasis::DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
DeferredRelease(Layer().SystemLayer(), aTactic);
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

} // namespace Inet
} // namespace chip
14 changes: 0 additions & 14 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,5 @@ inline bool EndPointBasis::IsOpenEndPoint() const
return lResult;
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
inline void EndPointBasis::DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
DeferredRelease(aTactic);
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

} // namespace Inet
} // namespace chip
8 changes: 5 additions & 3 deletions src/inet/IPEndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,11 +658,13 @@ IPPacketInfo * IPEndPointBasis::GetPacketInfo(const System::PacketBufferHandle &
return (lPacketInfo);
}

CHIP_ERROR IPEndPointBasis::PostPacketBufferEvent(chip::System::Layer & aLayer, System::Object & aTarget,
CHIP_ERROR IPEndPointBasis::PostPacketBufferEvent(chip::System::Layer * aLayer, System::Object & aTarget,
System::EventType aEventType, System::PacketBufferHandle && aBuffer)
{
VerifyOrReturnError(aLayer != nullptr, CHIP_ERROR_INVALID_ARGUMENT);

const CHIP_ERROR error =
aLayer.PostEvent(aTarget, aEventType, (uintptr_t) System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(aBuffer));
aLayer->PostEvent(aTarget, aEventType, (uintptr_t) System::LwIPPacketBufferView::UnsafeGetLwIPpbuf(aBuffer));
if (error == CHIP_NO_ERROR)
{
// If PostEvent() succeeded, it has ownership of the buffer, so we need to release it (without freeing it).
Expand Down Expand Up @@ -941,7 +943,7 @@ CHIP_ERROR IPEndPointBasis::GetSocket(IPAddressType aAddressType, int aType, int
mSocket = ::socket(family, aType, aProtocol);
if (mSocket == -1)
return chip::System::MapErrorPOSIX(errno);
ReturnErrorOnFailure(SystemLayer().StartWatchingSocket(mSocket, &mWatch));
ReturnErrorOnFailure(Layer().SystemLayer()->StartWatchingSocket(mSocket, &mWatch));

mAddrType = aAddressType;

Expand Down
2 changes: 1 addition & 1 deletion src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ class DLL_EXPORT IPEndPointBasis : public EndPointBasis
#if CHIP_SYSTEM_CONFIG_USE_LWIP
public:
static struct netif * FindNetifFromInterfaceId(InterfaceId aInterfaceId);
static CHIP_ERROR PostPacketBufferEvent(chip::System::Layer & aLayer, System::Object & aTarget, System::EventType aEventType,
static CHIP_ERROR PostPacketBufferEvent(chip::System::Layer * aLayer, System::Object & aTarget, System::EventType aEventType,
System::PacketBufferHandle && aBuffer);

protected:
Expand Down
8 changes: 4 additions & 4 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ CHIP_ERROR InetLayer::NewRawEndPoint(IPVersion ipVer, IPProtocol ipProto, RawEnd

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = RawEndPoint::sPool.TryCreate(*mSystemLayer);
*retEndPoint = RawEndPoint::sPool.TryCreate();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "Raw");
Expand Down Expand Up @@ -572,7 +572,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint)

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = TCPEndPoint::sPool.TryCreate(*mSystemLayer);
*retEndPoint = TCPEndPoint::sPool.TryCreate();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "TCP");
Expand Down Expand Up @@ -612,7 +612,7 @@ CHIP_ERROR InetLayer::NewUDPEndPoint(UDPEndPoint ** retEndPoint)

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = UDPEndPoint::sPool.TryCreate(*mSystemLayer);
*retEndPoint = UDPEndPoint::sPool.TryCreate();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "UDP");
Expand Down Expand Up @@ -787,7 +787,7 @@ CHIP_ERROR InetLayer::ResolveHostAddress(const char * hostName, uint16_t hostNam
VerifyOrExit(hostNameLen <= NL_DNS_HOSTNAME_MAX_LEN, err = INET_ERROR_HOST_NAME_TOO_LONG);
VerifyOrExit(maxAddrs > 0, err = CHIP_ERROR_NO_MEMORY);

resolver = DNSResolver::sPool.TryCreate(*mSystemLayer);
resolver = DNSResolver::sPool.TryCreate();
if (resolver != nullptr)
{
resolver->InitInetLayerBasis(*this);
Expand Down
21 changes: 10 additions & 11 deletions src/inet/RawEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ CHIP_ERROR RawEndPoint::Bind(IPAddressType addrType, const IPAddress & addr, Int
ReturnErrorOnFailure(IPEndPointBasis::Bind(addrType, addr, 0, intfId));

#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
dispatch_queue_t dispatchQueue = SystemLayer().GetDispatchQueue();
dispatch_queue_t dispatchQueue = Layer().SystemLayer()->GetDispatchQueue();
if (dispatchQueue != nullptr)
{
unsigned long fd = static_cast<unsigned long>(mSocket);
Expand Down Expand Up @@ -350,7 +350,7 @@ CHIP_ERROR RawEndPoint::BindIPv6LinkLocal(InterfaceId intfId, const IPAddress &

optfail:
res = chip::System::MapErrorPOSIX(errno);
SystemLayer().StopWatchingSocket(&mWatch);
Layer().SystemLayer()->StopWatchingSocket(&mWatch);
close(mSocket);
mSocket = INET_INVALID_SOCKET_FD;
mAddrType = kIPAddressType_Unknown;
Expand Down Expand Up @@ -424,8 +424,8 @@ CHIP_ERROR RawEndPoint::Listen(IPEndPointBasis::OnMessageReceivedFunct onMessage

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
// Wait for ability to read on this endpoint.
ReturnErrorOnFailure(SystemLayer().SetCallback(mWatch, HandlePendingIO, reinterpret_cast<intptr_t>(this)));
ReturnErrorOnFailure(SystemLayer().RequestCallbackOnPendingRead(mWatch));
ReturnErrorOnFailure(Layer().SystemLayer()->SetCallback(mWatch, HandlePendingIO, reinterpret_cast<intptr_t>(this)));
ReturnErrorOnFailure(Layer().SystemLayer()->RequestCallbackOnPendingRead(mWatch));
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS

return CHIP_NO_ERROR;
Expand Down Expand Up @@ -467,7 +467,7 @@ void RawEndPoint::Close()
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (mSocket != INET_INVALID_SOCKET_FD)
{
SystemLayer().StopWatchingSocket(&mWatch);
Layer().SystemLayer()->StopWatchingSocket(&mWatch);
close(mSocket);
mSocket = INET_INVALID_SOCKET_FD;
}
Expand Down Expand Up @@ -930,11 +930,10 @@ u8_t RawEndPoint::LwIPReceiveRawMessage(void * arg, struct raw_pcb * pcb, struct
u8_t RawEndPoint::LwIPReceiveRawMessage(void * arg, struct raw_pcb * pcb, struct pbuf * p, ip_addr_t * addr)
#endif // LWIP_VERSION_MAJOR > 1 || LWIP_VERSION_MINOR >= 5
{
RawEndPoint * ep = static_cast<RawEndPoint *>(arg);
chip::System::Layer & lSystemLayer = ep->SystemLayer();
IPPacketInfo * pktInfo = NULL;
uint8_t enqueue = 1;
System::PacketBufferHandle buf = System::PacketBufferHandle::Adopt(p);
RawEndPoint * ep = static_cast<RawEndPoint *>(arg);
IPPacketInfo * pktInfo = NULL;
uint8_t enqueue = 1;
System::PacketBufferHandle buf = System::PacketBufferHandle::Adopt(p);

// Filtering based on the saved ICMP6 types (the only protocol currently supported.)
if ((ep->IPVer == kIPVersion_6) && (ep->IPProto == kIPProtocol_ICMPv6))
Expand Down Expand Up @@ -988,7 +987,7 @@ u8_t RawEndPoint::LwIPReceiveRawMessage(void * arg, struct raw_pcb * pcb, struct
pktInfo->DestPort = 0;
}

PostPacketBufferEvent(lSystemLayer, *ep, kInetEvent_RawDataReceived, std::move(buf));
PostPacketBufferEvent(ep->Layer().SystemLayer(), *ep, kInetEvent_RawDataReceived, std::move(buf));
}

return enqueue;
Expand Down
Loading

0 comments on commit 2780417

Please sign in to comment.