diff --git a/src/platform/Linux/PlatformManagerImpl.cpp b/src/platform/Linux/PlatformManagerImpl.cpp index 8aed077cf94d51..db52ba4cc96a07 100644 --- a/src/platform/Linux/PlatformManagerImpl.cpp +++ b/src/platform/Linux/PlatformManagerImpl.cpp @@ -226,9 +226,8 @@ CHIP_ERROR PlatformManagerImpl::_InitChipStack() { #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - mGLibMainLoop.reset(g_main_loop_new(nullptr, FALSE)); - GThread * thread = g_thread_new("gmain-matter", GLibMainLoopThread, mGLibMainLoop.get()); - g_thread_unref(thread); // detach the thread + mGLibMainLoop = g_main_loop_new(nullptr, FALSE); + mGLibMainLoopThread = g_thread_new("gmain-matter", GLibMainLoopThread, mGLibMainLoop); CallbackIndirection startedInd([](void *) { return G_SOURCE_REMOVE; }, nullptr); g_idle_add(G_SOURCE_FUNC(&CallbackIndirection::Callback), &startedInd); @@ -281,8 +280,9 @@ void PlatformManagerImpl::_Shutdown() Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - g_main_loop_quit(mGLibMainLoop.get()); - mGLibMainLoop.reset(); + g_main_loop_quit(mGLibMainLoop); + g_main_loop_unref(mGLibMainLoop); + g_thread_join(mGLibMainLoopThread); #endif } @@ -290,7 +290,7 @@ void PlatformManagerImpl::_Shutdown() CHIP_ERROR PlatformManagerImpl::RunOnGLibMainLoopThread(GSourceFunc callback, void * userData, bool wait) { - GMainContext * context = g_main_loop_get_context(mGLibMainLoop.get()); + GMainContext * context = g_main_loop_get_context(mGLibMainLoop); VerifyOrReturnError(context != nullptr, (ChipLogDetail(DeviceLayer, "Failed to get GLib main loop context"), CHIP_ERROR_INTERNAL)); diff --git a/src/platform/Linux/PlatformManagerImpl.h b/src/platform/Linux/PlatformManagerImpl.h index 82b6ace80c48e4..72925c7137ffc1 100644 --- a/src/platform/Linux/PlatformManagerImpl.h +++ b/src/platform/Linux/PlatformManagerImpl.h @@ -93,12 +93,8 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener static PlatformManagerImpl sInstance; #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - struct GLibMainLoopDeleter - { - void operator()(GMainLoop * loop) { g_main_loop_unref(loop); } - }; - using UniqueGLibMainLoop = std::unique_ptr; - UniqueGLibMainLoop mGLibMainLoop; + GMainLoop * mGLibMainLoop; + GThread * mGLibMainLoopThread; #endif }; diff --git a/src/platform/webos/PlatformManagerImpl.cpp b/src/platform/webos/PlatformManagerImpl.cpp index 0e66bae5bce41b..fb353702ad622f 100644 --- a/src/platform/webos/PlatformManagerImpl.cpp +++ b/src/platform/webos/PlatformManagerImpl.cpp @@ -143,9 +143,8 @@ void PlatformManagerImpl::WiFiIPChangeListener() CHIP_ERROR PlatformManagerImpl::_InitChipStack() { #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - mGLibMainLoop.reset(g_main_loop_new(nullptr, FALSE)); - GThread * thread = g_thread_new("gmain-matter", GLibMainLoopThread, mGLibMainLoop.get()); - g_thread_unref(thread); // detach the thread + mGLibMainLoop = g_main_loop_new(nullptr, FALSE); + mGLibMainLoopThread = g_thread_new("gmain-matter", GLibMainLoopThread, mGLibMainLoop); #endif #if CHIP_DEVICE_CONFIG_ENABLE_WIFI @@ -194,8 +193,9 @@ void PlatformManagerImpl::_Shutdown() Internal::GenericPlatformManagerImpl_POSIX::_Shutdown(); #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - g_main_loop_quit(mGLibMainLoop.get()); - mGLibMainLoop.reset(); + g_main_loop_quit(mGLibMainLoop); + g_main_loop_unref(mGLibMainLoop); + g_thread_join(mGLibMainLoopThread); #endif } diff --git a/src/platform/webos/PlatformManagerImpl.h b/src/platform/webos/PlatformManagerImpl.h index ac4f7c2ae3eee9..a46b1068345dc5 100644 --- a/src/platform/webos/PlatformManagerImpl.h +++ b/src/platform/webos/PlatformManagerImpl.h @@ -76,12 +76,8 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener #endif #if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP - struct GLibMainLoopDeleter - { - void operator()(GMainLoop * loop) { g_main_loop_unref(loop); } - }; - using UniqueGLibMainLoop = std::unique_ptr; - UniqueGLibMainLoop mGLibMainLoop; + GMainLoop * mGLibMainLoop; + GThread * mGLibMainLoopThread; #endif };