try_module_get简析

来源:互联网 发布:pdf全文翻译软件 编辑:程序博客网 时间:2024/05/22 17:43


 try_module_get 如果模块已经插入内核,则递增该模块引用计数;如果该模块还没有插入内核,则返回0表示出错

代码如下:

include/linux/module.h 

413 static inline int try_module_get(struct module *module)

414 {

415         int ret = 1;

416 

417         if (module) {

418                 unsigned int cpu = get_cpu();

419                 if (likely(module_is_live(module)))

420                         local_inc(&module->ref[cpu].count);

421                 else

422                         ret = 0;

423                 put_cpu();

424         }

425         return ret;

426 }

例如在fs/char_dev.c cdev_get中使用。

268 static struct kobject *cdev_get(struct cdev *p)

269 {

270         struct module *owner = p->owner;

271         struct kobject *kobj;

272         

273         if (owner && !try_module_get(owner))

274                 return NULL;

275         kobj = kobject_get(&p->kobj);

276         if (!kobj)

277                 module_put(owner);

278         return kobj;

279 }  

原创粉丝点击