123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553 |
- // Copyright (c) Athena Dev Teams - Licensed under GNU GPL
- // For more information, see LICENCE in the main folder
- #include "../common/cbasetypes.h"
- #include "../common/malloc.h"
- #include "../common/showmsg.h"
- #include "timer.h"
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <time.h>
- #ifdef WIN32
- #define WIN32_LEAN_AND_MEAN
- #include <windows.h> // GetTickCount()
- #else
- #include <unistd.h>
- #include <sys/time.h> // struct timeval, gettimeofday()
- #endif
- // If the server can't handle processing thousands of monsters
- // or many connected clients, please increase TIMER_MIN_INTERVAL.
- #define TIMER_MIN_INTERVAL 50
- #define TIMER_MAX_INTERVAL 60000
- // timers (array)
- static struct TimerData* timer_data = NULL;
- static int timer_data_max = 0;
- static int timer_data_num = 0;
- // free timers (array)
- static int* free_timer_list = NULL;
- static int free_timer_list_max = 0;
- static int free_timer_list_num = 0;
- // timer heap (binary min heap)
- static int* timer_heap = NULL;
- static int timer_heap_max = 0;
- static int timer_heap_num = 0;
- // server startup time
- time_t start_time;
- /*----------------------------
- * Timer debugging
- *----------------------------*/
- struct timer_func_list {
- struct timer_func_list* next;
- TimerFunc func;
- char* name;
- } *tfl_root = NULL;
- /// Sets the name of a timer function.
- int add_timer_func_list(TimerFunc func, char* name)
- {
- struct timer_func_list* tfl;
- if (name) {
- for( tfl=tfl_root; tfl != NULL; tfl=tfl->next )
- {// check suspicious cases
- if( func == tfl->func )
- ShowWarning("add_timer_func_list: duplicating function %08x(%s) as %s.\n",(int)tfl->func,tfl->name,name);
- else if( strcmp(name,tfl->name) == 0 )
- ShowWarning("add_timer_func_list: function %08X has the same name as %08X(%s)\n",(int)func,(int)tfl->func,tfl->name);
- }
- CREATE(tfl,struct timer_func_list,1);
- tfl->next = tfl_root;
- tfl->func = func;
- tfl->name = aStrdup(name);
- tfl_root = tfl;
- }
- return 0;
- }
- /// Returns the name of the timer function.
- char* search_timer_func_list(TimerFunc func)
- {
- struct timer_func_list* tfl;
- for( tfl=tfl_root; tfl != NULL; tfl=tfl->next )
- if (func == tfl->func)
- return tfl->name;
- return "unknown timer function";
- }
- /*----------------------------
- * Get tick time
- *----------------------------*/
- /// platform-abstracted tick retrieval
- static unsigned int tick(void)
- {
- #if defined(WIN32)
- return GetTickCount();
- #elif (defined(_POSIX_TIMERS) && _POSIX_TIMERS > 0 && defined(_POSIX_MONOTONIC_CLOCK) /* posix compliant */) || (defined(__FreeBSD_cc_version) && __FreeBSD_cc_version >= 500005 /* FreeBSD >= 5.1.0 */)
- struct timespec tval;
- clock_gettime(CLOCK_MONOTONIC, &tval);
- return tval.tv_sec * 1000 + tval.tv_nsec / 1000000;
- #else
- struct timeval tval;
- gettimeofday(&tval, NULL);
- return tval.tv_sec * 1000 + tval.tv_usec / 1000;
- #endif
- }
- //////////////////////////////////////////////////////////////////////////
- #if defined(TICK_CACHE) && TICK_CACHE > 1
- //////////////////////////////////////////////////////////////////////////
- // tick is cached for TICK_CACHE calls
- static unsigned int gettick_cache;
- static int gettick_count = 1;
- unsigned int gettick_nocache(void)
- {
- gettick_count = TICK_CACHE;
- gettick_cache = tick();
- return gettick_cache;
- }
- unsigned int gettick(void)
- {
- return ( --gettick_count == 0 ) ? gettick_nocache() : gettick_cache;
- }
- //////////////////////////////
- #else
- //////////////////////////////
- // tick doesn't get cached
- unsigned int gettick_nocache(void)
- {
- return tick();
- }
- unsigned int gettick(void)
- {
- return tick();
- }
- //////////////////////////////////////////////////////////////////////////
- #endif
- //////////////////////////////////////////////////////////////////////////
- /*======================================
- * CORE : Timer Heap
- *--------------------------------------*/
- #define BHEAP_PARENT(pos) ( ((pos) - 1)/2 )
- #define BHEAP_LEFT(pos) ( (pos)*2 + 1 )
- #define BHEAP_RIGHT(pos) ( (pos)*2 + 2 )
- /// Adds a timer to the timer_heap
- static
- void push_timer_heap(int tid)
- {
- int pos;
- // check available space
- if( timer_heap_num >= timer_heap_max )
- {
- timer_heap_max += 256;
- if( timer_heap )
- RECREATE(timer_heap, int, timer_heap_max);
- else
- CREATE(timer_heap, int, timer_heap_max);
- memset(timer_heap + (timer_heap_max - 256), 0, sizeof(int)*256);
- }
- // add the timer
- pos = timer_heap_num++;
- timer_heap[pos] = tid;
- // restore binary heap properties
- while( pos > 0 )
- {
- int parent = BHEAP_PARENT(pos);
- if( DIFF_TICK(timer_data[tid].tick, timer_data[timer_heap[parent]].tick) > 0 )
- break;// done
- swap(timer_heap[pos], timer_heap[parent]);
- pos = parent;
- }
- }
- /// Removes a timer from the timer_heap
- static
- bool pop_timer_heap(int tid)
- {
- int pos;
- // find the timer
- pos = 0;
- while( pos < timer_heap_num )
- {// search in the order current-left-right
- int left = BHEAP_LEFT(pos);
- int right = BHEAP_RIGHT(pos);
- if( timer_heap[pos] == tid )
- break;// found the timer
- if( left < timer_heap_num && DIFF_TICK(timer_data[tid].tick, timer_data[timer_heap[left]].tick) >= 0 )
- {// try left child
- pos = left;
- continue;
- }
- if( right < timer_heap_num && DIFF_TICK(timer_data[tid].tick, timer_data[timer_heap[right]].tick) >= 0 )
- {// try right child
- pos = right;
- continue;
- }
- // back and right
- while( true )
- {
- int parent;
- if( pos == 0 )
- return false;// not found
- parent = BHEAP_PARENT(pos);
- right = BHEAP_RIGHT(parent);
- if( pos != right && right < timer_heap_num && DIFF_TICK(timer_data[tid].tick, timer_data[timer_heap[right]].tick) >= 0 )
- break;// try this right
- pos = parent;
- }
- pos = right;
- }
- if( pos >= timer_heap_num )
- return false;// not found
- // remove timer
- timer_heap[pos] = timer_heap[--timer_heap_num];
- // restore binary heap properties
- while( pos < timer_heap_num )
- {
- int left = BHEAP_LEFT(pos);
- int right = BHEAP_RIGHT(pos);
- if( left < timer_heap_num && DIFF_TICK(timer_data[timer_heap[pos]].tick, timer_data[timer_heap[left]].tick) > 0 )
- {
- if( right < timer_heap_num && DIFF_TICK(timer_data[timer_heap[left]].tick, timer_data[timer_heap[right]].tick) > 0 )
- {
- swap(timer_heap[pos], timer_heap[right]);
- pos = right;
- }
- else
- {
- swap(timer_heap[pos], timer_heap[left]);
- pos = left;
- }
- }
- else if( right < timer_heap_num && DIFF_TICK(timer_data[timer_heap[pos]].tick, timer_data[timer_heap[right]].tick) > 0 )
- {
- swap(timer_heap[pos], timer_heap[right]);
- pos = right;
- }
- else
- {
- break;// done
- }
- }
- return true;
- }
- /*==========================
- * Timer Management
- *--------------------------*/
- // diff_tick limits (2*24*60*60*1000 is 2 days ; 2*60*60*1000 is 2 hours)
- #define FUTURE_DIFF_TICK ( INT_MIN + 2*24*60*60*1000 )
- #define MAX_DIFF_TICK ( INT_MAX - 2*60*60*1000 )
- #define MIN_DIFF_TICK ( -2*60*60*1000 )
- #define PAST_DIFF_TICK ( -2*24*60*60*1000 )
- /// Adjusts the tick value to a valid tick_diff range.
- /// Returns false if the tick is invalid.
- static
- bool adjust_tick(unsigned int* tick)
- {
- int diff;
-
- if( tick == NULL )
- return false;
- diff = DIFF_TICK(*tick, gettick());
- if( diff <= FUTURE_DIFF_TICK || diff > MAX_DIFF_TICK )
- {
- ShowWarning("adjust_tick: tick diff too far in the future %d, adjusting to the maximum %d\n", diff, MAX_DIFF_TICK);
- *tick -= (diff - MAX_DIFF_TICK);
- }
- else if( diff < PAST_DIFF_TICK )
- {
- return false;
- }
- else if( diff < MIN_DIFF_TICK )
- {
- ShowWarning("adjust_tick: tick diff too far in the past %d, adjusting to the minimm %d\n", diff, MIN_DIFF_TICK);
- *tick += (diff - MAX_DIFF_TICK);
- }
- return true;
- }
- /// Releases a timer.
- static
- void release_timer(int tid)
- {
- if( timer_data[tid].type == 0 )
- return;// already released
- memset(&timer_data[tid], 0, sizeof(struct TimerData));
- if( free_timer_list_num >= free_timer_list_max )
- {
- free_timer_list_max += 256;
- if( free_timer_list )
- RECREATE(free_timer_list, int, free_timer_list_max);
- else
- CREATE(free_timer_list, int, free_timer_list_max);
- memset(free_timer_list + (free_timer_list_max - 256), 0, sizeof(int)*256);
- }
- free_timer_list[free_timer_list_num++] = tid;
- }
- /// Returns a free timer id.
- static int acquire_timer(void)
- {
- int tid;
- // select a free timer
- tid = timer_data_num;
- while( free_timer_list_num )
- {
- int pos = --free_timer_list_num;
- if( free_timer_list[pos] < timer_data_num )
- {
- tid = free_timer_list[pos];
- break;
- }
- }
- // check available space
- if( tid >= timer_data_max )
- {
- timer_data_max += 256;
- if( timer_data )
- RECREATE(timer_data, struct TimerData, timer_data_max);
- else
- CREATE(timer_data, struct TimerData, timer_data_max);
- memset(timer_data + (timer_data_max - 256), 0, sizeof(struct TimerData)*256);
- }
- if( tid >= timer_data_num )
- timer_data_num = tid + 1;
- return tid;
- }
- /// Starts a new timer that is deleted once it expires (single-use).
- /// Returns the timer's id.
- int add_timer(unsigned int tick, TimerFunc func, int id, intptr data)
- {
- int tid;
- if( !adjust_tick(&tick) )
- {
- ShowError("add_timer: tick out of range (tick=%u %08x[%s] id=%d data=%d diff_tick=%d)\n", tick, (int)func, search_timer_func_list(func), id, data, DIFF_TICK(tick, gettick()));
- return INVALID_TIMER;
- }
- tid = acquire_timer();
- timer_data[tid].tick = tick;
- timer_data[tid].func = func;
- timer_data[tid].id = id;
- timer_data[tid].data = data;
- timer_data[tid].type = TIMER_ONCE_AUTODEL;
- timer_data[tid].interval = 1000;
- push_timer_heap(tid);
- return tid;
- }
- /// Starts a new timer that automatically restarts itself (infinite loop until manually removed).
- /// Returns the timer's id, or -1 if it fails.
- int add_timer_interval(unsigned int tick, TimerFunc func, int id, intptr data, int interval)
- {
- int tid;
- if( interval < 1 )
- {
- ShowError("add_timer_interval: invalid interval (tick=%u %08x[%s] id=%d data=%d diff_tick=%d)\n", tick, (int)func, search_timer_func_list(func), id, data, DIFF_TICK(tick, gettick()));
- return INVALID_TIMER;
- }
- if( !adjust_tick(&tick) )
- {
- ShowError("add_timer_interval: tick out of range (tick=%u %08x[%s] id=%d data=%d diff_tick=%d)\n", tick, (int)func, search_timer_func_list(func), id, data, DIFF_TICK(tick, gettick()));
- return INVALID_TIMER;
- }
- tid = acquire_timer();
- timer_data[tid].tick = tick;
- timer_data[tid].func = func;
- timer_data[tid].id = id;
- timer_data[tid].data = data;
- timer_data[tid].type = TIMER_INTERVAL;
- timer_data[tid].interval = interval;
- push_timer_heap(tid);
- return tid;
- }
- /// Retrieves internal timer data
- const struct TimerData* get_timer(int tid)
- {
- if( tid >= 0 && tid < timer_data_num && timer_data[tid].type != 0 )
- return &timer_data[tid];
- return NULL;
- }
- /// Marks a timer specified by 'id' for immediate deletion once it expires.
- /// Param 'func' is used for debug/verification purposes.
- /// Returns 0 on success, < 0 on failure.
- int delete_timer(int tid, TimerFunc func)
- {
- if( tid < 0 || tid >= timer_data_num || timer_data[tid].type == 0 )
- {
- ShowError("delete_timer error : no such timer %d (%08x(%s))\n", tid, (int)func, search_timer_func_list(func));
- return -1;
- }
- if( timer_data[tid].func != func )
- {
- ShowError("delete_timer error : function mismatch %08x(%s) != %08x(%s)\n", (int)timer_data[tid].func, search_timer_func_list(timer_data[tid].func), (int)func, search_timer_func_list(func));
- return -2;
- }
- if( timer_data[tid].type&TIMER_REMOVE_HEAP )
- // timer func being executed, make sure it's marked for removal when it ends
- timer_data[tid].type = TIMER_FORCE_REMOVE|TIMER_REMOVE_HEAP;
- else if( pop_timer_heap(tid) )
- release_timer(tid);
- return 0;
- }
- /// Adjusts a timer's expiration time.
- /// Returns the new tick value, or -1 if it fails.
- int addtick_timer(int tid, unsigned int tick)
- {
- return settick_timer(tid, timer_data[tid].tick+tick);
- }
- /// Modifies a timer's expiration time (an alternative to deleting a timer and starting a new one).
- /// Returns the new tick value, or -1 if it fails.
- int settick_timer(int tid, unsigned int tick)
- {
- if( tid < 0 || tid >= timer_data_num || timer_data[tid].type == 0 )
- {
- ShowError("settick_timer error : no such timer %d\n", tid);
- return -1;
- }
- if( timer_data[tid].tick == tick )
- return tick;
- if( !adjust_tick(&tick) )
- {
- ShowError("settick_timer: tick out of range, leaving timer unmodified (tid=%d tick=%u %08x[%s] diff_tick=%d)\n", tid, tick, (int)timer_data[tid].func, search_timer_func_list(timer_data[tid].func), DIFF_TICK(tick, gettick()));
- return -1;
- }
- pop_timer_heap(tid);
- if( tick == -1 )
- tick = 0;// -1 is reserved for error
- timer_data[tid].type &= ~TIMER_REMOVE_HEAP;
- timer_data[tid].tick = tick;
- push_timer_heap(tid);
- return tick;
- }
- /// Executes all expired timers.
- /// Returns the value of the smallest non-expired timer (or 1 second if there aren't any).
- int do_timer(unsigned int tick)
- {
- int diff = 1000; // return value
- // process all timers one by one
- while( timer_heap_num )
- {
- int tid = timer_heap[0]; // first element in heap (=>smallest)
- diff = DIFF_TICK(timer_data[tid].tick, tick);
- if( diff > 0 )
- break; // no more expired timers to process
- pop_timer_heap(tid);
- // mark timer as 'to be removed'
- timer_data[tid].type |= TIMER_REMOVE_HEAP;
- if( timer_data[tid].func )
- {
- if( diff < -1000 )
- // 1秒以上の大幅な遅延が発生しているので、
- // timer処理タイミングを現在値とする事で
- // 呼び出し時タイミング(引数のtick)相対で処理してる
- // timer関数の次回処理タイミングを遅らせる
- timer_data[tid].func(tid, tick, timer_data[tid].id, timer_data[tid].data);
- else
- timer_data[tid].func(tid, timer_data[tid].tick, timer_data[tid].id, timer_data[tid].data);
- }
- // in the case the function didn't change anything...
- if( timer_data[tid].type & TIMER_REMOVE_HEAP || timer_data[tid].type == TIMER_FORCE_REMOVE )
- {
- timer_data[tid].type &= ~TIMER_REMOVE_HEAP;
- switch( timer_data[tid].type )
- {
- case TIMER_FORCE_REMOVE:
- case TIMER_ONCE_AUTODEL:
- release_timer(tid);
- break;
- case TIMER_INTERVAL:
- if( DIFF_TICK(timer_data[tid].tick, tick) < -1000 )
- timer_data[tid].tick = tick + timer_data[tid].interval;
- else
- timer_data[tid].tick += timer_data[tid].interval;
- push_timer_heap(tid);
- break;
- }
- }
- }
- if( diff < TIMER_MIN_INTERVAL )
- return TIMER_MIN_INTERVAL;
- if( diff > TIMER_MAX_INTERVAL )
- return TIMER_MAX_INTERVAL;
- return diff;
- }
- unsigned long get_uptime(void)
- {
- return (unsigned long)difftime(time(NULL), start_time);
- }
- void timer_init(void)
- {
- time(&start_time);
- }
- void timer_final(void)
- {
- struct timer_func_list *tfl;
- struct timer_func_list *next;
- for( tfl=tfl_root; tfl != NULL; tfl = next ) {
- next = tfl->next; // copy next pointer
- aFree(tfl->name); // free structures
- aFree(tfl);
- }
- if (timer_data) aFree(timer_data);
- if (timer_heap) aFree(timer_heap);
- if (free_timer_list) aFree(free_timer_list);
- }
|