Skip to content

Commit

Permalink
Stop assuming a particular threading model when generating shutdown e…
Browse files Browse the repository at this point in the history
…vents.

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.
  • Loading branch information
bzbarsky-apple committed Feb 17, 2023
1 parent 50ed10e commit dd9019f
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,8 @@ void AllClustersAppCommandHandler::OnRebootSignalHandler(BootReasonType bootReas
{
if (ConfigurationMgr().StoreBootReason(static_cast<uint32_t>(bootReason)) != CHIP_NO_ERROR)
{
Server::GetInstance().DispatchShutDownAndStopEventLoop();
Server::GetInstance().GenerateShutDownEvent();
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); });
}
else
{
Expand Down
2 changes: 2 additions & 0 deletions examples/lighting-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <app-common/zap-generated/ids/Clusters.h>
#include <app/ConcreteAttributePath.h>
#include <app/clusters/network-commissioning/network-commissioning.h>
#include <app/server/Server.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/Linux/NetworkCommissioningDriver.h>

Expand Down Expand Up @@ -107,6 +108,7 @@ void UiAppMainLoopImplementation::RunMainLoop()

void UiAppMainLoopImplementation::SignalSafeStopMainLoop()
{
Server::GetInstance().GenerateShutDownEvent();
example::Ui::StopEventLoop();
}

Expand Down
3 changes: 2 additions & 1 deletion examples/platform/linux/AppMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ void StopSignalHandler(int signal)
}
else
{
Server::GetInstance().DispatchShutDownAndStopEventLoop();
Server::GetInstance().GenerateShutDownEvent();
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().StopEventLoopTask(); });
}
}
#endif // !defined(ENABLE_CHIP_SHELL)
Expand Down
12 changes: 10 additions & 2 deletions examples/platform/linux/AppMain.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand All @@ -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(); });
}
};

/**
Expand Down
12 changes: 1 addition & 11 deletions src/app/server/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -449,10 +440,9 @@ void Server::RejoinExistingMulticastGroups()
}
}

void Server::DispatchShutDownAndStopEventLoop()
void Server::GenerateShutDownEvent()
{
PlatformMgr().ScheduleWork([](intptr_t) { PlatformMgr().HandleServerShuttingDown(); });
PlatformMgr().ScheduleWork(StopEventLoop);
}

void Server::ScheduleFactoryReset()
Expand Down
8 changes: 4 additions & 4 deletions src/app/server/Server.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,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
Expand Down Expand Up @@ -361,10 +361,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();

Expand Down
6 changes: 5 additions & 1 deletion src/lib/shell/MainLoopMW320.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit dd9019f

Please sign in to comment.