mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
delay with atomic works
This commit is contained in:
@@ -2,3 +2,5 @@
|
||||
|
||||
- <https://lwn.net/Kernel/LDD3/> the best, but always outdated, book. Updated source: <https://github.com/martinezjavier/ldd3>
|
||||
- <https://github.com/agelastic/eudyptula>
|
||||
- <https://lwn.net>
|
||||
- <http://www.makelinux.net>
|
||||
|
||||
@@ -1,27 +1,29 @@
|
||||
/*
|
||||
Usage:
|
||||
|
||||
insmod /delay.ko
|
||||
rmmod delay
|
||||
insmod /sleep.ko
|
||||
rmmod sleep
|
||||
|
||||
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/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 (1) {
|
||||
while (atomic_read(&run)) {
|
||||
printk(KERN_INFO "%d\n", i);
|
||||
usleep_range(1000000, 1000001);
|
||||
i++;
|
||||
@@ -41,8 +43,6 @@ int init_module(void)
|
||||
|
||||
void cleanup_module(void)
|
||||
{
|
||||
/* This waits for the work to finish. From docstring: */
|
||||
/* > Cancel @work and wait for its execution to finish. */
|
||||
cancel_work_sync(&work);
|
||||
atomic_set(&run, 0);
|
||||
destroy_workqueue(queue);
|
||||
}
|
||||
Reference in New Issue
Block a user