1.linux内核(线程/进程)同步——内存屏障
2.linux内核(线程/进程)同步——原子操作
3.linux内核(线程/进程)同步——自旋锁
4.linux内核(线程/进程)同步——信号量
5.linux内核(线程/进程)同步——互斥锁
6.linux内核(线程/进程)同步——完成量
完成量
完成量(completion)相当于初始值被初始化成0的信号量,所有实体直接进入等待状态。触发一次完成唤醒一个实体,也可以一次全部唤醒。
相关api在completion.h
//声明完成量
struct completion x;
//初始化完成量
void init_completion(struct completion *x)
//等待完成
void wait_for_completion(struct completion *);
int wait_for_completion_interruptible(struct completion *x);
int wait_for_completion_killable(struct completion *x);
unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout);
long wait_for_completion_interruptible_timeout(struct completion *x, unsigned long timeout);
long wait_for_completion_killable_timeout(struct completion *x, unsigned long timeout);
bool try_wait_for_completion(struct completion *x);
bool completion_done(struct completion *x);
//发送完成信号
void complete(struct completion *);
void complete_all(struct completion *);
完成量字符设备驱动
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <linux/cdev.h>
#include <linux/slab.h>
#include <asm/uaccess.h>
MODULE_LICENSE("GPL");
static int data = 1;
module_param(data, int, 0644); //声明模块参数
dev_t devid;
struct cdev char_dev;
struct class * char_class;
int buffer_size = 100;
char * char_data;
static struct completion data_lock;
static int open(struct inode * node, struct file * fl){
return 0;
}
static long ioctl(struct file * fl, unsigned int cmd, unsigned long arg){
return 0;
}
static ssize_t read(struct file * fl, char __user * buf, size_t len, loff_t * offset){
int ret = 0,copy_len,data_len;
wait_for_completion(&data_lock);
data_len = strlen(char_data)+1;
if(fl->f_pos + len > data_len)
copy_len = data_len - fl->f_pos; //超过长度,复制剩余部分
else
copy_len = len; //没超过
ret = copy_to_user(buf,char_data+fl->f_pos,copy_len);
ret = copy_len - ret;
*offset += ret; //移动文件指针
return ret;
}
static ssize_t write(struct file * fl, const char __user * buf, size_t len, loff_t * offset){
int ret = 0,copy_len,data_len = buffer_size;
complete(&data_lock);
if(fl->f_pos + len > data_len)
copy_len = data_len - fl->f_pos; //超过长度,复制剩余部分
else
copy_len = len; //没超过
ret = copy_from_user(char_data+fl->f_pos,buf,copy_len);
ret = copy_len - ret;
*offset += ret; //移动文件指针
return ret;
}
struct file_operations my_opts = {
.owner = THIS_MODULE,
.open = open,
.read = read,
.write = write,
.unlocked_ioctl = ioctl
};
static int __init char_init(void){
int ret = 0;
devid = MKDEV(241, 1); //换算设备号
ret = register_chrdev_region(devid, 1, "char_test");//注册设备,在/proc/drivers下面可以看到
if (ret < 0)
goto err0;
cdev_init(&char_dev,&my_opts); //绑定opt结构体
char_dev.owner = THIS_MODULE;
ret = cdev_add(&char_dev,devid,1); //注册字符设备驱动
if (ret < 0)
goto err1;
char_class = class_create(THIS_MODULE,"char_test"); //在/sys/class中创建文件夹
device_create(char_class,NULL,devid,NULL,"char_test_dev_%d",1);//在上一步文件夹中创建char_test_dev_1
char_data = kzalloc(buffer_size,GFP_KERNEL);
init_completion(&data_lock);
printk("char init\n");
return 0;
err1:
unregister_chrdev_region(devid, 1);
err0:
return ret;
}
static void __exit char_exit(void){
kfree(char_data);
unregister_chrdev_region(devid, 1);
cdev_del(&char_dev);
device_destroy(char_class,devid);
class_destroy(char_class);
printk("char exit\n");
}
module_init(char_init);
module_exit(char_exit);