Skip to content

EntropyEngine::Core::TimerService

Service for managing timers with WorkGraph backing. More…

#include <TimerService.h>

Inherits from EntropyEngine::Core::EntropyService, EntropyEngine::Core::EntropyObject

Name
structConfig
Configuration for the timer service.
Name
~TimerService() override
Destroys the timer service and cancels all active timers.
virtual const char *version() const override
virtual voidunload() override
virtual TypeSystem::TypeIDtypeId() const override
virtual voidstop() override
virtual voidstart() override
voidsetWorkService(Concurrency::WorkService * workService)
Sets the WorkService reference (must be called before start).
TimerscheduleTimer(std::chrono::steady_clock::duration interval, Timer::WorkFunction work, bool repeating =false, Concurrency::ExecutionType executionType =Concurrency::ExecutionType::AnyThread)
Schedules a timer to execute after a delay.
size_tprocessReadyTimers()
Checks for ready timers and schedules them for execution.
virtual const char *name() const override
virtual voidload() override
virtual const char *id() const override
size_tgetActiveTimerCount() const
Gets the number of currently active timers.
virtual std::vector< TypeSystem::TypeID >dependsOnTypes() const override
virtual std::vector< std::string >dependsOn() const override
TimerService()
Creates a timer service with default configuration.
TimerService(const Config & config)
Creates a timer service with custom configuration.
Name
classTimer

Public Functions inherited from EntropyEngine::Core::EntropyService

Name
~EntropyService() override =default
ServiceStatestate() const
virtual const char *className() const override
Runtime class name for diagnostics and reflection.

Protected Functions inherited from EntropyEngine::Core::EntropyService

Name
voidsetState(ServiceState s)

Friends inherited from EntropyEngine::Core::EntropyService

Name
classEntropyServiceRegistry

Protected Classes inherited from EntropyEngine::Core::EntropyObject

Name
structHandleCore
Optional handle identity stamped by an owner/registry.

Public Functions inherited from EntropyEngine::Core::EntropyObject

Name
virtual~EntropyObject() =default
virtual const TypeSystem::TypeInfo *typeInfo() const
Optional richer type information; may be null.
booltryRetain() const
Attempts to retain only if the object is still alive.
virtual std::stringtoString() const
Human-readable short string (class@ptr by default).
voidretain() const
Increments the reference count.
voidrelease() const
Decrements the reference count and deletes when it reaches zero.
uint32_trefCount() const
Current reference count (approximate under contention).
EntropyObject &operator=(const EntropyObject & ) =delete
EntropyObject &operator=(EntropyObject && ) =delete
boolhasHandle() const
template <class OwnerT >
OwnerT *
handleOwnerAs() const
Returns the stamped owner pointer cast to the requested type.
const void *handleOwner() const
uint32_thandleIndex() const
uint64_thandleId() const
uint32_thandleGeneration() const
WeakControlBlock *getWeakControlBlock() const
Lazily retrieves or creates the weak control block.
virtual std::stringdescription() const
Long-form description; defaults to toString().
virtual std::stringdebugString() const
Debug-oriented string including refcount and handle when present.
virtual const char *className() const
Runtime class name for diagnostics and reflection.
virtual uint64_tclassHash() const
Stable type hash for cross-language identification.
EntropyObject() =default
EntropyObject(EntropyObject && ) =delete
EntropyObject(const EntropyObject & ) =delete

Protected Functions inherited from EntropyEngine::Core::EntropyObject

Name
void_setHandleIdentity(void * owner, uint32_t index, uint32_t generation)
void_clearHandleIdentity()

Protected Attributes inherited from EntropyEngine::Core::EntropyObject

Name
std::atomic< WeakControlBlock * >_weakBlock
Lazily allocated control block for weak refs.
std::atomic< uint32_t >_refCount
Thread-safe retain/release counter.
struct EntropyEngine::Core::EntropyObject::HandleCore_handle

Friends inherited from EntropyEngine::Core::EntropyObject

Name
structHandleAccess
class EntropyEngine::Core::TimerService;

Service for managing timers with WorkGraph backing.

TimerService provides NSTimer-style delayed execution integrated with the EntropyEngine service architecture. All timers are backed by yieldable WorkGraph nodes, allowing efficient scheduling without dedicated timer threads.

Key features:

  • One-shot and repeating timers
  • Main thread or background execution
  • Automatic integration with WorkService
  • No polling overhead - uses yieldable nodes
  • RAII-safe timer management

