码迷,mamicode.com
首页 > 系统相关 > 详细

Linux驱动之同步、互斥、阻塞的应用

时间:2018-08-15 17:05:06      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:dwr   and   获取   0x03   ase   \n   lock   rem   nal   

同步、互斥、阻塞的概念:

同步:在并发程序设计中,各进程对公共变量的访问必须加以制约,这种制约称为同步。

互斥机制:访问共享资源的代码区叫做临界区,这里的共享资源可能被多个线程需要,但这些共享资源又不能被同时访问,因此临界区需要以某种互斥机制加以保护,以确保共享资源被互斥访问。

阻塞与非阻塞:阻塞调用是指调用结果返回之前,当前线程会被挂起,调用线程只有在得到结果之后才会返回。非阻塞调用指在不能立刻得到结果之前,该调用不会阻塞当前线程,而是直接返回。

在按键驱动的例子中,如果有多个应用程序调用按键驱动的设备文件,这时候就要利用同步与互斥的概念对这个种情况进行处理:

1、利用原子变量标志来判断设备文件是否被打开,原子变量在操作的时候不能被打断,它是利用关闭中断的方式实现的,一旦关闭了中断,内核将不能对进程进行调度,这就保证了原子性。

直接修改驱动代码,先定义一个原子变量

 static atomic_t open_flag = ATOMIC_INIT(1);     //定义原子变量open_flag 并初始化为1

接着修改打开文件的函数与关闭文件的函数,初始化时open_flag 为1,一旦打开函数被调用则会减1变为0。关闭函数被调用后会加1又变成1。

a、在sixth_drv_open 中利用atomic_dec_and_test函数判断是否已经被调用,如果返回值为0,说明已经被调用。调用atomic_inc函数,并且返回。

b、在sixth_drv_close中第调用atomic_inc。

static int sixth_drv_open (struct inode * inode, struct file * file)
{
    int ret;


    if(atomic_dec_and_test(&open_flag)==0)//自检后是否为0,不为0说明已经被人调用
    {
        atomic_inc(&open_flag);//原子变量+1
        return -EBUSY;
    }
    
    ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]);
    if(ret)
    {
        printk("open failed 1\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]);
    if(ret)
    {
        printk("open failed 2\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]);
    if(ret)
    {
        printk("open failed 3\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]);
    if(ret)
    {
        printk("open failed 4\n");
        return -1;
    }
    
    return 0;
}


static int sixth_drv_close(struct inode * inode, struct file * file)
{
    atomic_inc(&open_flag);//原子变量+1
    
    free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]);

     free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]);

    free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]);

    free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]);

    return 0;
}

 

2、利用信号量对打开的文件进行保护:信号量(semaphore)是用于保护临界区的一种常用方法,只有得到信号量的进程才能执行临界区代码。当获取不到信号量时,进程进入休眠等待状态。
 
直接修改驱动代码,先定义一个互斥锁
static DECLARE_MUTEX(button_lock);     //定义互斥锁

接着更改按键驱动中打开文件的函数与关闭文件的函数:

a、在sixth_drv_open函数中如果文件打开方式非阻塞的,那么调用down_trylock函数获取信号量,此函数如果获取不到信号量,直接返回;如果打开文件的方式是阻塞的,那么调用down函数,如果获取不到信号量,则将进程休眠直到获取信号量为止。

b、在sixth_drv_close函数利用up函数直接释放掉信号量。

static int sixth_drv_open (struct inode * inode, struct file * file)
{
    int ret;

    if(file->f_flags & O_NONBLOCK)//非阻塞方式
    {
        if(down_trylock(&button_lock))//获取信号量失败则返回
            return -EBUSY;
    }
    else    
        down(&button_lock);//获得信号量
    
    ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]);
    if(ret)
    {
        printk("open failed 1\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]);
    if(ret)
    {
        printk("open failed 2\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]);
    if(ret)
    {
        printk("open failed 3\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]);
    if(ret)
    {
        printk("open failed 4\n");
        return -1;
    }
    
    return 0;
}


static int sixth_drv_close(struct inode * inode, struct file * file)
{
    up(&button_lock);//释放信号量
    
    free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]);

     free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]);

    free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]);

    free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]);

    return 0;
}

将完整的按键驱动的源代码贴出

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <asm/io.h>        //含有iomap函数iounmap函数
#include <asm/uaccess.h>//含有copy_from_user函数
#include <linux/device.h>//含有类相关的处理函数
#include <asm/arch/regs-gpio.h>//含有S3C2410_GPF0等相关的
#include <linux/irq.h>    //含有IRQ_HANDLED\IRQ_TYPE_EDGE_RISING
#include <asm-arm/irq.h>   //含有IRQT_BOTHEDGE触发类型
#include <linux/interrupt.h> //含有request_irq、free_irq函数
#include <linux/poll.h>
#include <asm-generic/errno-base.h>  //含有各种错误返回值
//#include <asm-arm\arch-s3c2410\irqs.h>



