sched: wait_bit sample

This commit is contained in:
Cheng Jian
2021-06-05 18:08:00 +08:00
parent e892ff59fe
commit 9bf617b883
2 changed files with 131 additions and 0 deletions
@@ -0,0 +1,76 @@
# ------------------------------------------------------------------------------
#
# Makefile for the LDD-LinuxDeviceDrivers.
#
# Author: gatieme
# Create: 2016-07-29 15:50:46
# Last modified: 2016-07-29 16:10:29
# Description:
# This program is loaded as a kernel(v2.6.18 or later) module.
# Use "make install" to load it into kernel.
# Use "make remove" to remove the module out of kernel.
#
# ------------------------------------------------------------------------------
# my driver description
DRIVER_VERSION := "1.0.0"
DRIVER_AUTHOR := "Gatieme @ AderStep Inc..."
DRIVER_DESC := "Linux input module for Elo MultiTouch(MT) devices"
DRIVER_LICENSE := "Dual BSD/GPL"
MODULE_NAME := wake_bit
#EXTRA_CFLAGS += -I$(SRC)
ifneq ($(KERNELRELEASE),)
obj-m := $(MODULE_NAME).o #print_vmarea.o
else
KERNELDIR ?= /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
modules:
make -C $(KERNELDIR) M=$(PWD) modules
modules_install:
make -C $(KERNELDIR) M=$(PWD) modules_install
insmod:
sudo insmod $(MODULE_NAME).ko
reinsmod:
sudo rmmod $(MODULE_NAME)
sudo insmod $(MODULE_NAME).ko
github:
cd $(ROOT) && make github
rmmod:
sudo rmmod $(MODULE_NAME)
clean:
make -C $(KERNELDIR) M=$(PWD) clean
rm -f modules.order Module.symvers Module.markers
.PHNOY:
modules modules_install clean
endif
@@ -0,0 +1,55 @@
#include <linux/init.h>
#include <linux/module.h>
#include <linux/printk.h>
#include <linux/kthread.h>
#include <linux/wait_bit.h>
#define pr_fmt(fmt) "WAIT_BIT" fmt
MODULE_LICENSE("Dual BSD/GPL");
#define WAIT_BIT 0
static unsigned long flag;
static int wait_bit_func(void *data)
{
while (!kthread_should_stop()) {
wait_on_bit(&flag, WAIT_BIT, TASK_INTERRUPTIBLE);
pr_info("[%s %d]\n", __func__, __LINE__);
set_bit(WAIT_BIT, &flag);
}
return 0;
}
struct task_struct *wait_task = NULL;
static __init int wait_bit_test_init(void)
{
printk("wait_bit module init\n");
wait_task = kthread_create(wait_bit_func, NULL, "wait_bit_thread");
if (wait_task)
wake_up_process(wait_task);
clear_bit(WAIT_BIT, &flag);
smp_mb__after_atomic();
wake_up_bit(&flag, WAIT_BIT);
return 0;
}
static __exit void wait_bit_test_exit(void)
{
if (wait_task)
kthread_stop(wait_task);
printk(KERN_ALERT "wait_bit module exit\n");
}
module_init(wait_bit_test_init);
module_exit(wait_bit_test_exit);