1.  

概念:

  1.  

1> 阻塞操作      是指在执行设备操作时,若不能获得资源,则挂起进程直到满足操作条件后再进行操作。被挂起的进程进入休眠,被从调度器移走,直到条件满足;

  1.  

2> 非阻塞操作  在不能进行设备操作时,并不挂起,它或者放弃,或者不停地查询,直到可以进行操作。非阻塞应用程序通常使用select系统调用查询是否可以对设备进行无阻塞的访问最终会引发设备驱动中poll函数执行。

  1.  
  2. 1 /*
  3. * Theme for understanding the blocking and unblocking mechanism in device
  4. * driven module and difference of both modes
  5. *
  6. * Two aspects should be considered,firstly, when reading process is proceeding,
  7. * data must be existed in FIFO. Secconly, FIFO is non-full is prerequisite for
  8. * the writing process.
  9. *
  10. *Copyright (C) Continential- weizhen.diao@conti.engineering.com
  11. *
  12. */
  13.  
  14. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/sched.h>
  17. #include <linux/init.h>
  18. #include <linux/cdev.h>
  19. #include <linux/slab.h>
  20. #include <linux/poll.h>
  21.  
  22. #define conti_globalfifo_SIZE 0x1000
  23. #define FIFO_CLEAR 0x1
  24.  
  25. /*major devive number*/
  26. #define conti_globalfifo_MAJOR 231
  27.  
  28. static int conti_globalfifo_major = conti_globalfifo_MAJOR;
  29. module_param(conti_globalfifo_major, int, S_IRUGO);
  30.  
  31. /*conti_globalfifo device struct*/
  32. struct conti_globalfifo_dev {
  33. struct cdev cdev;
  34. unsigned int current_len;
  35. unsigned char mem[conti_globalfifo_SIZE];
  36. struct mutex mutex; //signal for concurrency control
  37. wait_queue_head_t r_wait; //list of kernel bidirectional loops for blocking reading
  38. wait_queue_head_t w_wait; //list of kernel bidirectional loops for blocking writing
  39. };
  40.  
  41. struct conti_globalfifo_dev *conti_globalfifo_devp;
  42.  
  43. static int conti_globalfifo_open(struct inode *inode, struct file *filp)
  44. {
  45. filp->private_data = conti_globalfifo_devp;
  46. return ;
  47. }
  48.  
  49. static int conti_globalfifo_release(struct inode *inode, struct file *filp)
  50. {
  51. return ;
  52. }
  53.  
  54. static long conti_globalfifo_ioctl(struct file *filp, unsigned int cmd,
  55. unsigned long arg)
  56. {
  57. struct conti_globalfifo_dev *dev = filp->private_data;
  58.  
  59. switch (cmd) {
  60. case FIFO_CLEAR:
  61. mutex_lock(&dev->mutex);
  62. dev->current_len = ;
  63. memset(dev->mem, , conti_globalfifo_SIZE);
  64. mutex_unlock(&dev->mutex);
  65.  
  66. printk(KERN_INFO "conti_globalfifo is set to zero\n");
  67. break;
  68.  
  69. default:
  70. return -EINVAL;
  71. }
  72. return ;
  73. }
  74.  
  75. static unsigned int conti_globalfifo_poll(struct file *filp, poll_table * wait)
  76. {
  77. unsigned int mask = ;
  78. struct conti_globalfifo_dev *dev = filp->private_data;
  79.  
  80. mutex_lock(&dev->mutex);
  81.  
  82. poll_wait(filp, &dev->r_wait, wait);
  83. poll_wait(filp, &dev->w_wait, wait);
  84.  
  85. if (dev->current_len != ) {
  86. mask |= POLLIN | POLLRDNORM;
  87. }
  88.  
  89. if (dev->current_len != conti_globalfifo_SIZE) {
  90. mask |= POLLOUT | POLLWRNORM;
  91. }
  92.  
  93. mutex_unlock(&dev->mutex);
  94. return mask;
  95. }
  96.  
  97. static ssize_t conti_globalfifo_read(struct file *filp, char __user *buf,
  98. size_t count, loff_t *ppos)
  99. {
  100. int ret;
  101. struct conti_globalfifo_dev *dev = filp->private_data;
  102. DECLARE_WAITQUEUE(wait, current);
  103.  
  104. mutex_lock(&dev->mutex);
  105. add_wait_queue(&dev->r_wait, &wait);
  106.  
  107. while (dev->current_len == ) {
  108. if (filp->f_flags & O_NONBLOCK) {
  109. ret = -EAGAIN;
  110. goto out;
  111. }
  112. __set_current_state(TASK_INTERRUPTIBLE);
  113. mutex_unlock(&dev->mutex);
  114.  
  115. schedule();
  116. if (signal_pending(current)) {
  117. ret = -ERESTARTSYS;
  118. goto out2;
  119. }
  120.  
  121. mutex_lock(&dev->mutex);
  122. }
  123.  
  124. if (count > dev->current_len)
  125. count = dev->current_len;
  126.  
  127. if (copy_to_user(buf, dev->mem, count)) {
  128. ret = -EFAULT;
  129. goto out;
  130. } else {
  131. memcpy(dev->mem, dev->mem + count, dev->current_len - count);
  132. dev->current_len -= count;
  133. printk(KERN_INFO "read %d bytes(s),current_len:%d\n", count,
  134. dev->current_len);
  135.  
  136. wake_up_interruptible(&dev->w_wait);
  137.  
  138. ret = count;
  139. }
  140. out:
  141. mutex_unlock(&dev->mutex);
  142. out2:
  143. remove_wait_queue(&dev->w_wait, &wait);
  144. set_current_state(TASK_RUNNING);
  145. return ret;
  146. }
  147.  
  148. static ssize_t conti_globalfifo_write(struct file *filp, const char __user *buf,
  149. size_t count, loff_t *ppos)
  150. {
  151. struct conti_globalfifo_dev *dev = filp->private_data;
  152. int ret;
  153. DECLARE_WAITQUEUE(wait, current);
  154.  
  155. mutex_lock(&dev->mutex);
  156. add_wait_queue(&dev->w_wait, &wait);
  157.  
  158. while (dev->current_len == conti_globalfifo_SIZE) {
  159. if (filp->f_flags & O_NONBLOCK) {
  160. ret = -EAGAIN;
  161. goto out;
  162. }
  163. __set_current_state(TASK_INTERRUPTIBLE);
  164.  
  165. mutex_unlock(&dev->mutex);
  166.  
  167. schedule();
  168. if (signal_pending(current)) {
  169. ret = -ERESTARTSYS;
  170. goto out2;
  171. }
  172.  
  173. mutex_lock(&dev->mutex);
  174. }
  175.  
  176. if (count > conti_globalfifo_SIZE - dev->current_len)
  177. count = conti_globalfifo_SIZE - dev->current_len;
  178.  
  179. if (copy_from_user(dev->mem + dev->current_len, buf, count)) {
  180. ret = -EFAULT;
  181. goto out;
  182. } else {
  183. dev->current_len += count;
  184. printk(KERN_INFO "written %d bytes(s),current_len:%d\n", count,
  185. dev->current_len);
  186.  
  187. wake_up_interruptible(&dev->r_wait);
  188.  
  189. ret = count;
  190. }
  191.  
  192. out:
  193. mutex_unlock(&dev->mutex);
  194. out2:
  195. remove_wait_queue(&dev->w_wait, &wait);
  196. set_current_state(TASK_RUNNING);
  197. return ret;
  198. }
  199.  
  200. static const struct file_operations conti_globalfifo_fops = {
  201. .owner = THIS_MODULE,
  202. .read = conti_globalfifo_read,
  203. .write = conti_globalfifo_write,
  204. .unlocked_ioctl = conti_globalfifo_ioctl,
  205. .poll = conti_globalfifo_poll,
  206. .open = conti_globalfifo_open,
  207. .release = conti_globalfifo_release,
  208. };
  209.  
  210. static void conti_globalfifo_setup_cdev(struct conti_globalfifo_dev *dev, int index)
  211. {
  212. int err, devno = MKDEV(conti_globalfifo_major, index);
  213.  
  214. cdev_init(&dev->cdev, &conti_globalfifo_fops);
  215. dev->cdev.owner = THIS_MODULE;
  216. err = cdev_add(&dev->cdev, devno, );
  217. if (err)
  218. printk(KERN_NOTICE "Error %d adding conti_globalfifo%d", err, index);
  219. }
  220.  
  221. static int __init conti_globalfifo_init(void)
  222. {
  223. int ret;
  224. dev_t devno = MKDEV(conti_globalfifo_major, );
  225.  
  226. if (conti_globalfifo_major)
  227. ret = register_chrdev_region(devno, , "conti_globalfifo");
  228. else {
  229. ret = alloc_chrdev_region(&devno, , , "conti_globalfifo");
  230. conti_globalfifo_major = MAJOR(devno);
  231. }
  232. if (ret < )
  233. return ret;
  234.  
  235. conti_globalfifo_devp = kzalloc(sizeof(struct conti_globalfifo_dev), GFP_KERNEL);
  236. if (!conti_globalfifo_devp) {
  237. ret = -ENOMEM;
  238. goto fail_malloc;
  239. }
  240.  
  241. conti_globalfifo_setup_cdev(conti_globalfifo_devp, );
  242.  
  243. mutex_init(&conti_globalfifo_devp->mutex);
  244. init_waitqueue_head(&conti_globalfifo_devp->r_wait);
  245. init_waitqueue_head(&conti_globalfifo_devp->w_wait);
  246.  
  247. return ;
  248.  
  249. fail_malloc:
  250. unregister_chrdev_region(devno, );
  251. return ret;
  252. }
  253. module_init(conti_globalfifo_init);
  254.  
  255. static void __exit conti_globalfifo_exit(void)
  256. {
  257. cdev_del(&conti_globalfifo_devp->cdev);
  258. kfree(conti_globalfifo_devp);
  259. unregister_chrdev_region(MKDEV(conti_globalfifo_major, ), );
  260. }
  261. module_exit(conti_globalfifo_exit);
  262.  
  263. MODULE_AUTHOR(" <Hemingway <weizhen.diao@conti.engineering.com>>");
  1. MODULE_LICENSE("GPL v2");

