Skip to content
Repos

DevPlaceApp

C++ 806 B29 lines Opens in site

Directory Raw Download

#pragma once
// Spec: SPEC.md NFR-THR-01. Worker threads never touch UI state: they post closures here and the
// SDL main thread drains them. Hard cap: kMaxPending closures; overflow drops the oldest and counts it.
#include <atomic>
#include <cstdint>
#include <deque>
#include <functional>
#include <mutex>

namespace dp {

class UiQueue {
 public:
  static constexpr std::size_t kMaxPending = 4096;

  static UiQueue& instance();
  // Any thread.
  void post(std::function<void()> fn);
  // Main thread only. Returns the number of closures run (bounded by kMaxPending per call).
  std::size_t drain();
  std::uint64_t dropped() const { return dropped_.load(); }

 private:
  std::mutex mutex_;
  std::deque<std::function<void()>> items_;
  std::atomic<std::uint64_t> dropped_{0};
};

}  // namespace dp
42.9ms