hrtimer和work工作队列的使用
1.hrtimers - 为高分辨率kernel定时器,可作为超时或周期性定时器使用
1). hrtimer_init初始化定时器工作模式。
hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
vibe_timer.function = vibrator_timer_func;
/* 设置定时器的回调函数,定时器到时该函数将被调用 */
static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
注:该回调函数为原子操作不能被中断
2). hrtimer_start的第二个参数用于设置超时参数。
hrtimer_start(&vibe_timer,
ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
3). INIT_WORK初始化工作队列。
INIT_WORK(&vibe_work, vibe_work_func);
static void vibe_work_func(struct work_struct *work)
4). schedule_work调用工作队列。
schedule_work(&vibe_work);
- /* linux/drivers/ker-driver.c
- * Author: Woodpecker <Pecker.hu@gmail.com>
- *
- * kernel-driver
- *
- * This file is subject to the terms and conditions of the GNU General Public
- * License. See the file COPYING in the main directory of this archive for
- * more details.
- *
- */
- #include <linux/module.h> /* MODULE_LICENSE */
- #include <linux/kernel.h> /* printk,pr_info */
- #include <linux/errno.h> /* EINVAL,EAGAIN,etc. */
- #include <linux/err.h> /* IS_ERR */
- #include <linux/fb.h> /* FB header file */
- #include <linux/init.h> /* module_init */
- #include <linux/semaphore.h> /* init_MUTEX APIs */
- #include <linux/mm.h> /* vm_area_struct */
- #include <linux/dma-mapping.h> /* DMA APIs */
- #include <linux/delay.h> /* mdelay,msleep */
- #include <linux/hrtimer.h>
- #include <linux/time.h> /* struct timespec */
- #define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);
- static struct hrtimer vibe_timer;
- static struct work_struct vibe_work;
- static void vibe_work_func(struct work_struct *work)
- {
- KER_PRINT("vibe_work_func:msleep(50)/n");
- msleep(50); /* CPU sleep */
- }
- static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
- {
- struct timespec uptime;
- do_posix_clock_monotonic_gettime(&uptime);
- KER_PRINT("Time:%lu.%02lu/n",
- (unsigned long) uptime.tv_sec,
- (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
- KER_PRINT("vibrator_timer_func/n");
- schedule_work(&vibe_work);
- return HRTIMER_NORESTART;
- }
- static int __init ker_driver_init(void)
- {
- int value = 2000; /* Time out setting,2 seconds */
- struct timespec uptime;
- KER_PRINT("ker_driver_init/n");
- hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
- vibe_timer.function = vibrator_timer_func;
- hrtimer_start(&vibe_timer,
- ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
- //static inline ktime_t ktime_set(const long secs, const unsigned long nsecs) 第一个参数为秒,第二个为纳秒
- do_posix_clock_monotonic_gettime(&uptime);
- KER_PRINT("Time:%lu.%02lu/n",
- (unsigned long) uptime.tv_sec,
- (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
- INIT_WORK(&vibe_work, vibe_work_func); /* Intialize the work queue */
- return 0;
- }
- static void __exit ker_driver_exit(void)
- {
- hrtimer_cancel(&vibe_timer);
- }
- module_init(ker_driver_init);
- module_exit(ker_driver_exit);
- MODULE_AUTHOR("Woodpecker <Pecker.hu@gmail.com>");
- MODULE_DESCRIPTION("Kernel driver");
- MODULE_LICENSE("GPL");
驱动的运行结果:
hrtimer和work工作队列的使用的更多相关文章
- Linux下的hrtimer高精度定时器【转】
转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...
- linux下jiffies定时器和hrtimer高精度定时器【转】
本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...
- 工作队列(workqueue) create_workqueue/schedule_work/queue_work
--- 项目需要,在驱动模块里用内核计时器timer_list实现了一个状态机.郁闷的是,运行时总报错"Scheduling while atomic",网上搜了一下:" ...
- 第3.3 案例2: 工作队列 job queue
第2个案例就是工作队列,典型的点对点的消息,一个Producer发送一个工作消息到队列去,具有Listener类的Consumer能够从工作队列中获得一个工作情况的消息,这个消息被这个消费者消费掉之后 ...
- 【译】RabbitMQ:工作队列(Work Queue)
在第一篇我们写了两个程序通过一个命名的队列分别发送和接收消息.在这一篇,我们将创建一个工作队列在多个工作线程间分发耗时的工作任务. 工作队列的核心思想是避免立刻处理资源密集型任务导致必须等待其执行完成 ...
- rabbitmq消息队列——"工作队列"
二."工作队列" 在第一节中我们发送接收消息直接从队列中进行.这节中我们会创建一个工作队列来分发处理多个工作者中的耗时性任务. 工作队列主要是为了避免进行一些必须同步等待的资源密集 ...
- RabbitMQ入门教程——工作队列
什么是工作队列 工作队列是为了避免等待一些占用大量资源或时间操作的一种处理方式.我们把任务封装为消息发送到队列中,消费者在后台不停的取出任务并且执行.当运行了多个消费者工作进程时,队列中的任务将会在每 ...
- RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)
工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...
- RabbitMQ 工作队列
创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...
随机推荐
- Leetcode: Palindrome Pairs
Given a list of unique words, find all pairs of distinct indices (i, j) in the given list, so that t ...
- SWF类标准开头Tag
[SWF(width="800", height="600", backgroundColor="#ffffff", frameRate=& ...
- TPageControl组件
TPageControl的功能是创建多个Dialog页,而这些重叠的每一个页Dialog就是通过TTabSheet对象实现的
- poj 算法 分类
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 最近AC题:2528 更新时间:2011.09.22 ...
- 搜集好的java技术帖子,持续更新,java程序员的要求
1.Java NIO 系列教程 2.Java实现 二叉搜索树算法(BST) 3. Java 并发工具包 java.util.concurrent 用户指南 4.架构师之路系列:http://blog. ...
- C++之路进阶——codevs2366(朋友圈)
2366 朋友圈 2012年省队选拔赛河北 时间限制: 10 s 空间限制: 256000 KB 题目等级 : 大师 Master 题目描述 Description 在很久很久以前,曾经 ...
- Tomcat8.5
说明:Tomcat服务器上一个符合J2EE标准的Web服务器,在tomcat中无法运行EJB程序,如果要运行可以选择能够运行EJB程序的容器WebLogic,WebSphere,Jboss等Tomca ...
- 在linux中的virtualbox无法挂载usb设备的解决方法
方法来源于网络. 在安装完virtualbox之后,virtualbox会建立一个名为 vboxusers 的组,将你的用户名加入到该组即可. 命令参考: #usermod -a -G vboxuse ...
- C语言初学者代码中的常见错误与瑕疵(2)
问题: 另一种阶乘 大家都知道阶乘这个概念,举个简单的例子:5!=1*2*3*4*5. 现在我们引入一种新的阶乘概念,将原来的每个数相乘变为i不大于n的所有奇数相乘 例如:5!!=1*3*5.现在明白 ...
- HTML5动画软件工具编辑器 HTML5动画分类 工具推荐
接下来介绍几款制作HTML5动画的工具,它们可以分为几类: 1.导出canvas动画: Flash CC(13.1).Animation.Radi 2.导出DIV+CSS3动画: HTML5 Make ...