Skip to content

Commit

Permalink
wayland_backend: Handle shutting down input thread
Browse files Browse the repository at this point in the history
  • Loading branch information
misyltoad committed Apr 13, 2024
1 parent 89cd1ca commit f88723f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 20 deletions.
15 changes: 8 additions & 7 deletions src/waitable.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,16 @@ namespace gamescope
class CFunctionWaitable final : public IWaitable
{
public:
CFunctionWaitable( int nFD, std::function<void()> fnPollFunc )
CFunctionWaitable( int nFD, std::function<void()> fnPollFunc = nullptr )
: m_nFD{ nFD }
, m_fnPollFunc{ fnPollFunc }
{
}

void OnPollIn() final
{
m_fnPollFunc();
if ( m_fnPollFunc )
m_fnPollFunc();
}

void Drain()
Expand Down Expand Up @@ -260,7 +261,7 @@ namespace gamescope
epoll_ctl( m_nEpollFD, EPOLL_CTL_DEL, pWaitable->GetFD(), nullptr );
}

void PollEvents( int nTimeOut = -1 )
int PollEvents( int nTimeOut = -1 )
{
epoll_event events[MaxEvents];

Expand All @@ -269,15 +270,15 @@ namespace gamescope
int nEventCount = epoll_wait( m_nEpollFD, events, MaxEvents, nTimeOut );

if ( !m_bRunning )
return;
return 0;

if ( nEventCount < 0 )
{
if ( errno == EAGAIN )
if ( errno == EAGAIN || errno == EINTR )
continue;

g_WaitableLog.errorf_errno( "Failed to epoll_wait in CAsyncWaiter" );
return;
return nEventCount;
}

for ( int i = 0; i < nEventCount; i++ )
Expand All @@ -288,7 +289,7 @@ namespace gamescope
pWaitable->HandleEvents( event.events );
}

return;
return nEventCount;
}
}

Expand Down
39 changes: 26 additions & 13 deletions src/wayland_backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "defer.hpp"
#include "convar.h"
#include "refresh_rate.h"
#include "waitable.h"

#include <cstring>
#include <unordered_map>
Expand Down Expand Up @@ -320,6 +321,7 @@ namespace gamescope
{
public:
CWaylandInputThread();
~CWaylandInputThread();

bool Init( CWaylandBackend *pBackend );

Expand All @@ -341,6 +343,8 @@ namespace gamescope

CWaylandBackend *m_pBackend = nullptr;

CWaiter<4> m_Waiter;

std::thread m_Thread;
std::atomic<bool> m_bInitted = { false };

Expand Down Expand Up @@ -1919,6 +1923,15 @@ namespace gamescope
{
}

CWaylandInputThread::~CWaylandInputThread()
{
m_bInitted = true;
m_bInitted.notify_all();

m_Waiter.Shutdown();
m_Thread.join();
}

bool CWaylandInputThread::Init( CWaylandBackend *pBackend )
{
m_pBackend = pBackend;
Expand Down Expand Up @@ -1970,9 +1983,20 @@ namespace gamescope
{
m_bInitted.wait( false );

if ( !m_Waiter.IsRunning() )
return;

int nFD = wl_display_get_fd( m_pBackend->GetDisplay() );
if ( nFD < 0 )
{
abort();
}

CFunctionWaitable waitable( nFD );
m_Waiter.AddWaitable( &waitable );

int nRet = 0;
for ( ;; )
while ( m_Waiter.IsRunning() )
{
if ( ( nRet = wl_display_dispatch_queue_pending( m_pBackend->GetDisplay(), m_pQueue ) ) < 0 )
{
Expand All @@ -1987,22 +2011,11 @@ namespace gamescope
abort();
}

pollfd pollfd =
{
.fd = nFD,
.events = POLLIN,
};
if ( ( nRet = poll( &pollfd, 1, -1 ) ) <= 0 )
if ( ( nRet = m_Waiter.PollEvents() ) <= 0 )
{
int nErrno = errno;
wl_display_cancel_read( m_pBackend->GetDisplay() );
if ( nRet < 0 )
{
if ( nErrno == EINTR || nErrno == EAGAIN )
continue;

abort();
}

assert( nRet == 0 );
continue;
Expand Down

0 comments on commit f88723f

Please sign in to comment.