Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify SystemPool and lib/support/Pool #9590

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions examples/shell/shell_common/cmd_send.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <lib/core/CHIPCore.h>
#include <lib/shell/Engine.h>
Expand All @@ -26,6 +27,7 @@
#include <messaging/ExchangeMgr.h>
#include <platform/CHIPDeviceLayer.h>
#include <protocols/secure_channel/PASESession.h>
#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SecureSessionMgr.h>
#include <transport/raw/TCP.h>
Expand Down
1 change: 1 addition & 0 deletions src/app/tests/integration/chip_im_initiator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -724,6 +724,7 @@ int main(int argc, char * argv[])
chip::DeviceLayer::PlatformMgr().RunEventLoop();

chip::app::InteractionModelEngine::GetInstance()->Shutdown();
gTransportManager.Close();
ShutdownChip();
exit:
if (err != CHIP_NO_ERROR || (gCommandRespCount != kMaxCommandMessageCount + kTotalFailureCommandMessageCount))
Expand Down
2 changes: 1 addition & 1 deletion src/app/tests/integration/chip_im_responder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ int main(int argc, char * argv[])
}

chip::app::InteractionModelEngine::GetInstance()->Shutdown();

gTransportManager.Close();
ShutdownChip();

return EXIT_SUCCESS;
Expand Down
2 changes: 2 additions & 0 deletions src/include/platform/CHIPDeviceEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ enum InternalEventTypes
kEventTypeNotSet = kRange_Internal,
kNoOp,
kCallWorkFunct,
kChipLambdaEvent,
kChipSystemLayerEvent,
kCHIPoBLESubscribe,
kCHIPoBLEUnsubscribe,
Expand Down Expand Up @@ -318,6 +319,7 @@ struct ChipDeviceEvent final
union
{
ChipDevicePlatformEvent Platform;
System::LambdaBridge LambdaEvent;
struct
{
::chip::System::EventType Type;
Expand Down
4 changes: 4 additions & 0 deletions src/include/platform/internal/GenericPlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,10 @@ void GenericPlatformManagerImpl<ImplClass>::_DispatchEvent(const ChipDeviceEvent
Impl()->DispatchEventToSystemLayer(event);
break;

case DeviceEventType::kChipLambdaEvent:
event->LambdaEvent.LambdaProxy(static_cast<const void *>(event->LambdaEvent.LambdaBody));
break;

case DeviceEventType::kCallWorkFunct:
// If the event is a "call work function" event, call the specified function.
event->CallWorkFunct.WorkFunct(event->CallWorkFunct.Arg);
Expand Down
17 changes: 16 additions & 1 deletion src/inet/DNSResolver.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <inet/IPAddress.h>
#include <inet/InetError.h>
#include <inet/InetLayerBasis.h>
#include <lib/core/ReferenceCounted.h>
#include <system/SystemPool.h>

#define NL_DNS_HOSTNAME_MAX_LEN (253)

Expand Down Expand Up @@ -61,6 +63,13 @@ enum DNSOptions
kDNSOption_Default = kDNSOption_AddrFamily_Any
};

class DNSResolver;
class DNSResolverDeletor
{
public:
static void Release(DNSResolver * obj);
};

/**
* @class DNSResolver
*
Expand All @@ -70,7 +79,7 @@ enum DNSOptions
* interface available for the application layer.
*
*/
class DNSResolver : public InetLayerBasis
class DNSResolver : public InetLayerBasis, public AtomicReferenceCounted<DNSResolver, DNSResolverDeletor>
{
private:
friend class InetLayer;
Expand Down Expand Up @@ -103,6 +112,7 @@ class DNSResolver : public InetLayerBasis
*/
typedef void (*OnResolveCompleteFunct)(void * appState, CHIP_ERROR err, uint8_t addrCount, IPAddress * addrArray);

friend class DNSResolverDeletor;
static chip::System::ObjectPool<DNSResolver, INET_CONFIG_NUM_DNS_RESOLVERS> sPool;

/**
Expand Down Expand Up @@ -168,5 +178,10 @@ class DNSResolver : public InetLayerBasis
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
};

inline void DNSResolverDeletor::Release(DNSResolver * obj)
{
DNSResolver::sPool.ReleaseObject(obj);
}

} // namespace Inet
} // namespace chip
14 changes: 0 additions & 14 deletions src/inet/EndPointBasis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,5 @@ 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(static_cast<System::LayerLwIP *>(Layer().SystemLayer()), aTactic);
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

} // namespace Inet
} // namespace chip
3 changes: 1 addition & 2 deletions src/inet/EndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#include <inet/InetError.h>
#include <inet/InetInterface.h>
#include <inet/InetLayerEvents.h>

#include <lib/core/ReferenceCounted.h>
#include <lib/support/DLLUtil.h>

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
Expand Down Expand Up @@ -124,7 +124,6 @@ class DLL_EXPORT EndPointBasis : public InetLayerBasis

uint8_t mLwIPEndPointType;

void DeferredFree(chip::System::Object::ReleaseDeferralErrorTactic aTactic);
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

void InitEndPointBasis(InetLayer & aInetLayer, void * aAppState = nullptr);
Expand Down
1 change: 1 addition & 0 deletions src/inet/IPEndPointBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#include <inet/EndPointBasis.h>

#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>

#if CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down
8 changes: 4 additions & 4 deletions src/inet/InetLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ CHIP_ERROR InetLayer::NewTCPEndPoint(TCPEndPoint ** retEndPoint)

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

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

VerifyOrReturnError(State == kState_Initialized, CHIP_ERROR_INCORRECT_STATE);

*retEndPoint = UDPEndPoint::sPool.TryCreate();
*retEndPoint = UDPEndPoint::sPool.CreateObject();
if (*retEndPoint == nullptr)
{
ChipLogError(Inet, "%s endpoint pool FULL", "UDP");
Expand Down Expand Up @@ -728,7 +728,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();
resolver = DNSResolver::sPool.CreateObject();
if (resolver != nullptr)
{
resolver->InitInetLayerBasis(*this);
Expand Down Expand Up @@ -760,7 +760,7 @@ CHIP_ERROR InetLayer::ResolveHostAddress(const char * hostName, uint16_t hostNam
onComplete(appState, err, (err == CHIP_NO_ERROR) ? 1 : 0, addrArray);
}

resolver->Release();
DNSResolver::sPool.ReleaseObject(resolver);
resolver = nullptr;

ExitNow(err = CHIP_NO_ERROR);
Expand Down
4 changes: 3 additions & 1 deletion src/inet/InetLayerBasis.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,14 @@ class InetLayer;
* InetLayer object.
*
*/
class InetLayerBasis : public chip::System::Object
class InetLayerBasis : public System::Object
{
public:
InetLayer & Layer() const;
bool IsCreatedByInetLayer(const InetLayer & aInetLayer) const;

void * AppState;

protected:
void InitInetLayerBasis(InetLayer & aInetLayer, void * aAppState = nullptr);

Expand Down
37 changes: 34 additions & 3 deletions src/inet/TCPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1197,12 +1197,43 @@ void TCPEndPoint::Free()
if (err != CHIP_NO_ERROR)
Abort();

// Release the Retain() that happened when the end point was allocated
// [on LwIP, the object may still be alive if DoClose() used the
// EndPointBasis::DeferredFree() method.]
// Release the Retain() that happened when the end point was allocated [on
// LwIP, the object may still be alive if DoClose() used the DeferredFree()
// method.]
Release();
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void TCPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
System::LayerLwIP * lSystemLayer = static_cast<System::LayerLwIP *>(Layer().SystemLayer());
CHIP_ERROR err = lSystemLayer->ScheduleLambda([this] { this->Release(); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
this->Release();
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}
else
{
Release();
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

#if INET_TCP_IDLE_CHECK_INTERVAL > 0
void TCPEndPoint::SetIdleTimeout(uint32_t timeoutMS)
{
Expand Down
26 changes: 24 additions & 2 deletions src/inet/TCPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@

#include <inet/EndPointBasis.h>
#include <inet/IPAddress.h>

#include <system/SystemLayer.h>
#include <system/SystemPacketBuffer.h>
#include <system/SystemPool.h>

#include <utility>

Expand All @@ -48,6 +49,13 @@ namespace Inet {

class InetLayer;

class TCPEndPoint;
class TCPEndPointDeletor
{
public:
static void Release(TCPEndPoint * obj);
};

/**
* @brief Objects of this class represent TCP transport endpoints.
*
Expand All @@ -56,7 +64,7 @@ class InetLayer;
* endpoints (SOCK_STREAM sockets on Linux and BSD-derived systems) or LwIP
* TCP protocol control blocks, as the system is configured accordingly.
*/
class DLL_EXPORT TCPEndPoint : public EndPointBasis
class DLL_EXPORT TCPEndPoint : public EndPointBasis, public AtomicReferenceCounted<TCPEndPoint, TCPEndPointDeletor>
{
friend class InetLayer;
friend class ::chip::Transport::TCPTest;
Expand Down Expand Up @@ -391,6 +399,14 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
*/
void Free();

enum ReleaseDeferralErrorTactic
{
kReleaseDeferralErrorTactic_Ignore, /**< No action. */
kReleaseDeferralErrorTactic_Release, /**< Release immediately. */
kReleaseDeferralErrorTactic_Die, /**< Die with message. */
};
void DeferredFree(ReleaseDeferralErrorTactic aTactic);

/**
* @brief Extract whether TCP connection is established.
*/
Expand Down Expand Up @@ -585,6 +601,7 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
constexpr static size_t kMaxReceiveMessageSize = System::PacketBuffer::kMaxSizeWithoutReserve;

private:
friend class TCPEndPointDeletor;
static chip::System::ObjectPool<TCPEndPoint, INET_CONFIG_NUM_TCP_ENDPOINTS> sPool;

chip::System::PacketBufferHandle mRcvQueue;
Expand Down Expand Up @@ -701,6 +718,11 @@ class DLL_EXPORT TCPEndPoint : public EndPointBasis
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS
};

inline void TCPEndPointDeletor::Release(TCPEndPoint * obj)
{
TCPEndPoint::sPool.ReleaseObject(obj);
}

#if INET_CONFIG_ENABLE_TCP_SEND_IDLE_CALLBACKS && INET_CONFIG_OVERRIDE_SYSTEM_TCP_USER_TIMEOUT
inline uint16_t TCPEndPoint::MaxTCPSendQueuePolls(void)
{
Expand Down
33 changes: 32 additions & 1 deletion src/inet/UDPEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -434,10 +434,41 @@ void UDPEndPoint::Free()
#if CHIP_SYSTEM_CONFIG_USE_LWIP
DeferredFree(kReleaseDeferralErrorTactic_Die);
#else // !CHIP_SYSTEM_CONFIG_USE_LWIP
Release();
UDPEndPoint::sPool.ReleaseObject(this);
#endif // !CHIP_SYSTEM_CONFIG_USE_LWIP
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP
void UDPEndPoint::DeferredFree(ReleaseDeferralErrorTactic aTactic)
{
if (!CHIP_SYSTEM_CONFIG_USE_SOCKETS || IsLWIPEndPoint())
{
System::LayerLwIP * lSystemLayer = static_cast<System::LayerLwIP *>(Layer().SystemLayer());
CHIP_ERROR err = lSystemLayer->ScheduleLambda([this] { UDPEndPoint::sPool.ReleaseObject(this); });
if (err != CHIP_NO_ERROR)
{
switch (aTactic)
{
case kReleaseDeferralErrorTactic_Ignore:
break;

case kReleaseDeferralErrorTactic_Release:
UDPEndPoint::sPool.ReleaseObject(this);
break;

case kReleaseDeferralErrorTactic_Die:
VerifyOrDie(false);
break;
}
}
}
else
{
UDPEndPoint::sPool.ReleaseObject(this);
}
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

/**
* A synonym for <tt>SendTo(addr, port, INET_NULL_INTERFACEID, msg, sendFlags)</tt>.
*/
Expand Down
10 changes: 9 additions & 1 deletion src/inet/UDPEndPoint.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@

#include "inet/IPEndPointBasis.h"
#include <inet/IPAddress.h>

#include <system/SystemPacketBuffer.h>
#include <system/SystemPool.h>

#if CHIP_SYSTEM_CONFIG_USE_DISPATCH
#include <dispatch/dispatch.h>
Expand Down Expand Up @@ -70,6 +70,14 @@ class DLL_EXPORT UDPEndPoint : public IPEndPointBasis
void Close();
void Free();

enum ReleaseDeferralErrorTactic
{
kReleaseDeferralErrorTactic_Ignore, /**< No action. */
kReleaseDeferralErrorTactic_Release, /**< Release immediately. */
kReleaseDeferralErrorTactic_Die, /**< Die with message. */
};
void DeferredFree(ReleaseDeferralErrorTactic aTactic);

private:
UDPEndPoint(const UDPEndPoint &) = delete;

Expand Down
Loading