linux内核同步机制之自旋锁

定义:

        最多只能被一个可执行线程持有。如果一个执行线程试图获得一个被争用的自旋锁,那么该线程就会一直进行忙循环----旋转----等待锁重新可用。

        自旋锁有“加锁”和“解锁”两种状态。“加锁”一直在寻求“解锁”,“解锁”马上会寻求“加锁”,并原地打转,所以加锁位置的代码进入临界区执行,直到解锁。

注意:

    1.占用临界区的时间必须短;

    2.拥有自旋锁期间所在的CPU进程抢占被关闭

    3.占有自旋锁期间不能调用引起阻塞的函数(原子上下文)比如:copy_from_user(), kmalloc(), msleep()

    4.自旋锁可以被用于中断上下文

特殊化自旋锁:读写自旋锁,顺序锁,RCU锁

用例:

#include <linux/spinlock.h>
spinlock_t lock;    //定义自旋锁

spin_lock_init(&lock);    //初始化自旋锁

spin_lock(&lock);    // 获取自旋锁

...临界区...

spin_unlock(&lock)

驱动程序代码:

#include <linux/device.h>
#include <linux/interrupt.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <mach/gpio.h>
#include <linux/delay.h>
#include <mach/regs-gpio.h>  /*S5PV210_GPH3_BASE*/
 
#define EINT_DEVICE_ID			1
 
#define DRIVER_NAME				"key_eint_race"
#define err(msg) 				printk(KERN_ERR "%s: " msg "\n", DRIVER_NAME)
#define __debug(fmt, arg...)	printk(KERN_DEBUG fmt, ##arg)
 
#define GPH3CON					(unsigned long)(S5PV210_GPH3_BASE+ 0x00)
#define GPH3DAT					(unsigned long)(S5PV210_GPH3_BASE + 0x04)
#define GPH2UP					(unsigned long)(S5PV210_GPH2_BASE + 0x08)
 
static int major = 0;		/* Driver Major Number */
static int minor = 0;		/* Driver Minor Number */
struct class *key_class;
static struct device *key_device;
 
static unsigned int key;
 
static spinlock_t my_spin_lock; //
 
static unsigned int deal_key_value(unsigned int data)
{
	key = data;
	udelay(1000);
	return key;
}
 
static unsigned int deal_key_value_excl(unsigned int data)
{
	unsigned int value;
	unsigned long flag;
	/*
	*	自旋锁解决了SMP多处理的并发问题,对于内核的抢占也可以起到关闭的作用,
	*   可仍没有解决中断产生的并发问题,但是不用担心,我们可以采用spin_lock
	*   的变体spin_lock_irqsave函数完成同时关中断的功能
	*
	*   注意:此处用自旋锁并不合理,因为我们的临界区udelay了1ms(时间很长),
	*   但我们本例子的目的是验证自旋锁的作用,在实际编程中要特别注意
	*/
	spin_lock_irqsave(&my_spin_lock,flag);		//获取自旋锁
	value =deal_key_value(data);
	spin_unlock_irqrestore(&my_spin_lock,flag);		//释放自旋锁
	
	return value;
}
 
irqreturn_t buttons_interrupt(int irq, void *dev_id)
{	
	deal_key_value_excl((unsigned int)dev_id);
	//__debug("in eint function...\n");
	return IRQ_HANDLED;
}
 
static void key_io_port_init(void)
{
	unsigned long reg_val;
	
	reg_val = readl(GPH3CON);
	reg_val &= ~((0x0f<<0) | (0x0f<<4));
	reg_val |= ((0x01<<0) | (0x01<<4));
	writel(reg_val, GPH3CON);
 
	reg_val = readl(GPH3DAT);
	reg_val &= ~((0x01<<0) | (0x01<<1));
	writel(reg_val, GPH3DAT);
 
	reg_val = readl(GPH2UP);
	reg_val &= ~(0x03<<8);
	reg_val |= 0x02<<8;
	writel(reg_val, GPH2UP);
}
 
static ssize_t key_read(struct file *filp, char *buf, size_t count, loff_t *f_pos)
{
	int key_num;
	int cpy_len;
	int retval;
	
	key_num = deal_key_value_excl(current->pid);
	
	cpy_len = min(sizeof(key_num), count);
	retval = copy_to_user(buf, &key_num, cpy_len);
	
	return (cpy_len - retval);
}
 
/* Driver Operation structure */
static struct file_operations key_fops = {
	.owner = THIS_MODULE,
	.read = key_read,
};
 
 
static int __init key_eint_init(void)
{
	int retval;
	
	key_io_port_init();
	
	spin_lock_init(&my_spin_lock);
 
	
	//__debug("in key_eint_init\n");
	
	retval = set_irq_type(IRQ_EINT(20),IRQ_TYPE_EDGE_FALLING);
	if(retval){
		err("IRQ_EINT20 set irq type failed");
		goto error;
	}
	
	retval = request_irq(IRQ_EINT(20), buttons_interrupt, IRQF_DISABLED, 
			"KEY1", (void *)EINT_DEVICE_ID);
	if(retval){
		err("request eint20 failed");
		goto error;
	}
	
	/* Driver register */
	major = register_chrdev(major, DRIVER_NAME, &key_fops);
	if(major < 0){
		err("register char device fail");
		retval = major;
		goto error_register;
	}
	key_class=class_create(THIS_MODULE,DRIVER_NAME);
	if(IS_ERR(key_class)){
		err("class create failed!");
		retval =  PTR_ERR(key_class);
		goto error_class;
	}
	key_device=device_create(key_class,NULL, MKDEV(major, minor), NULL,DRIVER_NAME);
	if(IS_ERR(key_device)){
		err("device create failed!");
		retval = PTR_ERR(key_device);
		goto error_device;
	}
	__debug("register myDriver OK! Major = %d\n", major);
	return 0;
 
error_device:
	class_destroy(key_class);
error_class:
	unregister_chrdev(major, DRIVER_NAME);
error_register:
	free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
error:
	return retval;
}
 
static void __exit key_eint_exit(void)
{
	//__debug("in key_eint_exit\n");
	
	free_irq(IRQ_EINT(20), (void *)EINT_DEVICE_ID);
 
	unregister_chrdev(major, DRIVER_NAME);
	device_destroy(key_class,MKDEV(major, minor));
	class_destroy(key_class);
 
	return;
}
 
module_init(key_eint_init);
module_exit(key_eint_exit);
 
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Eric");

应用程序代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
 
/*Linux内核是抢占式内核,在一个系统调用未结束前,另一个系统调用也可以进入进程上下文,访问同一缓冲区*/
int main(void)
{	
	int status;
	pid_t pid;
	
	//打开文件raceStation
	int fd_driver;
	if((fd_driver = open("/dev/key_eint_race", O_RDWR)) < 0){
		printf("file open error\n");
		exit(1);
	}
	//创建子进程
 	if((pid = fork()) < 0){
		perror("fork:");
		exit(1);
	}
	else if(pid == 0){					//判断如果是子进程
		int num;
		while(1){
			read(fd_driver,&num,sizeof(num));	
			printf("the num value is <son-%d>: %d\n",getpid(), num);
 
		//	usleep(50*1000);
		}
		close(fd_driver);
	}else{								//判断如果是父进程
		int num;
		while(1){	
			read(fd_driver,&num,sizeof(num));	
			printf("the num value is <father-%d>: %d\n",getpid(), num);
 
		//	usleep(50*1000);
		}
		pid = wait(&status);
		close(fd_driver);
	}
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值