/**
 * @file timers.h
 * @brief Include functions for initialization, starting and destroing timers
 *  
 * Timers are system structures, that allows threads raise asynchronous event.
 * There is no need to allocate memory for this event, like threads must when 
 * creating new thread. The timer handling function is called after delay,
 * that thread can specified. Timers handling functions runs in context of thread, 
 * that was interuppted. Timers are storage in list, where timer structure has 
 * pointer to next timer.
 * 
 */

#ifndef TIMERS_H_
#define TIMERS_H_

/**
 * Count of procesor ticks per a microsecond
 */
#define MSIM_FREQUENCY_USEC 1  //ticks per usec // was 7

/**
 * Count of procesor ticks per a second
 */
#define MSIM_FREQUENCY (MSIM_FREQUENCY_USEC*1000000) //ticks per second

/**
 * Structure for storing list of timers
 */
struct _timer_list {
	/** pointer to head of list */
	struct timer *head;
	/** pointer to tail of list */
	struct timer *tail;
};
/** usual notation with .._t */
typedef struct _timer_list timer_list;
/** usual notation with .._t */
typedef struct timer_list timer_list_t;

/**
 * Structure for storing timer information
 */
struct timer {
	/** count of microseconds of delay */
	unsigned int usec;
	/** cycle of procesor on which the timer stops */
	unsigned int stop_cycle;
	/** pointer to handler function */
	void (* handler) (struct timer *, void *);
	/** pointer to data of handler */
	void * data;
	/** pointer to next timer in list */
	struct timer *next;
	/** pointer to previous timer in list */
	struct timer *prev;
};
/** usual notation with .._t */ 
typedef struct timer timer_t;
 
int timer_init (timer_t * tmr, const unsigned int usec, void (* handler) (timer_t *, void *), void * data);
void timer_start (timer_t * tmr);
void timer_destroy (timer_t * tmr);
int timer_pending (timer_t * tmr);
void timer_check ();
unsigned get_sysutime ();
int init_timers ();
void timer_debug (void);

#endif /*TIMERS_H_*/
