GFXprim
2D bitmap graphics library with emphasis on speed and correctness
Loading...
Searching...
No Matches
gp_timer.h
Go to the documentation of this file.
1// SPDX-License-Identifier: LGPL-2.1-or-later
2/*
3 * Copyright (C) 2009-2023 Cyril Hrubis <metan@ucw.cz>
4 */
5
11#ifndef UTILS_GP_TIMER_H
12#define UTILS_GP_TIMER_H
13
14#include <stdint.h>
15#include <utils/gp_types.h>
16#include <input/gp_types.h>
17
21#define GP_TIMER_STOP UINT32_MAX
22
26struct gp_timer {
27 union {
28 gp_heap_head heap;
29 gp_timer *next;
30 };
31
33 uint64_t expires;
34
36 const char *id;
37
44 uint32_t period;
45
47 uint32_t running:1;
49 uint32_t in_callback:1;
51 uint32_t res_in_callback:1;
57 uint32_t free_on_stop:1;
58
59
60
62 void *_priv;
63
70 uint32_t (*callback)(struct gp_timer *self);
71
73 void *priv;
74
84 void (*stopped)(gp_timer *self);
85
86 char data[];
87};
88
89#define GP_TIMER_DECLARE(name, texpires, tperiod, tid, tcallback, tpriv) \
90 gp_timer name = { \
91 .expires = texpires, \
92 .period = tperiod, \
93 .id = tid, \
94 .callback = tcallback, \
95 .priv = tpriv \
96 }
105static inline int gp_timer_is_running(const gp_timer *timer)
106{
107 return timer->running;
108}
109
115void gp_timer_queue_dump(const gp_timer *queue);
116
133void gp_timer_queue_ins(gp_timer **queue, uint64_t now, gp_timer *timer);
134
147
158int gp_timer_queue_process(gp_timer **queue, uint64_t now);
159
166static inline unsigned int gp_timer_queue_size(const gp_timer *queue)
167{
168 return queue ? queue->heap.children + 1 : 0;
169}
170
183gp_timer *gp_timer_alloc(uint32_t expires_ms, uint32_t period_ms, const char *id,
184 uint32_t (*callback)(gp_timer *), void *priv);
185
196
197#endif /* UTILS_GP_TIMER_H */
gp_timer * gp_timer_alloc(uint32_t expires_ms, uint32_t period_ms, const char *id, uint32_t(*callback)(gp_timer *), void *priv)
Allocates a new timer.
void gp_timer_free(gp_timer *self)
Frees timer allocated by gp_timer_alloc().
void gp_timer_queue_dump(const gp_timer *queue)
Prints the structrue of binary heap into stdout, only for debugging.
void gp_timer_queue_ins(gp_timer **queue, uint64_t now, gp_timer *timer)
Inserts timer into the timer priority queue.
void gp_timer_queue_rem(gp_timer **queue, gp_timer *timer)
Removes timer from timer queue.
static int gp_timer_is_running(const gp_timer *timer)
Returns if timer is running.
Definition gp_timer.h:105
static unsigned int gp_timer_queue_size(const gp_timer *queue)
Returns size of the queue, i.e. number of timers.
Definition gp_timer.h:166
int gp_timer_queue_process(gp_timer **queue, uint64_t now)
Processes queue, all timers with expires <= now are processed.
A timer.
Definition gp_timer.h:26
uint32_t running
Set if timer is inserted into a queue.
Definition gp_timer.h:47
uint32_t res_in_callback
Set if timer was rescheduled from callback.
Definition gp_timer.h:51
uint32_t(* callback)(struct gp_timer *self)
Timer callback.
Definition gp_timer.h:70
void * priv
A user private pointer.
Definition gp_timer.h:73
uint32_t in_callback
Set during the run of the timer callback.
Definition gp_timer.h:49
void * _priv
Library private pointer. Do not touch!
Definition gp_timer.h:62
uint64_t expires
Initial xpiration time, set by user, modified by the queue.
Definition gp_timer.h:33
uint32_t period
User variable may be used to store the timer period.
Definition gp_timer.h:44
uint32_t free_on_stop
Set if gp_timer_free() was called when timer was running.
Definition gp_timer.h:57
const char * id
Timer name showed in debug messages.
Definition gp_timer.h:36
void(* stopped)(gp_timer *self)
An optional function to cleanup when timer is stopped.
Definition gp_timer.h:84
Common header for types.