| 1 |
/* |
| 2 |
* Cisco router simulation platform. |
| 3 |
* Copyright (c) 2005,2006 Christophe Fillot ([email protected]) |
| 4 |
* |
| 5 |
* timer.h: Management of timers. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef __TIMER_H__ |
| 9 |
#define __TIMER_H__ 1 |
| 10 |
|
| 11 |
#include <sys/types.h> |
| 12 |
#include <pthread.h> |
| 13 |
#include "utils.h" |
| 14 |
|
| 15 |
/* Default number of Timer Queues */ |
| 16 |
#define TIMERQ_NUMBER 10 |
| 17 |
|
| 18 |
/* Timer definitions */ |
| 19 |
typedef m_uint64_t timer_id; |
| 20 |
|
| 21 |
typedef struct timer_entry timer_entry_t; |
| 22 |
typedef struct timer_queue timer_queue_t; |
| 23 |
|
| 24 |
/* Defines callback function format */ |
| 25 |
typedef int (*timer_proc)(void *,timer_entry_t *); |
| 26 |
|
| 27 |
/* Timer flags */ |
| 28 |
#define TIMER_DELETED 1 |
| 29 |
#define TIMER_RUNNING 2 |
| 30 |
#define TIMER_BOUNDARY 4 |
| 31 |
|
| 32 |
/* Number of entries in hash table */ |
| 33 |
#define TIMER_HASH_SIZE 512 |
| 34 |
|
| 35 |
/* Timer properties */ |
| 36 |
struct timer_entry { |
| 37 |
long interval; /* Interval in msecs */ |
| 38 |
m_tmcnt_t expire,offset; /* Next execution date */ |
| 39 |
timer_proc callback; /* User callback function */ |
| 40 |
void *user_arg; /* Optional user data */ |
| 41 |
int flags; /* Flags */ |
| 42 |
timer_id id; /* Unique identifier */ |
| 43 |
int level; /* Criticity level */ |
| 44 |
|
| 45 |
timer_queue_t *queue; /* Associated Timer Queue */ |
| 46 |
timer_entry_t *prev,*next; /* Double linked-list */ |
| 47 |
}; |
| 48 |
|
| 49 |
/* Timer Queue */ |
| 50 |
struct timer_queue { |
| 51 |
timer_entry_t * volatile list; /* List of timers */ |
| 52 |
pthread_mutex_t lock; /* Mutex for concurrent accesses */ |
| 53 |
pthread_cond_t schedule; /* Scheduling condition */ |
| 54 |
pthread_t thread; /* Thread running timer loop */ |
| 55 |
int volatile running; /* Running flag */ |
| 56 |
int timer_count; /* Number of timers */ |
| 57 |
int level; /* Sum of criticity levels */ |
| 58 |
timer_queue_t *next; /* Next Timer Queue (for pools) */ |
| 59 |
}; |
| 60 |
|
| 61 |
/* Lock and unlock access to a timer queue */ |
| 62 |
#define TIMERQ_LOCK(queue) pthread_mutex_lock(&(queue)->lock) |
| 63 |
#define TIMERQ_UNLOCK(queue) pthread_mutex_unlock(&(queue)->lock) |
| 64 |
|
| 65 |
/* Remove a timer */ |
| 66 |
int timer_remove(timer_id id); |
| 67 |
|
| 68 |
/* Create a new timer */ |
| 69 |
timer_id timer_create_entry(m_tmcnt_t interval,int boundary,int level, |
| 70 |
timer_proc callback,void *user_arg); |
| 71 |
|
| 72 |
/* Create a timer on boundary, with an offset */ |
| 73 |
timer_id timer_create_with_offset(m_tmcnt_t interval,m_tmcnt_t offset, |
| 74 |
int level,timer_proc callback, |
| 75 |
void *user_arg); |
| 76 |
|
| 77 |
/* Set a new interval for a timer */ |
| 78 |
int timer_set_interval(timer_id id,long interval); |
| 79 |
|
| 80 |
/* Create a new timer queue */ |
| 81 |
timer_queue_t *timer_create_queue(void); |
| 82 |
|
| 83 |
/* Flush queues */ |
| 84 |
void timer_flush_queues(void); |
| 85 |
|
| 86 |
/* Add a specified number of queues to the pool */ |
| 87 |
int timer_pool_add_queues(int nr_queues); |
| 88 |
|
| 89 |
/* Initialize timer sub-system */ |
| 90 |
int timer_init(void); |
| 91 |
|
| 92 |
#endif |