calog/libs/calogTimer.h
2026-07-03 02:13:23 -05:00

29 lines
1.5 KiB
C

// calogTimer.h -- calog timer library.
//
// One background thread drives every timer and invokes each script callback on its OWN
// owning context (marshalled like any calog function value, so the callback runs on the
// thread that created the timer). Three natives:
// timerAfter(ms, function) -> id fire the callback once, ms milliseconds from now
// timerEvery(ms, function) -> id fire the callback every ms milliseconds
// timerCancel(id) -> nil stop a pending or repeating timer (id from the above)
//
// If a timer's owning context is gone by the time it fires, the invoke fails cleanly and the
// timer auto-cancels itself. ms may be an integer or a real and must be non-negative. The
// registry (and its background thread) is process-wide and reference-counted across runtimes.
#ifndef CALOG_TIMER_H
#define CALOG_TIMER_H
#include "calog.h"
// Register the timer natives (timerAfter, timerEvery, timerCancel) on a runtime. The shared
// registry is reference-counted, so this is safe to call on any number of runtimes.
int32_t calogTimerRegister(CalogT *calog);
// Release every still-pending timer's callback and (once the last registered runtime
// unregisters) stop the background thread and free the timer list. Like calogExport, call
// this while the timer-owning contexts are still ALIVE -- before you close them and before
// calogDestroy -- because a pending callback is a live reference into its owner's interpreter.
void calogTimerShutdown(void);
#endif