Skip to content

Commit

Permalink
graphics_view: also clear semaphor on fx reset
Browse files Browse the repository at this point in the history
  • Loading branch information
JoepVanlier committed Jul 14, 2024
1 parent d550dbb commit 784ed02
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
12 changes: 8 additions & 4 deletions plugin/components/graphics_view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,8 @@ void YsfxGraphicsView::Impl::BackgroundWork::stop()
std::lock_guard<std::mutex> lock{m_messagesMutex};
while (!m_messages.empty())
m_messages.pop();

m_sema.clear();
}

void YsfxGraphicsView::Impl::BackgroundWork::postMessage(std::shared_ptr<Message> message)
Expand All @@ -737,10 +739,12 @@ void YsfxGraphicsView::Impl::BackgroundWork::run()
std::shared_ptr<Message> msg = popNextMessage();
jassert(msg);

switch (msg->m_type) {
case '@gfx':
processGfxMessage(static_cast<GfxMessage &>(*msg));
break;
if (msg) {
switch (msg->m_type) {
case '@gfx':
processGfxMessage(static_cast<GfxMessage &>(*msg));
break;
}
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions plugin/utility/rt_semaphore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ void RTSemaphore::wait()
throw std::system_error(ec);
}

void RTSemaphore::clear()
{
std::error_code ec;
clear(ec);
if (ec)
throw std::system_error(ec);
}

bool RTSemaphore::try_wait()
{
std::error_code ec;
Expand Down Expand Up @@ -120,6 +128,15 @@ void RTSemaphore::wait(std::error_code& ec) noexcept
} while (1);
}

void RTSemaphore::clear(std::error_code& ec) noexcept
{
ec.clear();
mach_timespec_t timeout;
timeout.tv_sec = 0 / 1000;
timeout.tv_nsec = (0 % 1000) * (1000L * 1000L);
while (semaphore_timedwait(sem_, timeout) == KERN_SUCCESS);
}

bool RTSemaphore::try_wait(std::error_code& ec) noexcept
{
return timed_wait(0, ec);
Expand Down Expand Up @@ -189,6 +206,12 @@ void RTSemaphore::post(std::error_code& ec) noexcept
ec = std::error_code(GetLastError(), std::system_category());
}

void RTSemaphore::clear(std::error_code& ec) noexcept
{
ec.clear();
while (WaitForSingleObject(sem_, 0) == WAIT_OBJECT_0);
}

void RTSemaphore::wait(std::error_code& ec) noexcept
{
ec.clear();
Expand Down Expand Up @@ -305,6 +328,17 @@ static bool absolute_timeout(uint32_t milliseconds, timespec &result, std::error
return true;
}

void RTSemaphore::clear(std::error_code& ec) noexcept
{
ec.clear();
timespec abs;
absolute_timeout(0, abs, ec);

// Note that we don't really need to update the absolute timestep because if there are
// still events pending, it will just pop them off the stack.
while (sem_timedwait(&sem_, &abs) == 0);
}

bool RTSemaphore::timed_wait(uint32_t milliseconds, std::error_code& ec) noexcept
{
ec.clear();
Expand Down
2 changes: 2 additions & 0 deletions plugin/utility/rt_semaphore.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,13 @@ class RTSemaphore {

void post();
void wait();
void clear();
bool try_wait();
bool timed_wait(uint32_t milliseconds);

void post(std::error_code& ec) noexcept;
void wait(std::error_code& ec) noexcept;
void clear(std::error_code& ec) noexcept;
bool try_wait(std::error_code& ec) noexcept;
bool timed_wait(uint32_t milliseconds, std::error_code& ec) noexcept;

Expand Down

0 comments on commit 784ed02

Please sign in to comment.