static struct class *sixth_drv_class;//
static struct class_device *sixth_drv_class_dev;//类下面的设备
static int sixthmajor;

static unsigned long *gpfcon = NULL;
static unsigned long *gpfdat = NULL;
static unsigned long *gpgcon = NULL;
static unsigned long *gpgdat = NULL;

struct fasync_struct *sixth_fasync;
    
static unsigned int key_val;

struct pin_desc 
{
    unsigned int pin;
    unsigned int key_val;
};

static struct pin_desc  pins_desc[4] = 
{
    {S3C2410_GPF0,0x01},
    {S3C2410_GPF2,0x02},
    {S3C2410_GPG3,0x03},
    {S3C2410_GPG11,0x04}
};


static unsigned int ev_press;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);//注册一个等待队列button_waitq

 static atomic_t open_flag = ATOMIC_INIT(1);     //定义原子变量open_flag 并初始化为1

static DECLARE_MUTEX(button_lock);     //定义互斥锁

 
/*
  *0x01、0x02、0x03、0x04表示按键被按下
  */
  
/*
  *0x81、0x82、0x83、0x84表示按键被松开
  */

/*
  *利用dev_id的值为pins_desc来判断是哪一个按键被按下或松开
  */
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
    unsigned int pin_val;
    struct pin_desc * pin_desc = (struct pin_desc *)dev_id;//取得哪个按键被按下的状态
    
    pin_val = s3c2410_gpio_getpin(pin_desc->pin);
    
    if(pin_val) //按键松开
        key_val = 0x80 | pin_desc->key_val;
    else
        key_val = pin_desc->key_val;


    wake_up_interruptible(&button_waitq);   /* 唤醒休眠的进程 */
    ev_press = 1;    
    
    kill_fasync(&sixth_fasync, SIGIO, POLL_IN);//发生信号给进程
    
    return IRQ_HANDLED;
}



