Skip to content

Commit

Permalink
Fix ScheduleLambda and Add unit-tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kghost committed Nov 4, 2021
1 parent 7440624 commit 6ea8928
Show file tree
Hide file tree
Showing 18 changed files with 200 additions and 34 deletions.
8 changes: 6 additions & 2 deletions src/include/platform/PlatformManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ class PlatformManager
* before calling Shutdown.
*/
void RunEventLoop();
void ProcessDeviceEvents();

/**
* Process work items until StopEventLoopTask is called.
Expand Down Expand Up @@ -222,9 +223,7 @@ class PlatformManager
friend class Internal::GenericThreadStackManagerImpl_OpenThread_LwIP;
template <class>
friend class Internal::GenericConfigurationManagerImpl;
#if CHIP_SYSTEM_CONFIG_USE_LWIP
friend class System::PlatformEventing;
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP

/*
* PostEvent can be called safely on any thread without locking the stack.
Expand Down Expand Up @@ -351,6 +350,11 @@ inline void PlatformManager::RunEventLoop()
static_cast<ImplClass *>(this)->_RunEventLoop();
}

inline void PlatformManager::ProcessDeviceEvents()
{
static_cast<ImplClass *>(this)->_ProcessDeviceEvents();
}

/**
* @brief
* Starts the stack on its own task with an associated event queue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,18 @@ void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_RunEventLoop(void)
}
}

template <class ImplClass>
void GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_ProcessDeviceEvents()
{
ChipDeviceEvent event;
BaseType_t eventReceived = xQueueReceive(mChipEventQueue, &event, 0);
while (eventReceived == pdTRUE)
{
Impl()->DispatchEvent(&event);
eventReceived = xQueueReceive(mChipEventQueue, &event, 0);
}
}

template <class ImplClass>
CHIP_ERROR GenericPlatformManagerImpl_FreeRTOS<ImplClass>::_StartEventLoopTask(void)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class GenericPlatformManagerImpl_FreeRTOS : public GenericPlatformManagerImpl<Im
void _UnlockChipStack(void);
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
void _RunEventLoop(void);
void _ProcessDeviceEvents();
CHIP_ERROR _StartEventLoopTask(void);
CHIP_ERROR _StopEventLoopTask();
CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ CHIP_ERROR GenericPlatformManagerImpl_POSIX<ImplClass>::_PostEvent(const ChipDev
}

template <class ImplClass>
void GenericPlatformManagerImpl_POSIX<ImplClass>::ProcessDeviceEvents()
void GenericPlatformManagerImpl_POSIX<ImplClass>::_ProcessDeviceEvents()
{
while (!mChipEventQueue.Empty())
{
Expand Down Expand Up @@ -180,7 +180,7 @@ void GenericPlatformManagerImpl_POSIX<ImplClass>::_RunEventLoop()

SystemLayerSocketsLoop().HandleEvents();

ProcessDeviceEvents();
_ProcessDeviceEvents();
} while (mShouldRunEventLoop.load(std::memory_order_relaxed));
SystemLayerSocketsLoop().EventLoopEnds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl<ImplC
void _UnlockChipStack();
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
void _RunEventLoop();
void _ProcessDeviceEvents();

CHIP_ERROR _StartEventLoopTask();
CHIP_ERROR _StopEventLoopTask();
CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration);
Expand All @@ -107,8 +109,6 @@ class GenericPlatformManagerImpl_POSIX : public GenericPlatformManagerImpl<ImplC

inline ImplClass * Impl() { return static_cast<ImplClass *>(this); }

void ProcessDeviceEvents();

DeviceSafeQueue mChipEventQueue;
std::atomic<bool> mShouldRunEventLoop;
static void * EventLoopTaskMain(void * arg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ CHIP_ERROR GenericPlatformManagerImpl_Zephyr<ImplClass>::_PostEvent(const ChipDe
}

template <class ImplClass>
void GenericPlatformManagerImpl_Zephyr<ImplClass>::ProcessDeviceEvents()
void GenericPlatformManagerImpl_Zephyr<ImplClass>::_ProcessDeviceEvents()
{
ChipDeviceEvent event;

Expand All @@ -148,7 +148,7 @@ void GenericPlatformManagerImpl_Zephyr<ImplClass>::_RunEventLoop(void)

SystemLayerSocketsLoop().HandleEvents();

ProcessDeviceEvents();
_ProcessDeviceEvents();
}
SystemLayerSocketsLoop().EventLoopEnds();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class GenericPlatformManagerImpl_Zephyr : public GenericPlatformManagerImpl<Impl
void _UnlockChipStack(void);
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
void _RunEventLoop(void);
void _ProcessDeviceEvents();
CHIP_ERROR _StartEventLoopTask(void);
CHIP_ERROR _StopEventLoopTask();
CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration);
Expand All @@ -89,7 +90,6 @@ class GenericPlatformManagerImpl_Zephyr : public GenericPlatformManagerImpl<Impl
ImplClass * Impl() { return static_cast<ImplClass *>(this); }
void SysUpdate();
void SysProcess();
void ProcessDeviceEvents();

static void EventLoopTaskMain(void * thisPtr, void *, void *);
};
Expand Down
12 changes: 6 additions & 6 deletions src/lib/support/LambdaBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#pragma once

#include <string.h>
#include <type_traits>

#include <lib/core/CHIPConfig.h>

Expand All @@ -35,17 +36,16 @@ class LambdaBridge
static_assert(CHIP_CONFIG_LAMBDA_EVENT_ALIGN % alignof(Lambda) == 0, "lambda align too large");

// Implicit cast a capture-less lambda into a raw function pointer.
mLambdaProxy = [](const std::aligned_storage<CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN> & body) {
(*reinterpret_cast<const Lambda *>(&body))();
};
memcpy(&mLambdaBody, &lambda, sizeof(Lambda));
mLambdaProxy = [](const LambdaStorage & body) { (*reinterpret_cast<const Lambda *>(&body))(); };
::memcpy(&mLambdaBody, &lambda, sizeof(Lambda));
}

void operator()() const { mLambdaProxy(mLambdaBody); }

private:
void (*mLambdaProxy)(const std::aligned_storage<CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN> & body);
std::aligned_storage<CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN> mLambdaBody;
using LambdaStorage = std::aligned_storage<CHIP_CONFIG_LAMBDA_EVENT_SIZE, CHIP_CONFIG_LAMBDA_EVENT_ALIGN>::type;
void (*mLambdaProxy)(const LambdaStorage & body);
LambdaStorage mLambdaBody;
};

static_assert(std::is_trivial<LambdaBridge>::value, "LambdaBridge is not trivial");
Expand Down
5 changes: 5 additions & 0 deletions src/platform/Darwin/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ void PlatformManagerImpl::_RunEventLoop()
dispatch_semaphore_wait(mRunLoopSem, DISPATCH_TIME_FOREVER);
}

void PlatformManagerImpl::_ProcessDeviceEvents()
{
// Not implemented
}

CHIP_ERROR PlatformManagerImpl::_Shutdown()
{
// Call up to the base class _Shutdown() to perform the bulk of the shutdown.
Expand Down
1 change: 1 addition & 0 deletions src/platform/Darwin/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
CHIP_ERROR _StartEventLoopTask();
CHIP_ERROR _StopEventLoopTask();
void _RunEventLoop();
void _ProcessDeviceEvents();
void _LockChipStack(){};
bool _TryLockChipStack() { return false; };
void _UnlockChipStack(){};
Expand Down
4 changes: 0 additions & 4 deletions src/platform/PlatformEventSupport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@

#include <platform/PlatformManager.h>

#if CHIP_SYSTEM_CONFIG_USE_LWIP

namespace chip {
namespace System {

Expand Down Expand Up @@ -62,5 +60,3 @@ CHIP_ERROR PlatformEventing::StartTimer(System::Layer & aLayer, System::Clock::T

} // namespace System
} // namespace chip

#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
56 changes: 51 additions & 5 deletions src/platform/fake/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include <platform/PlatformManager.h>

#include <queue>

namespace chip {
namespace DeviceLayer {

Expand All @@ -43,21 +45,62 @@ class PlatformManagerImpl final : public PlatformManager
// ===== Methods that implement the PlatformManager abstract interface.

CHIP_ERROR _InitChipStack() { return CHIP_NO_ERROR; }
CHIP_ERROR _Shutdown() { return CHIP_NO_ERROR; }

CHIP_ERROR _AddEventHandler(EventHandlerFunct handler, intptr_t arg = 0) { return CHIP_ERROR_NOT_IMPLEMENTED; }
void _RemoveEventHandler(EventHandlerFunct handler, intptr_t arg = 0) {}
void _ScheduleWork(AsyncWorkFunct workFunct, intptr_t arg = 0) {}
void _RunEventLoop() {}

void _RunEventLoop()
{
do
{
_ProcessDeviceEvents();
} while (mShouldRunEventLoop);
}

void _ProcessDeviceEvents()
{
while (!mQueue.empty())
{
const ChipDeviceEvent & event = mQueue.front();
_DispatchEvent(&event);
mQueue.pop();
}
}

CHIP_ERROR _StartEventLoopTask() { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR _StopEventLoopTask() { return CHIP_ERROR_NOT_IMPLEMENTED; }
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event) { return CHIP_NO_ERROR; }
void _DispatchEvent(const ChipDeviceEvent * event) {}

CHIP_ERROR _StopEventLoopTask()
{
mShouldRunEventLoop = false;
return CHIP_NO_ERROR;
}

CHIP_ERROR _PostEvent(const ChipDeviceEvent * event)
{
mQueue.emplace(*event);
return CHIP_NO_ERROR;
}

void _DispatchEvent(const ChipDeviceEvent * event)
{
switch (event->Type)
{
case DeviceEventType::kChipLambdaEvent:
event->LambdaEvent();
break;

default:
break;
}
}

CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration) { return CHIP_ERROR_NOT_IMPLEMENTED; }

void _LockChipStack() {}
bool _TryLockChipStack() { return true; }
void _UnlockChipStack() {}
CHIP_ERROR _Shutdown() { return CHIP_ERROR_NOT_IMPLEMENTED; }

CHIP_ERROR _GetCurrentHeapFree(uint64_t & currentHeapFree);
CHIP_ERROR _GetCurrentHeapUsed(uint64_t & currentHeapUsed);
Expand All @@ -75,6 +118,9 @@ class PlatformManagerImpl final : public PlatformManager
friend class Internal::BLEManagerImpl;

static PlatformManagerImpl sInstance;

bool mShouldRunEventLoop = true;
std::queue<ChipDeviceEvent> mQueue;
};

/**
Expand Down
4 changes: 2 additions & 2 deletions src/platform/mbed/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ CHIP_ERROR PlatformManagerImpl::_PostEvent(const ChipDeviceEvent * eventPtr)
return CHIP_NO_ERROR;
}

void PlatformManagerImpl::ProcessDeviceEvents()
void PlatformManagerImpl::_ProcessDeviceEvents()
{
mQueue.dispatch(0);
}
Expand Down Expand Up @@ -178,7 +178,7 @@ void PlatformManagerImpl::_RunEventLoop()

SystemLayerSocketsLoop().HandleEvents();

ProcessDeviceEvents();
_ProcessDeviceEvents();
}
SystemLayerSocketsLoop().EventLoopEnds();

Expand Down
3 changes: 1 addition & 2 deletions src/platform/mbed/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,12 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
void _UnlockChipStack();
CHIP_ERROR _PostEvent(const ChipDeviceEvent * event);
void _RunEventLoop();
void _ProcessDeviceEvents();
CHIP_ERROR _StartEventLoopTask();
CHIP_ERROR _StopEventLoopTask();
CHIP_ERROR _StartChipTimer(System::Clock::Timeout duration);
CHIP_ERROR _Shutdown();

void ProcessDeviceEvents();

// ===== Members for internal use by the following friends.

friend PlatformManager & PlatformMgr(void);
Expand Down
5 changes: 1 addition & 4 deletions src/system/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ static_library("system") {
output_name = "libSystemLayer"

sources = [
"PlatformEventSupport.h",
"SystemAlignSize.h",
"SystemClock.cpp",
"SystemClock.h",
Expand Down Expand Up @@ -170,10 +171,6 @@ static_library("system") {
}
}

if (chip_system_config_use_lwip) {
sources += [ "PlatformEventSupport.h" ]
}

if (chip_with_nlfaultinjection) {
sources += [ "SystemFaultInjection.cpp" ]
public_deps += [ "${nlfaultinjection_root}:nlfaultinjection" ]
Expand Down
2 changes: 0 additions & 2 deletions src/system/SystemLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@

#pragma once

#include <type_traits>

// Include configuration headers
#include <system/SystemConfig.h>

Expand Down
1 change: 1 addition & 0 deletions src/system/tests/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ chip_test_suite("tests") {
"TestSystemErrorStr.cpp",
"TestSystemObject.cpp",
"TestSystemPacketBuffer.cpp",
"TestSystemScheduleLambda.cpp",
"TestSystemTimer.cpp",
"TestSystemWakeEvent.cpp",
"TestTimeSource.cpp",
Expand Down
Loading

0 comments on commit 6ea8928

Please sign in to comment.