Skip to content

Commit 33d7caf

Browse files
committed
GPU Display Frontend: Use condition_variable to signal stop and config updates instead of busy loop / sleep
1 parent fc5cf3f commit 33d7caf

9 files changed

Lines changed: 56 additions & 17 deletions

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontend.cxx‎

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ GPUDisplayFrontend* createGPUDisplayFrontendWayland();
4444
#include <exception>
4545
#include <algorithm>
4646
#include <iterator>
47-
#include <thread>
4847
#include <utility>
4948

5049
#ifdef GPUCA_BUILD_EVENT_DISPLAY_QT
@@ -87,16 +86,30 @@ void GPUDisplayFrontend::waitForFPSLimit()
8786
mNextFrameTime = now;
8887
return;
8988
}
90-
const auto period = std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(1. / cfg.fpsRate)); // TODO: wake up on HasExternalGUISignals
89+
const auto period = std::chrono::duration_cast<std::chrono::steady_clock::duration>(std::chrono::duration<double>(1. / cfg.fpsRate));
9190
mNextFrameTime += period;
9291
if (mNextFrameTime < now - period) { // More than one frame behind (slow rendering, first frame), do not try to catch up
9392
mNextFrameTime = now;
9493
}
95-
std::this_thread::sleep_until(mNextFrameTime);
94+
std::unique_lock lock(mConfigUpdateMutex);
95+
mConfigUpdateCV.wait_until(lock, mNextFrameTime, [this] { return mConfigUpdatePending; });
96+
}
97+
98+
void GPUDisplayFrontend::signalConfigUpdate()
99+
{
100+
{
101+
std::lock_guard lock(mConfigUpdateMutex);
102+
mConfigUpdatePending = true;
103+
}
104+
mConfigUpdateCV.notify_one();
96105
}
97106

98107
void GPUDisplayFrontend::HandleExternalGUISignals()
99108
{
109+
{
110+
std::lock_guard lock(mConfigUpdateMutex);
111+
mConfigUpdatePending = false; // Before fetching the updates, so that updates sent in the meantime signal again
112+
}
100113
HandleExternalGUIKeys();
101114

102115
const bool gui = isGUIRunning();
@@ -131,8 +144,11 @@ void GPUDisplayFrontend::HandleExternalGUISignals()
131144

132145
void GPUDisplayFrontend::sendExternalGUIKey(int32_t v)
133146
{
134-
std::lock_guard lock(mExternalGUIKeyMutex);
135-
mExternalGUIKeys.emplace_back(v);
147+
{
148+
std::lock_guard lock(mExternalGUIKeyMutex);
149+
mExternalGUIKeys.emplace_back(v);
150+
}
151+
signalConfigUpdate();
136152
}
137153

138154
int32_t GPUDisplayFrontend::getDisplayControl() const { return mDisplay->mCfgC.mDisplayControl.load(); }
@@ -177,6 +193,7 @@ int32_t GPUDisplayFrontend::startGUI()
177193
#ifdef GPUCA_BUILD_EVENT_DISPLAY_QT
178194
if (!mGUI) {
179195
mGUIChannel = std::make_unique<GPUDisplayGUIChannel>();
196+
mGUIChannel->updatePosted = [this]() { signalConfigUpdate(); };
180197
mGUI = std::make_unique<GPUDisplayGUIWrapper>(*mGUIChannel);
181198
}
182199
if (!mGUI->isRunning()) {
@@ -319,17 +336,13 @@ int32_t GPUDisplayFrontend::StartDisplay()
319336

320337
void GPUDisplayFrontend::StopDisplay()
321338
{
322-
{
323-
std::lock_guard lock(mExitStateMutex);
324-
if (mDisplayRunning) {
325-
mDisplay->mCfgC.mDisplayControl.store(2);
326-
StopDisplayA();
327-
mDisplay->UnblockDisplay();
328-
}
329-
}
330-
while (mDisplayRunning) {
331-
std::this_thread::sleep_for(std::chrono::microseconds(10000)); // TODO: Signal by semaphore
339+
std::unique_lock lock(mExitStateMutex);
340+
if (mDisplayRunning) {
341+
mDisplay->mCfgC.mDisplayControl.store(2);
342+
StopDisplayA();
343+
mDisplay->UnblockDisplay();
332344
}
345+
mDisplayStoppedCV.wait(lock, [this] { return !mDisplayRunning; });
333346
}
334347

335348
void GPUDisplayFrontend::StopDisplayA() {}

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontend.h‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <memory>
2121
#include <chrono>
2222
#include <mutex>
23+
#include <condition_variable>
2324
#include <string>
2425
#include <vector>
2526

@@ -84,7 +85,8 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
8485
virtual int32_t FrontendMain() = 0;
8586
virtual void StopDisplayA();
8687
static void* FrontendThreadWrapper(void*);
87-
void waitForFPSLimit(); // Sleep until the next frame is due according to fpsRate (only in FPS_LIMIT_RATE mode, see GPUDisplayBackend::fpsLimitMode)
88+
void waitForFPSLimit(); // Sleep until the next frame is due according to fpsRate (only in FPS_LIMIT_RATE mode, see GPUDisplayBackend::fpsLimitMode), or until an external config update is signalled
89+
void signalConfigUpdate(); // Signal a config update from an external source (console keys, Qt GUI), wakes up waitForFPSLimit
8890

8991
static constexpr int32_t INIT_WIDTH = 1024, INIT_HEIGHT = 768; // Initial window size, before maximizing
9092
static constexpr int32_t MIN_WIDTH = 200, MIN_HEIGHT = 150; // Minimum window size
@@ -99,7 +101,11 @@ class GPUDisplayFrontend : public GPUDisplayFrontendInterface
99101

100102
std::mutex mExternalGUIKeyMutex;
101103
std::vector<int32_t> mExternalGUIKeys; // Keys sent by external entity (usually console), may be ignored by backend.
104+
std::mutex mConfigUpdateMutex;
105+
std::condition_variable mConfigUpdateCV; // Notified by signalConfigUpdate
106+
bool mConfigUpdatePending = false; // Protected by mConfigUpdateMutex, set by signalConfigUpdate, cleared by HandleExternalGUISignals
102107
std::mutex mExitStateMutex;
108+
std::condition_variable mDisplayStoppedCV; // Notified with mExitStateMutex held when the frontend sets mDisplayRunning to false, StopDisplay waits for it
103109
std::array<uint8_t, KEY_MAP_MAX> mKeyDownMap = {};
104110

105111
// Keyboard / Mouse actions

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendGlfw.cxx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ int32_t GPUDisplayFrontendGlfw::FrontendMain()
347347
glfwDestroyWindow(mWindow);
348348
glfwTerminate();
349349
mDisplayRunning = false;
350+
mDisplayStoppedCV.notify_all();
350351
}
351352

352353
return retVal;

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendGlut.cxx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ int32_t GPUDisplayFrontendGlut::FrontendMain()
259259
std::lock_guard lock(mExitStateMutex);
260260
setDisplayControl(2);
261261
mDisplayRunning = false;
262+
mDisplayStoppedCV.notify_all();
262263
}
263264

264265
ExitDisplay();

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendNone.cxx‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,19 @@ int32_t GPUDisplayFrontendNone::FrontendMain()
2727
if (InitDisplay()) {
2828
return 1;
2929
}
30+
{
31+
std::lock_guard lock(mExitStateMutex);
32+
mDisplayRunning = true;
33+
}
3034
do {
3135
HandleUpdatesAndDrawGLScene();
3236
waitForFPSLimit();
3337
} while (getDisplayControl() != 2);
38+
ExitDisplay();
39+
{
40+
std::lock_guard lock(mExitStateMutex);
41+
mDisplayRunning = false;
42+
mDisplayStoppedCV.notify_all();
43+
}
3444
return 0;
3545
}

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendWayland.cxx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,7 @@ int32_t GPUDisplayFrontendWayland::FrontendMain()
626626
{
627627
std::lock_guard lock(mExitStateMutex);
628628
mDisplayRunning = false;
629+
mDisplayStoppedCV.notify_all();
629630
}
630631

