自旋锁(spin lock)是一种对临界资源进行互斥手访问的典型手段,其名称来源于它的工作方式。为了获得一个自旋锁,在某CPU上运行的代码需先执行一个原子操作,该操作测试并设置(test-and-set)某个内存变量,由于它是原子操作,所以在该操作完成之前其他执行单元不可能访问这个内存变量。如果测试结果表明锁已经空闲,则程序获得这个自旋锁并继续执行;如果测试结果表明锁仍被占用,程序将在一个小的循环内重复这个“测试并设置”操作,即进行所谓的“自旋”,通俗地说就是“在原地打转”。当自旋锁的持有者通过重置该变量释放这个自旋锁后,某个等待的“测试并设置”操作向其调用者报告锁已释放。 理解自旋锁最简单的方法是把它作为一个变量看待,该变量把一个临界区或者标记为“我当前在运行,请稍等一会”或者标记为“我当前不在运行,可以被使用”。如果A执行单元首先进入例程,它将持有自旋锁;当B执行单元试图进入同一个例程时,将获知自旋锁已被持有,需等到A执行单元释放后才能进入。 使用自旋锁的方法很简单,执行以下三个步骤即可。 第一步:在程序的最开始定义一个自旋锁: - spinlock_t lock; //定义一个自旋锁
复制代码第二步:在程序的初始化函数中初始化自旋锁: - spin_lock_init(lock)//初始化自旋锁
复制代码第三步:将需要包装自旋锁的临界区用自旋锁函数封装起来: - spin_lock(lock)//获取自旋锁,保护临界区
- …//被保护的临界区程序段
- spin_unlock(lock)//释放自旋锁
复制代码 自旋锁主要针对多核CPU或单核CPU但内核可抢占的情况,对于单核CPU且内核不支持抢占的系统,自旋锁退化为空操作。在单CPU和内核可抢占的系统中,自旋锁持有期间内核的抢占将被禁止。由于内核可抢占的单CPU系统的行为实际很类似于SMP系统,因此,在这样的单CPU系统中使用自旋锁仍十分必要。 尽管用了自旋锁可以保证临界区不受别的CPU和本CPU内的抢占进程打扰,但是得到锁的代码路径在执行临界区的时候还可能受到中断和底半部(BH)的影响。为了防止这种影响,就需要用到自旋锁的衍生。spin_lock()/spin_unlock()是自旋锁机制的基础,它们和开关中断开中断 local_irq_enable()/local_irq_disable()、开关底半部开底半部local_bh_enable()/local_bh_disable()、开关中断并保存状态字 local_irq_save()/local_irq_restore()结合就形成了整套自旋锁机制,关系如下所示: - spin_lock_irq() = spin_lock() + local_irq_disable()
- spin_unlock_irq() = spin_unlock() + local_irq_enable()
- spin_lock_irqsave() = spin_unlock() + local_irq_save()
- spin_unlock_irqrestore() = spin_unlock() + local_irq_restore()
- spin_lock_bh() = spin_lock() + local_bh_disable()
- spin_unlock_bh() = spin_unlock() + local_bh_enable()
复制代码在使用自旋锁时,应该注意以下问题: l 自旋锁实际上是忙等锁,当锁不可用时,CPU一直循环执行“测试并设置”该锁直到可用而取得该锁,CPU在等待自旋锁时不做任何有用的工作,仅仅是等待。因此,只有在占用锁的时间极短的情况下,使用自旋锁才是合理的。当临界区很大或有共享设备的时候,需要较长时间占用锁,使用自旋锁会降低系统的性能。 l 自旋锁可能导致系统死锁。引发这个问题最常见的情况是递归使用一个自旋锁,即如果一个已经拥有某个自旋锁的CPU想第二次获得这个自旋锁,则该CPU将死锁。此外,如果进程获得自旋锁之后再阻塞,也有可能导致死锁的发生。copy_from_user()、copy_to_user()和 kmalloc()等函数都有可能引起阻塞,因此在自旋锁的占用期间不能调用这些函数。 将前面基于sysfs创建的LED驱动程序的LED读写函数添加自旋锁。参考源码如下: - #include <linux/module.h>
- #include <linux/init.h>
- #include <linux/platform_device.h>
- #include <mach/hardware.h>
- #include <asm/mach-types.h>
- #include <linux/gpio.h>
- #include <plat/gpio-cfg.h>
- spinlock_t spin;
- /*
- * X4412:
- *
- * LED1 -> D22 -> XEINT14
- * LED2 -> D23 -> XEINT15
- * LED3 -> D24 -> XEINT22
- * LED4 -> D25 -> XEINT23
- */
- static int __x4412_led_status[4] = { 0 };
- static void __x4412_led_probe(void) //初始化LED对应GPIO口
- {
- int ret;
- ret = gpio_request(EXYNOS4_GPX1(6), "GPX1");
- if(ret)
- printk("x4412-led: request gpio GPX1(6) fail\n");
- s3c_gpio_setpull(EXYNOS4_GPX1(6), S3C_GPIO_PULL_UP);
- gpio_direction_output(EXYNOS4_GPX1(6), 1);
- ret = gpio_request(EXYNOS4_GPX1(7), "GPX1");
- if(ret)
- printk("x4412-led: request gpio GPX1(7) fail\n");
- s3c_gpio_setpull(EXYNOS4_GPX1(7), S3C_GPIO_PULL_UP);
- gpio_direction_output(EXYNOS4_GPX1(7), 1);
- ret = gpio_request(EXYNOS4_GPX2(6), "GPX2");
- if(ret)
- printk("x4412-led: request gpio GPX2(6) fail\n");
- s3c_gpio_setpull(EXYNOS4_GPX2(6), S3C_GPIO_PULL_UP);
- gpio_direction_output(EXYNOS4_GPX2(6), 1);
- ret = gpio_request(EXYNOS4_GPX2(7), "GPX2");
- if(ret)
- printk("x4412-led: request gpio GPX2(7) fail\n");
- s3c_gpio_setpull(EXYNOS4_GPX2(7), S3C_GPIO_PULL_UP);
- gpio_direction_output(EXYNOS4_GPX2(7), 1);
- __x4412_led_status[0] = 0; //定义四盏LED灯的默认状态
- __x4412_led_status[1] = 0;
- __x4412_led_status[2] = 0;
- __x4412_led_status[3] = 0;
- }
- static void __x4412_led_remove(void) //释放GPIO口
- {
- gpio_free(EXYNOS4_GPX1(6));
- gpio_free(EXYNOS4_GPX1(7));
- gpio_free(EXYNOS4_GPX2(6));
- gpio_free(EXYNOS4_GPX2(7));
- }
- static ssize_t x4412_led_read(struct device *dev, struct device_attribute *attr, char *buf)
- {
- if(!strcmp(attr->attr.name, "led1")) //根据从应用读取的文件名称确定操作指定LED灯
- {
- spin_lock(&spin);
- if(__x4412_led_status[0] != 0) //返回给应用LED灯当前的状态
- return strlcpy(buf, "1\n", 3);
- else
- return strlcpy(buf, "0\n", 3);
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led2"))
- {
- spin_lock(&spin);
- if(__x4412_led_status[1] != 0)
- return strlcpy(buf, "1\n", 3);
- else
- return strlcpy(buf, "0\n", 3);
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led3"))
- {
- spin_lock(&spin);
- if(__x4412_led_status[2] != 0)
- return strlcpy(buf, "1\n", 3);
- else
- return strlcpy(buf, "0\n", 3);
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led4"))
- {
- spin_lock(&spin);
- if(__x4412_led_status[3] != 0)
- return strlcpy(buf, "1\n", 3);
- else
- return strlcpy(buf, "0\n", 3);
- spin_unlock(&spin);
- }
- return strlcpy(buf, "\n", 3);
- }
- static ssize_t x4412_led_write(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
- {
- unsigned long on = simple_strtoul(buf, NULL, 10);//从应用读取需要写入的数据
- if(!strcmp(attr->attr.name, "led1"))
- {
- spin_lock(&spin);
- if(on) //如果收到1,则将对应标志位置1,同时将对应GPIO置0,对应LED灯亮
- {
- gpio_direction_output(EXYNOS4_GPX1(6), 0);
- __x4412_led_status[0] = 1;
- }
- else //如果收到0,则将对应标志位清0,同时将对应GPIO置1,对应LED灯灭
- {
- gpio_direction_output(EXYNOS4_GPX1(6), 1);
- __x4412_led_status[0] = 0;
- }
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led2"))
- {
- spin_lock(&spin);
- if(on)
- {
- gpio_direction_output(EXYNOS4_GPX1(7), 0);
- __x4412_led_status[1] = 1;
- }
- else
- {
- gpio_direction_output(EXYNOS4_GPX1(7), 1);
- __x4412_led_status[1] = 0;
- }
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led3"))
- {
- spin_lock(&spin);
- if(on)
- {
- gpio_direction_output(EXYNOS4_GPX2(6), 0);
- __x4412_led_status[2] = 1;
- }
- else
- {
- gpio_direction_output(EXYNOS4_GPX2(6), 1);
- __x4412_led_status[2] = 0;
- }
- spin_unlock(&spin);
- }
- else if(!strcmp(attr->attr.name, "led4"))
- {
- spin_lock(&spin);
- if(on)
- {
- gpio_direction_output(EXYNOS4_GPX2(7), 0);
- __x4412_led_status[3] = 1;
- }
- else
- {
- gpio_direction_output(EXYNOS4_GPX2(7), 1);
- __x4412_led_status[3] = 0;
- }
- spin_unlock(&spin);
- }
-
- return count;
- }
- //kobject目录下的四个文件对应的属性,读写函数
- static DEVICE_ATTR(led1, 0666, x4412_led_read, x4412_led_write);
- static DEVICE_ATTR(led2, 0666, x4412_led_read, x4412_led_write);
- static DEVICE_ATTR(led3, 0666, x4412_led_read, x4412_led_write);
- static DEVICE_ATTR(led4, 0666, x4412_led_read, x4412_led_write);
- static struct attribute * x4412_led_sysfs_entries[] = { //对应kobject目录下的四个文件
- &dev_attr_led1.attr,
- &dev_attr_led2.attr,
- &dev_attr_led3.attr,
- &dev_attr_led4.attr,
- NULL,
- };
- static struct attribute_group x4412_led_attr_group = {
- .name = NULL,
- .attrs = x4412_led_sysfs_entries, //指定注册的kobject对应的文件属性接入点
- };
- static int x4412_led_probe(struct platform_device *pdev)
- {
- __x4412_led_probe(); //初始化LED对应GPIO口
- return sysfs_create_group(&pdev->dev.kobj, &x4412_led_attr_group);//注册kobject
- }
- static int x4412_led_remove(struct platform_device *pdev)
- {
- __x4412_led_remove(); //释放GPIO
- sysfs_remove_group(&pdev->dev.kobj, &x4412_led_attr_group);//注销kobject
- return 0;
- }
- static struct platform_driver x4412_led_driver = {
- .probe = x4412_led_probe, //平台驱动探测函数
- .remove = x4412_led_remove,
- .driver = {
- .name = "x4412-led",
- },
- };
- static struct platform_device x4412_led_device = {
- .name = "x4412-led",
- .id = -1,
- };
- static int __devinit x4412_led_init(void)
- {
- int ret;
- printk("x4412 led driver\r\n");
- ret = platform_device_register(&x4412_led_device); //注册平台设备
- if(ret)
- printk("failed to register x4412 led device\n");
- ret = platform_driver_register(&x4412_led_driver); //注册平台驱动
- if(ret)
- printk("failed to register x4412 led driver\n");
- spin_lock_init(&spin);
- return ret;
- }
- static void x4412_led_exit(void)
- {
- platform_driver_unregister(&x4412_led_driver);
- }
- module_init(x4412_led_init);
- module_exit(x4412_led_exit);
- MODULE_LICENSE("GPL");
- MODULE_AUTHOR("www.9tripod.com");
- MODULE_DESCRIPTION("x4412 led driver");
复制代码 自旋锁又被衍生出多种自旋锁,如读写自旋锁,顺序锁等。自旋锁不关心锁定的临界区究竟进行怎样的操作,不管是读还是写,它都一视同仁。即便多个执行单元同时读取临界资源也会被锁住。实际上,对共享资源并发访问时,多个执行单元同时读取它是不会有问题的,自旋锁的衍生锁读写自旋锁(rwlock)可允许读的并发。 读写自旋锁是一种比自旋锁粒度更小的锁机制,它保留了“自旋”的概念,但是在写操作方面,只能最多有一个写进程,在读操作方面,同时可以有多个读执行单元。当然,读和写也不能同时进行。 读写自旋锁的使用方法和自旋锁很相似,只不过它将读和写分离开来,其相关操作如下所示。 一:定义和初始化读写自旋锁 - rwlock_t my_rwlock;
- rwlock_t my_rwlock = RW_LOCK_UNLOCKED; /* 静态初始化 */
- rwlock_init(&my_rwlock); /* 动态初始化 */
复制代码二:读锁定 - void read_lock(rwlock_t *lock);
- void read_lock_irqsave(rwlock_t *lock, unsigned long flags);
- void read_lock_irq(rwlock_t *lock);
- void read_lock_bh(rwlock_t *lock);
复制代码三:读解锁 - void read_unlock(rwlock_t *lock);
- void read_unlock_irqrestore(rwlock_t *lock, unsigned long flags);
- void read_unlock_irq(rwlock_t *lock);
- void read_unlock_bh(rwlock_t *lock);
复制代码四:写锁定 - void write_lock(rwlock_t *lock);
- void write_lock_irqsave(rwlock_t *lock, unsigned long flags);
- void write_lock_irq(rwlock_t *lock);
- void write_lock_bh(rwlock_t *lock);
- int write_trylock(rwlock_t *lock);
复制代码五:写解锁 - void write_unlock(rwlock_t *lock);
- void write_unlock_irqrestore(rwlock_t *lock, unsigned long flags);
- void write_unlock_irq(rwlock_t *lock);
- void write_unlock_bh(rwlock_t *lock);
复制代码在对共享资源进行读取之前,应该先调用写锁定函数,完成之后应调用写解锁函数。在前面的实验中我们创建了一个字符设备驱动x4412-cdev.c,通过put_user和get_user函数和应用程序交互数据。对该驱动的读写函数添加读写自旋锁,参考源码如下: - #include <linux/module.h>
- #include <linux/fs.h>
- #include <linux/errno.h>
- #include <linux/init.h>
- #include <linux/cdev.h>
- #include <asm/uaccess.h>
- #include <linux/device.h>
- static int x4412_cdev_major;
- static struct cdev x4412_cdev;
- static struct class *cdev_class;
- rwlock_t lock; //定义 rwlock
- int x4412_cdev_open(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- int x4412_cdev_release(struct inode *inode, struct file *filp)
- {
- return 0;
- }
- static ssize_t x4412_cdev_read(struct file *filp, char __user *buf,size_t size,loff_t *ppos)
- {
- int read_sizes = 0;
- char *p;
- char Message[]="This is x4412/ibox devboard.";
- p = Message;
- write_lock(&lock);
- while(size && *p)
- {
- if(put_user(*(p++),buf++))
- return -EINVAL;
- size--;
- read_sizes++;
- }
- write_unlock(&lock);
- return read_sizes;
- }
- static ssize_t x4412_cdev_write(struct file *filp, const char __user *buf,size_t size, loff_t *ppos)
- {
- int i;
- char str;
- read_lock(&lock);
- for(i=0;i<size;i++)
- {
- if(!get_user(str,buf++))
- printk("%c",str);
- }
- read_unlock(&lock);
- printk("\r\n");
- return size;
- }
- static const struct file_operations x4412_fops =
- {
- .owner = THIS_MODULE,
- .read = x4412_cdev_read,
- .write = x4412_cdev_write,
- .open = x4412_cdev_open,
- .release = x4412_cdev_release,
- };
- static void x4412_setup_cdev(void)
- {
- int err, devno = MKDEV(x4412_cdev_major, 0);
- cdev_init(&x4412_cdev, &x4412_fops);
- err = cdev_add(&x4412_cdev, devno, 1);
- if (err)
- printk(KERN_NOTICE "Error %d adding x4412_cdev", err);
- }
- static void x4412_clear_cdev(void)
- {
- cdev_del(&x4412_cdev);
- unregister_chrdev_region(MKDEV(x4412_cdev_major, 0), 1);
- }
- int x4412_cdev_init(void)
- {
- int result;
- dev_t devno;
- result = alloc_chrdev_region(&devno, 0, 1, "x4412-cdev");
- x4412_cdev_major = MAJOR(devno);
- if(result < 0)
- {
- printk("register x4412-cdev error!\r\n");
- return result;
- }
- printk("x4412-cdev main dev number:%d\r\n",x4412_cdev_major);
- x4412_setup_cdev();
- cdev_class = class_create(THIS_MODULE,"x4412-cdev");
- if(IS_ERR(cdev_class))
- {
- x4412_clear_cdev();
- return PTR_ERR(cdev_class);
- }
- device_create(cdev_class,NULL,MKDEV(x4412_cdev_major,0),NULL,"x4412-cdev");
- rwlock_init(&lock); //初始化 rwlock
- return 0;
- }
- void x4412_cdev_exit(void)
- {
- device_destroy(cdev_class,MKDEV(x4412_cdev_major,0));
- class_destroy(cdev_class);
- x4412_clear_cdev();
- }
- MODULE_AUTHOR("www.9tripod.com");
- MODULE_LICENSE("GPL");
- module_init(x4412_cdev_init);
- module_exit(x4412_cdev_exit);
复制代码 |