spinlock 学习记录
来源:互联网 发布:淘宝定时上架什么意思 编辑:程序博客网 时间:2024/06/16 05:12
</pre>2. spinlock</h1><p>spinlock 具有阻塞性。</p><p>spin_lock(&lock);</p><p>spin_unlock(&lock);</p><p></p><p>if( !spin_trylock(&lock) )</p><p><span style="white-space:pre"></span>return EBUSY;</p><p></p><p>/* 修改函数:<span style="background-color: rgb(240, 240, 240);">foo_show </span>*/</p><p><pre name="code" class="html">/* * Sample kobject implementation * * Copyright (C) 2004-2007 Greg Kroah-Hartman <greg@kroah.com> * Copyright (C) 2007 Novell Inc. * * Released under the GPL version 2 only. * */#include <linux/kobject.h>#include <linux/string.h>#include <linux/sysfs.h>#include <linux/module.h>#include <linux/init.h>#include <asm/uaccess.h>#include <linux/delay.h>#include <linux/spinlock.h>/* * This module shows how to create a simple subdirectory in sysfs called * /sys/kernel/kobject-example In that directory, 3 files are created: * "foo", "baz", and "bar". If an integer is written to these files, it can be * later read out of it. */static atomic_t atomic_lock=ATOMIC_INIT(1);DEFINE_SPINLOCK(lock);static int foo;static int baz;static int bar;/* * The "foo" file where a static variable is read from and written to. */static ssize_t foo_show(struct kobject *kobj, struct kobj_attribute *attr,char *buf){if( !spin_trylock(&lock)){return -EBUSY;}mdelay(5000);return sprintf(buf, "%d\n", foo);}static ssize_t foo_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){spin_unlock(&lock);//printk("222222222\n");sscanf(buf, "%du", &foo);return count;}static struct kobj_attribute foo_attribute =__ATTR(foo, 0666, foo_show, foo_store);/* * More complex function where we determine which variable is being accessed by * looking at the attribute for the "baz" and "bar" files. */static ssize_t b_show(struct kobject *kobj, struct kobj_attribute *attr, char *buf){int var;if (strcmp(attr->attr.name, "baz") == 0)var = baz;elsevar = bar;return sprintf(buf, "%d\n", var);}static ssize_t b_store(struct kobject *kobj, struct kobj_attribute *attr, const char *buf, size_t count){int var;sscanf(buf, "%du", &var);if (strcmp(attr->attr.name, "baz") == 0)baz = var;elsebar = var;return count;}static struct kobj_attribute baz_attribute =__ATTR(baz, 0666, b_show, b_store);static struct kobj_attribute bar_attribute =__ATTR(bar, 0666, b_show, b_store);/* * Create a group of attributes so that we can create and destroy them all * at once. */static struct attribute *attrs[] = {&foo_attribute.attr,&baz_attribute.attr,&bar_attribute.attr,NULL,/* need to NULL terminate the list of attributes */};/* * An unnamed attribute group will put all of the attributes directly in * the kobject directory. If we specify a name, a subdirectory will be * created for the attributes with the directory being the name of the * attribute group. */static struct attribute_group attr_group = {.attrs = attrs,};static struct kobject *example_kobj;static int __init example_init(void){int retval;/* * Create a simple kobject with the name of "kobject_example", * located under /sys/kernel/ * * As this is a simple directory, no uevent will be sent to * userspace. That is why this function should not be used for * any type of dynamic kobjects, where the name and number are * not known ahead of time. */example_kobj = kobject_create_and_add("kobject_example", kernel_kobj);if (!example_kobj)return -ENOMEM;/* Create the files associated with this kobject */retval = sysfs_create_group(example_kobj, &attr_group);if (retval)kobject_put(example_kobj);return retval;}static void __exit example_exit(void){kobject_put(example_kobj);}module_init(example_init);module_exit(example_exit);MODULE_LICENSE("GPL");MODULE_AUTHOR("Greg Kroah-Hartman <greg@kroah.com>");
Test way:
Terminal :
cat /sys/kernel/kobject_example/foo
cat /sys/kernel/kobject_example/foo #提示设备或资源忙
只有解除限制 echo 2 > /sys/kernel/kobject_example/foo
才可以进行 cat /sys/kernel/kobject_example/foo
2. 遇到的问题
int spin_trylock(&lock)
lock 处于上锁时,返回0 .
可以参考x86 atomic_dec_and_test()实现,理解会深刻一些.
0 0
- spinlock 学习记录
- SPINLOCK学习
- spinlock
- spinlock
- Spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- spinlock
- skynet学习之spinlock.h
- nginx的spinlock的学习
- 不可睡眠锁:自旋锁spinlock编码学习
- linux shell中的变量
- CDUESTC 2016 假期赛1 D题
- GCD总结0
- jquery,獲取父級對象索引失敗,未理解,另附別樣解決方案
- 腾讯QQ第三方登录1:申请接入(OAuth2.0协议)
- spinlock 学习记录
- 67. Remove Duplicates from Sorted Array
- POJ3268Silver Cow Party(单源最短路径变形)
- 机器视觉学习笔记(10)——使用鼠标获取像素点信息
- 10 整理的简答题
- cvCreateTrackbar() 添加滚动条调节参数
- 数组的区间调整
- Kafka Notes
- 【CodeForces 610B】Vika and Squares