Skip to content

Commit

Permalink
Add macro for the RunWithMatterContextLock function
Browse files Browse the repository at this point in the history
  • Loading branch information
wqx6 committed Jan 10, 2025
1 parent a331147 commit b14b2c7
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
18 changes: 12 additions & 6 deletions src/inet/TCPEndPointImplLwIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -788,9 +788,9 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p

if (arg != nullptr)
{
TCPEndPointImplLwIP * listenEP = static_cast<TCPEndPointImplLwIP *>(arg);
TCPEndPointImplLwIP * conEP = nullptr;
System::LayerFreeRTOS & lSystemLayer = static_cast<System::LayerFreeRTOS &>(listenEP->GetSystemLayer());
TCPEndPointImplLwIP * listenEP = static_cast<TCPEndPointImplLwIP *>(arg);
TCPEndPointImplLwIP * conEP = nullptr;
System::Layer & lSystemLayer = listenEP->GetSystemLayer();

// Tell LwIP we've accepted the connection so it can decrement the listen PCB's pending_accepts counter.
tcp_accepted(listenEP->mTCP);
Expand All @@ -805,8 +805,14 @@ err_t TCPEndPointImplLwIP::LwIPHandleIncomingConnection(void * arg, struct tcp_p
if (err == CHIP_NO_ERROR)
{
TCPEndPoint * connectEndPoint = nullptr;
err = lSystemLayer.RunOnMatterContext(
#if CHIP_SYSTEM_CONFIG_NO_LOCKING
// TODO This should be in Matter context but we cannot use SystemLayer.ScheduleLambda() here as the allocated endpoint
// will be used in the following.
err = listenEP->GetEndPointManager().NewEndPoint(&connectEndPoint);
#else
err = lSystemLayer.RunWithMatterContextLock(
[&listenEP, &connectEndPoint]() { return listenEP->GetEndPointManager().NewEndPoint(&connectEndPoint); });
#endif
conEP = static_cast<TCPEndPointImplLwIP *>(connectEndPoint);
}

Expand Down Expand Up @@ -954,8 +960,8 @@ void TCPEndPointImplLwIP::LwIPHandleError(void * arg, err_t lwipErr)
{
if (arg != nullptr)
{
TCPEndPointImplLwIP * ep = static_cast<TCPEndPointImplLwIP *>(arg);
System::LayerFreeRTOS & lSystemLayer = static_cast<System::LayerFreeRTOS &>(ep->GetSystemLayer());
TCPEndPointImplLwIP * ep = static_cast<TCPEndPointImplLwIP *>(arg);
System::Layer & lSystemLayer = ep->GetSystemLayer();

// At this point LwIP has already freed the PCB. Since the thread that owns the TCPEndPoint may
// try to use the PCB before it receives the TCPError event posted below, we set the PCB to NULL
Expand Down
8 changes: 4 additions & 4 deletions src/platform/PlatformEventSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,17 @@ CHIP_ERROR PlatformEventing::StartTimer(System::Layer & aLayer, System::Clock::T
return PlatformMgr().StartChipTimer(delay);
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
void PlatformEventing::LockMatterStack(System::Layer & aLayer)
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
void PlatformEventing::LockMatterStack()
{
PlatformMgr().LockChipStack();
}

void PlatformEventing::UnlockMatterStack(System::Layer & aLayer)
void PlatformEventing::UnlockMatterStack()
{
PlatformMgr().UnlockChipStack();
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING

} // namespace System
} // namespace chip
6 changes: 3 additions & 3 deletions src/system/PlatformEventSupport.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class PlatformEventing
public:
static CHIP_ERROR ScheduleLambdaBridge(System::Layer & aLayer, LambdaBridge && bridge);
static CHIP_ERROR StartTimer(System::Layer & aLayer, System::Clock::Timeout aTimeout);
#if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
static void LockMatterStack(System::Layer & aLayer);
static void UnlockMatterStack(System::Layer & aLayer);
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
static void LockMatterStack();
static void UnlockMatterStack();
#endif
};

Expand Down
10 changes: 5 additions & 5 deletions src/system/SystemLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ CHIP_ERROR Layer::ScheduleLambdaBridge(LambdaBridge && bridge)
return lReturn;
}

#if CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
CHIP_ERROR LayerFreeRTOS::RunOnMatterContext(std::function<CHIP_ERROR()> func)
#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
CHIP_ERROR Layer::RunWithMatterContextLock(std::function<CHIP_ERROR()> func)
{
CHIP_ERROR err = CHIP_NO_ERROR;
PlatformEventing::LockMatterStack(*this);
PlatformEventing::LockMatterStack();
err = func();
PlatformEventing::UnlockMatterStack(*this);
PlatformEventing::UnlockMatterStack();
return err;
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP || CHIP_SYSTEM_CONFIG_USE_OPEN_THREAD_ENDPOINT
#endif // !CHIP_SYSTEM_CONFIG_NO_LOCKING

} // namespace System
} // namespace chip
6 changes: 4 additions & 2 deletions src/system/SystemLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ class DLL_EXPORT Layer
return ScheduleLambdaBridge(std::move(bridge));
}

#if !CHIP_SYSTEM_CONFIG_NO_LOCKING
CHIP_ERROR RunWithMatterContextLock(std::function<CHIP_ERROR()>);
#endif

private:
CHIP_ERROR ScheduleLambdaBridge(LambdaBridge && bridge);

Expand All @@ -232,8 +236,6 @@ class DLL_EXPORT Layer

class LayerFreeRTOS : public Layer
{
public:
CHIP_ERROR RunOnMatterContext(std::function<CHIP_ERROR()>);
};

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
Expand Down

0 comments on commit b14b2c7

Please sign in to comment.