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

View File

@@ -2,3 +2,5 @@
- <https://lwn.net/Kernel/LDD3/> the best, but always outdated, book. Updated source: <https://github.com/martinezjavier/ldd3> - <https://lwn.net/Kernel/LDD3/> the best, but always outdated, book. Updated source: <https://github.com/martinezjavier/ldd3>
- <https://github.com/agelastic/eudyptula> - <https://github.com/agelastic/eudyptula>
- <https://lwn.net>
- <http://www.makelinux.net>

View File

@@ -1,27 +1,29 @@
/* /*
Usage: Usage:
insmod /delay.ko insmod /sleep.ko
rmmod delay rmmod sleep
dmesg prints an integer every second until rmmod. dmesg prints an integer every second until rmmod.
Since insmod returns, this Illustrates how the work queues are asynchronous. Since insmod returns, this also illustrates how the work queues are asynchronous.
*/ */
#include <linux/delay.h> #include <linux/delay.h>
#include <linux/kernel.h> #include <linux/kernel.h>
#include <linux/module.h> #include <linux/module.h>
#include <linux/types.h> /* atomic_t */
#include <linux/workqueue.h> #include <linux/workqueue.h>
MODULE_LICENSE("GPL"); MODULE_LICENSE("GPL");
static struct workqueue_struct *queue; static struct workqueue_struct *queue;
static atomic_t run = ATOMIC_INIT(1);
static void work_func(struct work_struct *work) static void work_func(struct work_struct *work)
{ {
int i = 0; int i = 0;
while (1) { while (atomic_read(&run)) {
printk(KERN_INFO "%d\n", i); printk(KERN_INFO "%d\n", i);
usleep_range(1000000, 1000001); usleep_range(1000000, 1000001);
i++; i++;
@@ -41,8 +43,6 @@ int init_module(void)
void cleanup_module(void) void cleanup_module(void)
{ {
/* This waits for the work to finish. From docstring: */ atomic_set(&run, 0);
/* > Cancel @work and wait for its execution to finish. */
cancel_work_sync(&work);
destroy_workqueue(queue); destroy_workqueue(queue);
} }