yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::util::UiThreadQueue Class Reference

Moves work from service threads (gRPC, HTTP) onto the UI thread. More...

#include <ui_thread_queue.h>

Classes

struct  Call
 
struct  Entry
 
struct  IsStatusOr
 
struct  IsStatusOr< absl::StatusOr< T > >
 

Public Member Functions

 UiThreadQueue ()=default
 
 ~UiThreadQueue ()
 
 UiThreadQueue (const UiThreadQueue &)=delete
 
UiThreadQueue & operator= (const UiThreadQueue &)=delete
 
void BindToCurrentThread ()
 
bool IsUiThread () const
 
bool RunsInline () const
 
void SetWaker (std::function< void()> waker)
 
bool Post (std::function< void()> task)
 
size_t Drain ()
 
bool HasPendingTasks () const
 
void Shutdown ()
 
bool IsShutdown () const
 
template<typename Fn , typename R = std::invoke_result_t<Fn&>>
R RunOnUiThread (Fn fn, std::chrono::milliseconds timeout=kDefaultTimeout)
 Runs fn on the UI thread and returns its result.
 

Static Public Member Functions

static UiThreadQueue & Get ()
 

Static Public Attributes

static constexpr std::chrono::milliseconds kDefaultTimeout {5000}
 

Private Member Functions

absl::Status RunAndWait (std::function< void()> body, std::chrono::milliseconds timeout)
 
bool Enqueue (Entry entry)
 
void Wake ()
 

Private Attributes

std::mutex mu_
 
std::deque< Entry > queue_
 
std::thread::id ui_thread_id_
 
bool bound_ = false
 
bool shutdown_ = false
 
std::mutex waker_mu_
 
std::function< void()> waker_
 

Detailed Description

Moves work from service threads (gRPC, HTTP) onto the UI thread.

Editor, canvas and ROM state is owned by the thread that runs the frame loop. Socket threads must not touch it directly, so they hand a task to this queue and the Controller runs it at the start of the next frame, before ImGui::NewFrame().

Lifecycle:

  1. The Controller calls BindToCurrentThread() and SetWaker() once the window backend exists.
  2. Every frame, the Controller calls Drain() before NewFrame.
  3. On quit, the Controller calls Shutdown() and SetWaker({}). Shutdown must happen before services are torn down, so a gRPC handler blocked in RunOnUiThread() returns Cancelled instead of holding up grpc::Server::Shutdown().

Get() returns the process-wide instance. A singleton is used because the gRPC and HTTP services are built by Application, apart from the Controller, and need to reach the same queue without new constructor plumbing. Tests construct their own UiThreadQueue instances.

Definition at line 44 of file ui_thread_queue.h.

Constructor & Destructor Documentation

◆ UiThreadQueue() [1/2]

yaze::util::UiThreadQueue::UiThreadQueue ( )
default

Referenced by Get().

◆ ~UiThreadQueue()

yaze::util::UiThreadQueue::~UiThreadQueue ( )

Definition at line 9 of file ui_thread_queue.cc.

References Shutdown().

Here is the call graph for this function:

◆ UiThreadQueue() [2/2]

yaze::util::UiThreadQueue::UiThreadQueue ( const UiThreadQueue & )
delete

Member Function Documentation

◆ operator=()

UiThreadQueue & yaze::util::UiThreadQueue::operator= ( const UiThreadQueue & )
delete

◆ Get()

UiThreadQueue & yaze::util::UiThreadQueue::Get ( )
static

◆ BindToCurrentThread()

void yaze::util::UiThreadQueue::BindToCurrentThread ( )

Definition at line 23 of file ui_thread_queue.cc.

References bound_, mu_, shutdown_, and ui_thread_id_.

◆ IsUiThread()

bool yaze::util::UiThreadQueue::IsUiThread ( ) const

Definition at line 30 of file ui_thread_queue.cc.

References bound_, mu_, and ui_thread_id_.

◆ RunsInline()

bool yaze::util::UiThreadQueue::RunsInline ( ) const

Definition at line 35 of file ui_thread_queue.cc.

References bound_, mu_, shutdown_, and ui_thread_id_.

◆ SetWaker()

void yaze::util::UiThreadQueue::SetWaker ( std::function< void()> waker)

