Skip to content

Commit a313dd1

Browse files
committed
MultiThreadedCompositor: Fix synchronisation of composition triggering.  ♥ 14:48
Sadly, even though it *looks* like it should work, `std::binary_semaphore` is just the wrong tool here. Fortunately, C++20 *also* added “wait for this `std::atomic<>` value to change”, which is more or less what we want. (It still has the annoying “you have to explicitly `notify_one()`” bit which is not required by the underlying OS API, but 🤷‍♀️)
1 parent 8143d5a commit a313dd1

File tree

1 file changed

+11
-10
lines changed

1 file changed

+11
-10
lines changed

src/server/compositor/multi_threaded_compositor.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
#include <thread>
3434
#include <chrono>
3535
#include <future>
36-
#include <semaphore>
3736
#include <boost/throw_exception.hpp>
3837

3938
using namespace std::literals::chrono_literals;
@@ -60,7 +59,7 @@ class CompositingFunctor
6059
compositor_factory{db_compositor_factory},
6160
group(group),
6261
scene(scene),
63-
wakeup{0},
62+
needs_wakeup{false},
6463
running{true},
6564
force_sleep{fixed_composite_delay},
6665
display_listener{display_listener},
@@ -121,7 +120,9 @@ class CompositingFunctor
121120
while (running)
122121
{
123122
/* Wait until compositing has been scheduled or we are stopped */
124-
wakeup.acquire();
123+
needs_wakeup.wait(false);
124+
// We've been awoken; reset the trigger
125+
needs_wakeup = false;
125126

126127
/*
127128
* Check if we are running before compositing, since we may have
@@ -166,8 +167,8 @@ class CompositingFunctor
166167

167168
void schedule_compositing()
168169
{
169-
wakeup.try_acquire();
170-
wakeup.release();
170+
needs_wakeup = true;
171+
needs_wakeup.notify_one();
171172
}
172173

173174
void schedule_compositing(geometry::Rectangle const& damage)
@@ -178,16 +179,16 @@ class CompositingFunctor
178179

179180
if (took_damage)
180181
{
181-
wakeup.try_acquire();
182-
wakeup.release();
182+
needs_wakeup = true;
183+
needs_wakeup.notify_one();
183184
}
184185
}
185186

186187
void stop()
187188
{
188189
running = false;
189-
wakeup.try_acquire();
190-
wakeup.release();
190+
needs_wakeup = true;
191+
needs_wakeup.notify_one();
191192
}
192193

193194
void wait_until_started()
@@ -211,7 +212,7 @@ class CompositingFunctor
211212
std::shared_ptr<mc::DisplayBufferCompositorFactory> const compositor_factory;
212213
mg::DisplaySyncGroup& group;
213214
std::shared_ptr<mc::Scene> const scene;
214-
std::binary_semaphore wakeup;
215+
std::atomic<bool> needs_wakeup;
215216
std::atomic<bool> running;
216217
std::chrono::milliseconds force_sleep{-1};
217218
std::shared_ptr<DisplayListener> const display_listener;

0 commit comments

Comments
 (0)