Chinaunix首页 | 论坛 | 博客
  • 博客访问: 186994
  • 博文数量: 45
  • 博客积分: 1456
  • 博客等级: 上尉
  • 技术积分: 535
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-21 17:47
文章分类
文章存档

2012年(1)

2009年(43)

2008年(1)

我的朋友

分类: LINUX

2009-11-11 13:19:19

include/linux/timer.h
struct timer_list {
struct list_head entry;
unsigned long expires;

spinlock_t lock;
unsigned long magic;

void (*function)(unsigned long);
unsigned long data;

struct tvec_t_base_s *base;
};


kernel/timer.c
struct tvec_t_base_s {
spinlock_t lock;
unsigned long timer_jiffies;
struct timer_list *running_timer;
tvec_root_t tv1;
tvec_t tv2;
tvec_t tv3;
tvec_t tv4;
tvec_t tv5;
} ____cacheline_aligned_in_smp;

typedef struct tvec_t_base_s tvec_base_t;




The processing of timer_list

1. Init

init_timer(apptimeout)

/***
* init_timer - initialize a timer.
* @timer: the timer to be initialized
*
* init_timer() must be done to a timer prior calling *any* of the
* other timer functions.
*/
static inline void init_timer(struct timer_list * timer)
{
timer->base = NULL;
timer->magic = TIMER_MAGIC;
spin_lock_init(&timer->lock);
}

2. setup the independent field of timer_list (include data, function, expires..)
apptimeout.data = (unsigned long)master_conntrack;
apptimeout.function = free_appdata_by_timeout;
apptimeout.expires = jiffies + layer7_appdata_timeout;

3. add the timer_list
add_timer(apptimeout);

static inline void add_timer(struct timer_list * timer)
{
__mod_timer(timer, timer->expires);
}

int __mod_timer(struct timer_list *timer, unsigned long expires)

4. delete the timer_list
del_timer

/***
* del_timer - deactive a timer.
* @timer: the timer to be deactivated
*
* del_timer() deactivates a timer - this works on both active and inactive
* timers.
*
* The function returns whether it has deactivated a pending timer or not.
* (ie. del_timer() of an inactive timer returns 0, del_timer() of an
* active timer returns 1.)
*/
int del_timer(struct timer_list *timer)
{
unsigned long flags;
tvec_base_t *base;

check_timer(timer);

repeat:
base = timer->base;
if (!base)
return 0;
spin_lock_irqsave(&base->lock, flags);
if (base != timer->base) {
spin_unlock_irqrestore(&base->lock, flags);
goto repeat;
}
list_del(&timer->entry);
/* Need to make sure that anybody who sees a NULL base also sees the list ops */
smp_wmb();
timer->base = NULL;
spin_unlock_irqrestore(&base->lock, flags);

return 1;
}
阅读(1509) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~