linux等待队列使用案例

本文详细介绍了Linux内核中等待队列在设备驱动阻塞IO和线程挂起中的应用。通过示例代码展示了如何使用等待队列进行任务阻塞与唤醒,以及在f2fs文件系统GC线程中的实践。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 等待队列在设备驱动中实现阻塞IO

struct test_dev{
struct cdev cdev;
unsigned int current_len;
unsigned char mem[SIZE];
struct mutex mutex;
wait_queue_head_t r_wait;
wait_queue_head_t w_wait;

};
static ssize_t test_read(struct file *filp,char __user *buf,size_t count,loff_t *ppos)
{
int ret;
struct test_dev *dev=filp->private_data;
DECLARE_WAITQUEUE(wait,current);
mutex_lock(&dev->mutex);
add_wait_queue(&dev->r_wait,&wait);
while(dev->current_len == 0){
if(filp->f_flags & O_NONBLOCK){
ret=-EAGAIN;
goto out;
}
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->mutex);
schedule();
if(signal_pending(current)){
ret=-ERESTARTSYS;
goto out2;
}
mutex_lock(&dev->mutex);
}
if(count>dev->current_len)
count=dev->current_len;
if(copy_to_user(buf,dev->mem,count)){
ret=-EFAULT;
goto out;
}else{
memcpy(dev->mem,dev->mem+count,dev->current_len-count);
dev->current_len-=count;
wake_up_interruptible(&dev->w_wait);
ret=count;
}
out:
mutex_unlock(&dev->mutex);
out2:
remove_wait_queue(&dev->r_wait,&wait);
set_current_state(TASK_RUNNING);
return ret;
}
static ssize_t test_write(struct file *filp,const char __user *buf,size_t count,loff_t *ppos)
{
int ret;
struct test_dev *dev=filp->private_data;
DECLARE_WAITQUEUE(wait,current);
mutex_lock(&dev->mutex);
add_wait_queue(&dev->w_wait,&wait);
while(dev->current_len == SIZE){
if(filp->f_flags & O_NONBLOCK){
ret=-EAGAIN;
goto out;
}
__set_current_state(TASK_INTERRUPTIBLE);
mutex_unlock(&dev->mutex);
schedule();
if(signal_pending(current)){
ret=-ERESTARTSYS;
goto out2;
}
mutex_lock(&dev->mutex);
}
if(count>SIZE-dev->current_len)
count=SIZE-dev->current_len;
if(copy_from_user(dev->mem+dev->current_len,buf,count)){
ret=-EFAULT;
goto out;
}else{
dev->current_len+=count;
wake_up_interruptible(&dev->r_wait);
ret=count;
}
out:
mutex_unlock(&dev->mutex);
out2:
remove_wait_queue(&dev->w_wait,&wait);
set_current_state(TASK_RUNNING);
return ret;
}
static int __init test_init(void)
{

init_waitqueue_head(&test_devp->r_wait);
init_waitqueue_head(&test_devp->w_wait);


}
module_init(test_init);
当需要阻塞当前task的时候,先调用add_wait_queue把当前task添加到等待队列,然后调用shedule()释放cpu,需要唤醒的时候调用wake_up_interruptible唤醒等待队列,这样之前阻塞的task就会重新被调度。

2. 等待队列实现线程的挂起

这里以f2fs的gc线程举例说明。
static int gc_thread_func(void *data)
{
struct f2fs_sb_info *sbi = data;
struct f2fs_gc_kthread *gc_th = sbi->gc_thread;
*wait_queue_head_t wq = &sbi->gc_thread->gc_wait_queue_head;
unsigned int wait_ms;
wait_ms = gc_th->min_sleep_time;
set_freezable();
do {
wait_event_interruptible_timeout(wq,
kthread_should_stop() || freezing(current) ||
gc_th->gc_wake,
msecs_to_jiffies(wait_ms));

do_gc:
stat_inc_bggc_count(sbi);
/
if return value is not zero, no victim was selected /
if (f2fs_gc(sbi, test_opt(sbi, FORCE_FG_GC), true, NULL_SEGNO))
wait_ms = gc_th->no_gc_sleep_time;
trace_f2fs_background_gc(sbi->sb, wait_ms,
prefree_segments(sbi), free_segments(sbi));
/
balancing f2fs’s metadata periodically */
f2fs_balance_fs_bg(sbi);
next:
sb_end_write(sbi->sb);
} while (!kthread_should_stop());
return 0;
}
int f2fs_start_gc_thread(struct f2fs_sb_info *sbi)
{

init_waitqueue_head(&sbi->gc_thread->gc_wait_queue_head);
sbi->gc_thread->f2fs_gc_task = kthread_run(gc_thread_func, sbi,
“f2fs_gc-%u:%u”, MAJOR(dev), MINOR(dev));
if (IS_ERR(gc_th->f2fs_gc_task)) {
err = PTR_ERR(gc_th->f2fs_gc_task);
kvfree(gc_th);
sbi->gc_thread = NULL;
}
out:
return err;
}
调用init_waitqueue_head初始化等待队列,在线程函数里面调用wait_event_interruptible_timeout阻塞当前线程等待超时时间到达后自动唤醒继续执行,以此达到自动调节gc的频度。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值