【Linux驱动篇】长延时、短延时和睡眠延时

一、长延时

msecs_to_jiffies(msec); //将毫秒数转换为jiffies数
timer_before(a, b);
timer_after(b, a);

二、短延时

udelay(unsigned long usecs);
ndelay(unsigned long nsecs);
mdelay(unsigned long msecs);

以上三个都是忙等待,类似于while(time){time–;}会一直占用CPU,所以对于毫秒级mdelay函数不建议使用,转而使用msleep函数代替。前两者主要用于硬件上对延时要求高的时候使用。

三、睡眠延时

void msleep(unsigned int msecs)
{
    unsigned long timeout = msecs_to_jiffies(msecs) + 1;

	while (timeout)
		timeout = schedule_timeout_uninterruptible(timeout);
}

unsigned long msleep_interruptible(unsigned int msecs)
{
    unsigned long timeout = msecs_to_jiffies(msecs) + 1;

	while (timeout && !signal_pending(current))
		timeout = schedule_timeout_interruptible(timeout);
	return jiffies_to_msecs(timeout);
}

signed long __sched schedule_timeout_uninterruptible(signed long timeout)
{
	__set_current_state(TASK_UNINTERRUPTIBLE);
	return schedule_timeout(timeout);
}

signed long __sched schedule_timeout(signed long timeout)
{
	struct timer_list timer;
	unsigned long expire;

	switch (timeout)
	{
	case MAX_SCHEDULE_TIMEOUT:
		/*
		 * These two special cases are useful to be comfortable
		 * in the caller. Nothing more. We could take
		 * MAX_SCHEDULE_TIMEOUT from one of the negative value
		 * but I' d like to return a valid offset (>=0) to allow
		 * the caller to do everything it want with the retval.
		 */
		schedule();
		goto out;
	default:
		/*
		 * Another bit of PARANOID. Note that the retval will be
		 * 0 since no piece of kernel is supposed to do a check
		 * for a negative retval of schedule_timeout() (since it
		 * should never happens anyway). You just have the printk()
		 * that will tell you if something is gone wrong and where.
		 */
		if (timeout < 0) {
			printk(KERN_ERR "schedule_timeout: wrong timeout "
				"value %lx\n", timeout);
			dump_stack();
			current->state = TASK_RUNNING;
			goto out;
		}
	}

	expire = timeout + jiffies;

	setup_timer_on_stack(&timer, process_timeout, (unsigned long)current);
	__mod_timer(&timer, expire, false, TIMER_NOT_PINNED);
	schedule();
	del_singleshot_timer_sync(&timer);

	/* Remove the timer from the object tracker */
	destroy_timer_on_stack(&timer);

	timeout = expire - jiffies;

 out:
	return timeout < 0 ? 0 : timeout;
}

static void process_timeout(unsigned long __data)
{
	wake_up_process((struct task_struct *)__data);
}


//in include/linux/sched.h
static inline int signal_pending(struct task_struct *p) {
    return unlikely(test_tsk_thread_flag(p, TIF_SIGPENDING)); // p->thread_info->flags中TIF_SIGPENDING位是否置位
}
#define TIF_SIGPENDING          2   

static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag) {
    return test_ti_thread_flag(task_thread_info(tsk), flag);
}

static inline int test_ti_thread_flag(struct thread_info *ti, int flag) {  
    return test_bit(flag, &ti->flags);
}

static inline int test_bit(int nr, const volatile void * addr) {  
    return (1UL &(((const int *) addr)[nr >> 5] >>(nr & 31) )) != 0UL; // 检测addr的第nr位是否为1(addr右起最低位为第0位)

  msleep()是不可打断的睡眠,msleep_interruptible()是可打断的睡眠,所以在条件判断中!signal_pending(current)判断是否是被信号唤醒的(被信号唤醒signal_pending(current)返回值不为0),如果不是则继续执行schedule_timeout_interruptible()进行睡眠
  setup_timer_on_stack创建一个定时器,到期时间为expire = timeout + jiffies,到期后执行process_timeout,此函数就是用来调用wake_up_process唤醒当前进程,从schedule()下一句继续执行,但由于可能是被信号提前唤醒,所以执行timeout = expire - jiffies;得到剩余timeout的睡眠时间,返回给用户

四、jiffies和Hz

  节拍代表时钟相邻两次中断的时间
  Hz代表节拍率,意思是时钟1s内产生的中断次数,所以节拍=1/Hz
  jiffies用来统计系统启动以来系统中产生的总节拍数。该变量在系统启动时被初始化为0,接下来没进行一次时钟中断,jiffies自动加1。因此,知道了总的节拍数,然后再除以Hz,即可知系统的运行时间(jiffies/Hz)。
  同理,expire = jiffies + Hz代表延时一秒,expire = jiffies + n * Hz代表延时n秒

No pains, no gains

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值