Skip to content

Commit

Permalink
Use platform API to cancel thread during shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
yufengwangca committed Feb 16, 2022
1 parent b484373 commit 7b827a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
38 changes: 20 additions & 18 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,9 +208,6 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
{
CHIP_ERROR err;
struct sigaction action;
#if CHIP_WITH_GIO
GError * error = nullptr;
#endif

memset(&action, 0, sizeof(action));
action.sa_handler = SignalHandler;
Expand All @@ -221,6 +218,22 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()
sigaction(SIGUSR2, &action, NULL);
sigaction(SIGTSTP, &action, NULL);

#if CHIP_WITH_GIO
GError * error = nullptr;

this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));

std::thread gdbusThread(GDBus_Thread);
mGdbusThreadHandle = gdbusThread.native_handle();
gdbusThread.detach();
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
std::thread wifiIPThread(WiFIIPChangeListener);
mWifiIPThreadHandle = wifiIPThread.native_handle();
wifiIPThread.detach();
#endif

// Initialize the configuration system.
err = Internal::PosixConfig::Init();
SuccessOrExit(err);
Expand All @@ -234,15 +247,6 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack()

mStartTime = System::SystemClock().GetMonotonicTimestamp();

#if CHIP_WITH_GIO
this->mpGDBusConnection = UniqueGDBusConnection(g_bus_get_sync(G_BUS_TYPE_SYSTEM, nullptr, &error));
mGdbusThread = std::thread(GDBus_Thread);
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
mWifiIPThread = std::thread(WiFIIPChangeListener);
#endif

exit:
return err;
}
Expand Down Expand Up @@ -270,18 +274,16 @@ CHIP_ERROR PlatformManagerImpl::_Shutdown()
}

#if CHIP_WITH_GIO
if (mGdbusThread.joinable())
if (pthread_cancel(mGdbusThreadHandle) != 0)
{
pthread_cancel(mGdbusThread.native_handle());
mGdbusThread.join();
ChipLogError(DeviceLayer, "Failed to cancel gDBus thread");
}
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
if (mWifiIPThread.joinable())
if (pthread_cancel(mWifiIPThreadHandle) != 0)
{
pthread_cancel(mWifiIPThread.native_handle());
mWifiIPThread.join();
ChipLogError(DeviceLayer, "Failed to cancel WiFI IP Change Listener thread");
}
#endif

Expand Down
4 changes: 2 additions & 2 deletions src/platform/Linux/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,11 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
using UniqueGDBusConnection = std::unique_ptr<GDBusConnection, GDBusConnectionDeleter>;
UniqueGDBusConnection mpGDBusConnection;

std::thread mGdbusThread;
std::thread::native_handle_type mGdbusThreadHandle;
#endif

#if CHIP_DEVICE_CONFIG_ENABLE_WIFI
std::thread mWifiIPThread;
std::thread::native_handle_type mWifiIPThreadHandle;
#endif
};

Expand Down

0 comments on commit 7b827a8

Please sign in to comment.