当前位置:   article > 正文

【Linux驱动篇】长延时、短延时和睡眠延时_schedule_timeout_uninterruptible的替代函数

schedule_timeout_uninterruptible的替代函数

一、长延时

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

二、短延时

udelay(unsigned long usecs);
ndelay(unsigned long nsecs);
mdelay(unsigned long msecs);
  • 1
  • 2
  • 3

以上三个都是忙等待,类似于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位)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

  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

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号