linux内核完成量completion使用说明

内核中完成量completion用来做同步操作。一段程序可以等待条件满足时才被唤醒继续执行。

使用流程:

定义并初始化完成量

 DECLARE_COMPLETION(mycompletion);

等待完成量

   wait_for_completion(&mycompletion);
   wait_for_completion_io_timeout(&mycompletion,timeout);

唤醒完成量

complete(&mycompletion);

以下是内核模块的代码:

#include <linux/uaccess.h>
#include <linux/fs.h>
#include <linux/stat.h>
#include <linux/cdev.h>
#include <linux/io.h>
#include <linux/gpio.h>
#include <linux/slab.h>
#include <linux/irq.h>
#include <linux/mutex.h>
#include <linux/interrupt.h>
#include <linux/bug.h>			/* For BUG_ON.  */
#include <linux/cpu.h>
#include <linux/init.h> /* Needed for the macros */
#include <linux/kernel.h> /* Needed for pr_info() */
#include <linux/module.h> /* Needed by all modules */
#include <linux/delay.h>
#include <linux/smp.h>
#include <linux/kernel_stat.h>
#include <linux/sched.h>
#include <linux/percpu-defs.h>
#include <linux/wait.h>
#include <linux/gpio/driver.h>
#include <linux/atomic.h>
#include <linux/platform_device.h>
#include <linux/poll.h>
#include <linux/kfifo.h>
#include <linux/completion.h>

#define DELAY_CMD_WRITE   _IOW('A',0,unsigned int)
#define DELAY_CMD_READ   _IOW('A',1,unsigned int)

#define MY_MAX_MINORS  1
#define   DEVICE_NAME  "device name 500"

DEFINE_KFIFO(myfifo, char, 1024);

struct my_device_data {
	struct cdev cdev;
};

DECLARE_WAIT_QUEUE_HEAD(wq);


static struct my_device_data devs[MY_MAX_MINORS];

static int my_open(struct inode *inode, struct file *file) {
	pr_info("my_open \n");
	return 0;
}

static ssize_t my_write(struct file *filp, const char __user *user_buffer,
		size_t size, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("write");
	ret = kfifo_from_user(&myfifo, user_buffer, size, &len);
	if (ret != 0) {
		pr_err("kfifo_from_user error");
		return 0;
	}
	if (len <= 0)
		return 0;
	*offset += len;
	wake_up(&wq);
	return len;
}

static ssize_t my_read(struct file *filp, char __user *user_buffer,
		size_t count, loff_t *offset) {
	int ret;
	unsigned int len = 0;
	pr_info("read");
	ret = kfifo_to_user(&myfifo, user_buffer, count, &len);
	if (len <= 0)
		return 0;
	*offset += len;
	return len;
}

unsigned int my_poll(struct file *flip, struct poll_table_struct *table) {
	int mask = 0;
	pr_info("my_poll \n");
	poll_wait(flip, &wq, table);
	if (kfifo_is_empty(&myfifo)) {

	} else {
		mask |= POLLIN | POLLRDNORM;
	}
	return mask;
}

int my_close(struct inode *inode, struct file *flip) {
	pr_info("my_close \n");
	return 0;
}

 DECLARE_COMPLETION(mycompletion);

static long my_ioctl(struct file *file, unsigned int cmd, unsigned long arg) {
	pr_info("cmd %d\n", cmd);
	switch (_IOC_NR(cmd)) {
	case 0: {
			pr_info("i am waiting \n");
			wait_for_completion(&mycompletion);
			pr_info("haha,i get the message\n");
			break;
		}

	case 1: {
		pr_info("complete \n");
		complete(&mycompletion);
		break;
	}

	default: {
		pr_info("default: \n");
	}
	}
	return 0;
}

static const struct file_operations my_fops = { .owner = THIS_MODULE, .open =
		my_open, .read = my_read, .write = my_write, .poll = my_poll, .release =
		my_close, .unlocked_ioctl = my_ioctl,

};

static dev_t mydev;

static __init int my_init(void) {
	int i;
	int err;
	pr_info("a3 init_module\n");err = alloc_chrdev_region(&mydev, 0, MY_MAX_MINORS, DEVICE_NAME);
	if (err != 0) {
		return err;
	}

	for (i = 0; i < MY_MAX_MINORS; i++) {
		/* initialize devs[i] fields */
		cdev_init(&devs[i].cdev, &my_fops);
		cdev_add(&devs[i].cdev, MKDEV(MAJOR(mydev), i), 1);
	}
return 0;
}

static void __exit my_exit(void) {
		int i;
		pr_info("a3 cleanup_module\n");for (i = 0; i < MY_MAX_MINORS; i++) {
		/* release devs[i] fields */
		cdev_del(&devs[i].cdev);
		}
		unregister_chrdev_region(mydev, MY_MAX_MINORS);
}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("81553652@qq.com");
MODULE_DESCRIPTION("andy one-key driver");
MODULE_ALIAS("one-key");

以下是应用测试代码:

#include <stdint.h>
#include <sys/types.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <stdio.h>
#include <pthread.h>
#include <poll.h>

#define DELAY_CMD_0_WRITE   _IOW('A',0,unsigned int)
#define DELAY_CMD_1_READ   _IOW('A',1,unsigned int)

static void* write01(void *ptr) {
	int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		printf("error open\n");
		return NULL;
	}
	printf("write start \n");
	ioctl(fd, DELAY_CMD_0_WRITE, 100);
	printf("write end \n");
	close(fd);
	return NULL;
}

static void* read01(void *ptr) {
	int fd = open("/dev/t01", O_RDWR, O_NONBLOCK);
	if (fd < 0) {
		perror("open");
		printf("error open\n");
		return NULL;
	}
	printf("read start \n");
	ioctl(fd, DELAY_CMD_1_READ, 100);
	printf("read end \n");
	close(fd);
	return NULL;
}

/**
 * first read then  write
 */
int main() {
	pthread_t thread, thread2;
	pthread_create(&thread, NULL, write01, NULL);
	sleep(2);
	pthread_create(&thread2, NULL, read01, NULL);
	pthread_join(thread, NULL);
	pthread_join(thread2, NULL);
	printf("main exit\n");
	return EXIT_SUCCESS;
}

测试说明:
应用先发0,再发1 ,驱动中的0会等待1的完成量。
可以看打印信息。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值