Skip to content

Commit

Permalink
Explicitly wait for glib main thread to exit
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Oct 26, 2022
1 parent 8b84485 commit 3ac0239
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 23 deletions.
12 changes: 6 additions & 6 deletions src/platform/Linux/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -281,16 +280,17 @@ void PlatformManagerImpl::_Shutdown()
Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_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
}

#if CHIP_DEVICE_CONFIG_WITH_GLIB_MAIN_LOOP && CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
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));

Expand Down
8 changes: 2 additions & 6 deletions src/platform/Linux/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GMainLoop, GLibMainLoopDeleter>;
UniqueGLibMainLoop mGLibMainLoop;
GMainLoop * mGLibMainLoop;
GThread * mGLibMainLoopThread;
#endif
};

Expand Down
10 changes: 5 additions & 5 deletions src/platform/webos/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -194,8 +193,9 @@ void PlatformManagerImpl::_Shutdown()
Internal::GenericPlatformManagerImpl_POSIX<PlatformManagerImpl>::_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
}

Expand Down
8 changes: 2 additions & 6 deletions src/platform/webos/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<GMainLoop, GLibMainLoopDeleter>;
UniqueGLibMainLoop mGLibMainLoop;
GMainLoop * mGLibMainLoop;
GThread * mGLibMainLoopThread;
#endif
};

Expand Down

0 comments on commit 3ac0239

Please sign in to comment.