delay with atomic works

This commit is contained in:
Ciro Santilli
2017-05-16 07:56:01 +01:00
parent e157a9ac14
commit 39d041484d
2 changed files with 9 additions and 7 deletions

48
kernel_module/sleep.c Normal file
View File

@@ -0,0 +1,48 @@
/*
Usage:
insmod /sleep.ko
rmmod sleep
dmesg prints an integer every second until rmmod.
Since insmod returns, this also illustrates how the work queues are asynchronous.
*/
#include <linux/delay.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/types.h> /* atomic_t */
#include <linux/workqueue.h>
MODULE_LICENSE("GPL");
static struct workqueue_struct *queue;
static atomic_t run = ATOMIC_INIT(1);
static void work_func(struct work_struct *work)
{
int i = 0;
while (atomic_read(&run)) {
printk(KERN_INFO "%d\n", i);
usleep_range(1000000, 1000001);
i++;
if (i == 10)
i = 0;
}
}
DECLARE_WORK(work, work_func);
int init_module(void)
{
queue = create_workqueue("myworkqueue");
queue_work(queue, &work);
return 0;
}
void cleanup_module(void)
{
atomic_set(&run, 0);
destroy_workqueue(queue);
}