Definition at line 46 of file ui_thread_queue.cc.

References waker_, and waker_mu_.

Referenced by yaze::Controller::OnEntry().

◆ Post()

bool yaze::util::UiThreadQueue::Post ( std::function< void()> task)

Definition at line 53 of file ui_thread_queue.cc.

References Enqueue().

Referenced by yaze::Controller::RequestScreenshot().

Here is the call graph for this function:

◆ Drain()

size_t yaze::util::UiThreadQueue::Drain ( )

Definition at line 87 of file ui_thread_queue.cc.

References mu_, and queue_.

Referenced by yaze::Controller::OnLoad().

◆ HasPendingTasks()

bool yaze::util::UiThreadQueue::HasPendingTasks ( ) const

Definition at line 101 of file ui_thread_queue.cc.

References mu_, and queue_.

Referenced by yaze::Controller::OnInput().

◆ Shutdown()

void yaze::util::UiThreadQueue::Shutdown ( )

◆ IsShutdown()

bool yaze::util::UiThreadQueue::IsShutdown ( ) const

Definition at line 127 of file ui_thread_queue.cc.

References mu_, and shutdown_.

◆ RunOnUiThread()

template<typename Fn , typename R = std::invoke_result_t<Fn&>>
R yaze::util::UiThreadQueue::RunOnUiThread ( Fn fn,
std::chrono::milliseconds timeout = kDefaultTimeout )
inline

Runs fn on the UI thread and returns its result.

fn must return absl::Status or absl::StatusOr<T>.

  • On the UI thread, or with no UI thread bound: runs inline.
  • Otherwise: posts fn and waits up to timeout.
  • Returns DeadlineExceeded if fn did not start before timeout. fn is then never run, so it may safely capture the caller's stack by reference. If fn already started, the call waits for it to finish.
  • Returns Cancelled after Shutdown().

Definition at line 111 of file ui_thread_queue.h.

References RunAndWait().

Here is the call graph for this function:

◆ RunAndWait()

absl::Status yaze::util::UiThreadQueue::RunAndWait ( std::function< void()> body,
std::chrono::milliseconds timeout )
private

◆ Enqueue()

bool yaze::util::UiThreadQueue::Enqueue ( Entry entry)
private

Definition at line 60 of file ui_thread_queue.cc.

References mu_, queue_, shutdown_, and Wake().

Referenced by Post(), and RunAndWait().

Here is the call graph for this function:

◆ Wake()

void yaze::util::UiThreadQueue::Wake ( )
private

Definition at line 80 of file ui_thread_queue.cc.

References waker_, and waker_mu_.

Referenced by Enqueue().

Member Data Documentation

◆ kDefaultTimeout

constexpr std::chrono::milliseconds yaze::util::UiThreadQueue::kDefaultTimeout {5000}
staticconstexpr

Definition at line 48 of file ui_thread_queue.h.

◆ mu_

std::mutex yaze::util::UiThreadQueue::mu_
mutableprivate

◆ queue_

std::deque<Entry> yaze::util::UiThreadQueue::queue_
private

Definition at line 156 of file ui_thread_queue.h.

Referenced by Drain(), Enqueue(), HasPendingTasks(), and Shutdown().

◆ ui_thread_id_

std::thread::id yaze::util::UiThreadQueue::ui_thread_id_
private

Definition at line 157 of file ui_thread_queue.h.

Referenced by BindToCurrentThread(), IsUiThread(), RunAndWait(), and RunsInline().

◆ bound_

bool yaze::util::UiThreadQueue::bound_ = false
private

Definition at line 158 of file ui_thread_queue.h.

Referenced by BindToCurrentThread(), IsUiThread(), RunAndWait(), and RunsInline().

◆ shutdown_

bool yaze::util::UiThreadQueue::shutdown_ = false
private

◆ waker_mu_

std::mutex yaze::util::UiThreadQueue::waker_mu_
private

Definition at line 162 of file ui_thread_queue.h.

Referenced by SetWaker(), and Wake().

◆ waker_

std::function<void()> yaze::util::UiThreadQueue::waker_
private

Definition at line 163 of file ui_thread_queue.h.

Referenced by SetWaker(), and Wake().


The documentation for this class was generated from the following files: