这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » Completions 同步机制

共1条 1/1 1 跳转至

Completions 同步机制

菜鸟
2007-03-29 17:46:08     打赏

Completion用来解决动态创建的semaphore,在多CPU的kernel中,因为动态destory产生的竞争条件.典型的是一个进程new 一个Closed semaphore,然后把这个semaphone传递给另外一个CPU上的进程, 然后down(),.那么如果这时候另外CPU上的进程UP(),那么,down()和up()就同时并发执行, 那么第一个线程就被唤醒,如果它马上delete那个semaphore, 那么up()就操作在一个未知的memory上.

那么解决的方法很显然是避免up()和down()并发执行,那么就用complete()和wait_for_complete()进行改进,保证complete()和wait_for_complete()不会同时并发执行.

complete() <----> up()
wait_for_complete() <-----> down()

贴一个LINUX Device Driver 里的例子代码:

/*
* complete.c -- the writers awake the readers
*
* Copyright (C) 2003 Alessandro Rubini and Jonathan Corbet
* Copyright (C) 2003 O'Reilly & Associates
*
* The source code in this file can be freely used, adapted,
* and redistributed in source or binary form, so long as an
* acknowledgment appears in derived source files. The citation
* should list that the code comes from the book "Linux Device
* Drivers" by Alessandro Rubini and Jonathan Corbet, published
* by O'Reilly & Associates. No warranty is attached;
* we cannot take responsibility for errors or fitness for use.
*
* $Id: complete.c,v 1.2 2004/09/26 07:02:43 gregkh Exp $
*/

#include <linux/module.h>
#include <linux/init.h>

#include <linux/sched.h> /* current and everything */
#include <linux/kernel.h> /* printk() */
#include <linux/fs.h> /* everything... */
#include <linux/types.h> /* size_t */
#include <linux/completion.h>

MODULE_LICENSE("Dual BSD/GPL");

static int complete_major = 0;

DECLARE_COMPLETION(comp);

ssize_t complete_read (struct file *filp, char __user *buf, size_t count, loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) going to sleep\n",
current->pid, current->comm);
wait_for_completion(&comp);
printk(KERN_DEBUG "awoken %i (%s)\n", current->pid, current->comm);
return 0; /* EOF */
}

ssize_t complete_write (struct file *filp, const char __user *buf, size_t count,
loff_t *pos)
{
printk(KERN_DEBUG "process %i (%s) awakening the readers...\n",
current->pid, current->comm);
complete(&comp);
return count; /* succeed, to avoid retrial */
}


struct file_operations complete_fops = {
.owner = THIS_MODULE,
.read = complete_read,
.write = complete_write,
};


int complete_init(void)
{
int result;

/*
* Register your major, and accept a dynamic number
*/
result = register_chrdev(complete_major, "complete", &complete_fops);
if (result < 0)
return result;
if (complete_major == 0)
complete_major = result; /* dynamic */
return 0;
}

void complete_cleanup(void)
{
unregister_chrdev(complete_major, "complete");
}

module_init(complete_init);
module_exit(complete_cleanup);

——————————



关键词: Completions     同步     机制     complet    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]