#ifndef TIMERQUEUE_H #define TIMERQUEUE_H #pragma once #include "config.h" #ifdef __cplusplus extern "C" { #endif typedef struct timer_entry timer_entry; typedef struct timer_queue_s timer_queue_t; typedef void (*timerqueue_callback)(timer_queue_t *q, timer_entry *timer, int err); struct timer_entry { /** * User data to be associated with this entry. * Applications normally will put the instance of object that * owns the timer entry in this field. */ void *user_data; /** * Arbitrary ID assigned by the user/owner of this entry. * Applications can use this ID to distinguish multiple * timer entries that share the same callback and user_data. */ int id; /** * Callback to be called when the timer expires. */ timerqueue_callback cb; /** * Internal usage only, user dont use it */ void *_private[4]; }; /** timer queue implemented by sorted list */ TOOLKIT_API int timer_sortedlist_create(timer_queue_t **p_q); /** timer queue implemented by heap */ TOOLKIT_API int timer_heap_create(timer_queue_t **p_q); /** timer queue implemented by wheel */ TOOLKIT_API int timer_wheel_create(int num_wheel, int wheel_time, timer_queue_t **p_q); /** * destroy timer queue */ TOOLKIT_API void timer_queue_destroy(timer_queue_t *q); /** * set timer * @param delay delay time from now, in milliseconds, zero means due immediately */ TOOLKIT_API int timer_queue_schedule(timer_queue_t *q, timer_entry *entry, unsigned int delay); /** * cancel timer */ TOOLKIT_API int timer_queue_cancel(timer_queue_t *q, timer_entry *entry, int cancel); /** * get timer count in the heap */ TOOLKIT_API int timer_queue_get_count(timer_queue_t *q); /** * poll the timer heap * @param next_delay next delay in milliseconds, can be null, if next_delay == -1, no timer in the heap * @return the number of triggered timers */ TOOLKIT_API int timer_queue_poll(timer_queue_t *q, int *next_delay); /** * poll only one timer in the time heap * @param p_entry pointer to the timer which is expired, if not will be NULL * @param next_delay next delay in milliseconds, can be null, if next_delay == -1, no timer in the heap * @return the number of triggered timers */ TOOLKIT_API int timer_queue_poll_one(timer_queue_t *q, timer_entry **p_entry, int *next_delay); #ifdef __cplusplus } // extern "C" { #endif #endif // TIMERQUEUE_H