diff options
author | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-10 16:12:07 -0700 |
---|---|---|
committer | Ben Longbons <b.r.longbons@gmail.com> | 2011-09-10 16:12:07 -0700 |
commit | f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1 (patch) | |
tree | d9b013ab252968ec1e90e721f7b2ab819af0acb0 /src/common/timer.h | |
parent | 5939e1bec75f2550d3ce109b9cd9a5d22c0626c2 (diff) | |
parent | 723fb5d3431b847526c433a13aa74485cfb564a3 (diff) | |
download | tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.gz tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.bz2 tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.tar.xz tmwa-f841b6fdcc802e73d52da0e67ee192c0c2c1c7e1.zip |
Merge commit '723fb5d3431b847526c433a13aa74485cfb564a3'
Diffstat (limited to 'src/common/timer.h')
-rw-r--r-- | src/common/timer.h | 81 |
1 files changed, 47 insertions, 34 deletions
diff --git a/src/common/timer.h b/src/common/timer.h index c6c4b52..e363a56 100644 --- a/src/common/timer.h +++ b/src/common/timer.h @@ -1,47 +1,60 @@ -// original : core.h 2003/03/14 11:55:25 Rev 1.4 +#ifndef TIMER_H +#define TIMER_H -#ifndef _TIMER_H_ -#define _TIMER_H_ +# include "sanity.h" -#define BASE_TICK 5 - -#define TIMER_ONCE_AUTODEL 1 -#define TIMER_INTERVAL 2 -#define TIMER_REMOVE_HEAP 16 - -#define DIFF_TICK(a,b) ((int)((a)-(b))) - -// Struct declaration +enum TIMER_TYPE +{ + TIMER_ONCE_AUTODEL = 1, + TIMER_INTERVAL = 2, +}; +/// This is needed to produce a signed result when 2 ticks are subtracted +# define DIFF_TICK(a,b) ((int32_t)((a)-(b))) + +// TODO replace with signed 64-bit to make code more clear and protect from the future +typedef uint32_t tick_t; +typedef uint32_t interval_t; +typedef uint32_t timer_id; +// BUG: pointers are stored in here +typedef int32_t custom_id_t; +typedef int32_t custom_data_t; +typedef void (*timer_func) (timer_id, tick_t, custom_id_t, custom_data_t); struct TimerData { - unsigned int tick; - int (*func) (int, unsigned int, int, int); - int id; - int data; - int type; - int interval; - int heap_pos; + /// When it will be triggered + tick_t tick; + /// What will be done + timer_func func; + /// Arbitrary data. WARNING, callers are stupid and put pointers in here + // Should we change to void* or intptr_t ? + custom_id_t id; + custom_data_t data; + /// Type of timer - 0 initially + enum TIMER_TYPE type; + /// Repeat rate + interval_t interval; }; -// Function prototype declaration - -unsigned int gettick_nocache (void); -unsigned int gettick (void); +/// Server time, in milliseconds, since the epoch, +/// but use of 32-bit integers means it wraps every 49 days. +// The only external caller of this function is the core.c main loop, but that makes sense +// in fact, it might make more sense if gettick() ALWAYS returned that cached value +tick_t gettick_nocache (void); +/// This function is called enough that it's worth caching the result for +/// the next 255 times +tick_t gettick (void); -int add_timer (unsigned int, int (*)(int, unsigned int, int, int), int, int); -int add_timer_interval (unsigned int, int (*)(int, unsigned int, int, int), - int, int, int); -int delete_timer (int, int (*)(int, unsigned int, int, int)); +timer_id add_timer (tick_t, timer_func, custom_id_t, custom_data_t); +timer_id add_timer_interval (tick_t, timer_func, custom_id_t, custom_data_t, interval_t); +void delete_timer (timer_id, timer_func); -int addtick_timer (int tid, unsigned int tick); -struct TimerData *get_timer (int tid); +tick_t addtick_timer (timer_id, interval_t); +struct TimerData *get_timer (timer_id tid); -int do_timer (unsigned int tick); +/// Do all timers scheduled before tick, and return the number of milliseconds until the next timer happens +interval_t do_timer (tick_t tick); -int add_timer_func_list (int (*)(int, unsigned int, int, int), char *); -char *search_timer_func_list (int (*)(int, unsigned int, int, int)); -extern void timer_final (); -#endif // _TIMER_H_ +#endif // TIMER_H |