blocking and unblocking mechanism for linux drivern code的更多相关文章

  1. linux source code search

    https://elixir.bootlin.com/linux/latest/source/fs/eventpoll.c#L1120

  2. The Select mechanism in linux for block mechanism

    Today, some one mention theknowledge of Select Mechanism. It's better to konw something about it ! O ...

  3. Whats meaning of “EXPORT_SYMBOL” in Linux kernel code?

    EXPORT_SYMBOL的作用是什么? EXPORT_SYMBOL标签内定义的函数或者符号对全部内核代码公开,不用修改内核代码就可以在您的内核模块中直接调用,即使用EXPORT_SYMBOL可以将一 ...

  4. [linux]安装code::blocks

    1.安装基本编译环境 $sudo apt-get install build-essential $sudo apt-get install gdb 2.安装codeblock $sudo apt-g ...

  5. [转] KVM Internals, code and more

    KVM Kernel-based Virtual Machine Internals, code and more http://slides.com/braoru/kvm#/ What behind ...

  6. linux内核设计模式

    原文来自:http://lwn.net/Articles/336224/ 选择感兴趣内容简单翻译了下: 在内核社区一直以来的兴趣是保证质量.我们需要保证和改善质量是显而易见的.但是如何做到却不是那么简 ...

  7. Linux 驱动开发

    linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...

  8. Process Kill Technology && Process Protection Against In Linux

    目录 . 引言 . Kill Process By Kill Command && SIGNAL . Kill Process By Resource Limits . Kill Pr ...

  9. 【转载】关于Embedded Linux启动的经典问题

    转载自:http://linux.chinaunix.net/techdoc/install/2009/04/13/1107608.shtml 发信人: armlinux (armlinux), 信区 ...

