From 7b827a89a9de3333424c3e66220b53f22014173d Mon Sep 17 00:00:00 2001 From: Yufeng Wang Date: Tue, 15 Feb 2022 18:27:13 -0800 Subject: [PATCH] Use platform API to cancel thread during shutdown --- src/platform/Linux/PlatformManagerImpl.cpp | 38 ++++++++++++---------- src/platform/Linux/PlatformManagerImpl.h | 4 +-- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index a505a03a522bbf..42b8d4dff19dc4 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -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; @@ -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); @@ -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; } @@ -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 diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index 712e7b5b6911a9..15b001ac46a518 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -100,11 +100,11 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener using UniqueGDBusConnection = std::unique_ptr; 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 };