From 22666738da8bb775683b531a55ac798ac02c1a26 Mon Sep 17 00:00:00 2001 From: Boris Zbarsky Date: Fri, 17 Feb 2023 09:39:19 -0500 Subject: [PATCH] Stop assuming a particular threading model when generating shutdown events. (#25139) We should be able to generate a shutdown event regardless of whether we're doing RunEventLoop or StartEventLoopTask. That means leaving the shutdown of the event loop to the API consumer, since those cases need to be handled somewhat differently. --- .../linux/AllClustersCommandDelegate.cpp | 3 ++- examples/lighting-app/linux/main.cpp | 2 ++ examples/platform/linux/AppMain.cpp | 3 ++- examples/platform/linux/AppMain.h | 12 ++++++++++-- src/app/server/Server.cpp | 12 +----------- src/app/server/Server.h | 8 ++++---- src/lib/shell/MainLoopMW320.cpp | 6 +++++- 7 files changed, 26 insertions(+), 20 deletions(-) diff --git a/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp b/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp index 2c2f5385c57ba4..af82296a5ecdbc 100644 --- a/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp +++ b/examples/all-clusters-app/linux/AllClustersCommandDelegate.cpp @@ -162,7 +162,8 @@ void AllClustersAppCommandHandler::OnRebootSignalHandler(BootReasonType bootReas { if (ConfigurationMgr().StoreBootReason(static_cast(bootReason)) != CHIP_NO_ERROR) { - Server::GetInstance().DispatchShutDownAndStopEventLoop(); + Server::GetInstance().GenerateShutDownEvent(); + PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); }); } else { diff --git a/examples/lighting-app/linux/main.cpp b/examples/lighting-app/linux/main.cpp index 9f04fdfb040cad..5af91faac1227f 100644 --- a/examples/lighting-app/linux/main.cpp +++ b/examples/lighting-app/linux/main.cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -107,6 +108,7 @@ void UiAppMainLoopImplementation::RunMainLoop() void UiAppMainLoopImplementation::SignalSafeStopMainLoop() { + Server::GetInstance().GenerateShutDownEvent(); example::Ui::StopEventLoop(); } diff --git a/examples/platform/linux/AppMain.cpp b/examples/platform/linux/AppMain.cpp index 1ad6c3212b84cc..ce107b340ee609 100644 --- a/examples/platform/linux/AppMain.cpp +++ b/examples/platform/linux/AppMain.cpp @@ -140,7 +140,8 @@ void StopSignalHandler(int signal) } else { - Server::GetInstance().DispatchShutDownAndStopEventLoop(); + Server::GetInstance().GenerateShutDownEvent(); + PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); }); } } #endif // !defined(ENABLE_CHIP_SHELL) diff --git a/examples/platform/linux/AppMain.h b/examples/platform/linux/AppMain.h index 368486b6767594..44554e3a8b7253 100644 --- a/examples/platform/linux/AppMain.h +++ b/examples/platform/linux/AppMain.h @@ -55,7 +55,11 @@ class AppMainLoopImplementation * Stop the above `RunMainLoop` function. * * Generally should contain at least a - * Server::GetInstance().DispatchShutDownAndStopEventLoop() + * + * Server::GetInstance().GenerateShutDownEvent() + * + * and then call StopEventLoopTask() in whatever way is appropriate for the + * way the event loop was started. */ virtual void SignalSafeStopMainLoop() = 0; }; @@ -64,7 +68,11 @@ class DefaultAppMainLoopImplementation : public AppMainLoopImplementation { public: void RunMainLoop() override { chip::DeviceLayer::PlatformMgr().RunEventLoop(); } - void SignalSafeStopMainLoop() override { chip::Server::GetInstance().DispatchShutDownAndStopEventLoop(); } + void SignalSafeStopMainLoop() override + { + chip::Server::GetInstance().GenerateShutDownEvent(); + chip::DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); }); + } }; /** diff --git a/src/app/server/Server.cpp b/src/app/server/Server.cpp index fa11ff0a71b044..e2e1824b008177 100644 --- a/src/app/server/Server.cpp +++ b/src/app/server/Server.cpp @@ -70,15 +70,6 @@ using chip::Transport::UdpListenParameters; namespace { -void StopEventLoop(intptr_t arg) -{ - CHIP_ERROR err = chip::DeviceLayer::PlatformMgr().StopEventLoopTask(); - if (err != CHIP_NO_ERROR) - { - ChipLogError(AppServer, "Stopping event loop: %" CHIP_ERROR_FORMAT, err.Format()); - } -} - class DeviceTypeResolver : public chip::Access::AccessControl::DeviceTypeResolver { public: @@ -451,10 +442,9 @@ void Server::RejoinExistingMulticastGroups() } } -void Server::DispatchShutDownAndStopEventLoop() +void Server::GenerateShutDownEvent() { PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().HandleServerShuttingDown(); }); - PlatformMgr().ScheduleWork(StopEventLoop); } void Server::ScheduleFactoryReset() diff --git a/src/app/server/Server.h b/src/app/server/Server.h index e1a8a6cd9847a5..45bcf0a7290515 100644 --- a/src/app/server/Server.h +++ b/src/app/server/Server.h @@ -312,7 +312,7 @@ struct CommonCaseDeviceServerInitParams : public ServerInitParams * after `Server::Shutdown()` is called. * * TODO: Separate lifecycle ownership for some more capabilities that should not belong to - * common logic, such as `DispatchShutDownAndStopEventLoop`. + * common logic, such as `GenerateShutDownEvent`. * * TODO: Replace all uses of GetInstance() to "reach in" to this state from all cluster * server common logic that deal with global node state with either a common NodeState @@ -370,10 +370,10 @@ class Server app::DefaultAttributePersistenceProvider & GetDefaultAttributePersister() { return mAttributePersister; } /** - * This function send the ShutDown event before stopping - * the event loop. + * This function causes the ShutDown event to be generated async on the + * Matter event loop. Should be called before stopping the event loop. */ - void DispatchShutDownAndStopEventLoop(); + void GenerateShutDownEvent(); void Shutdown(); diff --git a/src/lib/shell/MainLoopMW320.cpp b/src/lib/shell/MainLoopMW320.cpp index 7dd9a987f013b6..ea7ca5de0a70df 100644 --- a/src/lib/shell/MainLoopMW320.cpp +++ b/src/lib/shell/MainLoopMW320.cpp @@ -161,7 +161,11 @@ static void AtExitShell(void); static CHIP_ERROR ShutdownHandler(int argc, char ** argv) { streamer_printf(streamer_get(), "Shutdown and Goodbye\r\n"); - chip::Server::GetInstance().DispatchShutDownAndStopEventLoop(); + Server::GetInstance().GenerateShutDownEvent(); + // TODO: This is assuming that we did (on a different thread from this one) + // RunEventLoop(), not StartEventLoopTask(). It will not work correctly + // with StartEventLoopTask(). + DeviceLayer::PlatformMgr().ScheduleWork([](intptr_t) { DeviceLayer::PlatformMgr().StopEventLoopTask(); }); AtExitShell(); exit(0); return CHIP_NO_ERROR;