631632
if (mCursorShapeDevice) {

‎GPU/GPUTracking/display/frontend/GPUDisplayFrontendX11.cxx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,7 @@ int32_t GPUDisplayFrontendX11::FrontendMain()
476476
{
477477
std::lock_guard lock(mExitStateMutex);
478478
mDisplayRunning = false;
479+
mDisplayStoppedCV.notify_all();
479480
}
480481

481482
return (0);

‎GPU/GPUTracking/display/frontend/GPUDisplayGUI.cxx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ void GPUDisplayGUI::timerTick()
6565
}
6666
}
6767
const bool apply = newState && mUpdate.empty() && mState.appliedUpdate == mLastSentId; // State contains all our updates, and we have no unsent changes
68-
if (!mUpdate.empty() && mLastSentId - mLastAppliedId < mChannel.maxUpdatesAhead) {
68+
const bool post = !mUpdate.empty() && mLastSentId - mLastAppliedId < mChannel.maxUpdatesAhead;
69+
if (post) {
6970
if (mChannel.update) { // Previous update not yet picked up by the display, merge
7071
auto& update = *mChannel.update;
7172
update.keys.insert(update.keys.end(), mUpdate.keys.begin(), mUpdate.keys.end());
@@ -81,6 +82,9 @@ void GPUDisplayGUI::timerTick()
8182
}
8283
lock.unlock();
8384

85+
if (post) {
86+
mChannel.updatePosted();
87+
}
8488
if (apply) {
8589
applySettings();
8690
}

‎GPU/GPUTracking/display/frontend/GPUDisplayGUIChannel.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <chrono>
2020
#include <condition_variable>
2121
#include <cstdint>
22+
#include <functional>
2223
#include <mutex>
2324
#include <optional>
2425
#include <string>
@@ -57,6 +58,7 @@ struct GPUDisplayGUIChannel {
5758
std::chrono::milliseconds replyTimeout; // Maximum time the GUI waits for a reply
5859
uint32_t maxUpdatesAhead = 0; // GUI waits for the reply if it is that many updates ahead of the last one it applied
5960
std::string windowName; // Name of the display window, set when starting the GUI
61+
std::function<void()> updatePosted; // Set by the display, called by the GUI (without holding mutex) after posting an update, wakes up the display
6062
};
6163
} // namespace o2::gpu
6264

0 commit comments

Comments
 (0)