Integration:

  • Registered with ServiceRegistry during application startup
  • Depends on WorkService for execution
  • Main thread timers execute via WorkService::executeMainThreadWork()
  • Background timers execute on worker threads

Perfect for:

  • Delayed UI updates
  • Periodic polling
  • Timeout handling
  • Animation timing
  • Retry logic
// In EntropyApplication setup
auto timerService = std::make_shared<TimerService>();
services.registerService<TimerService>(timerService);
// Later, from any system
auto& timers = app.getServices().get<TimerService>();
// One-shot timer
auto timer = timers->scheduleTimer(
std::chrono::seconds(5),
[]{ LOG_INFO("5 seconds elapsed"); }
);
// Repeating timer on main thread
auto frameTimer = timers->scheduleTimer(
std::chrono::milliseconds(16),
[]{ updateFrame(); },
true, // Repeating
ExecutionType::MainThread
);
// Cancel timer early
frameTimer.invalidate();
~TimerService() override

Destroys the timer service and cancels all active timers.

inline virtual const char * version() const override

Reimplements: EntropyEngine::Core::EntropyService::version

virtual void unload() override

Reimplements: EntropyEngine::Core::EntropyService::unload

inline virtual TypeSystem::TypeID typeId() const override

Reimplements: EntropyEngine::Core::EntropyService::typeId

virtual void stop() override

Reimplements: EntropyEngine::Core::EntropyService::stop

virtual void start() override

Reimplements: EntropyEngine::Core::EntropyService::start

void setWorkService(
Concurrency::WorkService * workService
)

Sets the WorkService reference (must be called before start).

Parameters:

  • workService The WorkService to use for timer execution

Exceptions:

  • std::runtime_error if WorkContractGroup registration fails

This method allows the application to inject the WorkService dependency after both services have been created. Must be called after load() and before start().

Timer scheduleTimer(
std::chrono::steady_clock::duration interval,
Timer::WorkFunction work,
bool repeating =false,
Concurrency::ExecutionType executionType =Concurrency::ExecutionType::AnyThread
)

Schedules a timer to execute after a delay.

Parameters:

  • interval Time to wait before first execution
  • work Function to execute when timer fires
  • repeating If true, timer repeats; if false, fires once
  • executionType Where to execute: MainThread or AnyThread

Return: Timer handle for cancellation and status checking

Creates a new timer that executes the provided work function after the specified interval. For repeating timers, the work executes repeatedly at the interval until cancelled.

Thread-safe. Can be called from any thread. The work function will execute on the specified execution context (main thread or worker threads).

// One-shot timeout
auto timeout = service.scheduleTimer(
std::chrono::seconds(30),
[]{ handleTimeout(); },
false // One-shot
);
// Repeating poll every 100ms
auto poll = service.scheduleTimer(
std::chrono::milliseconds(100),
[]{ checkStatus(); },
true // Repeating
);
// Main thread UI update at 60 FPS
auto frameUpdate = service.scheduleTimer(
std::chrono::milliseconds(16),
[]{ renderFrame(); },
true, // Repeating
ExecutionType::MainThread
);
size_t processReadyTimers()

Checks for ready timers and schedules them for execution.

Return: Number of timers that were woken up and scheduled

Examines timers that are waiting for their scheduled time and wakes up any whose time has arrived. Call this periodically from your main loop to ensure timers fire promptly, especially when the system is idle.

// In main loop
while (running) {
timerService->processReadyTimers();
// ... other work ...
std::this_thread::sleep_for(10ms);
}
inline virtual const char * name() const override

Reimplements: EntropyEngine::Core::EntropyService::name

virtual void load() override

Reimplements: EntropyEngine::Core::EntropyService::load

inline virtual const char * id() const override

Reimplements: EntropyEngine::Core::EntropyService::id

size_t getActiveTimerCount() const

Gets the number of currently active timers.

Return: Count of timers that haven’t been cancelled or completed

virtual std::vector< TypeSystem::TypeID > dependsOnTypes() const override

Reimplements: EntropyEngine::Core::EntropyService::dependsOnTypes

inline virtual std::vector< std::string > dependsOn() const override

Reimplements: EntropyEngine::Core::EntropyService::dependsOn

TimerService()

Creates a timer service with default configuration.

explicit TimerService(
const Config & config
)

Creates a timer service with custom configuration.

Parameters:

  • config Service configuration
friend class Timer(
Timer
);

Updated on 2026-01-26 at 16:50:32 -0500