24 std::lock_guard<std::mutex> lock(
mu_);
31 std::lock_guard<std::mutex> lock(
mu_);
36 std::lock_guard<std::mutex> lock(
mu_);
49 std::lock_guard<std::mutex> lock(
waker_mu_);
61 bool was_empty =
false;
63 std::lock_guard<std::mutex> lock(
mu_);
67 was_empty =
queue_.empty();
68 queue_.push_back(std::move(entry));
81 std::lock_guard<std::mutex> lock(
waker_mu_);
88 std::deque<Entry> batch;
90 std::lock_guard<std::mutex> lock(
mu_);
95 for (
Entry& entry : batch) {
102 std::lock_guard<std::mutex> lock(
mu_);
107 std::deque<Entry> dropped;
109 std::lock_guard<std::mutex> lock(
mu_);
113 for (
Entry& entry : dropped) {
118 std::lock_guard<std::mutex> lock(entry.call->mu);
123 entry.call->cv.notify_all();
128 std::lock_guard<std::mutex> lock(
mu_);
133 std::chrono::milliseconds timeout) {
134 bool run_inline =
false;
136 std::lock_guard<std::mutex> lock(
mu_);
138 return absl::CancelledError(
"UI thread queue is shut down");
149 return absl::OkStatus();
152 auto call = std::make_shared<Call>();
155 entry.
task = [call, body = std::move(body)]() {
157 std::lock_guard<std::mutex> lock(call->mu);
167 std::lock_guard<std::mutex> lock(call->mu);
170 call->cv.notify_all();
172 if (!
Enqueue(std::move(entry))) {
173 return absl::CancelledError(
"UI thread queue is shut down");
176 std::unique_lock<std::mutex> lock(call->mu);
177 const bool finished = call->cv.wait_for(lock, timeout, [&call]() {
184 return absl::DeadlineExceededError(
185 "UI thread did not run the task within " +
186 std::to_string(timeout.count()) +
" ms");
194 return absl::CancelledError(
195 "UI thread queue shut down before the task ran");
197 return absl::OkStatus();
Moves work from service threads (gRPC, HTTP) onto the UI thread.
std::thread::id ui_thread_id_
void BindToCurrentThread()
std::deque< Entry > queue_
bool Post(std::function< void()> task)
bool Enqueue(Entry entry)
std::function< void()> waker_
static UiThreadQueue & Get()
bool HasPendingTasks() const
absl::Status RunAndWait(std::function< void()> body, std::chrono::milliseconds timeout)
void SetWaker(std::function< void()> waker)
constexpr int kMainLoopHiddenWaitMs
int MainLoopEventWaitMs(const MainLoopIdleState &state)
Returns how long the frame loop may block waiting for an event.
constexpr std::chrono::milliseconds kMainLoopIdleAfter
constexpr int kMainLoopIdleWaitMs
Inputs for the frame loop's idle decision.
std::chrono::steady_clock::duration quiet_for
std::shared_ptr< Call > call
std::function< void()> task