Last active
September 11, 2023 07:41
-
-
Save itrobotics/4bdc417071f560b220ba to your computer and use it in GitHub Desktop.
a simple wait queue example for linux kernel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************* | |
* Copyright (c) 2015 Song Yang @ ittraining | |
* | |
* All rights reserved. | |
* This program is free to use, but the ban on selling behavior. | |
* Modify the program must keep all the original text description. | |
* | |
* 保留所有權利。 | |
* 本程式可任意使用,但是禁止販售行為。 | |
* 修改程式時必須保留所有原有文字說明。 | |
* | |
* Email: [email protected] | |
* Blog : http://blog.ittraining.com.tw | |
*******************************************************************************/ | |
#include <linux/module.h> | |
#include <linux/init.h> | |
#include <linux/sched.h> | |
#include <linux/time.h> | |
#include <linux/delay.h> | |
#include<linux/workqueue.h> | |
MODULE_LICENSE("GPL"); | |
MODULE_AUTHOR("ITtraining.com.tw"); | |
MODULE_DESCRIPTION("A Simple Blocking IO device RaspPi"); | |
/* declare a wait queue*/ | |
static wait_queue_head_t my_wait_queue; | |
/* declare a work queue*/ | |
struct work_struct workq; | |
void my_workqueue_handler(struct work_struct *work) | |
{ | |
printk("WORK QUEUE: I'm just a timer to wake up the sleeping moudlue. \n"); | |
msleep(10000); /* sleep */ | |
printk("WORK QUEUE: time up MODULE !! wake up !!!! \n"); | |
wake_up_interruptible(&my_wait_queue); | |
} | |
/* | |
* INIT_MODULE -- MODULE START -- | |
* */ | |
int init_module(void) | |
{ | |
printk("Wait queue example ....\n"); | |
// -- initialize the work queue | |
INIT_WORK(&workq, my_workqueue_handler); | |
schedule_work(&workq); | |
// -- initialize the WAIT QUEUE head | |
init_waitqueue_head(& my_wait_queue); | |
printk("MODULE: This moudle is goint to sleep....\n"); | |
interruptible_sleep_on(&my_wait_queue); | |
printk("MODULE: Wakeup Wakeup I am Waked up........\n"); | |
return 0; | |
} | |
/* | |
* CLEANUP_MODULE -- MODULE END -- | |
* */ | |
void cleanup_module(void) | |
{ | |
printk("<1> Start to cleanup \n"); | |
} |
This looks like a race condition, shouldn't init_waitqueue_head
be called before schedule_work
? Otherwise, it's possible the thread could be pre-empted and the workqueue thread could call wake_up_interruptible
before init_waitqueue_head
was called. Right?
interruptible_sleep_on has been deprecated for a decade.
This program is free to use, but the ban on selling behavior.
This is a obvious violation of GPL. Please either remove MODULE_LICENSE("GPL");
or use a GPL compatible license.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is one with QEMU + Buildroot + BusyBox boilerplate for kernel 4.9: https://github.com/cirosantilli/linux-kernel-module-cheat/blob/e496f5d538f893d94b24f2779479f071cec40a5d/kernel_module/wait_queue.c