linux字符设备驱动程序的设计之休眠
2012-11-15 12:43
218 查看
linux字符设备驱动程序的设计之休眠 休眠对进程来说,意味着等待将来的某个时间发生。如何以安全的方式进入休眠,需要注意以下两点:
永远不要在原子上下文中进入休眠。
当进程被唤醒时,我们永远无法知道休眠了多长时间,或者休眠期间发生了什么事情。 等待队列就是一个进程链表,其中包含了等待某个特定事件的所有进程。在linux中,一个等待队列通过一个“等待队列头(wait quene head)”来管理,等待队列头是一个类型为wait_quene_head_t的结构体,定义在<linux/wait,h>中,可通过静态定义并初始化一个等待队列头:
DECLARE_WAIT_QUENE_HEAD(name);
或者使用动态方法:
wait_quene_head_t my_quene;
init_waitquene_head (&my_quene);
<2>唤醒休眠wake_up()
永远不要在原子上下文中进入休眠。
当进程被唤醒时,我们永远无法知道休眠了多长时间,或者休眠期间发生了什么事情。 等待队列就是一个进程链表,其中包含了等待某个特定事件的所有进程。在linux中,一个等待队列通过一个“等待队列头(wait quene head)”来管理,等待队列头是一个类型为wait_quene_head_t的结构体,定义在<linux/wait,h>中,可通过静态定义并初始化一个等待队列头:
DECLARE_WAIT_QUENE_HEAD(name);
define DECLARE_WAIT_QUEUE_HEAD(name) \ wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
或者使用动态方法:
wait_quene_head_t my_quene;
init_waitquene_head (&my_quene);
void __init_waitqueue_head(wait_queue_head_t *q, struct lock_class_key *key) { spin_lock_init(&q->lock); lockdep_set_class(&q->lock, key); INIT_LIST_HEAD(&q->task_list); }
typedef struct __wait_queue_head wait_queue_head_t;
struct __wait_queue_head { spinlock_t lock; struct list_head task_list; };
<1>、简单休眠
#define wait_event(wq, condition) \ do { \ if (condition) \ break; \ __wait_event(wq, condition); \ } while (0)
#define wait_event_timeout(wq, condition, timeout) \ ({ \ long __ret = timeout; \ if (!(condition)) \ __wait_event_timeout(wq, condition, __ret); \ __ret; \ })
#define wait_event_interruptible(wq, condition) \ ({ \ int __ret = 0; \ if (!(condition)) \ __wait_event_interruptible(wq, condition, __ret); \ __ret; \ })
#define wait_event_interruptible_timeout(wq, condition, timeout) \ ({ \ long __ret = timeout; \ if (!(condition)) \ __wait_event_interruptible_timeout(wq, condition, __ret); \ __ret; \ })
<2>唤醒休眠wake_up()
#define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL)
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
相关文章推荐
- Linux设备驱动程序学习(5)-高级字符驱动程序操作[(2)阻塞型I/O和休眠]
- linux字符设备驱动程序设计概述
- Linux字符设备驱动程序的设计
- Linux驱动程序设计--字符设备设备号
- Linux字符设备驱动程序的设计(初级)
- Linux字符设备驱动程序的设计
- Linux字符设备驱动程序开发(3)-LED驱动程序设计
- Linux设备驱动程序第三版学习(6)- 高级字符驱动程序操作(续1) - 进程休眠
- Linux设备驱动程序第三版学习(6)- 高级字符驱动程序操作(续1)- 进程休眠 .
- 关于linux中字符设备驱动程序的设计
- Linux设备驱动程序学习(5)-高级字符驱动程序操作〔(2)阻塞型I/O和休眠〕
- Linux设备驱动程序学习(5) -高级字符驱动程序操作[(2)阻塞型I/O和休眠]
- 基于mini6410的linux驱动学习总结(四 设计字符设备驱动程序)
- Linux驱动程序设计——动态申请主设备号
- 【Linux 驱动】第三章 字符设备驱动程序 (详细,优秀)
- Linux设备驱动程序学习(6) -高级字符驱动程序操作[(3)设备文件的访问控制]
- 高级字符驱动程序操作(Linux设备驱动程序)
- 小学生学嵌入式:)Linux_字符设备驱动程序_初探
- linux设备驱动程序之简单字符设备驱动
- 嵌入式Linux简单字符设备驱动程序---helloworld