static int sixth_drv_open (struct inode * inode, struct file * file)
{
    int ret;


//    if(atomic_dec_and_test(&open_flag)==0)//自检后是否为0,不为0说明已经被人调用
//    {
//        atomic_inc(&open_flag);//原子变量+1
//        return -EBUSY;
//    }
    if(file->f_flags & O_NONBLOCK)//非阻塞方式
    {
        if(down_trylock(&button_lock))//获取信号量失败则返回
            return -EBUSY;
    }
    else    
        down(&button_lock);//获得信号量
    
    ret = request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "s1", (void * )&pins_desc[0]);
    if(ret)
    {
        printk("open failed 1\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "s2", (void * )& pins_desc[1]);
    if(ret)
    {
        printk("open failed 2\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "s3", (void * )&pins_desc[2]);
    if(ret)
    {
        printk("open failed 3\n");
        return -1;
    }
    ret = request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "s4", (void * )&pins_desc[3]);
    if(ret)
    {
        printk("open failed 4\n");
        return -1;
    }
    
    return 0;
}


static int sixth_drv_close(struct inode * inode, struct file * file)
{
//    atomic_inc(&open_flag);//原子变量+1
    up(&button_lock);//释放信号量
    
    free_irq(IRQ_EINT0 ,(void * )&pins_desc[0]);

     free_irq(IRQ_EINT2 ,(void * )& pins_desc[1]);

    free_irq(IRQ_EINT11 ,(void * )&pins_desc[2]);

    free_irq(IRQ_EINT19 ,(void * )&pins_desc[3]);

    return 0;
}

static ssize_t sixth_drv_read(struct file * file, char __user * userbuf, size_t count, loff_t * off)
{
    int ret;

    if(count != 1)
    {
        printk("read error\n");
        return -1;
    }

    if(file->f_flags & O_NONBLOCK)//非阻塞方式
    {
        if(!ev_press)//判断是否有按键按下,如果没有直接返回
        {
                key_val = 0;
                copy_to_user(userbuf, &key_val, 1);
                return -EBUSY;
        }
    }
    else//如果没有按键动作,直接进入休眠
        wait_event_interruptible(button_waitq, ev_press);//将当前进程放入等待队列button_waitq中
    
    ret = copy_to_user(userbuf, &key_val, 1);
    ev_press = 0;//按键已经处理可以继续睡眠
    
    if(ret)
    {
        printk("copy error\n");
        return -1;
    }
    
    return 1;
}

static unsigned int sixth_drv_poll(struct file *file, poll_table *wait)
{
    unsigned int ret = 0;
    poll_wait(file, &button_waitq, wait);//将当前进程放到button_waitq列表

    if(ev_press)
        ret |=POLLIN;//说明有数据被取到了

    return ret;
}



static int sixth_drv_fasync(int fd, struct file * file, int on)
{
    int err;
    printk("fansync_helper\n");
    err = fasync_helper(fd, file, on, &sixth_fasync);//初始化sixth_fasync
    if (err < 0)
        return err;
    return 0;
}


static struct file_operations sixth_drv_ops = 
{
    .owner   = THIS_MODULE,
    .open    =  sixth_drv_open,
    .read     = sixth_drv_read,
    .release = sixth_drv_close,
    .poll      =  sixth_drv_poll,
    .fasync   = sixth_drv_fasync,
    
};

static int sixth_drv_init(void)
{
    sixthmajor = register_chrdev(0, "buttons", &sixth_drv_ops);//注册驱动程序

    if(sixthmajor < 0)
        printk("failes 1 buttons_drv register\n");
    
    sixth_drv_class = class_create(THIS_MODULE, "buttons");//创建类
    if(sixth_drv_class < 0)
        printk("failes 2 buttons_drv register\n");
    sixth_drv_class_dev = class_device_create(sixth_drv_class, NULL, MKDEV(sixthmajor,0), NULL,"buttons");//创建设备节点
    if(sixth_drv_class_dev < 0)
        printk("failes 3 buttons_drv register\n");

    
    gpfcon = ioremap(0x56000050, 16);//重映射
    gpfdat = gpfcon + 1;
    gpgcon = ioremap(0x56000060, 16);//重映射
    gpgdat = gpgcon + 1;

    printk("register buttons_drv\n");
    return 0;
}

static void sixth_drv_exit(void)
{
    unregister_chrdev(sixthmajor,"buttons");

    class_device_unregister(sixth_drv_class_dev);
    class_destroy(sixth_drv_class);

    iounmap(gpfcon);
    iounmap(gpgcon);

    printk("unregister buttons_drv\n");
}


module_init(sixth_drv_init);
module_exit(sixth_drv_exit);

MODULE_LICENSE("GPL");

接着改写测试程序,测试加入阻塞方式打开文件,在fd = open(filename, O_RDWR|O_NONBLOCK)函数中加入O_NONBLOCK即可以按阻塞方式打开。

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>

static int fd;

//static void fifth_testsignal(int signum)
//{
//    unsigned char key_val;
//    
//    printf("signal = %d\n",signum);

//    read(fd, &key_val, 1);
//         printf("signumkey_val: 0x%x\n\n",key_val);
//}

/*
  *usage ./buttonstest
  */
int main(int argc, char **argv)
{
    char* filename="dev/buttons";
   int oflags,ret;
   unsigned char key_val;
    
    fd = open(filename, O_RDWR|O_NONBLOCK);//打开dev/firstdrv设备文件,非阻塞方式打开
    if (fd < 0)//小于0说明没有成功
    {
        printf("error, can‘t open %s\n", filename);
        return 0;
    }
    
    if(argc !=1)
    {
        printf("Usage : %s ",argv[0]);
     return 0;
    }
//    signal(SIGIO, fifth_testsignal);//注册一个信号,函数为fifth_testsignal
//    
//    fcntl(fd, F_SETOWN, getpid());  // 告诉内核,发给谁
//    
//    oflags = fcntl(fd, F_GETFL); //取得当前的状态
//    
//    fcntl(fd, F_SETFL, oflags | FASYNC);  // 改变fasync标记,最终会调用到驱动的faync > fasync_helper:初始化/释放fasync_struct
    
  while(1)
  {
       ret = read(fd, &key_val, 1);
         printf("ret = %d,key_val: 0x%x\n",ret,key_val);
    sleep(5);
  }
    
   return 0;
}
将驱动程序与测试程序编译后运行。发现以阻塞方式运行的测试程序如果再次运行会处于睡眠状态;如果以非阻塞方式再次运行程序,会导致第二个程序退出。
 
以上只是记录了怎么调用内核函数来实现互斥、阻塞机制,具体原理还未分析,后面再分析。
 
 

Linux驱动之同步、互斥、阻塞的应用

标签:dwr   and   获取   0x03   ase   \n   lock   rem   nal   

原文地址:https://www.cnblogs.com/andyfly/p/9482077.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!