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)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...
随机推荐
- VCL Tclientsocket, Tserversocket控件安装方法
菜单component->Install Packets 按Add按钮,选择delphi目录里的bin目录下的dclsockets70.bpl(delphi2010是dclsockets140. ...
- nyist 47 过河问题
http://acm.nyist.net/JudgeOnline/problem.php?pid=47 过河问题 时间限制:1000 ms | 内存限制:65535 KB 难度:5 描述 在漆 ...
- [转] FastJson---高性能JSON开发包
原文地址: FastJson---高性能JSON开发包 Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发.1.遵循http://json.org标准,为 ...
- (转)Aspone.Cells设置Cell数据格式 Setting Display Formats of Numbers and Dates
Setting Display Formats Using Microsoft Excel: Right-click on any desired cell and select Format Cel ...
- logstash5.x改变
5.x版本 logstash中 elasticsearch插件的workers,无法配置大于1,会提示 This plugin uses the shared and doesn't need thi ...
- 夺命雷公狗---DEDECMS----3快速入门之隐藏
如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...
- [经典php视频]构建正则表达式解析网页中的图像标记<img>
这是高洛峰php视频中的一段,视频中一边分析需要的功能,一边构建greg_match函数的参数,边讲解边实战,是非常好的一种构建功能的演示. 你不可能把浩瀚的IT资料都记在脑袋里,也不可能随时随地透过 ...
- OpenGL拾取注意事项
GLFrame框架本身不支持拾取,需要自己实现.以下代码是实现拾取功能的注意事项: void Test::doSelection(int xPos, int yPos) { GLfloat aspec ...
- SSAS的维度表之间的关系只能有一个不能有多个
我们在SSAS中创建维度的时候,有时候可能一个维度需要用到多个表的字段作为维度属性,那么这多个表之间势必存在关联关系,但是切记维度表之间的关联关系有且只能有一个不能有多个,下面我们来看一个例子. 现在 ...
- Android多线程通信之Handler
主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...