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)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...
随机推荐
- SQL 自动增长 identity
create table Users( id ,),--id 从10000开始,增加长度为1 name ), ); --执行三次这个语句 insert into Users values('小昆虫') ...
- Python学习总结9:目录操作方法汇总
os.getcwd():得到当前工作目录,即当前Python脚本工作的目录路径os.listdir():返回指定目录下的所有文件和目录名os.remove():函数用来删除一个文件os.removed ...
- php setcookie(name, value, expires, path, domain, secure) 参数详解
setcookie() 定义一个和其余的 HTTP 标头一起发送的 cookie.和其它标头一样,cookie 必须在脚本的任何其它输出之前发送(这是协议限制).这 需要将本函数的调用放到任何输出之前 ...
- HDU 4898 The Revenge of the Princess’ Knight(后缀数组+二分+暴力)(2014 Multi-University Training Contest 4)
Problem Description There is an old country and the king fell in love with a devil. The devil always ...
- contentProvider
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools=&q ...
- [转]IP动态切换脚本
因为公司办公室要设置固定IP才行,而家里的IP段和公司是不一样的,家里采用了DHCP机制,这样每次就得改IP设置,很是不方便,就写了这个脚本来动态切换,很流畅的说!WINXP,WIN7测试通过~嘿嘿~ ...
- 14---Net基础加强
更新中,敬请期待............ 复习-匿名类型 Xml介绍
- PAT乙级 1005. 继续(3n+1)猜想 (25)
1005. 继续(3n+1)猜想 (25) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 卡拉兹(Callatz ...
- [Ubuntu] Install subversion1.8 on Ubuntu13.10
Subversion1.8 is difference far away from subversion1.7, here is the steps to install subversion1.8. ...
- 为centos添加额外的源
使用这个命令: yum install epel-release