随机推荐

  1. 【AR实验室】OpenGL ES绘制相机(OpenGL ES 1.0版本)

    0x00 - 前言 之前做一些移动端的AR应用以及目前看到的一些AR应用,基本上都是这样一个套路:手机背景显示现实场景,然后在该背景上进行图形学绘制.至于图形学绘制时,相机外参的解算使用的是V-SLA ...

  2. 在传统.NET Framework 上运行ASP.NET Core项目

    新的项目我们想用ASP.NET Core来开发,但是苦于我们历史的遗产很多,比如<使用 JavaScriptService 在.NET Core 里实现DES加密算法>,我们要估计等到.N ...

  3. JavaScript性能优化

    如今主流浏览器都在比拼JavaScript引擎的执行速度,但最终都会达到一个理论极限,即无限接近编译后程序执行速度. 这种情况下决定程序速度的另一个重要因素就是代码本身. 在这里我们会分门别类的介绍J ...

  4. Dapper.Contrib:GetAsync<T> only supports an entity with a [Key] or an [ExplicitKey] property

    异常处理:http://www.cnblogs.com/dunitian/p/4523006.html#dapper 原来Model是这样滴 修改后是这样滴 注意点:Model里面的Table和Key ...

  5. HTML5 语义元素(二)文本内容

    上一篇介绍的是关于页面结构方面的语义元素,本篇介绍文本内容方面,包含:<bdi>.<details>.<summary>.<mark>.<outp ...

  6. Velocity笔记--使用Velocity获取动态Web项目名的问题

    以前使用jsp开发的时候,可以通过request很轻松的获取到根项目名,现在换到使用velocity渲染视图,因为已经不依赖servlet,request等一些类的环境,而Web项目的根项目名又不是写 ...

  7. C++随笔:.NET CoreCLR之corleCLR核心探索之coreconsole(1)

    一看这个标题,是不去取名有点绕呢?或者是,还有些问题?报告LZ...你的标题取得有问题,是个病句!↖(^ω^)↗!!!先不要急,其实我今天带给大家的就是CoreCLR中的coreclr.其中它是在名字 ...

  8. H3 BPM引擎API接口

    引擎API接口通过 Engine 对象进行访问,这个是唯一入口. 示例1:获取组织机构对象 this.Engine.Organization.GetUnit("组织ID"); 示例 ...

  9. Android—万能ListView适配器

    ListView是开发中最常用的控件了,但是总是会写重复的代码,浪费时间又没有意义. 最近参考一些资料,发现一个万能ListView适配器,代码量少,节省时间,总结一下分享给大家. 首先有一个自定义的 ...

  10. docker4dotnet #4 使用Azure云存储构建高速 Docker registry

    使用Docker来构建应用程序最常见的操作就是 docker run 或者 docker pull了,但是由于众所周知的原因,在国内想要高速稳定的获取docker hub上面的资源并不是件容易的事情, ...