天天看點

29.linux核心(線程/程序)同步——完成量

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);
           

繼續閱讀