Skip to content

Commit

Permalink
Switch to using a work scheduler instead of a stack lock for state up…
Browse files Browse the repository at this point in the history
…dates
  • Loading branch information
andy31415 committed Feb 16, 2023
1 parent 7a9f3b2 commit b59312a
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
23 changes: 20 additions & 3 deletions examples/common/imgui_ui/ui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <lib/support/logging/CHIPLogging.h>

#include <atomic>
#include <thread>

namespace example {
namespace Ui {
Expand Down Expand Up @@ -60,7 +61,7 @@ void UiInit(SDL_GLContext * gl_context, SDL_Window ** window)
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_WindowFlags window_flags = (SDL_WindowFlags) (SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
*window = SDL_CreateWindow("Light UI", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
*gl_context = SDL_GL_CreateContext(*window);
SDL_GL_MakeCurrent(*window, *gl_context);
Expand Down Expand Up @@ -189,10 +190,26 @@ void ImguiUi::Render()
}
}

void ImguiUi::ChipLoopUpdateCallback(intptr_t self)
{
ImguiUi * _this = reinterpret_cast<ImguiUi *>(self);
_this->ChipLoopStateUpdate();
sem_post(&_this->mChipLoopWaitSemaphore); // notify complete
}

void ImguiUi::UpdateState()
{
chip::DeviceLayer::StackLock lock;
ChipLoopStateUpdate();
chip::DeviceLayer::PlatformMgr().ScheduleWork(&ChipLoopUpdateCallback, reinterpret_cast<intptr_t>(this));
// ensure update is done when existing
if (sem_trywait(&mChipLoopWaitSemaphore) != 0)
{
if (!gUiRunning.load())
{
// UI should stop, no need to wait, probably chip main loop is stopped
return;
}
std::this_thread::yield();
}
}

} // namespace Ui
Expand Down
8 changes: 7 additions & 1 deletion examples/common/imgui_ui/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#include <AppMain.h>
#include <imgui_ui/windows/window.h>

#include <semaphore.h>

#include <list>
#include <memory>

Expand All @@ -33,7 +35,8 @@ namespace Ui {
class ImguiUi : public AppMainLoopImplementation
{
public:
virtual ~ImguiUi() = default;
ImguiUi() { sem_init(&mChipLoopWaitSemaphore, 0 /* shared */, 0); }
virtual ~ImguiUi() { sem_destroy(&mChipLoopWaitSemaphore); }

void AddWindow(std::unique_ptr<Window> window) { mWindows.push_back(std::move(window)); }

Expand All @@ -52,7 +55,10 @@ class ImguiUi : public AppMainLoopImplementation
// to CHIP API calls)
void ChipLoopStateUpdate();

sem_t mChipLoopWaitSemaphore;
std::list<std::unique_ptr<Window>> mWindows;

static void ChipLoopUpdateCallback(intptr_t self);
};

} // namespace Ui
Expand Down

0 comments on commit b59312a

Please sign in to comment.