内核定时机制API之del_timer_sync

int del_timer_sync(struct timer_list *timer)的作用类似del_timer,但是删除的时候如果timer正在执行,则会
等待timer执行完成再删除.
其源码分析如下:
int del_timer_sync(struct timer_list *timer)
{
#检查死锁的code
#ifdef CONFIG_LOCKDEP
	unsigned long flags;

	/*
	 * If lockdep gives a backtrace here, please reference
	 * the synchronization rules above.
	 */
	local_irq_save(flags);
	lock_map_acquire(&timer->lockdep_map);
	lock_map_release(&timer->lockdep_map);
	local_irq_restore(flags);
#endif
	/*
	 * don't use it in hardirq context, because it
	 * could lead to deadlock.
	 */
	 #这个函数不能在中断中调用,因为可能会造成死锁
	WARN_ON(in_irq() && !(timer->flags & TIMER_IRQSAFE));
	for (;;) {
	#开始尝试删除这个timer,如果这个timer正在执行,则等待timer执行完按成后再删除
		int ret = try_to_del_timer_sync(timer);
		if (ret >= 0)
			return ret;
		cpu_relax();
	}
}
int try_to_del_timer_sync(struct timer_list *timer)
{
	struct timer_base *base;
	unsigned long flags;
	int ret = -1;

	debug_assert_init(timer);

	base = lock_timer_base(timer, &flags);
	#如果timer 正在执行,则不能删除这个timer。可以看到只要base->running_timer 不等于timer ,这个timer就
	#没有再运行了.
	if (base->running_timer != timer)
		ret = detach_if_pending(timer, base, true);

	raw_spin_unlock_irqrestore(&base->lock, flags);

	return ret;
}

你可能感兴趣的:(Linux,源码分析,kernel常用API源码分析)