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);

  1. /* linux/drivers/ker-driver.c
  2. * Author: Woodpecker <Pecker.hu@gmail.com>
  3. *
  4. * kernel-driver
  5. *
  6. * This file is subject to the terms and conditions of the GNU General Public
  7. * License.  See the file COPYING in the main directory of this archive for
  8. * more details.
  9. *
  10. */
  11. #include <linux/module.h>     /* MODULE_LICENSE     */
  12. #include <linux/kernel.h>     /* printk,pr_info     */
  13. #include <linux/errno.h>      /* EINVAL,EAGAIN,etc. */
  14. #include <linux/err.h>            /* IS_ERR             */
  15. #include <linux/fb.h>         /* FB header file     */
  16. #include <linux/init.h>           /* module_init        */
  17. #include <linux/semaphore.h>  /* init_MUTEX APIs    */
  18. #include <linux/mm.h>         /* vm_area_struct     */
  19. #include <linux/dma-mapping.h>  /* DMA APIs             */
  20. #include <linux/delay.h>      /* mdelay,msleep      */
  21. #include <linux/hrtimer.h>
  22. #include <linux/time.h>           /* struct timespec    */
  23. #define KER_PRINT(fmt, ...) printk("<ker-driver>"fmt, ##__VA_ARGS__);
  24. static struct hrtimer vibe_timer;
  25. static struct work_struct vibe_work;
  26. static void vibe_work_func(struct work_struct *work)
  27. {
  28. KER_PRINT("vibe_work_func:msleep(50)/n");
  29. msleep(50); /* CPU sleep */
  30. }
  31. static enum hrtimer_restart vibrator_timer_func(struct hrtimer *timer)
  32. {
  33. struct timespec uptime;
  34. do_posix_clock_monotonic_gettime(&uptime);
  35. KER_PRINT("Time:%lu.%02lu/n",
  36. (unsigned long) uptime.tv_sec,
  37. (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
  38. KER_PRINT("vibrator_timer_func/n");
  39. schedule_work(&vibe_work);
  40. return HRTIMER_NORESTART;
  41. }
  42. static int __init ker_driver_init(void)
  43. {
  44. int value = 2000;   /* Time out setting,2 seconds */
  45. struct timespec uptime;
  46. KER_PRINT("ker_driver_init/n");
  47. hrtimer_init(&vibe_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
  48. vibe_timer.function = vibrator_timer_func;
  49. hrtimer_start(&vibe_timer,
  50. ktime_set(value / 1000, (value % 1000) * 1000000),HRTIMER_MODE_REL);
  51. //static inline ktime_t ktime_set(const long secs, const unsigned long nsecs)  第一个参数为秒,第二个为纳秒
  52. do_posix_clock_monotonic_gettime(&uptime);
  53. KER_PRINT("Time:%lu.%02lu/n",
  54. (unsigned long) uptime.tv_sec,
  55. (uptime.tv_nsec / (NSEC_PER_SEC / 100)));
  56. INIT_WORK(&vibe_work, vibe_work_func);  /* Intialize the work queue */
  57. return 0;
  58. }
  59. static void __exit ker_driver_exit(void)
  60. {
  61. hrtimer_cancel(&vibe_timer);
  62. }
  63. module_init(ker_driver_init);
  64. module_exit(ker_driver_exit);
  65. MODULE_AUTHOR("Woodpecker <Pecker.hu@gmail.com>");
  66. MODULE_DESCRIPTION("Kernel driver");
  67. MODULE_LICENSE("GPL");

驱动的运行结果:

hrtimer和work工作队列的使用的更多相关文章

  1. Linux下的hrtimer高精度定时器【转】

    转自:http://blog.csdn.net/waverider2012/article/details/38305785 hrtimer高精度定时器的interval由ktime_set(cons ...

  2. linux下jiffies定时器和hrtimer高精度定时器【转】

    本文转载自:http://blog.csdn.net/dosculler/article/details/7932315 一.jiffies定时器,HZ=100,精度只能达到10ms. 注:采用jif ...

  3. 工作队列(workqueue) create_workqueue/schedule_work/queue_work

    --- 项目需要,在驱动模块里用内核计时器timer_list实现了一个状态机.郁闷的是,运行时总报错"Scheduling while atomic",网上搜了一下:" ...

  4. 第3.3 案例2: 工作队列 job queue

    第2个案例就是工作队列,典型的点对点的消息,一个Producer发送一个工作消息到队列去,具有Listener类的Consumer能够从工作队列中获得一个工作情况的消息,这个消息被这个消费者消费掉之后 ...

  5. 【译】RabbitMQ:工作队列(Work Queue)

    在第一篇我们写了两个程序通过一个命名的队列分别发送和接收消息.在这一篇,我们将创建一个工作队列在多个工作线程间分发耗时的工作任务. 工作队列的核心思想是避免立刻处理资源密集型任务导致必须等待其执行完成 ...

  6. rabbitmq消息队列——"工作队列"

    二."工作队列" 在第一节中我们发送接收消息直接从队列中进行.这节中我们会创建一个工作队列来分发处理多个工作者中的耗时性任务. 工作队列主要是为了避免进行一些必须同步等待的资源密集 ...

  7. RabbitMQ入门教程——工作队列

    什么是工作队列 工作队列是为了避免等待一些占用大量资源或时间操作的一种处理方式.我们把任务封装为消息发送到队列中,消费者在后台不停的取出任务并且执行.当运行了多个消费者工作进程时,队列中的任务将会在每 ...

  8. RabbitMQ官方中文入门教程(PHP版) 第二部分:工作队列(Work queues)

    工作队列 在第一篇教程中,我们已经写了一个从已知队列中发送和获取消息的程序.在这篇教程中,我们将创建一个工作队列(Work Queue),它会发送一些耗时的任务给多个工作者(Works ). 工作队列 ...

  9. RabbitMQ 工作队列

    创建一个工作队列用来在工作者(consumer)间分发耗时任务. 工作队列的主要任务是:避免立刻执行资源密集型任务,然后必须等待其完成.相反地,我们进行任务调度:我们把任务封装为消息发送给队列.工作进 ...

随机推荐

  1. VCL Tclientsocket, Tserversocket控件安装方法

    菜单component->Install Packets 按Add按钮,选择delphi目录里的bin目录下的dclsockets70.bpl(delphi2010是dclsockets140. ...

  2. nyist 47 过河问题

    http://acm.nyist.net/JudgeOnline/problem.php?pid=47 过河问题 时间限制:1000 ms  |  内存限制:65535 KB 难度:5   描述 在漆 ...

  3. [转] FastJson---高性能JSON开发包

    原文地址: FastJson---高性能JSON开发包 Fastjson介绍 Fastjson是一个Java语言编写的JSON处理器,由阿里巴巴公司开发.1.遵循http://json.org标准,为 ...

  4. (转)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 ...

  5. logstash5.x改变

    5.x版本 logstash中 elasticsearch插件的workers,无法配置大于1,会提示 This plugin uses the shared and doesn't need thi ...

  6. 夺命雷公狗---DEDECMS----3快速入门之隐藏

    如果我们在工作的时候遇到上操蛋的老板,本来公司是做医疗器械的,但是老板突然老了句我们不做医疗了,我们该做电影网,那么我们可以先将原本的栏目进行修改成隐藏栏目, 主要是预防变态老板突然来句“电影网更不好 ...

  7. [经典php视频]构建正则表达式解析网页中的图像标记<img>

    这是高洛峰php视频中的一段,视频中一边分析需要的功能,一边构建greg_match函数的参数,边讲解边实战,是非常好的一种构建功能的演示. 你不可能把浩瀚的IT资料都记在脑袋里,也不可能随时随地透过 ...

  8. OpenGL拾取注意事项

    GLFrame框架本身不支持拾取,需要自己实现.以下代码是实现拾取功能的注意事项: void Test::doSelection(int xPos, int yPos) { GLfloat aspec ...

  9. SSAS的维度表之间的关系只能有一个不能有多个

    我们在SSAS中创建维度的时候,有时候可能一个维度需要用到多个表的字段作为维度属性,那么这多个表之间势必存在关联关系,但是切记维度表之间的关联关系有且只能有一个不能有多个,下面我们来看一个例子. 现在 ...

  10. Android多线程通信之Handler

    主线程 public class MainActivity extends ActionBarActivity { private Handler handler; // private Thread ...