try_module_get和module_put
来源:互联网 发布:阿里云计算认证考试 编辑:程序博客网 时间:2024/05/29 11:57
try_module_get
注解:
1>位置:/linux/kernel/module.c2>声明:static inline int try_module_get(structmodule *module)
3>功能:判断module模块是否处于活动状态,然后通过local_inc()宏将该模块的引用计数加14>返回值:
linux-2.6中返回值是一个整数,如果该模块处于活动状态且对它引用计数加1操作正确则返回1,否则返回0
linux-3.7.5中返回值是一个bool量,正确返回true,错误返回false!
实现方式Linux-2.6
- static inline int try_module_get(struct module *module)
- {
- int ret = 1;
- if (module) {
- unsigned int cpu = get_cpu();
- if (likely(module_is_live(module))) {
- local_inc(__module_ref_addr(module, cpu));
- trace_module_get(module, _THIS_IP_,
- local_read(__module_ref_addr(module, cpu)));
- }
- else
- ret = 0;
- put_cpu();
- }
- return ret;
- }
实现方式Linux-3.7.5中
- bool try_module_get(struct module *module)
- {
- bool ret = true;
- if (module) {
- preempt_disable();
- if (likely(module_is_live(module))) {
- __this_cpu_inc(module->refptr->incs);
- trace_module_get(module, _RET_IP_);
- } else
- ret = false;
- preempt_enable();
- }
- return ret;
- }
- EXPORT_SYMBOL(try_module_get);
module_put
注解:
1>声明:
Linux-3.7.5中void module_put(struct module *module)
Linux-2.6中static inline void module_put(struct module *module)
2>功能:使指定的模块使用量减一
实现方式Linux-2.6中,是空的 我很不解,求高手解释!
- static inline void module_put(struct module *module) ///不解!!
- {
- }
Linux-3.7.5中
- void module_put(struct module *module)
- {
- if (module) {
- preempt_disable();
- smp_wmb(); /* see comment in module_refcount */
- __this_cpu_inc(module->refptr->decs);
- trace_module_put(module, _RET_IP_);
- /* Maybe they're waiting for us to drop reference? */
- if (unlikely(!module_is_live(module)))
- wake_up_process(module->waiter);
- preempt_enable();
- }
- }
- EXPORT_SYMBOL(module_put);
这两个函数的使用实例,hello模块init函数
- /*模块初始化函数*/
- static int __init hello_init(void)
- {
- printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
- try_module_get(THIS_MODULE);
- printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
- module_put(THIS_MODULE);
- printk("<0>module_refcount(module):%d\n",module_refcount(THIS_MODULE));
- return 0;
- }
打印的结果
- [root@root hello模块]#
- Message from syslogd@localhost at Feb 2 09:07:45 ...
- kernel:module_refcount(module):1
- Message from syslogd@localhost at Feb 2 09:07:45 ...
- kernel:module_refcount(module):2
- Message from syslogd@localhost at Feb 2 09:07:45 ...
- kernel:module_refcount(module):1
由上面的程序可以看出来在模块加载的过程中,模块的使用量就是1了,然后用try_module_get把使用量变为2,再使用module_put把使用量变为1,加载完成后使用量为0
开始写程序的时候把module_put写在了__eixt中,导致加载了模块使用量一直为1,无法卸载只能重启!后来才知道rmmod是在调用module_exit之前检查模块的引用计数的,所以在exit之前就应该要module_put释放引用计数,这样一来把module_put写在init里面就可以解决了!
http://blog.csdn.net/jk110333/article/details/8564640
0 0
- (6)try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- linux try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- try_module_get和module_put
- linux-2.6内核模块引用计数的实现(try_module_get和module_put)
- linux-2.6内核模块引用计数的实现(try_module_get和module_put)
- linux-2.6内核模块引用计数的实现(try_module_get和module_put)
- try_module_get简析
- 模块API之try_module_get
- 模块API之module_put/__module_get
- 通过try_module_get 来检测需要的ko是否已经insmod了
- . 和 ->
- Cocos2d-x学习笔记(四)-------->场景切换
- C程序编译执行过程(转载)
- 求全组合
- App模块化及工程扩展
- cocos2d-x v3.2 FlappyBird 各个类对象具体代码分析(2)
- try_module_get和module_put
- Flex更改Alert提示框的字体大小和样式
- UITextField控件之ValueChanged事件
- 《UNIX环境高级编程》读书笔记之文件与目录(2)
- HDU-1548 A strange lift
- [解决]Xcode6模拟器虚拟键盘不显示
- Web 应用层分类和识别
- typedef和#define
- HTTP使用BASIC认证的原理及实现方法